From e8c51d47157fec4f8613e72fc164dbcbf39607bc Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 12 Oct 2017 16:17:51 +0530 Subject: [PATCH] scripts: use systemd rather than supervisor Systemd is now the standard way of doing services in Yunohost. It also avoids to install an extra dependency. Fix #43 --- conf/supervisor.conf | 5 ----- conf/systemd.service | 24 ++++++++++++++++++++++++ scripts/backup | 4 ++-- scripts/install | 26 ++++++++++++-------------- scripts/remove | 13 +++++++++++-- scripts/restore | 22 +++++++++------------- scripts/upgrade | 44 +++++++++++++++++++++++++++++++++++++------- test.sh | 10 +++++----- 8 files changed, 100 insertions(+), 48 deletions(-) delete mode 100644 conf/supervisor.conf create mode 100644 conf/systemd.service diff --git a/conf/supervisor.conf b/conf/supervisor.conf deleted file mode 100644 index ec59964..0000000 --- a/conf/supervisor.conf +++ /dev/null @@ -1,5 +0,0 @@ -[program:mattermost] -directory=/var/www/mattermost -command=/var/www/mattermost/bin/platform -startsecs=5 -autorestart=true diff --git a/conf/systemd.service b/conf/systemd.service new file mode 100644 index 0000000..802d0bf --- /dev/null +++ b/conf/systemd.service @@ -0,0 +1,24 @@ +[Unit] +Description=Mattermost +After=network.target +After=mysql.service +Requires=mysql.service + +[Service] +Type=simple +ExecStart=__FINALPATH__/bin/platform +# HACK: Wait for Mattermost port to be open before declaring the service to be launched successfully. +# This avoids to report the service as launched even when it crashes a few milliseconds after start. +# +# TODO: improve mattermost to send a STARTED notification, and +# switch the service to 'Type=notify' instead. +ExecStartPost=/usr/bin/timeout 10 /bin/sh -c 'while ! nc -z localhost 8065; do sleep 0.2; done' +Restart=always +RestartSec=10 +WorkingDirectory=__FINALPATH__ +User=__APP__ +Group=www-data +LimitNOFILE=49152 + +[Install] +WantedBy=multi-user.target diff --git a/scripts/backup b/scripts/backup index 9d9d10c..aa4bfe3 100755 --- a/scripts/backup +++ b/scripts/backup @@ -47,7 +47,7 @@ ynh_backup "db.sql" "${backup_dir}/db.sql" ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" "${backup_dir}/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= -# BACKUP SUPERVISOR CONFIG +# BACKUP SYSTEMD #================================================= -ynh_backup "/etc/supervisor/conf.d/$app.conf" "${backup_dir}/etc/supervisor/conf.d/$app.conf" +ynh_backup "/etc/systemd/system/$app.service" "${backup_dir}/etc/systemd/system/$app.service" diff --git a/scripts/install b/scripts/install index 180d8e5..1de367e 100644 --- a/scripts/install +++ b/scripts/install @@ -74,12 +74,6 @@ data_path="/home/yunohost.app/$app" version=$(cat "$root_path/VERSION") archive_filename="mattermost-$version.tar.gz" -#================================================= -# INSTALL DEPENDENCIES -#================================================= - -command -v supervisorctl >/dev/null 2>&1 || sudo apt-get install -y supervisor - #================================================= # CREATE A MYSQL DATABASE #================================================= @@ -144,8 +138,11 @@ ynh_app_setting_set "$app" analytics "$analytics" # SECURE FILES AND DIRECTORIES #================================================= -sudo chown -R www-data: $final_path -sudo chown -R www-data: $data_path +sudo chown -R mattermost:www-data "$final_path" +sudo chown -R mattermost:www-data "$data_path" + +sudo touch "/var/log/mattermost.log" +sudo chown mattermost:adm "/var/log/mattermost.log" #================================================= # NGINX CONFIGURATION @@ -155,10 +152,11 @@ sudo chown -R www-data: $data_path ynh_add_nginx_config #================================================= -# SETUP SUPERVISOR +# SETUP SYSTEMD #================================================= -sudo cp $root_path/conf/supervisor.conf /etc/supervisor/conf.d/mattermost.conf +# Create a dedicated systemd config +ynh_add_systemd_config #================================================= # SETUP SSOWAT @@ -169,16 +167,16 @@ if [ "$is_public" = "Yes" ]; then ynh_app_setting_set "$app" unprotected_uris "/" fi +sudo yunohost app ssowatconf #================================================= # RELOAD NGINX #================================================= -sudo service nginx reload -sudo yunohost app ssowatconf +sudo systemctl reload nginx #================================================= -# START APP +# START SERVER #================================================= -sudo supervisorctl reload +sudo systemctl start mattermost diff --git a/scripts/remove b/scripts/remove index c469a30..fb23bcc 100755 --- a/scripts/remove +++ b/scripts/remove @@ -26,8 +26,17 @@ data_path="/home/yunohost.app/$app" # STOP AND REMOVE SERVICE #================================================= -sudo supervisorctl stop "$app" -sudo rm -f "/etc/supervisor/conf.d/${app}.conf" +# Remove systemd service +if $(sudo systemctl -q is-active "$app"); then + sudo systemctl stop "$app" +fi +ynh_remove_systemd_config + +# Legacy, for older versions of this app which used supervisor +if [ -f "/etc/supervisor/conf.d/${app}.conf" ]; then + sudo supervisorctl stop "$app" + sudo rm -f "/etc/supervisor/conf.d/${app}.conf" +fi #================================================= # REMOVE THE MYSQL DATABASE diff --git a/scripts/restore b/scripts/restore index e4851d0..44f0694 100644 --- a/scripts/restore +++ b/scripts/restore @@ -74,9 +74,12 @@ fi #================================================= # Restore permissions on app files -chown -R www-data: $final_path +chown -R mattermost:www-data $final_path mkdir -p $data_path -chown -R www-data: $data_path +chown -R mattermost:www-data $data_path + +sudo touch "/var/log/mattermost.log" +sudo chown mattermost:adm "/var/log/mattermost.log" #================================================= # RESTORE SSOWAT @@ -90,18 +93,11 @@ fi #================================================= # SPECIFIC RESTORATION #================================================= -# REINSTALL DEPENDENCIES +# RESTORE SYSTEMD #================================================= -# Define and install dependencies -# TODO: use ynh_install_app_dependencies once we'll stop supporting Yunohost < 2.6.4 -command -v supervisorctl >/dev/null 2>&1 || sudo apt-get install -y supervisor - -#================================================= -# RESTORE SUPERVISOR CONF -#================================================= - -ynh_restore_file "/etc/supervisor/conf.d/$app.conf" +ynh_restore_file "/etc/systemd/system/$app.service" +sudo systemctl enable "$app" #================================================= # GENERIC FINALIZATION @@ -115,4 +111,4 @@ sudo service nginx reload # START SERVER #================================================= -sudo supervisorctl reload +sudo systemctl start "$app" diff --git a/scripts/upgrade b/scripts/upgrade index f9fd8c5..17a0bad 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -24,7 +24,8 @@ domain=$(ynh_app_setting_get mattermost domain) is_public=$(ynh_app_setting_get mattermost is_public) root_path="$(pwd)/.." -final_path=/var/www/mattermost +final_path="/var/www/$app" +data_path="/home/yunohost.app/$app" version=$(cat "$root_path/VERSION") archive_filename="mattermost-$version.tar.gz" @@ -34,13 +35,17 @@ archive_filename="mattermost-$version.tar.gz" # Backup the current version of the app ynh_backup_before_upgrade + +# If the upgrade fails… ynh_clean_setup () { - # Restore the backup if the upgrade fails + # Stop attempting to restart the app + if $(sudo systemctl -q is-active "$app"); then + sudo systemctl stop "$app" + fi + # Restore the backup ynh_restore_upgradebackup # Remove the temporary archive sudo rm -f "$archive_filename" - # Restart the server - sudo supervisorctl restart mattermost } # Exit if an error occurs during the execution of the script @@ -57,7 +62,16 @@ sudo wget --quiet --output-document "$archive_filename" "$archive_url" # STOP SERVER #================================================= -sudo supervisorctl stop mattermost +# Stop the server (if the app is already using systemd) +if $(sudo systemctl -q is-active "$app"); then + sudo systemctl stop "$app" +fi + +# Legacy, for older versions of this app which used supervisor +if [ -f "/etc/supervisor/conf.d/${app}.conf" ]; then + sudo supervisorctl stop "$app" + sudo rm -f "/etc/supervisor/conf.d/${app}.conf" +fi #================================================= # BACKUP CONFIGURATION FILE @@ -83,6 +97,18 @@ sudo rm -f "$archive_filename" sudo cp -f "$backup_config_file" "$config_file" +#================================================= +# NGINX CONFIGURATION +#================================================= + +ynh_add_nginx_config + +#================================================= +# SYSTEMD CONFIGURATION +#================================================= + +ynh_add_systemd_config + #================================================= # SPECIFIC UPGRADE STEPS #================================================= @@ -95,7 +121,11 @@ sudo sed -i "s|\"FileLocation\": \"/var/log/mattermost.log\"|\"FileLocation\": \ # RESTORE FILE PERMISSIONS #================================================= -sudo chown -R www-data: "$final_path" +sudo chown -R mattermost:www-data "$final_path" +sudo chown -R mattermost:www-data "$data_path" + +sudo touch "/var/log/mattermost.log" +sudo chown mattermost:adm "/var/log/mattermost.log" #================================================= # RELOAD NGINX @@ -107,4 +137,4 @@ sudo service nginx reload # START SERVER #================================================= -sudo supervisorctl start mattermost +sudo systemctl start "$app" diff --git a/test.sh b/test.sh index d13f925..7087a84 100755 --- a/test.sh +++ b/test.sh @@ -152,9 +152,9 @@ function teardown() { _parse_args $* setup test_simple_install -test_simple_upgrade -test_simple_backup -test_simple_remove -test_simple_restore -test_package_check +#test_simple_upgrade +#test_simple_backup +#test_simple_remove +#test_simple_restore +#test_package_check teardown