forked from Josue-T/synapse_ynh
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathupgrade
396 lines (308 loc) · 15.1 KB
/
upgrade
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/bin/bash
source _common.sh
source experimental_helper.sh
source /usr/share/yunohost/helpers
ynh_app_setting_set_default --key=php_upload_max_filesize --value=100M
#=================================================
# SET ALL CONSTANT
#=================================================
#=================================================
# MIGRATION 5 : Manage old settings
#=================================================
# Migrate from settings 'special_domain' to 'domain' and 'special_path' to 'path'
if [ -z "${domain:-}" ]; then
domain=$(ynh_app_setting_get --key=special_domain)
path=$(ynh_app_setting_get --key=special_path)
ynh_app_setting_set --key=domain --value=$domain
ynh_app_setting_set --key=path --value=$path
ynh_app_setting_delete --key=special_domain
ynh_app_setting_delete --key=special_path
ynh_app_setting_set --key=no_sso --value=true
fi
ensure_vars_set
# Define $server_name if not already defined
ynh_app_setting_set_default --app="$app" --key=server_name --value="$domain"
# Define $jitsi_server if not already defined
ynh_app_setting_set_default --app="$app" --key=jitsi_server --value='jitsi.riot.im'
if [ "$e2e_enabled_by_default" = "true" ] ; then
e2e_enabled_by_default="all"
ynh_app_setting_set --key=e2e_enabled_by_default --value=$e2e_enabled_by_default
fi
if [ "$e2e_enabled_by_default" = "false" ]; then
e2e_enabled_by_default="off"
ynh_app_setting_set --key=e2e_enabled_by_default --value=$e2e_enabled_by_default
fi
if [ "${registrations_require_3pid}" == none ]
then
registrations_require_3pid=email
ynh_app_setting_set --key=registrations_require_3pid --value=$registrations_require_3pid
fi
if [[ "${allowed_local_3pids_email}" =~ \'.*\' ]] # Also remove shit value from previous config panel
then
allowed_local_3pids_email=''
ynh_app_setting_set --key=allowed_local_3pids_email --value=$allowed_local_3pids_email
fi
if [[ "${allowed_local_3pids_msisdn}" =~ \'.*\' ]] # Also remove shit value from previous config panel
then
allowed_local_3pids_msisdn=''
ynh_app_setting_set --key=allowed_local_3pids_msisdn --value=$allowed_local_3pids_msisdn
fi
if [ -z "${enable_registration:-}" ]
then
if [ -z "${is_free_registration:-}" ]; then
is_free_registration=$(ynh_app_setting_get --key=is_""public)
fi
if [ "$is_free_registration" -eq "0" ]
then
enable_registration="false"
password_enabled="false"
else
enable_registration="true"
password_enabled="true"
fi
ynh_app_setting_set --key=enable_registration --value=$enable_registration
ynh_app_setting_set --key=password_enabled --value=$password_enabled
fi
# remove legacy env file into /etc/default
ynh_safe_rm /etc/default/coturn-$app
#=================================================
# MIGRATION 7 : STANDARDIZE SYSTEMD UNIT
#=================================================
if [ -e /etc/systemd/system/matrix-$app.service ]
then
ynh_script_progression 'Migrating systemd unit to standard name...'
systemctl stop matrix-$app.service || true
systemctl stop coturn-$app.service || true
yunohost service remove matrix-$app || true
yunohost service remove coturn-$app || true
ynh_safe_rm /etc/systemd/system/matrix-$app.service
ynh_safe_rm /etc/systemd/system/coturn-$app.service
touch /etc/systemd/system/$app.service
systemctl daemon-reload || true
fi
#=================================================
# STOP SERVICES
#=================================================
# We stop the service
ynh_systemctl --service=$app.service --action=stop
ynh_script_progression 'Managing migrations...'
#=================================================
# MIGRATION 6 : Migrate data directory
#=================================================
if [ -e "/var/lib/matrix-$app" ]; then
ynh_script_progression "Moving data directory to $data_dir..."
if [ -e "$data_dir" ]; then
old_data_dir_path="$data_dir$(date '+%Y%m%d.%H%M%S')"
ynh_print_warn "A data directory already exist. Data was renamed to $old_data_dir_path"
mv "$data_dir" "$old_data_dir_path"
fi
mv "/var/lib/matrix-$app" "$data_dir"
fi
#=================================================
# MIGRATION 3 : USE STANDARD ACCESS FOR CERTIFCATE
#=================================================
# Fix issue about certificates access
if [ ! $(grep "ssl-cert:x:[0-9]*:.*$app" /etc/group) ]
then
ynh_script_progression "Use standard access for certificate..."
adduser $app ssl-cert
adduser turnserver ssl-cert
fi
#=================================================
# MIGRATION 4 : CREATE A DH FILE
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
# For any update do it in all files
# Make dh cert for synapse if it doesn't exist
if [ ! -e /etc/ssl/private/dh2048.pem ]
then
ynh_script_progression "Creating a dh file..."
ynh_hide_warnings openssl dhparam -out /etc/ssl/private/dh2048.pem -outform PEM -dsaparam 2048
chown root:ssl-cert /etc/ssl/private/dh2048.pem
chmod 640 /etc/ssl/private/dh2048.pem
fi
#=================================================
# MIGRATION 2 : MULTINSTANCE SUPPORT
#=================================================
if [ ! -e /etc/matrix-$app/coturn.conf ]
then
ynh_script_progression "Creating an independant service for coturn..."
#=================================================
# CREATE AN INDEPENDANT SERVICE FOR COTURN
#=================================================
# Disable default config for turnserver and create a new service
systemctl stop coturn.service
#=================================================
# MAKE A CLEAN LOGROTATE CONFIG
#=================================================
ynh_config_add_logrotate /var/log/matrix-$app
fi
#=================================================
# MIGRATION 8 : Migrate database to managed database (Migrate db name from matrix_$app to $app)
#=================================================
if ynh_psql_db_shell <<< '\list' | grep matrix_$app; then
ynh_psql_drop_db "$db_name"
sudo --login --user=postgres psql <<< "ALTER DATABASE matrix_$app RENAME TO $db_name;"
ynh_psql_db_shell <<< "REASSIGN OWNED BY matrix_$app TO $db_user;"
ynh_psql_db_shell <<< "UPDATE pg_database SET datcollate='C', datctype='C' WHERE datname='$db_name';"
ynh_psql_db_shell <<< "DROP USER matrix_$app;"
# for unknown reason we need to set again the password for synapse user. Without this synapse can't authenticate to postgresql
ynh_psql_db_shell <<< "ALTER USER $db_user WITH ENCRYPTED PASSWORD '$db_pwd';"
fi
#=================================================
# MIGRATION 9 : migrate data path (from matrix-$app to $app)
#=================================================
if [ -e /home/yunohost.app/matrix-$app ]; then
mv -t $data_dir /home/yunohost.app/matrix-$app/*
ynh_safe_rm /home/yunohost.app/matrix-$app
fi
#=================================================
# MIGRATION 11 : make this app using full domain
#=================================================
ynh_app_setting_set --key=path --value=/
if yunohost app map -r --output-as json | jq -r '."'$domain'" | select( . != null ) | .[] | .id' | grep -v "$app" -q; then
ynh_print_warn 'An other app is installed on this domain. Now synapse require to be alone on the domain.'
ynh_print_warn 'To solve this you can:'
ynh_print_warn " - Remove or move all other app which use '$domain'"
ynh_print_warn ' - Change the domain of synapse. You can find more informations here: https://github.com/YunoHost-Apps/synapse_ynh/tree/testing/doc/ADMIN.md#change-url'
ynh_print_warn 'For more information you can see this issue: https://github.com/YunoHost-Apps/synapse_ynh/issues/443'
fi
#=================================================
# MIGRATION 12 : update system user and drop yunohost user
#=================================================
if grep -q "^matrix-$app" /etc/passwd; then
# Must stop php before remove user as user is used by php
systemctl stop php7.4-fpm.service || true
systemctl stop php8.2-fpm.service || true
systemctl stop php8.3-fpm.service || true
ynh_''system_user_delete --username=matrix-"$app" || true
yunohost user delete $app || true
ynh_''system_user_create --username=$app --home_dir=$code_dir
adduser $app ssl-cert
systemctl start php7.4-fpm.service || true
systemctl start php8.2-fpm.service || true
systemctl start php8.3-fpm.service || true
fi
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression "Upgrading source files..."
install_sources
#=================================================
# MIGRATION 1 : GENERATE SYNAPSE SECRET
#=================================================
if [ -z "${registration_shared_secret:-}" ] || [ "$form_secret" == "form_secret: " ]
then
ynh_script_progression "Generating synapse secret..."
# Generate config and keys
"$code_dir"/bin/python -m synapse.app.homeserver --keys-directory /etc/matrix-"$app"/ --generate-config --generate-keys --server-name "$server_name" --report-stats=no -c homeserver.yml
# Get random values from config
registration_shared_secret=$(egrep "^registration_shared_secret:" homeserver.yml | cut -d'"' -f2)
form_secret=$(egrep "^form_secret:" homeserver.yml | cut -d'"' -f2)
# store in yunohost settings
ynh_app_setting_set --key=registration_shared_secret --value="$registration_shared_secret"
ynh_app_setting_set --key=form_secret --value="$form_secret"
fi
#=================================================
#=================================================
# UPDATE SYNAPSE CONFIG
#=================================================
ynh_script_progression "Updating synapse config..."
ynh_config_add --jinja --template="homeserver.yaml" --destination="/etc/matrix-$app/homeserver.yaml"
ynh_config_add --template="log.yaml" --destination="/etc/matrix-$app/log.yaml"
#=================================================
# CREATE SMALL CAS SERVER
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
mkdir -p "$install_dir"
cp ../sources/cas_server.php "$install_dir"/
chmod u=rwX,g=rX,o= -R "$install_dir"
chown "$app":root -R "$install_dir"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression "Upgrading NGINX web server configuration..."
# Create a dedicated php-fpm config
ynh_script_progression "Configuring application..."
ynh_config_add_phpfpm
configure_nginx
#=================================================
# UPDATE COTURN CONFIG
#=================================================
ynh_script_progression "Updating Coturn config..."
configure_coturn
#=================================================
# ADD SCRIPT FOR COTURN CRON AND APP SERVICE
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
ynh_config_add --template="../sources/Coturn_config_rotate.sh" --destination="$code_dir/Coturn_config_rotate.sh"
ynh_config_add --template="../sources/update_synapse_for_appservice.sh" --destination="$code_dir/update_synapse_for_appservice.sh"
ynh_config_add --template=../sources/set_admin_user.sh --destination=$code_dir/set_admin_user.sh
# Ensure app-service folder has exists and the config file exit (Migration)
mkdir -p /etc/matrix-$app/app-service
if [ -e /etc/matrix-$app/conf.d/app_service.yaml ]; then
# ensure that the file is empty if no app service are installed (fix issue #385)
if [ ! "$(ls -A /etc/matrix-$app/app-service)" ]; then
echo "" > /etc/matrix-$app/conf.d/app_service.yaml
fi
else
touch /etc/matrix-$app/conf.d/app_service.yaml
fi
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add "$app" --log "/var/log/matrix-$app/homeserver.log" --needs_exposed_ports "$port_synapse_tls" --description 'Main matrix server service.'
yunohost service add "$app"-coturn --needs_exposed_ports "$port_turnserver_tls" --description 'Turn server for matrix server. Used for audio and video call.'
#=================================================
# UPDATE SYSTEMD
#=================================================
ynh_script_progression "Upgrading systemd configuration..."
# Create systemd service for synapse and turnserver
ynh_config_add_systemd --template=synapse.service
cp ../conf/default_coturn /etc/matrix-"$app"/coturn_env
ynh_config_add_systemd --service="$app"-coturn --template=synapse-coturn.service
#=================================================
# UPGRADE FAIL2BAN
#=================================================
ynh_script_progression "Reconfiguring Fail2Ban..."
ynh_config_add_fail2ban --logpath=/var/log/matrix-"$app"/homeserver.log
#=================================================
# SETUP PERMISSIONS
#=================================================
ynh_script_progression "Configuring permissions..."
if yunohost --output-as plain domain list | grep -q "^$server_name"'$'; then
if ! ynh_""permission_exists --permission=server_client_infos; then
ynh_""permission_create --permission=server_client_infos --url="$server_name"/.well-known/matrix \
--label="Server info for clients. (well-known)" --show_tile=false --allowed=visitors \
--auth_header=false --protected=true
else yunohost --output-as plain domain list | grep -q "^$server_name"'$'
ynh_""permission_url --permission=server_client_infos --url="$server_name"/.well-known/matrix \
--auth_header=false
ynh_""permission_update --permission=server_client_infos --label="Server info for clients. (well-known)" --show_tile=false \
--protected=true
fi
fi
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
ynh_script_progression "Protecting directories... (note that it could take a long time depending of your install size)"
set_permissions data
#=================================================
# UPDATE HOOKS
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE
# For any update do it in all files
ynh_replace --match=__APP__ --replace="$app" --file=../hooks/post_cert_update
ynh_replace --match=__DOMAIN__ --replace="$domain" --file=../hooks/post_cert_update
#=================================================
# RELOAD SERVICES
#=================================================
ynh_script_progression "Restarting Synapse services..."
ynh_systemctl --service="$app"-coturn.service --action=restart
ynh_systemctl --service="$app".service --action=restart --wait_until="Synapse now listening on TCP port $port_synapse_tls" --log_path="/var/log/matrix-$app/homeserver.log" --timeout=300
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression "Upgrade of $app completed"