aboutsummaryrefslogtreecommitdiff
path: root/scripts/upgrade
blob: fd58f16b9cad12de5201116f3c07cf51f88687d0 (plain)
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
#!/bin/bash

#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source _common.sh
source ynh_install_ruby__2
source ynh_add_swap
source /usr/share/yunohost/helpers

#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=4

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
is_public=$(ynh_app_setting_get --app=$app --key=is_public)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
language=$(ynh_app_setting_get --app=$app --key=language)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)

db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd)
admin_mail=$(ynh_user_get_info --username=$admin --key='mail')
port_web=$(ynh_app_setting_get --app=$app --key=port_web)
port_stream=$(ynh_app_setting_get --app=$app --key=port_stream)

paperclip_secret=$(ynh_app_setting_get --app=$app --key=paperclip_secret)
secret_key_base=$(ynh_app_setting_get --app=$app --key=secret_key_base)
otp_secret=$(ynh_app_setting_get --app=$app --key=otp_secret)
vapid_private_key=$(ynh_app_setting_get --app=$app --key=vapid_private_key)
vapid_public_key=$(ynh_app_setting_get --app=$app --key=vapid_public_key)

#=================================================
# CHECK VERSION
#=================================================
ynh_script_progression --message="Checking version..." --weight=1

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1

config="$final_path/live/.env.production"

# If db_name doesn't exist, create it
if [ -z "$db_name" ]; then
	db_name="${app}_production"
	ynh_app_setting_set --app=$app --key=db_name --value=$db_name
fi

# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
	final_path=/var/www/$app
	ynh_app_setting_set --app=$app --key=final_path --value=$final_path
fi

# Check if admin is not null
if [[ "$admin" = "" || "$language" = "" ]]; then
    echo "Unable to upgrade, please contact support"
    ynh_die
fi

# If port_web doesn't exist, create it, need for old install
if [[ -z "$port_web" ]]; then
	port_web=3000
	ynh_app_setting_set --app=$app --key=port_web --value=$port_web
fi

# If port_web doesn't exist, create it, need for old install
if [[ -z "$port_stream" ]]; then
	port_stream=4000
	ynh_app_setting_set --app=$app --key=port_stream --value=$port_stream
fi

# If db_pwd doesn't exist, create it, need for old install
if [[ -z "$db_pwd" ]]; then
	db_pwd=$(ynh_string_random)
	ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
	ynh_psql_test_if_first_run
	sudo --login --user=postgres psql -c"ALTER user $app WITH PASSWORD '$db_pwd'" postgres
	ynh_replace_string --match_string="DB_PASS=" --replace_string="DB_PASS=${db_pwd}" --target_file="$config"
fi

# If paperclip_secret doesn't exist, retrieve it or create it
if [[ -z "$paperclip_secret" ]]; then
	paperclip_secret=$(grep -oP "PAPERCLIP_SECRET=\K\w+" $config)
	if [[ -z "$paperclip_secret" ]]; then
	    paperclip_secret=$(head -n128 /dev/urandom | tail -n +1 | tr -dc -d 'a-z0-9' | head -c128)
	fi
	ynh_app_setting_set --app=$app --key=paperclip_secret --value="$paperclip_secret"
fi

# If secret_key_base doesn't exist, retrieve it or create it
if [[ -z "$secret_key_base" ]]; then
	secret_key_base=$(grep -oP "SECRET_KEY_BASE=\K\w+" $config)
	if [[ -z "$secret_key_base" ]]; then
	    secret_key_base=$(head -n128 /dev/urandom | tail -n +1 | tr -dc -d 'a-z0-9' | head -c128)
	fi
	ynh_app_setting_set --app=$app --key=secret_key_base --value="$secret_key_base"
fi

# If otp_secret doesn't exist, retrieve it or create it
if [[ -z "$otp_secret" ]]; then
	otp_secret=$(grep -oP "OTP_SECRET=\K\w+" $config)
	if [[ -z "$otp_secret" ]]; then
	    otp_secret=$(head -n128 /dev/urandom | tail -n +1 | tr -dc -d 'a-z0-9' | head -c128)
	fi
	ynh_app_setting_set --app=$app --key=otp_secret --value="$otp_secret"
