Browse Source

Add Matrix: Synapse Homeserver and Riot webclient

Thomas Buck 5 years ago
parent
commit
d72da6b11b

+ 2
- 0
README.md View File

@@ -24,6 +24,7 @@ What do you get if you point Sovereign at a server? All kinds of good stuff!
24 24
 -   Mobile push notifications and autodiscovery via [Z-Push](http://z-push.sourceforge.net/soswp/index.php?pages_id=1&t=home).
25 25
 -   Email client [automatic configuration](https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration).
26 26
 -   Jabber/[XMPP](http://xmpp.org/) instant messaging via [Prosody](http://prosody.im/).
27
+-   [Matrix](https://matrix.org/) via [Riot.im](https://about.riot.im).
27 28
 -   An RSS Reader via [Selfoss](http://selfoss.aditu.de/).
28 29
 -   [CalDAV](https://en.wikipedia.org/wiki/CalDAV) and [CardDAV](https://en.wikipedia.org/wiki/CardDAV) to keep your calendars and contacts in sync, via [NextCloud](http://nextcloud.com/).
29 30
 -   Your own VPN server via [OpenVPN](http://openvpn.net/index.php/open-source.html).
@@ -118,6 +119,7 @@ Create `A` or `CNAME` records which point to your server's IP address:
118 119
 * `news.example.com` (for Selfoss)
119 120
 * `cloud.example.com` (for NextCloud)
120 121
 * `git.example.com` (for gitea)
122
+* `matrix.example.com` (for riot)
121 123
 
122 124
 ### 6. Run the Ansible Playbooks
123 125
 

+ 1
- 1
roles/common/files/letsencrypt-gencert View File

@@ -17,7 +17,7 @@ for domain in "$@"; do
17 17
   fi
18 18
 
19 19
   # subdomains - www.foo.com mail.foo.com ...
20
-  for sub in www mail autoconfig fathom news cloud git; do
20
+  for sub in www mail autoconfig fathom news cloud git matrix status; do
21 21
     # only add if the DNS entry for the subdomain does actually exist
22 22
     if (getent hosts $sub.$domain > /dev/null); then
23 23
       if [ -z "$d" ]; then

+ 25
- 0
roles/matrix/defaults/main.yml View File

@@ -0,0 +1,25 @@
1
+matrix_subdomain: "matrix"
2
+matrix_domain: "{{ matrix_subdomain }}.{{ domain }}"
3
+
4
+riot_version: "1.0.3"
5
+riot_release: "https://github.com/vector-im/riot-web/releases/download/v{{ riot_version }}/riot-v{{ riot_version }}.tar.gz"
6
+
7
+secret_root: '{{ inventory_dir | realpath }}'
8
+secret_name: 'secret'
9
+secret: '{{ secret_root + "/" + secret_name }}'
10
+
11
+synapse_admin: "{{ admin_email }}"
12
+synapse_registration_secret: "{{ lookup('password', secret + '/' + 'synapse_registration_secret length=32 chars=ascii_letters,digits') }}"
13
+synapse_pw_pepper: "{{ lookup('password', secret + '/' + 'synapse_pw_pepper length=32 chars=ascii_letters,digits') }}"
14
+
15
+synapse_accounts:
16
+  - name: "{{ main_user_name }}"
17
+    password: "{{ lookup('password', secret + '/' + 'matrix_main_user_password length=32') }}"
18
+
19
+synapse_db_username: synapseuser
20
+synapse_db_password: "{{ lookup('password', secret + '/' + 'synapse_db_password length=32') }}"
21
+synapse_db_database: synapse
22
+
23
+# must match values in roles/common
24
+db_admin_username: 'postgres'
25
+db_admin_password: "{{ lookup('password', secret + '/' + 'db_admin_password length=32') }}"

+ 3
- 0
roles/matrix/files/etc_letsencrypt_postrenew_synapse.sh View File

@@ -0,0 +1,3 @@
1
+#!/bin/bash
2
+
3
+systemctl restart matrix-synapse.service

+ 5
- 0
roles/matrix/handlers/main.yml View File

@@ -0,0 +1,5 @@
1
+- name: restart synapse
2
+  command: systemctl restart matrix-synapse
3
+
4
+- name: restart apache
5
+  service: name=apache2 state=restarted

+ 5
- 0
roles/matrix/tasks/main.yml View File

@@ -0,0 +1,5 @@
1
+---
2
+# Provides the Synapse Matrix homeserver and the Riot.im client
3
+#
4
+- include: riot.yml tags=matrix
5
+- include: synapse.yml tags=matrix

+ 28
- 0
roles/matrix/tasks/riot.yml View File

@@ -0,0 +1,28 @@
1
+- name: Create temporary Riot directory
2
+  file: state=directory path=/root/riot
3
+
4
+- name: Download Riot {{ riot_version }} release
5
+  get_url:
6
+    url="{{ riot_release }}"
7
+    dest=/root/riot/riot-{{ riot_version }}.tar.gz
8
+
9
+- name: Extract Riot archive
10
+  unarchive:
11
+    copy: no
12
+    src: /root/riot/riot-{{ riot_version }}.tar.gz
13
+    dest: /root/riot/
14
+    creates: /root/riot/riot-v{{ riot_version }}/index.html
15
+
16
+- name: Delete old Riot webroot directory
17
+  file: state=absent path=/var/www/riot
18
+
19
+- name: Configure Riot
20
+  template:
21
+    src=root_riot_config.j2
22
+    dest=/root/riot/riot-v{{ riot_version }}/config.json
23
+
24
+- name: Copy Riot to document root
25
+  shell: cp -r /root/riot/riot-v{{ riot_version }} /var/www/riot
26
+
27
+- name: Set Riot ownership
28
+  action: file owner=www-data group=www-data path=/var/www/riot recurse=yes state=directory

+ 104
- 0
roles/matrix/tasks/synapse.yml View File

@@ -0,0 +1,104 @@
1
+- name: Ensure repository key for Synapse is in place
2
+  apt_key: url=https://matrix.org/packages/debian/repo-key.asc state=present
3
+  tags:
4
+    - dependencies
5
+
6
+- name: Add Synapse repository
7
+  apt_repository: repo="deb https://matrix.org/packages/debian/ {{ ansible_distribution_release }} main"
8
+  tags:
9
+    - dependencies
10
+
11
+- name: Install Synapse and dependencies from official repository
12
+  apt:
13
+    name: "{{ packages }}"
14
+    state: present
15
+    update_cache: yes
16
+  vars:
17
+    packages:
18
+    - python-psycopg2
19
+    - matrix-synapse
20
+  tags:
21
+    - dependencies
22
+
23
+- name: Add Synapse user to ssl-cert group
24
+  user: name=matrix-synapse group=ssl-cert
25
+
26
+- name: Create Synapse data directory
27
+  file: state=directory path=/data/{{ item }} owner=matrix-synapse group=root
28
+  with_items:
29
+    - matrix-synapse
30
+    - matrix-synapse/uploads
31
+    - matrix-synapse/media
32
+
33
+- name: Configure Synapse homeserver
34
+  template:
35
+    src=etc_matrix-synapse_homeserver.j2
36
+    dest=/etc/matrix-synapse/homeserver.yaml
37
+    owner=matrix-synapse
38
+    group=root
39
+  notify: restart synapse
40
+
41
+- name: Configure Synapse server name
42
+  template:
43
+    src=etc_matrix-synapse_conf.d_server_name.j2
44
+    dest=/etc/matrix-synapse/conf.d/server_name.yaml
45
+    owner=matrix-synapse
46
+    group=root
47
+  notify: restart synapse
48
+
49
+- name: Add Synapse postgres user
50
+  postgresql_user:
51
+    login_host=localhost
52
+    login_user={{ db_admin_username }}
53
+    login_password="{{ db_admin_password }}"
54
+    name={{ synapse_db_username }}
55
+    password="{{ synapse_db_password }}"
56
+    encrypted=yes
57
+    state=present
58
+
59
+- name: Create Synapse database
60
+  postgresql_db:
61
+    login_host=localhost
62
+    login_user={{ db_admin_username }}
63
+    login_password="{{ db_admin_password }}"
64
+    name={{ synapse_db_database }}
65
+    state=present
66
+    owner={{ synapse_db_username }}
67
+    encoding='UTF8'
68
+    lc_collate='C'
69
+    lc_ctype='C'
70
+    template='template0'
71
+
72
+- name: Add cert postrenew task
73
+  copy: src=etc_letsencrypt_postrenew_synapse.sh dest=/etc/letsencrypt/postrenew/synapse.sh mode=0755
74
+
75
+- name: Set firewall rules for Synapse
76
+  ufw: rule=allow port={{ item }} proto=tcp
77
+  with_items:
78
+    - 8448  # matrix federation
79
+  tags: ufw
80
+
81
+- name: Register new Synapse service
82
+  systemd: name=matrix-synapse daemon_reload=yes enabled=yes
83
+
84
+- name: Start new Synapse instance
85
+  service: name=matrix-synapse state=started
86
+
87
+- name: Create the Apache Matrix sites config files
88
+  template:
89
+    src=etc_apache2_sites-available_matrix.j2
90
+    dest=/etc/apache2/sites-available/matrix_{{ item.name }}.conf
91
+    owner=root
92
+    group=root
93
+  with_items: "{{ virtual_domains }}"
94
+  notify: restart apache
95
+
96
+- name: Enable Apache sites (creates new sites-enabled symlinks)
97
+  command: a2ensite matrix_{{ item }}.conf creates=/etc/apache2/sites-enabled/matrix_{{ item }}.conf
98
+  notify: restart apache
99
+  with_items: "{{ virtual_domains | json_query('[*].name') }}"
100
+
101
+- name: Create Matrix / Synapse accounts
102
+  command: register_new_matrix_user -u {{ item.name }} -p {{ item.password }} -t support -a -c /etc/matrix-synapse/homeserver.yaml http://localhost:8008
103
+  with_items: "{{ synapse_accounts }}"
104
+  ignore_errors: True

+ 20
- 0
roles/matrix/templates/etc_apache2_sites-available_matrix.j2 View File

@@ -0,0 +1,20 @@
1
+<VirtualHost *:80>
2
+    ServerName {{ matrix_subdomain }}.{{ item.name }}
3
+
4
+    Redirect permanent / https://{{ item.name }}/
5
+</VirtualHost>
6
+
7
+<VirtualHost *:443>
8
+    ServerName {{ matrix_subdomain }}.{{ item.name }}
9
+
10
+    SSLEngine               On
11
+    DocumentRoot            "/var/www/riot"
12
+    DirectoryIndex          index.html
13
+    Options                 -Indexes
14
+    HostnameLookups         Off
15
+
16
+    ProxyRequests           Off
17
+    ProxyPreserveHost       On
18
+    ProxyPass               /_matrix http://localhost:8008/_matrix nocanon
19
+    ProxyPassReverse        /_matrix http://localhost:8008/_matrix
20
+</VirtualHost>

+ 9
- 0
roles/matrix/templates/etc_matrix-synapse_conf.d_server_name.j2 View File

@@ -0,0 +1,9 @@
1
+# This file is autogenerated, and will be recreated on upgrade if it is deleted.
2
+# Any changes you make will be preserved.
3
+
4
+# The domain name of the server, with optional explicit port.
5
+# This is used by remote servers to connect to this server,
6
+# e.g. matrix.org, localhost:8080, etc.
7
+# This is also the last part of your UserID.
8
+#
9
+server_name: "{{ matrix_domain }}"

+ 1035
- 0
roles/matrix/templates/etc_matrix-synapse_homeserver.j2
File diff suppressed because it is too large
View File


+ 36
- 0
roles/matrix/templates/root_riot_config.j2 View File

@@ -0,0 +1,36 @@
1
+{
2
+    "default_hs_url": "https://{{ matrix_domain }}",
3
+    "default_is_url": "https://matrix.org",
4
+    "disable_custom_urls": false,
5
+    "disable_guests": false,
6
+    "disable_login_language_selector": false,
7
+    "disable_3pid_login": false,
8
+    "brand": "Riot",
9
+    "integrations_ui_url": "https://scalar.vector.im/",
10
+    "integrations_rest_url": "https://scalar.vector.im/api",
11
+    "integrations_jitsi_widget_url": "https://scalar.vector.im/api/widgets/jitsi.html",
12
+    "bug_report_endpoint_url": "https://riot.im/bugreports/submit",
13
+    "features": {
14
+        "feature_groups": "labs",
15
+        "feature_pinning": "labs"
16
+    },
17
+    "default_federate": true,
18
+    "default_theme": "light",
19
+    "roomDirectory": {
20
+        "servers": [
21
+            "{{ matrix_domain }}",
22
+            "matrix.org"
23
+        ]
24
+    },
25
+    "welcomeUserId": "@riot-bot:matrix.org",
26
+    "piwik": {
27
+        "url": "https://piwik.riot.im/",
28
+        "whitelistedHSUrls": ["https://matrix.org"],
29
+        "whitelistedISUrls": ["https://vector.im", "https://matrix.org"],
30
+        "siteId": 1
31
+    },
32
+    "enable_presence_by_hs_url": {
33
+        "{{ matrix_domain }}": true,
34
+        "https://matrix.org": false
35
+    }
36
+}

+ 8
- 0
roles/monitoring/files/etc_monit_conf.d_matrix View File

@@ -0,0 +1,8 @@
1
+check process synapse matching /opt/venvs/matrix-synapse/bin/python
2
+  group social
3
+  start program = "/bin/systemctl start matrix-synapse"
4
+  stop program = "/bin/systemctl stop matrix-synapse"
5
+  if failed port 8448 type tcp
6
+    with timeout 10 seconds
7
+    then restart
8
+  if 5 restarts within 5 cycles then timeout

+ 9
- 0
roles/monitoring/tasks/monit.yml View File

@@ -40,6 +40,10 @@
40 40
   stat: path=/etc/gitea/app.ini
41 41
   register: gitea_config_file
42 42
 
43
+- name: Determine if Synapse is installed
44
+  stat: path=/etc/matrix-synapse/homeserver.yaml
45
+  register: synapse_config_file
46
+
43 47
 - name: Copy ZNC monit service config files into place
44 48
   copy: src=etc_monit_conf.d_znc dest=/etc/monit/conf.d/znc
45 49
   notify: restart monit
@@ -70,6 +74,11 @@
70 74
   notify: restart monit
71 75
   when: gitea_config_file.stat.exists == True
72 76
 
77
+- name: Copy Synapse monit service config files into place
78
+  copy: src=etc_monit_conf.d_matrix dest=/etc/monit/conf.d/matrix
79
+  notify: restart monit
80
+  when: synapse_config_file.stat.exists == True
81
+
73 82
 - name: Copy monit service config files into place
74 83
   copy: src=etc_monit_conf.d_{{ item }} dest=/etc/monit/conf.d/{{ item }}
75 84
   with_items:

+ 1
- 0
site.yml View File

@@ -16,5 +16,6 @@
16 16
     - gitea
17 17
     - ircbouncer
18 18
     - xmpp
19
+    - matrix
19 20
     - vpn
20 21
     - monitoring  # Monitoring role should be last. See roles/monitoring/README.md

Loading…
Cancel
Save