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):
|
||||
print(f"CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';")
|
||||
print(f"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%%';")
|
||||
sql = f"CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';\n"
|
||||
sql += f"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%%';\n"
|
||||
|
||||
return sql
|
||||
|
||||
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:
|
||||
print(f"CREATE DATABASE {db};")
|
||||
print(f"GRANT ALL PRIVILEGES ON {db} TO {username};")
|
||||
sql += f"CREATE DATABASE {db};\n"
|
||||
sql += f"GRANT ALL PRIVILEGES ON {db} TO {username};\n"
|
||||
|
||||
return sql
|
||||
|
||||
def parse_env(service):
|
||||
"""
|
||||
@ -115,43 +119,51 @@ def gen_env(kv):
|
||||
|
||||
return env
|
||||
|
||||
def mariadb_init():
|
||||
if os.path.isfile("mariadb/.env"):
|
||||
print("mariadb/.env already exists... skipping")
|
||||
return
|
||||
def service_init(service):
|
||||
priv = f"{service}/.env"
|
||||
sql = ""
|
||||
|
||||
c = parse_env("mariadb")
|
||||
password = c["MYSQL_ROOT_PASSWORD"]
|
||||
# prevent overwrite
|
||||
if os.path.isfile(priv):
|
||||
print(f"{priv} already exists... skipping")
|
||||
return sql
|
||||
|
||||
sql_init(password)
|
||||
gen_env(c)
|
||||
c = parse_env(service)
|
||||
|
||||
def gitea_init():
|
||||
if os.path.isfile("gitea/.env"):
|
||||
print("gitea/.env already exists... skipping")
|
||||
return
|
||||
if service == "mariadb":
|
||||
password = c["MYSQL_ROOT_PASSWORD"]
|
||||
sql = sql_init(password)
|
||||
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)
|
||||
|
||||
f = open("gitea/.env", 'w')
|
||||
f = open(priv, 'w')
|
||||
f.write(env)
|
||||
f.close()
|
||||
|
||||
def seafile_init():
|
||||
c = parse_env("seafile")
|
||||
username = c["SEAFILE_MYSQL_DB_USER"]
|
||||
password = c["SEAFILE_MYSQL_DB_PASSWORD"]
|
||||
dbs = ["ccnet_db", "seafile_db", "seahub_db"]
|
||||
return sql
|
||||
|
||||
sql_add_user(username, password, *dbs)
|
||||
sql=""
|
||||
for service in ["mariadb", "gitea", "seafile"]:
|
||||
sql += service_init(service)
|
||||
|
||||
# seafile_init()
|
||||
mariadb_init()
|
||||
gitea_init()
|
||||
seafile_init()
|
||||
# attempt to write sql init script based on parameters
|
||||
if os.path.isfile("priv/init.sql"):
|
||||
print("priv/init.sql already exists... skipping")
|
||||
exit
|
||||
|
||||
f = open("priv/init.sql", 'w')
|
||||
f.write(sql)
|
||||
f.close()
|
||||
|
Loading…
x
Reference in New Issue
Block a user