-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbootstrap_admin.py
More file actions
71 lines (57 loc) · 2.05 KB
/
Copy pathbootstrap_admin.py
File metadata and controls
71 lines (57 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sys
import time
from sqlalchemy.exc import OperationalError
from flask_migrate import stamp
from aggregator import db
from aggregator.app import app, init_db
from aggregator.models import User
def required_env(name):
value = os.environ.get(name, "").strip()
if not value:
raise RuntimeError(f"{name} must be set in .env")
return value
def bootstrap_admin():
username = required_env("ADMIN_USERNAME")
email = required_env("ADMIN_EMAIL")
password = required_env("ADMIN_PASSWORD")
last_error = None
for attempt in range(1, 31):
try:
init_db()
break
except OperationalError as exc:
last_error = exc
print(f"Database not ready yet, retrying ({attempt}/30)...")
time.sleep(2)
else:
raise RuntimeError(f"Database did not become ready: {last_error}")
with app.app_context():
# Fresh installs created with db.create_all() need Alembic marked current
# so later `flask db upgrade` runs only future migrations.
stamp(revision="head")
user = User.query.filter_by(username=username).first()
email_owner = User.query.filter_by(email=email).first()
if email_owner and email_owner.username != username:
raise RuntimeError(
f"ADMIN_EMAIL is already used by user '{email_owner.username}'. "
"Choose a different ADMIN_EMAIL or update that user manually."
)
if user:
user.email = email
user.is_admin = True
user.set_password(password)
action = "updated"
else:
user = User(username=username, email=email, is_admin=True)
user.set_password(password)
db.session.add(user)
action = "created"
db.session.commit()
print(f"Admin user '{username}' {action}.")
if __name__ == "__main__":
try:
bootstrap_admin()
except Exception as exc:
print(f"Bootstrap failed: {exc}", file=sys.stderr)
sys.exit(1)