From 08cea75454ac323d7f0ff35af50952841f4732ed Mon Sep 17 00:00:00 2001 From: Sasi Olin Date: Jun 13 2022 07:18:01 +0000 Subject: Generate workers configuration from data --- diff --git a/pillar/role/matrix.sls b/pillar/role/matrix.sls index 797f524..b3a3781 100644 --- a/pillar/role/matrix.sls +++ b/pillar/role/matrix.sls @@ -215,6 +215,7 @@ nginx: - location /_matrix: - proxy_set_header: X-Forwarded-For $remote_addr - proxy_pass: http://localhost:8008 + - include: /etc/matrix-synapse/workers/nginx.conf enabled: True webhook.opensuse.org: config: diff --git a/salt/profile/matrix/config.sls b/salt/profile/matrix/config.sls index c6632df..0deffae 100644 --- a/salt/profile/matrix/config.sls +++ b/salt/profile/matrix/config.sls @@ -50,3 +50,41 @@ synapse_conf_file: - mode: 640 - user: root - group: synapse + +workers_conf_dir: + file.directory: + - name: /etc/matrix-synapse/workers/ + +workers_nginx_file: + file.managed: + - name: /etc/matrix-synapse/workers/nginx.conf + - source: salt://profile/matrix/files/workers.nginx + - template: jinja + - require: + - file: workers_conf_dir + +{% set workers = salt['pillar.get']('profile:matrix:workers') %} + +{% for app, types in workers.items() %} +{% for type in types.items() %} +{% for worker, port in type.get('workers').items() %} +/etc/matrix-synapse/workers/{{worker}}.yaml: + file.managed: + - source: salt://profile/matrix/files/worker.yaml + - template: jinja + - context: + worker: {{ worker }} + port: {{ port }} + app: {{ app }} + resources: {{ type.get('resources') }} + config {{ type.get('config') }} + - require: + - file: workers_conf_dir + - require_in: + - service: {{worker}}_service + - watch_in: + - module: {{worker}}_restart + +{% endfor %} +{% endfor %} +{% endfor %} diff --git a/salt/profile/matrix/files/worker.yaml b/salt/profile/matrix/files/worker.yaml new file mode 100644 index 0000000..c890856 --- /dev/null +++ b/salt/profile/matrix/files/worker.yaml @@ -0,0 +1,26 @@ +worker_app: synapse.app.{{ app }} +worker_name: {{ worker }} + +# The replication listener on the main synapse process. +worker_replication_host: 127.0.0.1 +worker_replication_http_port: 9093 + +worker_listeners: + - type: http + port: {{ port }} + resources: + - names: + {%- if resources %} + {%- for resource in resources.items() %} + - {{ resource }} + {%- endfor %} + {%- else %} + - client + - federation + {%- endif %} + +{% for setting, value in config.items() %} +{{ setting }}: {{ value }} +{% endfor %} + +worker_log_config: /etc/matrix-synapse/worker-log.yaml diff --git a/salt/profile/matrix/files/workers.nginx b/salt/profile/matrix/files/workers.nginx new file mode 100644 index 0000000..17dff87 --- /dev/null +++ b/salt/profile/matrix/files/workers.nginx @@ -0,0 +1,22 @@ +{% set workers = salt['pillar.get']('profile:matrix:workers') %} + +{% for app, types in workers.items() %} +{% for type in types.items() %} + +upstream {{ app }}_{{ loop.index }} { + {{ type.get('upstream_balancing') }} + {%- for worker, port in type.get('workers').items() %} + server 127.0.0.1:{{ port }}; # {{ worker }} + {%- endfor %} +} + +{%- for uri in type.get('rest').items() %} +location ~ {{ uri }} { + proxy_pass http://{{ app }}_{{ loop.index }}$request_uri; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header Host $host; +} +{% endfor %} + +{% endfor %} +{% endfor %} diff --git a/salt/profile/matrix/init.sls b/salt/profile/matrix/init.sls index 3b4aa2c..fd2ce94 100644 --- a/salt/profile/matrix/init.sls +++ b/salt/profile/matrix/init.sls @@ -9,3 +9,27 @@ synapse_restart: - m_name: matrix-synapse.target - require: - service: synapse_service + +{% set workers = salt['pillar.get']('profile:matrix:workers') %} + +{% for app, types in workers.items() %} +{% for type in types.items() %} +{% for worker, port in type.get('workers').items() %} + +{{worker}}_service: + service.running: + - name: matrix-synapse-worker@{{worker}}.service + - enable: True + - require: + - file: synapse_worker_systemd_file + +{{worker}}_restart: + module.wait: + - name: service.restart + - m_name: matrix-synapse-worker@{{worker}}.service + - require: + - service: {{worker}}_service + +{% endfor %} +{% endfor %} +{% endfor %}