fi

# If vapid_private_key doesn't exist, retrieve it or create it
if [[ -z "$vapid_private_key" ]]; then
	vapid_private_key=$(grep -oP "VAPID_PRIVATE_KEY=\K.+" $config)
	vapid_public_key=$(grep -oP "VAPID_PUBLIC_KEY=\K.+" $config)
	ynh_app_setting_set "$app" vapid_private_key "$vapid_private_key"
	ynh_app_setting_set "$app" vapid_public_key "$vapid_public_key"
fi

#Remove previous added repository
ynh_remove_extra_repo

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=442

# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
	ynh_clean_check_starting
	# restore it if the upgrade fails
	ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

#=================================================
# CHECK THE PATH
#=================================================

# Normalize the URL path syntax
# N.B. : this is for app installations before YunoHost 2.7
# where this value might be something like /foo/ or foo/
# instead of /foo ....
# If nobody installed your app before 2.7, then you may
# safely remove this line
path_url=$(ynh_normalize_url_path --path_url=$path_url)

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=22

ynh_systemd_action --service_name=${app}-web --action="stop" --log_path=systemd --line_match="Stopped"
ynh_systemd_action --service_name=${app}-sidekiq --action="stop" --log_path=systemd --line_match="Stopped"
ynh_systemd_action --service_name=${app}-streaming --action="stop" --log_path=systemd --line_match="Stopped"

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
    ynh_script_progression --message="Upgrading source files..." --weight=14

    # Download Mastodon
    tmpdir="$(mktemp -d)"

    mkdir $tmpdir/system
    if [ -d "$final_path/live/public/system" ]; then
	    rsync -a "$final_path/live/public/system" "$tmpdir/."
    fi
    rsync -a "$config" "$tmpdir/."
    ynh_secure_remove --file="$final_path/live"
    ynh_setup_source --dest_dir="$final_path/live"

    # Temporary workaround for https://github.com/tootsuite/mastodon/issues/13292
    ynh_replace_string --match_string="sidekiq-unique-jobs (6.0.18)" --replace_string="sidekiq-unique-jobs (6.0.20)" --target_file="$final_path/live/Gemfile.lock"
    
    if [ -d "$tmpdir/system" ]; then
	    rsync -a "$tmpdir/system" "$final_path/live/public/."
    fi
    rsync -a "$tmpdir/.env.production" "$final_path/live/."
    ynh_secure_remove --file="$tmpdir"

    # Clean files which are not needed anymore
    ynh_secure_remove --file="$final_path/live/config/initializers/timeout.rb"
fi

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading nginx web server configuration..." --weight=3

ynh_add_nginx_config 'port_web port_stream'

#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=24

ynh_remove_nodejs
ynh_install_nodejs --nodejs_version=$NODEJS_VERSION
ynh_install_app_dependencies $pkg_dependencies	
ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key="https://dl.yarnpkg.com/debian/pubkey.gpg"

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=7

# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir=$final_path

#=================================================
# SPECIFIC UPGRADE
#=================================================
# ADD SWAP IF NEEDED
#=================================================
ynh_script_progression --message="Adding swap if needed..." --weight=7

total_memory=$(ynh_get_ram --total)
swap_needed=0

if [ $total_memory -lt $MEMORY_NEEDED ]; then
	# Need a minimum of 8Go of memory
	swap_needed=$(($MEMORY_NEEDED - $total_memory))
fi

ynh_script_progression --message="Adding $swap_needed Mo to swap..." --weight=1
ynh_add_swap --size=$swap_needed

#=================================================
# INSTALLING RUBY AND BUNDLER
#=================================================
ynh_script_progression --message="Installing Ruby..." --weight=424

ynh_install_ruby --ruby_version=$RUBY_VERSION
pushd "$final_path/live"
	gem update --system
	gem install bundler:$BUNDLER_VERSION --no-document
popd

#=================================================
# MODIFY A CONFIG FILE
#=================================================
ynh_script_progression --message="Modifying a config file..." --weight=1

