-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
executable file
·60 lines (52 loc) · 1.47 KB
/
init_db.py
File metadata and controls
executable file
·60 lines (52 loc) · 1.47 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
import os
import shlex
import time
import pymysql
DB_ROOT_USER = os.getenv("DB_ROOT_USER", "root")
DB_ROOT_PASSWD = os.getenv("DB_ROOT_PASSWD")
DB_USER = os.getenv("DB_USER")
DB_PASSWD = os.getenv("DB_PASSWD")
db_user = ""
db_passwd = ""
if DB_ROOT_USER and DB_ROOT_PASSWD:
db_user = "root"
db_passwd = DB_ROOT_PASSWD
elif DB_USER and DB_PASSWD:
db_user = DB_USER
db_passwd = DB_PASSWD
else:
db_user = "root"
db_passwd = ""
DB_HOST = os.getenv("DB_HOST", "seatable-mysql")
DB_PORT = int(os.getenv("DB_PORT", "3306"))
DATABASE_NAME = os.getenv("DATABASE_NAME", "scheduler")
def wait_for_mysql():
while True:
try:
connection = pymysql.connect(
host=DB_HOST, port=DB_PORT, user=db_user, passwd=db_passwd
)
except Exception as e:
print("waiting for mysql server to be ready: %s", e)
time.sleep(10)
continue
print("mysql server ready")
connection.close()
return
wait_for_mysql()
if db_user == "root":
sql = 'mysql -h %s -u%s -p%s -e "CREATE DATABASE IF NOT EXISTS %s;"' % (
shlex.quote(DB_HOST),
shlex.quote(db_user),
shlex.quote(db_passwd),
DATABASE_NAME,
)
os.system(sql)
sql = "mysql -h %s -u%s -p%s %s </opt/scheduler/database/initial_tables.sql" % (
shlex.quote(DB_HOST),
shlex.quote(db_user),
shlex.quote(db_passwd),
DATABASE_NAME,
)
os.system(sql)
print("Initalization of database successful")