Compare commits

..

4 Commits

Author SHA1 Message Date
uumas
2be85606c5 v0.0.2 2023-04-19 15:33:14 +03:00
uumas
db29f26d7a formatting 2023-04-19 15:29:15 +03:00
uumas
e8696ea656 Remove worker pidfiles, not needed as result of removing synchrotron-balancer 2023-04-19 15:27:01 +03:00
uumas
0f86bfbc02 Implement synchrotron balancing in nginx, replaces need for matrix-synchrotron-balancer 2023-04-19 15:19:30 +03:00
13 changed files with 51 additions and 135 deletions

View File

@@ -5,6 +5,5 @@ Roles for matrix services
# To do # To do
- Add argument spec validation - Add argument spec validation
- Move ldap auth provider from password providers to collections - Move ldap auth provider from password providers to collections
- Implement synchrotron balancing in nginx
- Make updates optional (use state: present instead of state: latest unless requested) - Make updates optional (use state: present instead of state: latest unless requested)
- Only restart extras being changed, not all - Only restart extras being changed, not all

View File

@@ -3,7 +3,7 @@
namespace: uumas namespace: uumas
name: matrix name: matrix
description: Matrix roles description: Matrix roles
version: 0.0.1 version: 0.0.2
readme: README.md readme: README.md
repository: https://git.uumas.fi/uumas/ansible-matrix repository: https://git.uumas.fi/uumas/ansible-matrix
license_file: LICENSE license_file: LICENSE

View File

@@ -21,13 +21,15 @@ nginx_upstreams:
additional_options: additional_options:
- "client_max_body_size {{ matrix_max_upload_size_mb }}M" - "client_max_body_size {{ matrix_max_upload_size_mb }}M"
synchrotron_balancer: synapse_generic_sync:
servers: "{{ ['8183'] if synapse_workers.generic_sync is defined else '' }}" servers: "{{ synapse_workers.generic_sync | default('') }}"
method: 'hash $mxid_localpart'
locations: locations:
- name: '~ ^/_matrix/client/(api/v1|r0|v3)/events$' - name: '~ ^/_matrix/client/(api/v1|r0|v3)/events$'
- name: "{{ '^/_matrix/client/(v2_alpha|r0|v3)/sync$' if 'generic_init_sync' not in synapse_workers | default('') else '' }}" - name: "{{ '^/_matrix/client/(v2_alpha|r0|v3)/sync$' if 'generic_init_sync' not in synapse_workers | default('') else '' }}"
synchrotron_init: synapse_generic_init_sync:
servers: "{{ ['8184'] if synapse_workers.generic_init_sync is defined else '' }}" servers: "{{ synapse_workers.generic_init_sync | default('') }}"
method: 'hash $mxid_localpart'
locations: locations:
- name: '~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$' - name: '~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$'
- name: '~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$' - name: '~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$'
@@ -190,14 +192,25 @@ nginx_upstreams:
nginx_maps: nginx_maps:
sync: sync:
var: "{{ 'arg_since' if synapse_workers.generic_sync is defined and synapse_workers.generic_init_sync is defined else '' }}" var: "{{ 'arg_since' if synapse_workers.generic_sync is defined and synapse_workers.generic_init_sync is defined else '' }}"
rules: rules:
default: synchrotron_balancer default: synapse_generic_sync
"''": synchrotron_init "''": synapse_generic_init_sync
locations: locations:
- name: '~ ^/_matrix/client/(r0|v3)/sync$' - name: '~ ^/_matrix/client/(r0|v3)/sync$'
additional_options: additional_options:
- 'proxy_read_timeout 1h' - 'proxy_read_timeout 1h'
mxid_localpart_urlparam:
var: arg_access_token
rules:
default: $arg_access_token
"'~syt_(?<username>.*?)_.*'": $username
mxid_localpart:
var: http_authorization
rules:
default: $http_authorization
"'~Bearer syt_(?<username>.*?)_.*'": $username
"''": $mxid_localpart_urlparam
nginx_servers: nginx_servers:
- listen: - listen:
@@ -225,14 +238,14 @@ nginx_servers:
- synapse_main - synapse_main
- matrix_media_repo - matrix_media_repo
- synapse_media_repository - synapse_media_repository
- synapse_generic_sync
- synapse_generic_init_sync
- synapse_generic_client - synapse_generic_client
- synapse_generic_login - synapse_generic_login
- synapse_generic_event_send - synapse_generic_event_send
- synapse_generic_pagination - synapse_generic_pagination
- synapse_user_dir - synapse_user_dir
- synapse_frontend_proxy - synapse_frontend_proxy
- synchrotron_balancer
- synchrotron_init
- synapse_device_persister - synapse_device_persister
- synapse_typing_persister - synapse_typing_persister
- synapse_account_persister - synapse_account_persister

