From f6682e66ac6d41ac318f3200584647d6a2c1db50 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Holcroft Date: Thu, 5 Jul 2018 23:21:03 +0200 Subject: [PATCH] Rewrite uwsgi to use the helper --- conf/nginx.conf | 2 +- conf/systemd.service | 18 ------- conf/uwsgi-app@.service | 20 ++++++++ conf/{syncserver.ini => uwsgi.ini} | 3 +- scripts/_common.sh | 77 ++++++++++++++++++++++++++++++ scripts/backup | 5 +- scripts/install | 34 ++++++------- scripts/remove | 15 +----- scripts/restore | 32 ++++++++++--- scripts/upgrade | 61 +++++++++++++++-------- 10 files changed, 187 insertions(+), 80 deletions(-) delete mode 100644 conf/systemd.service create mode 100644 conf/uwsgi-app@.service rename conf/{syncserver.ini => uwsgi.ini} (96%) diff --git a/conf/nginx.conf b/conf/nginx.conf index 4053bb9..1ff683f 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -13,5 +13,5 @@ location __PATH__/ { uwsgi_read_timeout 3600; uwsgi_param SCRIPT_NAME __PATH__; uwsgi_modifier1 30; - uwsgi_pass unix:///tmp/__NAME__.socket; + uwsgi_pass unix:///var/run/uwsgi/__NAME__.socket; } diff --git a/conf/systemd.service b/conf/systemd.service deleted file mode 100644 index 0c105b8..0000000 --- a/conf/systemd.service +++ /dev/null @@ -1,18 +0,0 @@ -[Unit] -Description=uWSGI instance for __APP__ -Requires=network.target -After=network.target - -[Service] -User=__APP__ -Group=__APP__ -RemainAfterExit=yes -WorkingDirectory=__FINALPATH__ -ExecStart=/usr/bin/uwsgi \ - --ini __FINALPATH__/syncserver.ini \ - --socket /tmp/__APP__.socket -Restart=always -StandardError=syslog - -[Install] -WantedBy=multi-user.target diff --git a/conf/uwsgi-app@.service b/conf/uwsgi-app@.service new file mode 100644 index 0000000..888994d --- /dev/null +++ b/conf/uwsgi-app@.service @@ -0,0 +1,20 @@ +[Unit] +Description=%i uWSGI app +After=syslog.target + +[Service] +ExecStart=/usr/bin/uwsgi \ + --ini /etc/uwsgi/apps-available/%i.ini \ + --socket /var/run/uwsgi/%i.socket \ + --chmod-socket=775 \ + --logto /var/log/uwsgi/app/%i +User=%i +Group=www-data +Restart=on-failure +KillSignal=SIGQUIT +Type=notify +StandardError=syslog +NotifyAccess=all + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/conf/syncserver.ini b/conf/uwsgi.ini similarity index 96% rename from conf/syncserver.ini rename to conf/uwsgi.ini index f57ab8d..0eeb8b6 100644 --- a/conf/syncserver.ini +++ b/conf/uwsgi.ini @@ -2,13 +2,12 @@ plugins = python master = true protocol = uwsgi -socket = /tmp/__APP__.socket +socket = /var/run/uwsgi/__APP__.socket virtualenv = __FINALPATH__/local wsgi-file = __FINALPATH__/syncserver.wsgi python-path = __FINALPATH__/local enable-threads = true close-on-exec = true -umask = 0660 [app:main] use = egg:syncserver diff --git a/scripts/_common.sh b/scripts/_common.sh index 05a7907..2df1d17 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,2 +1,79 @@ #!/bin/bash +ynh_check_global_uwsgi_config () { + uwsgi --version || ynh_die "You need to add uwsgi (and appropriate plugin) as a dependency" + + cp ../conf/uwsgi-app@.service /etc/systemd/system/uwsgi-app@.service + + # make sure the folder for sockets exists and set authorizations + mkdir -p /var/run/uwsgi/ + chown root:www-data /var/run/uwsgi/ + chmod -R 775 /var/run/uwsgi/ + + # make sure the folder for logs exists and set authorizations + mkdir -p /var/log/uwsgi/app/ + chown root:www-data /var/log/uwsgi/app/ + chmod -R 775 /var/log/uwsgi/app/ +} + +# Create a dedicated uwsgi ini file to use with generic uwsgi service +# It will install generic uwsgi.socket and +# +# This will use a template in ../conf/uwsgi.ini +# and will replace the following keywords with +# global variables that should be defined before calling +# this helper : +# +# __APP__ by $app +# __PATH__ by $path_url +# __FINALPATH__ by $final_path +# +# usage: ynh_add_systemd_config +# +# to interact with your service: `systemctl uwsgi-app@app` +ynh_add_uwsgi_service () { + ynh_check_global_uwsgi_config + + # www-data group is needed since it is this nginx who will start the service + usermod --append --groups www-data "$app" || ynh_die "It wasn't possible to add user $app to group www-data" + + finaluwsgiini="/etc/uwsgi/apps-available/$app.ini" + ynh_backup_if_checksum_is_different "$finaluwsgiini" + cp ../conf/uwsgi.ini "$finaluwsgiini" + + # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable. + # Substitute in a nginx config file only if the variable is not empty + if test -n "${final_path:-}"; then + ynh_replace_string "__FINALPATH__" "$final_path" "$finaluwsgiini" + fi + if test -n "${path_url:-}"; then + ynh_replace_string "__PATH__" "$path_url" "$finaluwsgiini" + fi + if test -n "${app:-}"; then + ynh_replace_string "__APP__" "$app" "$finaluwsgiini" + fi + ynh_store_file_checksum "$finaluwsgiini" + + chown root: "$finaluwsgiini" + + systemctl daemon-reload + systemctl enable "uwsgi-app@$app.service" + + # Add as a service + yunohost service add "uwsgi-app@$app.service" --log "/var/log/uwsgi/app/$app" +} + +# Remove the dedicated uwsgi ini file +# +# usage: ynh_remove_systemd_config +ynh_remove_uwsgi_service () { + finaluwsgiini="/etc/uwsgi/apps-available/$app.ini" + if [ -e "$finaluwsgiini" ]; then + systemctl stop "uwsgi-app@$app.service" + systemctl disable "uwsgi-app@$app.service" + yunohost service remove "uwsgi-app@$app.service" + + ynh_secure_remove "$finaluwsgiini" + ynh_secure_remove "/var/log/uwsgi/app/$app" + fi +} \ No newline at end of file diff --git a/scripts/backup b/scripts/backup index 645fec4..b091c98 100644 --- a/scripts/backup +++ b/scripts/backup @@ -60,7 +60,8 @@ ynh_mysql_dump_db "$db_name" > db.sql ynh_backup "/etc/logrotate.d/$app" #================================================= -# BACKUP SYSTEMD +# BACKUP THE UWSGI FILES #================================================= -ynh_backup "/etc/systemd/system/$app.service" +ynh_backup "/etc/uwsgi/apps-available/$app.ini" +ynh_backup "/etc/systemd/system/uwsgi-app@.service" \ No newline at end of file diff --git a/scripts/install b/scripts/install index 4681c7f..665cb8e 100644 --- a/scripts/install +++ b/scripts/install @@ -114,10 +114,10 @@ ynh_system_user_create "$app" "$final_path" #================================================= # SPECIFIC SETUP #================================================= -# SETUP SYSTEMD +# SETUP UWSGI #================================================= -ynh_add_systemd_config +ynh_add_uwsgi_service #================================================= # create config file syncserver.ini @@ -128,17 +128,19 @@ secret=$(ynh_string_random) ynh_app_setting_set "$app" secret "$secret" # Copy Files -cp ../conf/syncserver.ini "$final_path/syncserver.ini" -ynh_replace_string "__APP__" "$app" "$final_path/syncserver.ini" -ynh_replace_string "__DOMAIN__" "$domain" "$final_path/syncserver.ini" -ynh_replace_string "__PATH__" "$path_url" "$final_path/syncserver.ini" -ynh_replace_string "__NAME__" "$app" "$final_path/syncserver.ini" -ynh_replace_string "__FINALPATH__" "$final_path" "$final_path/syncserver.ini" -ynh_replace_string "__SECRET__" "$secret" "$final_path/syncserver.ini" +ynh_replace_string "__APP__" "$app" "$finaluwsgiini" +ynh_replace_string "__DOMAIN__" "$domain" "$finaluwsgiini" +ynh_replace_string "__PATH__" "$path_url" "$finaluwsgiini" +ynh_replace_string "__NAME__" "$app" "$finaluwsgiini" +ynh_replace_string "__FINALPATH__" "$final_path" "$finaluwsgiini" +ynh_replace_string "__SECRET__" "$secret" "$finaluwsgiini" -ynh_replace_string "__DB_USER__" "$db_user" "$final_path/syncserver.ini" -ynh_replace_string "__DB_PWD__" "$db_pwd" "$final_path/syncserver.ini" -ynh_replace_string "__DB_NAME__" "$db_name" "$final_path/syncserver.ini" +ynh_replace_string "__DB_USER__" "$db_user" "$finaluwsgiini" +ynh_replace_string "__DB_PWD__" "$db_pwd" "$finaluwsgiini" +ynh_replace_string "__DB_NAME__" "$db_name" "$finaluwsgiini" + +rm "$final_path/syncserver.ini" +ln -s "$finaluwsgiini" "$final_path/syncserver.ini" #================================================= # MODIFY A CONFIG FILE @@ -183,12 +185,6 @@ virtualenv "$final_path/local" ynh_use_logrotate -#================================================= -# ADVERTISE SERVICE IN ADMIN PANEL -#================================================= - -yunohost service add "$app" -l /var/log/$app/$app.log - #================================================= # SETUP SSOWAT #================================================= @@ -200,5 +196,5 @@ ynh_app_setting_set "$app" skipped_uris "/" # RELOAD NGINX #================================================= -systemctl start "$app.service" +systemctl start "uwsgi-app@$app.service" systemctl reload nginx diff --git a/scripts/remove b/scripts/remove index 95d5a4f..4582699 100644 --- a/scripts/remove +++ b/scripts/remove @@ -1,6 +1,5 @@ #!/bin/bash - #================================================= # GENERIC START #================================================= @@ -24,20 +23,10 @@ db_name=$app #================================================= # STANDARD REMOVE #================================================= -# STOP AND REMOVE SERVICE -#================================================= - -ynh_remove_systemd_config - -#================================================= -# REMOVE SERVICE FROM ADMIN PANEL +# REMOVE UWSGI #================================================= -if yunohost service status | grep -q "$app" -then - echo "Remove $app service" - yunohost service remove "$app" -fi +ynh_remove_uwsgi_service #================================================= # REMOVE DEPENDENCIES diff --git a/scripts/restore b/scripts/restore index 60aa59d..7bd71c0 100644 --- a/scripts/restore +++ b/scripts/restore @@ -86,17 +86,37 @@ ynh_install_app_dependencies python-dev python-virtualenv \ uwsgi uwsgi-plugin-python #================================================= -# ADVERTISE SERVICE IN ADMIN PANEL +# RESTORE THE UWSGI MECANICS +#================================================= + +ynh_restore_file "/etc/systemd/system/uwsgi-app@.service" + +# make sure the folder for sockets exists and set authorizations +mkdir -p /var/run/uwsgi/ +chown root:www-data /var/run/uwsgi/ +chmod -R 775 /var/run/uwsgi/ + +# make sure the folder for logs exists and set authorizations +mkdir -p /var/log/uwsgi/app/ +chown root:www-data /var/log/uwsgi/app/ +chmod -R 775 /var/log/uwsgi/app/ + +#================================================= +# RESTORE SERVICE #================================================= -yunohost service add "$app" --log "/var/log/$app/$app.log" +usermod --append --groups www-data "$app" + +ynh_restore_file "/etc/uwsgi/apps-available/$app.ini" + +systemctl daemon-reload +systemctl enable "uwsgi-app@$app.service" #================================================= -# RESTORE SYSTEMD +# ADVERTISE SERVICE IN ADMIN PANEL #================================================= -ynh_restore_file "/etc/systemd/system/$app.service" -systemctl enable "$app.service" +yunohost service add "uwsgi-app@$app.service" --log "/var/log/uwsgi/app/$app" #================================================= # RESTORE THE LOGROTATE CONFIGURATION @@ -110,5 +130,5 @@ ynh_restore_file "/etc/logrotate.d/$app" # RELOAD NGINX AND PHP-FPM #================================================= -systemctl start "$app.service" +systemctl start "uwsgi-app@$app.service" systemctl reload nginx diff --git a/scripts/upgrade b/scripts/upgrade index 6b901b0..07c11f5 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -63,7 +63,7 @@ if [ -e /opt/yunohost/ffsync ]; then ynh_secure_remove /var/log/ffsync.log ynh_secure_remove /opt/yunohost/ffsync - ynh_add_systemd_config + yunohost service remove "$app" fi #================================================= @@ -96,6 +96,12 @@ path_url=$(ynh_normalize_url_path "$path_url") ynh_install_app_dependencies python-dev python-virtualenv \ uwsgi uwsgi-plugin-python +#================================================= +# SPECIFIC SETUP UWSGI +#================================================= + +ynh_add_uwsgi_service + #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= @@ -136,17 +142,41 @@ ynh_system_user_create "$app" #================================================= # Copy Files -cp ../conf/syncserver.ini "$final_path/syncserver.ini" -ynh_replace_string "__APP__" "$app" "$final_path/syncserver.ini" -ynh_replace_string "__DOMAIN__" "$domain" "$final_path/syncserver.ini" -ynh_replace_string "__PATH__" "$path_url" "$final_path/syncserver.ini" -ynh_replace_string "__NAME__" "$app" "$final_path/syncserver.ini" -ynh_replace_string "__FINALPATH__" "$final_path" "$final_path/syncserver.ini" -ynh_replace_string "__SECRET__" "$secret" "$final_path/syncserver.ini" +ynh_replace_string "__APP__" "$app" "$finaluwsgiini" +ynh_replace_string "__DOMAIN__" "$domain" "$finaluwsgiini" +ynh_replace_string "__PATH__" "$path_url" "$finaluwsgiini" +ynh_replace_string "__NAME__" "$app" "$finaluwsgiini" +ynh_replace_string "__FINALPATH__" "$final_path" "$finaluwsgiini" +ynh_replace_string "__SECRET__" "$secret" "$finaluwsgiini" + +ynh_replace_string "__DB_USER__" "$db_user" "$finaluwsgiini" +ynh_replace_string "__DB_PWD__" "$db_pwd" "$finaluwsgiini" +ynh_replace_string "__DB_NAME__" "$db_name" "$finaluwsgiini" + +rm "$final_path/syncserver.ini" +ln -s "$finaluwsgiini" "$final_path/syncserver.ini" + +#================================================= +# pip installation +#================================================= + +virtualenv "$final_path/local" +# Init virtualenv +( + set +o nounset + source "$final_path/local/bin/activate" + set -o nounset + cd "$final_path" + pip install --upgrade pip + CFLAGS="-Wno-error -Wno-error=format-security" \ + ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future" \ + pip install --requirement "$final_path/requirements.txt" + + python "$final_path/setup.py" develop + + touch "$final_path/local/COMPLETE" +) -ynh_replace_string "__DB_USER__" "$db_user" "$final_path/syncserver.ini" -ynh_replace_string "__DB_PWD__" "$db_pwd" "$final_path/syncserver.ini" -ynh_replace_string "__DB_NAME__" "$db_name" "$final_path/syncserver.ini" #================================================= # GENERIC FINALIZATION @@ -162,12 +192,6 @@ ynh_replace_string "__DB_NAME__" "$db_name" "$final_path/syncserver.ini" ynh_use_logrotate -#================================================= -# ADVERTISE SERVICE IN ADMIN PANEL -#================================================= - -yunohost service add "$app" -l /var/log/$app/$app.log - #================================================= # SETUP SSOWAT #================================================= @@ -178,6 +202,5 @@ ynh_app_setting_set "$app" skipped_uris "/" # RELOAD NGINX #================================================= -systemctl stop "$app.service" -systemctl start "$app.service" +systemctl start "uwsgi-app@$app.service" systemctl reload nginx