cleaing up init script to simplify sql script generation
This commit is contained in:
parent
9181f71a96
commit
95750a7120
80
init.py
80
init.py
@ -65,14 +65,18 @@ def prompt_fill(key):
|
|||||||
|
|
||||||
|
|
||||||
def sql_init(password):
|
def sql_init(password):
|
||||||
print(f"CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';")
|
sql = f"CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';\n"
|
||||||
print(f"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%%';")
|
sql += f"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%%';\n"
|
||||||
|
|
||||||
|
return sql
|
||||||
|
|
||||||
def sql_add_user(username, password, *databases):
|
def sql_add_user(username, password, *databases):
|
||||||
print(f"CREATE USER {username} IDENTIFIED BY {password};")
|
sql = f"CREATE USER {username} IDENTIFIED BY '{password}';\n"
|
||||||
for db in databases:
|
for db in databases:
|
||||||
print(f"CREATE DATABASE {db};")
|
sql += f"CREATE DATABASE {db};\n"
|
||||||
print(f"GRANT ALL PRIVILEGES ON {db} TO {username};")
|
sql += f"GRANT ALL PRIVILEGES ON {db} TO {username};\n"
|
||||||
|
|
||||||
|
return sql
|
||||||
|
|
||||||
def parse_env(service):
|
def parse_env(service):
|
||||||
"""
|
"""
|
||||||
@ -115,43 +119,51 @@ def gen_env(kv):
|
|||||||
|
|
||||||
return env
|
return env
|
||||||
|
|
||||||
def mariadb_init():
|
def service_init(service):
|
||||||
if os.path.isfile("mariadb/.env"):
|
priv = f"{service}/.env"
|
||||||
print("mariadb/.env already exists... skipping")
|
sql = ""
|
||||||
return
|
|
||||||
|
|
||||||
c = parse_env("mariadb")
|
# prevent overwrite
|
||||||
password = c["MYSQL_ROOT_PASSWORD"]
|
if os.path.isfile(priv):
|
||||||
|
print(f"{priv} already exists... skipping")
|
||||||
|
return sql
|
||||||
|
|
||||||
sql_init(password)
|
c = parse_env(service)
|
||||||
gen_env(c)
|
|
||||||
|
|
||||||
def gitea_init():
|
if service == "mariadb":
|
||||||
if os.path.isfile("gitea/.env"):
|
password = c["MYSQL_ROOT_PASSWORD"]
|
||||||
print("gitea/.env already exists... skipping")
|
sql = sql_init(password)
|
||||||
return
|
elif service == "gitea":
|
||||||
|
username = c["GITEA__database__USER"]
|
||||||
|
password = c["GITEA__database__PASSWD"]
|
||||||
|
db = c["GITEA__database__NAME"]
|
||||||
|
sql = sql_add_user(username, password, db)
|
||||||
|
elif service == "seafile":
|
||||||
|
username = c["SEAFILE_MYSQL_DB_USER"]
|
||||||
|
password = c["SEAFILE_MYSQL_DB_PASSWORD"]
|
||||||
|
dbs = ["ccnet_db", "seafile_db", "seahub_db"]
|
||||||
|
sql = sql_add_user(username, password, *dbs)
|
||||||
|
else:
|
||||||
|
print(f"service {service} not regonized!")
|
||||||
|
os.exit(1)
|
||||||
|
|
||||||
c = parse_env("gitea")
|
|
||||||
username = c["GITEA__database__USER"]
|
|
||||||
password = c["GITEA__database__PASSWD"]
|
|
||||||
db = c["GITEA__database__NAME"]
|
|
||||||
|
|
||||||
sql_add_user(username, password, db)
|
|
||||||
env = gen_env(c)
|
env = gen_env(c)
|
||||||
|
|
||||||
f = open("gitea/.env", 'w')
|
f = open(priv, 'w')
|
||||||
f.write(env)
|
f.write(env)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def seafile_init():
|
return sql
|
||||||
c = parse_env("seafile")
|
|
||||||
username = c["SEAFILE_MYSQL_DB_USER"]
|
|
||||||
password = c["SEAFILE_MYSQL_DB_PASSWORD"]
|
|
||||||
dbs = ["ccnet_db", "seafile_db", "seahub_db"]
|
|
||||||
|
|
||||||
sql_add_user(username, password, *dbs)
|
sql=""
|
||||||
|
for service in ["mariadb", "gitea", "seafile"]:
|
||||||
|
sql += service_init(service)
|
||||||
|
|
||||||
# seafile_init()
|
# attempt to write sql init script based on parameters
|
||||||
mariadb_init()
|
if os.path.isfile("priv/init.sql"):
|
||||||
gitea_init()
|
print("priv/init.sql already exists... skipping")
|
||||||
seafile_init()
|
exit
|
||||||
|
|
||||||
|
f = open("priv/init.sql", 'w')
|
||||||
|
f.write(sql)
|
||||||
|
f.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user