View File

@@ -9,10 +9,6 @@ matrix_max_upload_size_mb: 100
synapse_metrics: false synapse_metrics: false
synapse_presence: true synapse_presence: true
synchrotron_workers:
balancer: generic_sync
init: generic_init_sync
persister_workers: persister_workers:
- event_persister - event_persister
- typing_persister - typing_persister

View File

@@ -21,3 +21,10 @@
enabled: true enabled: true
loop: "{{ synapse_worker_services }}" loop: "{{ synapse_worker_services }}"
notify: Config matrix target notify: Config matrix target
- name: Disable matrix-synchrotrons
ansible.builtin.systemd:
daemon_reload: true
name: matrix-synchrotron@{{ item }}.service
enabled: false
loop: "{{ synapse_synchrotrons }}"

View File

@@ -1,23 +0,0 @@
---
- name: Ensure workdir exists for matrix-synchrotron-{{ item }}
ansible.builtin.file:
path: /opt/matrix-synchrotron/{{ item }}
state: directory
mode: 0755
- name: Ensure config in place for matrix-synchrotron-{{ item }}
ansible.builtin.template:
src: matrix-synchrotron-config.yaml.j2
dest: /opt/matrix-synchrotron/{{ item }}/config.yaml.ansibled
mode: 0644
notify: Config synapse service
register: config
- name: Copy config to final destination for matrix-synchrotron-{{ item }}
ansible.builtin.copy:
src: /opt/matrix-synchrotron/{{ item }}/config.yaml.ansibled
dest: /opt/matrix-synchrotron/{{ item }}/config.yaml
remote_src: true
mode: 0644
when: config.changed

View File

@@ -1,53 +1,13 @@
--- ---
- name: Install git and golang - name: Ensure matrix-synchrotron user not present
ansible.builtin.apt:
name:
- git
- golang
- name: Create matrix-synchrotron user
ansible.builtin.user: ansible.builtin.user:
name: matrix-synchrotron name: matrix-synchrotron
system: true state: absent
home: /opt/matrix-synchrotron remove: true
shell: /bin/false
- name: Install matrix-synchrotron - name: Ensure matrix-synchrotron unit not present
become_user: matrix-synchrotron ansible.builtin.file:
block: path: /etc/systemd/system/matrix-synchrotron@.service
- name: Clone matrix-synchrotron git repo state: absent
ansible.builtin.git: notify: Disable matrix-synchrotrons
repo: https://github.com/Sorunome/matrix-synchrotron-balancer.git
dest: /opt/matrix-synchrotron/src
force: true
register: install
- name: Build matrix-synchrotron
ansible.builtin.command: go build
args:
chdir: /opt/matrix-synchrotron/src
when: install.changed
notify: Config synapse service
- name: Configure matrix-synchrotron(s)
ansible.builtin.include_tasks: matrix-synchrotron.yml
loop: "{{ synapse_synchrotrons }}"
loop_control:
extended: true
- name: Put systemd matrix-synchrotron unit in place
ansible.builtin.template:
src: systemd/matrix-synchrotron@.service.j2
dest: /etc/systemd/system/matrix-synchrotron@.service
mode: "644"
register: systemd_unit
- name: Enable systemd unit matrix-synchrotron-{{ item }}
ansible.builtin.systemd:
daemon_reload: true
name: matrix-synchrotron@{{ item }}.service
enabled: true
state: restarted
when: systemd_unit.changed
loop: "{{ synapse_synchrotrons }}"

