1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2020 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script reads environment variables and generates a shared Synapse worker,
# nginx and supervisord configs depending on the workers requested
import os
import sys
def main(args, environ):
"""Read the desired list of workers from environment variables and generate
shared homeserver, nginx and supervisord configs.
Args:
environ: _Environ[str]
"""
# The contents of this string will be appended to the one generated by
# the homeserver, and is intended mainly for disabling functionality on the main
# when certain workers are spun up
homeserver_config = ""
# The contents of this string will be append to the base supervisord config
supervisord_config = ""
# An nginx site config. Will live in /etc/nginx/conf.d
nginx_config_template_header = """
server {
listen 80;
listen [::]:80;
# For the federation port
listen 8448 default_server;
listen [::]:8448 default_server;
server_name localhost;
"""
nginx_config_body = ""
nginx_config_template_end = """
# Send all other traffic to the main process
location ~* ^(\/_matrix|\/_synapse) {
proxy_pass http://localhost:8008;
proxy_set_header X-Forwarded-For $remote_addr;
# TODO: Can we move this to the default nginx.conf so all locations are
# affected?
#
# Nginx by default only allows file uploads up to 1M in size
# Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
client_max_body_size 50M;
}
}
"""
# Read desired worker configuration from environment
worker_types = environ.get("SYNAPSE_WORKERS")
worker_types = worker_types.split(",")
for worker_type in worker_types:
if worker_type == "pusher":
# Disable push handling from the main process
homeserver_config += """
start_pushers: false
"""
# Enable the pusher worker in supervisord
supervisord_config += """
"""
# This worker does not handle any REST endpoints
elif worker_type == "appservice":
# Disable appservice traffic sending from the main process
homeserver_config += """
notify_appservices: false
"""
# Enable the pusher worker in supervisord
supervisord_config += """
[program:synapse_user_dir]
command=/usr/local/bin/python -m synapse.app.user_dir \
--config-path=/config/homeserver.yaml \
--config-path=/config/workers/user_dir.yaml
autorestart=unexpected
exitcodes=0
"""
# This worker does not handle any REST endpoints
elif worker_type == "user_dir":
# Disable user directory updates on the main process
homeserver_config += """
update_user_directory: false
"""
# Enable the user directory worker in supervisord
supervisord_config += """
[program:synapse_user_dir]
command=/usr/local/bin/python -m synapse.app.user_dir \
--config-path=/config/homeserver.yaml \
--config-path=/config/workers/user_dir.yaml
autorestart=unexpected
exitcodes=0
"""
# Route user directory requests to this worker
nginx_config_body += """
location ~* ^/_matrix/client/(api/v1|r0|unstable)/user_directory/search$ {
proxy_pass http://localhost:8010;
proxy_set_header X-Forwarded-For $remote_addr;
}
"""
# Write out the config files
# Main homeserver config
with open("/config/main.yaml", "a") as f:
f.write(homeserver_config)
# Nginx config
with open("/config/nginx.conf", "w") as f:
f.write(nginx_config_template_header)
f.write(nginx_config_body)
f.write(nginx_config_template_end)
# Supervisord config
with open("/config/supervisord.conf", "a") as f:
f.write(supervisord_config)
if __name__ == "__main__":
main(sys.argv, os.environ)
|