123 lines
3.1 KiB
Python
Executable File
123 lines
3.1 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import os
|
|
import secrets
|
|
|
|
## A simple tool go generate an sql init script
|
|
|
|
# qconst sql="init.sql"
|
|
|
|
# cd priv
|
|
|
|
# seafile_user returns a sql command string to create seafile user and db
|
|
# def seafile_user():
|
|
# if 'SEAFILE_MYSQL_DB_USER' in#
|
|
|
|
# only allow aA-zZ, 0-9 and below "special" characters in passwords
|
|
ALLOWED_CHARS = [ord('!'), ord('@'), ord('#'), ord('%'), ord('&')]\
|
|
+ list(range(48,58))\
|
|
+ list(range(64,90))\
|
|
+ list(range(97,123))
|
|
|
|
# convert ints back to characters
|
|
ALLOWED_CHARS = [chr(c) for c in ALLOWED_CHARS]
|
|
|
|
|
|
def gen_pass(l):
|
|
"""
|
|
gen_pass returns a password of length l
|
|
|
|
:param l: is an integer that represents desired password length
|
|
"""
|
|
password = ""
|
|
for i in range(int(l)):
|
|
password += secrets.choice(ALLOWED_CHARS)
|
|
|
|
return password
|
|
|
|
def prompt_fill(key):
|
|
prompt = f"{key} was not set in config, please select an option below:\n"
|
|
prompt += f"\t1) leave blank (DEAFAULT)\n"
|
|
prompt += f"\t2) generate password\n"
|
|
prompt += f"\t3) enter value\n"
|
|
|
|
choice = input(prompt)
|
|
while choice not in "123":
|
|
print(f"error: {choice} unrecognized, please enter a number 1-3 or leave blank")
|
|
choice = input(prompt)
|
|
|
|
if choice == "1" or choice == "":
|
|
return ""
|
|
|
|
elif choice == "2":
|
|
return gen_pass(32)
|
|
|
|
elif choice == "3":
|
|
# run until user confirms input
|
|
user_input = ""
|
|
confirm = None
|
|
while confirm not in ["", "Y", "y"]:
|
|
user_input = input("please enter desired value:")
|
|
confirm = input(f"set {key} to {user_input} (Y/n)?")
|
|
|
|
return user_input
|
|
|
|
|
|
|
|
def sql_init(password):
|
|
print(f"CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';")
|
|
print(f"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%%';")
|
|
|
|
def sql_add_user(username, password, *databases):
|
|
print(f"CREATE USER {username} IDENTIFIED BY {password};")
|
|
for db in databases:
|
|
print(f"CREATE DATABASE {db};")
|
|
print(f"GRANT ALL PRIVILEGES ON {db} TO {username};")
|
|
|
|
def seafile_init():
|
|
print("setting up seafile...")
|
|
|
|
f = open("seafile/env")
|
|
c = dict()
|
|
for line in f:
|
|
# skip comments
|
|
if line[0] == "#":
|
|
continue
|
|
if line.isspace():
|
|
continue
|
|
|
|
kv = line.strip('\n').split("=")
|
|
|
|
key = kv[0]
|
|
val = kv[1].strip('"')
|
|
|
|
if val == "":
|
|
val = prompt_fill(key)
|
|
|
|
c[key] = val
|
|
|
|
print(c)
|
|
|
|
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)
|
|
|
|
seafile_init()
|
|
# def get_env():
|
|
# print(os.environ['PWD'])
|
|
|
|
# printf "testing the env sourcing Seafile host = %s\n", "$SEAFILE_MYSQL_DB_HOST"
|
|
|
|
# do not overwrite existing file
|
|
# test -f "$sql" || exit 1
|
|
# [ -f "$sql" ] && printf "failed: file exists\n" && exit 1
|
|
|
|
# printf "Generating sql script... "
|
|
# touch "$sql"
|
|
|
|
# # root
|
|
# printf "CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';\n" >> "$sql"
|
|
# printf "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%%'\n" >> "$sql"
|