@@ -34,22 +34,21 @@ def convert(src, dst, environ):
3434 outfile .write (rendered )
3535
3636
37- def generate_config_from_template (environ , ownership ):
37+ def generate_config_from_template (config_dir , config_path , environ , ownership ):
3838 """Generate a homeserver.yaml from environment variables
3939
4040 Args:
41+ config_dir (str): where to put generated config files
42+ config_path (str): where to put the main config file
4143 environ (dict): environment dictionary
4244 ownership (str): "<user>:<group>" string which will be used to set
4345 ownership of the generated configs
44-
45- Returns:
46- path to generated config file
4746 """
4847 for v in ("SYNAPSE_SERVER_NAME" , "SYNAPSE_REPORT_STATS" ):
4948 if v not in environ :
5049 error (
51- "Environment variable '%s' is mandatory when generating a config "
52- "file on-the-fly." % (v ,)
50+ "Environment variable '%s' is mandatory when generating a config file. "
51+ % (v ,)
5352 )
5453
5554 # populate some params from data files (if they exist, else create new ones)
@@ -78,10 +77,8 @@ def generate_config_from_template(environ, ownership):
7877 environ [secret ] = value
7978
8079 environ ["SYNAPSE_APPSERVICES" ] = glob .glob ("/data/appservices/*.yaml" )
81- if not os .path .exists ("/compiled" ):
82- os .mkdir ("/compiled" )
83-
84- config_path = "/compiled/homeserver.yaml"
80+ if not os .path .exists (config_dir ):
81+ os .mkdir (config_dir )
8582
8683 # Convert SYNAPSE_NO_TLS to boolean if exists
8784 if "SYNAPSE_NO_TLS" in environ :
@@ -98,8 +95,16 @@ def generate_config_from_template(environ, ownership):
9895 + '" unrecognized; exiting.'
9996 )
10097
98+ if "SYNAPSE_LOG_CONFIG" not in environ :
99+ environ ["SYNAPSE_LOG_CONFIG" ] = config_dir + "/log.config"
100+
101+ log ("Generating synapse config file " + config_path )
101102 convert ("/conf/homeserver.yaml" , config_path , environ )
102- convert ("/conf/log.config" , "/compiled/log.config" , environ )
103+
104+ log_config_file = environ ["SYNAPSE_LOG_CONFIG" ]
105+ log ("Generating log config file " + log_config_file )
106+ convert ("/conf/log.config" , log_config_file , environ )
107+
103108 subprocess .check_output (["chown" , "-R" , ownership , "/data" ])
104109
105110 # Hopefully we already have a signing key, but generate one if not.
@@ -114,13 +119,11 @@ def generate_config_from_template(environ, ownership):
114119 config_path ,
115120 # tell synapse to put generated keys in /data rather than /compiled
116121 "--keys-directory" ,
117- "/data" ,
122+ config_dir ,
118123 "--generate-keys" ,
119124 ]
120125 )
121126
122- return config_path
123-
124127
125128def run_generate_config (environ , ownership ):
126129 """Run synapse with a --generate-config param to generate a template config file
@@ -178,15 +181,36 @@ def main(args, environ):
178181 if mode == "generate" :
179182 return run_generate_config (environ , ownership )
180183
184+ if mode == "migrate_config" :
185+ # generate a config based on environment vars.
186+ config_dir = environ .get ("SYNAPSE_CONFIG_DIR" , "/data" )
187+ config_path = environ .get (
188+ "SYNAPSE_CONFIG_PATH" , config_dir + "/homeserver.yaml"
189+ )
190+ return generate_config_from_template (
191+ config_dir , config_path , environ , ownership
192+ )
193+
194+ if mode is not None :
195+ error ("Unknown execution mode '%s'" % (mode ,))
196+
181197 if "SYNAPSE_SERVER_NAME" in environ :
182198 # backwards-compatibility generate-a-config-on-the-fly mode
183199 if "SYNAPSE_CONFIG_PATH" in environ :
184200 error (
185201 "SYNAPSE_SERVER_NAME and SYNAPSE_CONFIG_PATH are mutually exclusive "
186- "except in `generate` mode."
202+ "except in `generate` or `migrate_config` mode."
187203 )
188204
189- config_path = generate_config_from_template (environ , ownership )
205+ config_path = "/compiled/homeserver.yaml"
206+ log (
207+ "Generating config file '%s' on-the-fly from environment variables.\n "
208+ "Note that this mode is deprecated. You can migrate to a static config\n "
209+ "file by running with 'migrate_config'. See the README for more details."
210+ % (config_path ,)
211+ )
212+
213+ generate_config_from_template ("/compiled" , config_path , environ , ownership )
190214 else :
191215 config_dir = environ .get ("SYNAPSE_CONFIG_DIR" , "/data" )
192216 config_path = environ .get (
0 commit comments