Factor out a run_generate_config function

This commit is contained in:
Richard van der Hoff 2019-06-24 18:55:37 +01:00
parent a52e1a3b6c
commit b1fddb7f69
1 changed files with 28 additions and 17 deletions

View File

@ -33,12 +33,6 @@ def convert(src, dst, environ):
outfile.write(rendered) outfile.write(rendered)
def check_arguments(environ, args):
for argument in args:
if argument not in environ:
error("Environment variable %s is mandatory, exiting." % argument)
def generate_config_from_template(environ, ownership): def generate_config_from_template(environ, ownership):
"""Generate a homeserver.yaml from environment variables """Generate a homeserver.yaml from environment variables
@ -108,17 +102,22 @@ def generate_config_from_template(environ, ownership):
return config_path return config_path
# Prepare the configuration def run_generate_config(environ):
mode = sys.argv[1] if len(sys.argv) > 1 else None """Run synapse with a --generate-config param to generate a template config file
ownership = "{}:{}".format(environ.get("UID", 991), environ.get("GID", 991))
args = ["python", "-m", "synapse.app.homeserver"]
# In generate mode, generate a configuration, missing keys, then exit Args:
if mode == "generate": environ (dict): environment dictionary
check_arguments(
environ, ("SYNAPSE_SERVER_NAME", "SYNAPSE_REPORT_STATS", "SYNAPSE_CONFIG_PATH") Never returns.
) """
args += [ for v in ("SYNAPSE_SERVER_NAME", "SYNAPSE_REPORT_STATS", "SYNAPSE_CONFIG_PATH"):
if v not in environ:
error("Environment variable '%s' is mandatory in `generate` mode." % (v,))
args = [
"python",
"-m",
"synapse.app.homeserver",
"--server-name", "--server-name",
environ["SYNAPSE_SERVER_NAME"], environ["SYNAPSE_SERVER_NAME"],
"--report-stats", "--report-stats",
@ -129,6 +128,15 @@ if mode == "generate":
] ]
os.execv("/usr/local/bin/python", args) os.execv("/usr/local/bin/python", args)
# Prepare the configuration
mode = sys.argv[1] if len(sys.argv) > 1 else None
ownership = "{}:{}".format(environ.get("UID", 991), environ.get("GID", 991))
# In generate mode, generate a configuration, missing keys, then exit
if mode == "generate":
run_generate_config(environ)
# In normal mode, generate missing keys if any, then run synapse # In normal mode, generate missing keys if any, then run synapse
else: else:
if "SYNAPSE_CONFIG_PATH" in environ: if "SYNAPSE_CONFIG_PATH" in environ:
@ -136,7 +144,10 @@ else:
else: else:
config_path = generate_config_from_template(environ, ownership) config_path = generate_config_from_template(environ, ownership)
args += [ args = [
"python",
"-m",
"synapse.app.homeserver",
"--config-path", "--config-path",
config_path, config_path,
# tell synapse to put any generated keys in /data rather than /compiled # tell synapse to put any generated keys in /data rather than /compiled