Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 555b6fa

Browse files
authored
Docker image: Add a migrate_config mode (#5567)
... to help people escape env var hell
1 parent 1ddc7b3 commit 555b6fa

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

changelog.d/5567.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update Docker image to deprecate the use of environment variables for configuration, and make the use of a static configuration the default.

docker/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,19 @@ For backwards-compatibility only, the docker image supports creating a dynamic
112112
configuration file based on environment variables. This is now deprecated, but
113113
is enabled when the `SYNAPSE_SERVER_NAME` variable is set (and `generate` is
114114
not given).
115+
116+
To migrate from a dynamic configuration file to a static one, run the docker
117+
container once with the environment variables set, and `migrate_config`
118+
commandline option. For example:
119+
120+
```
121+
docker run -it --rm \
122+
--mount type=volume,src=synapse-data,dst=/data \
123+
-e SYNAPSE_SERVER_NAME=my.matrix.host \
124+
-e SYNAPSE_REPORT_STATS=yes \
125+
matrixdotorg/synapse:latest migrate_config
126+
```
127+
128+
This will generate the same configuration file as the legacy mode used, but
129+
will store it in `/data/homeserver.yaml` instead of a temporary location. You
130+
can then use it as shown above at [Running synapse](#running-synapse).

docker/conf/homeserver.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ server_name: "{{ SYNAPSE_SERVER_NAME }}"
2121
pid_file: /homeserver.pid
2222
web_client: False
2323
soft_file_limit: 0
24-
log_config: "/compiled/log.config"
24+
log_config: "{{ SYNAPSE_LOG_CONFIG }}"
2525

2626
## Ports ##
2727

docker/start.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

125128
def 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

Comments
 (0)