#!/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 parse_env(service): """ parse_env returns a dictionary of env vars parsed from the base file :service: is a string of the service name to parse, must match folder name """ print(f"\nsetting up {service}...") f = open(f"{service}/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 return c def gen_env(kv): """ gen_env takes in a dictionary and returns the formatted env file :kv: is a dictionary of strings """ env = "" for key, value in kv.items(): env += f"{key}=\"{value}\"\n" return env def mariadb_init(): if os.path.isfile("mariadb/.env"): print("mariadb/.env already exists... skipping") return c = parse_env("mariadb") password = c["MYSQL_ROOT_PASSWORD"] sql_init(password) gen_env(c) def gitea_init(): if os.path.isfile("gitea/.env"): print("gitea/.env already exists... skipping") return 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.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"] sql_add_user(username, password, *dbs) # seafile_init() mariadb_init() gitea_init() seafile_init()