Add maintenance.py with set-admin-password and delete-all-data
This commit is contained in:
1 parent
e3575ea4c6
commit
5d8b89446c
1 file changed
+68
@@ -0,0 +1,68 @@
|
||||
"""The command-line tasks the StartOS actions run inside the container.
|
||||
|
||||
`set-admin-password` generates the password here and prints the credentials as
|
||||
one JSON object on stdout, so the password never travels from the host into the
|
||||
container. `delete-all-data` needs --confirm and keeps the admin and the
|
||||
settings.
|
||||
"""
|
||||
|
||||
import json
|
||||
import secrets
|
||||
import sys
|
||||
|
||||
import auth
|
||||
import database
|
||||
|
||||
GENERATED_PASSWORD_BYTES = 16
|
||||
|
||||
# Every table except admin and settings, children before parents.
|
||||
WIPE_ORDER = (
|
||||
"lot_consumptions",
|
||||
"lot_picks",
|
||||
"lots",
|
||||
"transactions",
|
||||
"markets",
|
||||
"sessions",
|
||||
"prices",
|
||||
"schema_version",
|
||||
)
|
||||
|
||||
USAGE = "usage: maintenance.py set-admin-password | delete-all-data --confirm"
|
||||
|
||||
|
||||
def set_admin_password():
|
||||
"""Generate a password, store it and print one JSON object with the credentials."""
|
||||
password = secrets.token_urlsafe(GENERATED_PASSWORD_BYTES)
|
||||
username = auth.reset_password(password)
|
||||
print(json.dumps({"username": username, "password": password}), flush=True)
|
||||
return 0
|
||||
|
||||
|
||||
def delete_all_data():
|
||||
"""Empty every table except admin and settings."""
|
||||
conn = database.connect()
|
||||
try:
|
||||
for table in WIPE_ORDER:
|
||||
conn.execute("DELETE FROM %s" % table)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
return 0
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
"""Run one maintenance command and return the exit status."""
|
||||
args = list(sys.argv[1:] if argv is None else argv)
|
||||
if args[:1] == ["set-admin-password"]:
|
||||
return set_admin_password()
|
||||
if args[:1] == ["delete-all-data"]:
|
||||
if "--confirm" not in args[1:]:
|
||||
print("delete-all-data krev --confirm.", file=sys.stderr)
|
||||
return 2
|
||||
return delete_all_data()
|
||||
print(USAGE, file=sys.stderr)
|
||||
return 2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user