ynh_backup_if_checksum_is_different --file="$config"
cp -f ../conf/.env.production.sample "$config"
ynh_replace_string --match_string="__DB_USER__" --replace_string="$app" --target_file="$config"
ynh_replace_string --match_string="__DB_NAME__" --replace_string="$db_name" --target_file="$config"
ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$config"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$config"
ynh_replace_string --match_string="__SMTP_FROM_ADDRESS__" --replace_string="$admin_mail" --target_file="$config"

language="$(echo $language | head -c 2)"
ynh_replace_string --match_string="__LANGUAGE__" --replace_string="$language" --target_file="$config"

ynh_replace_string --match_string="PAPERCLIP_SECRET=" --replace_string="PAPERCLIP_SECRET=$paperclip_secret" --target_file="$config"

ynh_replace_string --match_string="__SECRET_KEY_BASE__" --replace_string="$secret_key_base" --target_file="$config"

ynh_replace_string --match_string="__OTP_SECRET__" --replace_string="$otp_secret" --target_file="$config"

ynh_replace_string --match_string="__VAPID_PRIVATE_KEY__" --replace_string="$vapid_private_key" --target_file="$config"
ynh_replace_string --match_string="__VAPID_PUBLIC_KEY__" --replace_string="$vapid_public_key" --target_file="$config"

#=================================================
# UPGRADE MASTODON
#=================================================
ynh_script_progression --message="Upgrading Mastodon..." --weight=2640

chown -R "$app": "$final_path"

pushd "$final_path/live"
    ynh_use_nodejs
    bundle config deployment 'true'
    bundle config without 'development test'
    bundle install -j$(getconf _NPROCESSORS_ONLN)
    yarn install --pure-lockfile
    RAILS_ENV=production bundle exec rails assets:clean
    RAILS_ENV=production bundle exec rails assets:precompile
    RAILS_ENV=production bundle exec rails db:migrate
    RAILS_ENV=production bin/tootctl cache clear
popd

# Recalculate and store the checksum of the file for the next upgrade.
ynh_store_file_checksum --file="$config"

#=================================================
# SETUP CRON JOB FOR REMOVING CACHE
#=================================================
ynh_script_progression --message="Setuping a cron job for removing cache..." --weight=1

ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="../conf/cron"
cp -f ../conf/cron /etc/cron.d/$app 

#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=13

# Create a dedicated systemd config
ynh_replace_string --match_string="__PORT_WEB__" --replace_string="$port_web" --target_file="../conf/mastodon-web.service"
ynh_replace_string --match_string="__RBENVROOT__" --replace_string="$RBENV_ROOT" --target_file="../conf/mastodon-web.service"
ynh_replace_string --match_string="__RBENVROOT__" --replace_string="$RBENV_ROOT" --target_file="../conf/mastodon-sidekiq.service"
ynh_replace_string --match_string="__PORT_STREAM__" --replace_string="$port_stream" --target_file="../conf/mastodon-streaming.service"
ynh_replace_string --match_string="__NODEJS_PATH__" --replace_string="$nodejs_path" --target_file="../conf/mastodon-streaming.service"
ynh_add_systemd_config --service="$app-web" --template="mastodon-web.service"
ynh_add_systemd_config --service="$app-sidekiq" --template="mastodon-sidekiq.service"
ynh_add_systemd_config --service="$app-streaming" --template="mastodon-streaming.service"

#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
ynh_script_progression --message="Securing files and directories..." --weight=9

# Set permissions on app files
chown -R $app: $final_path

#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Upgrading SSOwat configuration..." --weight=1

# Make app public if necessary
if [ $is_public -eq 1 ]
then
	# unprotected_uris allows SSO credentials to be passed anyway
	ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
fi

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=48

ynh_systemd_action --service_name=${app}-web --action="start" --log_path=systemd --line_match="Listening on tcp"
ynh_systemd_action --service_name=${app}-sidekiq --action="start" --log_path=systemd --line_match="Starting processing"
ynh_systemd_action --service_name=${app}-streaming --action="start" --log_path=systemd --line_match="Worker 1 now listening"

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --weight=2

ynh_systemd_action --service_name=nginx --action=reload

#=================================================
# END OF SCRIPT
#=================================================

ynh_script_progression --message="Upgrade of $app completed" --last