View File

@@ -27,26 +27,22 @@
synapse_worker_services: [] synapse_worker_services: []
synapse_synchrotrons: [] synapse_synchrotrons: []
- name: Ensure worker pidfile dir in /run exists
ansible.builtin.file:
path: /run/matrix-synapse/
state: directory
owner: matrix-synapse
group: nogroup
mode: 0755
- name: Ensure tmpfiles config for pidfile dir in place
ansible.builtin.template:
src: tmpfiles-matrix-synapse.conf.j2
dest: /etc/tmpfiles.d/matrix-synapse.conf
mode: 0644
- name: Configure workers - name: Configure workers
ansible.builtin.include_tasks: worker_type.yml ansible.builtin.include_tasks: worker_type.yml
loop: "{{ synapse_workers | dict2items }}" loop: "{{ synapse_workers | dict2items }}"
loop_control: loop_control:
loop_var: synapse_worker loop_var: synapse_worker
- name: Configure synchrotron balancer - name: Ensure worker pidfile dir in /run not present (legacy)
ansible.builtin.file:
path: /run/matrix-synapse/
state: absent
- name: Ensure tmpfiles config for pidfile dir not present (legacy)
ansible.builtin.file:
path: /etc/tmpfiles.d/matrix-synapse.conf
state: absent
- name: Ensure synchrotron balancer removed (legacy)
ansible.builtin.include_tasks: matrix-synchrotrons.yml ansible.builtin.include_tasks: matrix-synchrotrons.yml
when: synapse_synchrotrons | length != 0 when: synapse_synchrotrons | length != 0

View File

@@ -1,12 +0,0 @@
# {{ ansible_managed }}
homeserver_url: http://localhost:8008 # homeserver URL for the whoami request
listener: localhost:{{ 8183 + ansible_loop.index0 }} # host:port to listen to
synchrotrons:
{% for port in synapse_workers[synchrotron_workers[item]] %}
- address: 127.0.0.1:{{ port }}
pid_file: /run/matrix-synapse/{{ synchrotron_workers[item] }}-{{ loop.index0 }}.pid
{% endfor %}
balancer:
interval: 2

View File

@@ -10,12 +10,10 @@ After=matrix-synapse.service
[Service] [Service]
Type=notify Type=notify
NotifyAccess=main NotifyAccess=main
PIDFile=/run/matrix-synapse/%i.pid
User=matrix-synapse User=matrix-synapse
WorkingDirectory=/var/lib/matrix-synapse WorkingDirectory=/var/lib/matrix-synapse
EnvironmentFile=-/etc/default/matrix-synapse EnvironmentFile=-/etc/default/matrix-synapse
ExecStart=/opt/venvs/matrix-synapse/bin/python -m synapse.app.generic_worker --config-path=/etc/matrix-synapse/homeserver.yaml --config-path=/etc/matrix-synapse/conf.d/ --config-path=/etc/matrix-synapse/workers/%i.yaml ExecStart=/opt/venvs/matrix-synapse/bin/python -m synapse.app.generic_worker --config-path=/etc/matrix-synapse/homeserver.yaml --config-path=/etc/matrix-synapse/conf.d/ --config-path=/etc/matrix-synapse/workers/%i.yaml
ExecStartPost=/bin/sh -c "echo $MAINPID > /run/matrix-synapse/%i.pid"
ExecReload=/bin/kill -HUP $MAINPID ExecReload=/bin/kill -HUP $MAINPID
Restart=always Restart=always
RestartSec=3 RestartSec=3

View File

@@ -1,17 +0,0 @@
# {{ ansible_managed }}
[Unit]
Description=Matrix Synchrotron %i
After=matrix-synapse.service
PartOf=matrix-synapse.service
[Service]
Type=simple
User=matrix-synchrotron
WorkingDirectory=/opt/matrix-synchrotron/%i
ExecStart=/opt/matrix-synchrotron/src/matrix-synchrotron-balancer
Restart=always
RestartSec=3
[Install]
WantedBy=matrix-synapse.service

View File

@@ -1 +0,0 @@
d /run/matrix-synapse 0755 matrix-synapse nogroup - -