From 5d8b89446c15b4472acd8f9a88631defaf8b605c Mon Sep 17 00:00:00 2001 From: ai-bot Date: Sat, 14 Nov 2026 12:17:00 +0000 Subject: [PATCH] Add maintenance.py with set-admin-password and delete-all-data --- maintenance.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 maintenance.py diff --git a/maintenance.py b/maintenance.py new file mode 100644 index 0000000..69055d6 --- /dev/null +++ b/maintenance.py @@ -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())