Update manage.py
This commit is contained in:
+22
-5
@@ -1,5 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Admin CLI. Uses the same BACKUP_DASH_DB env var as the API.
|
"""Admin CLI. Finds the database the same way the API does: BACKUP_DASH_DB from the environment,
|
||||||
|
else from /etc/backup-dashboard/backup-dashboard.env, else ./backup-dashboard.db (dev).
|
||||||
|
Run it as the service user so files it creates stay writable by the API:
|
||||||
|
|
||||||
|
sudo -u backupdash /opt/backup-dashboard/venv/bin/python manage.py create-key veeam-acme
|
||||||
|
|
||||||
python manage.py create-key veeam-acme # prints the key once
|
python manage.py create-key veeam-acme # prints the key once
|
||||||
python manage.py list-keys
|
python manage.py list-keys
|
||||||
@@ -7,17 +11,19 @@
|
|||||||
python manage.py list-jobs
|
python manage.py list-jobs
|
||||||
python manage.py delete-job 12 # e.g. a decommissioned job stuck as Overdue
|
python manage.py delete-job 12 # e.g. a decommissioned job stuck as Overdue
|
||||||
python manage.py seed-demo # 30 days of fake history for trying the UI
|
python manage.py seed-demo # 30 days of fake history for trying the UI
|
||||||
|
python manage.py --db ./dev.db --create seed-demo
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from app import db, repo
|
from app import db, repo
|
||||||
from app.auth import generate_key, hash_key
|
from app.auth import generate_key, hash_key
|
||||||
from app.models import IngestIn
|
from app.models import IngestIn
|
||||||
from app.settings import load_settings
|
from app.settings import DEFAULT_ENV_FILE, load_settings
|
||||||
from app.timeutil import to_db, utcnow
|
from app.timeutil import to_db, utcnow
|
||||||
|
|
||||||
|
|
||||||
@@ -94,6 +100,8 @@ def seed_demo(conn, args):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
|
p.add_argument("--db", type=Path, help="database file (overrides BACKUP_DASH_DB and the env file)")
|
||||||
|
p.add_argument("--create", action="store_true", help="create the database if it doesn't exist")
|
||||||
sub = p.add_subparsers(required=True)
|
sub = p.add_subparsers(required=True)
|
||||||
s = sub.add_parser("create-key"); s.add_argument("name"); s.set_defaults(fn=create_key)
|
s = sub.add_parser("create-key"); s.add_argument("name"); s.set_defaults(fn=create_key)
|
||||||
s = sub.add_parser("list-keys"); s.set_defaults(fn=list_keys)
|
s = sub.add_parser("list-keys"); s.set_defaults(fn=list_keys)
|
||||||
@@ -103,9 +111,18 @@ def main():
|
|||||||
s = sub.add_parser("seed-demo"); s.set_defaults(fn=seed_demo)
|
s = sub.add_parser("seed-demo"); s.set_defaults(fn=seed_demo)
|
||||||
args = p.parse_args()
|
args = p.parse_args()
|
||||||
|
|
||||||
settings = load_settings()
|
db_path = args.db or load_settings().db_path
|
||||||
db.init_db(settings.db_path)
|
# Refuse to silently create a fresh DB: that almost always means we're looking in the wrong
|
||||||
conn = db.connect(settings.db_path)
|
# place, and anything written there (like a new API key) would be invisible to the service.
|
||||||
|
if not db_path.exists() and not args.create:
|
||||||
|
sys.exit(
|
||||||
|
f"Database not found: {db_path.resolve()}\n"
|
||||||
|
f"Set BACKUP_DASH_DB in {DEFAULT_ENV_FILE} (or pass --db), "
|
||||||
|
"or add --create to start a new database there."
|
||||||
|
)
|
||||||
|
print(f"Using database {db_path.resolve()}", file=sys.stderr)
|
||||||
|
db.init_db(db_path)
|
||||||
|
conn = db.connect(db_path)
|
||||||
try:
|
try:
|
||||||
args.fn(conn, args)
|
args.fn(conn, args)
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user