-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_setup.py
More file actions
94 lines (77 loc) · 3.11 KB
/
docker_setup.py
File metadata and controls
94 lines (77 loc) · 3.11 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
docker_setup.py
---------------
Automatic admin account creation for Docker deployments.
Reads database connection and admin credentials from environment variables.
Automatically fixes MySQL authentication if needed.
"""
import os
import time
import pymysql
from sqlalchemy import text
from app import create_app, db
from app.models import User
def fix_mysql_auth():
host = os.getenv("MYSQL_HOST", "db")
user = os.getenv("MYSQL_USER", "novatalk")
password = os.getenv("MYSQL_PASSWORD", "novatalk")
root_pw = os.getenv("MYSQL_ROOT_PASSWORD", "root")
try:
conn = pymysql.connect(host=host, user=user, password=password, database="novatalk")
conn.close()
print("[*] MySQL authentication works, no fix needed.")
return
except Exception as e:
print(f"[*] MySQL login failed for '{user}', attempting to fix authentication ({type(e).__name__}: {e})")
try:
root_conn = pymysql.connect(host=host, user="root", password=root_pw)
with root_conn.cursor() as cur:
cur.execute("ALTER USER 'novatalk'@'%' IDENTIFIED WITH mysql_native_password BY 'novatalk';")
cur.execute("ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';")
cur.execute("FLUSH PRIVILEGES;")
root_conn.commit()
root_conn.close()
print("[*] MySQL authentication method fixed to mysql_native_password.")
except Exception as e2:
print(f"[*] Failed to apply MySQL auth fix ({type(e2).__name__}: {e2})")
def main():
admin_username = os.getenv("ADMIN_USERNAME", "admin")
admin_email = os.getenv("ADMIN_EMAIL", "admin@example.com")
admin_password = os.getenv("ADMIN_PASSWORD", "admin123")
admin_display = os.getenv("ADMIN_DISPLAY_NAME", "Administrator")
print("[*] Initializing NovaTalk (Docker setup mode)")
print(f"Admin: {admin_username} <{admin_email}>")
fix_mysql_auth() # auto-detect and fix MySQL auth before proceeding
app = create_app()
print("[*] Waiting for database connection...")
for i in range(60):
try:
with app.app_context():
db.session.execute(text("SELECT 1"))
print("[*] Database reachable!")
break
except Exception as e:
print(f" Attempt {i+1}/60: waiting... ({type(e).__name__}: {e})")
time.sleep(2)
else:
print("[*] Database not reachable after 120s, aborting setup.")
return
with app.app_context():
db.create_all()
print("[*] Tables created or verified.")
existing = User.query.filter_by(username=admin_username.lower()).first()
if existing:
print("[*] Admin already exists, skipping creation.")
return
admin = User(
display_name=admin_display,
username=admin_username.lower(),
email=admin_email.lower(),
is_admin=True,
)
admin.set_password(admin_password)
db.session.add(admin)
db.session.commit()
print(f"[*] Admin user created: {admin_username} ({admin_email})")
if __name__ == "__main__":
main()