From 0dd44b1c157020dcc2f9c635af7cca9bdd818838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Mon, 7 May 2018 23:08:03 +0200 Subject: [PATCH] Refactor change-url and use helper ynh_add_nginx_config --- conf/monitorix.conf | 2 +- conf/nginx.conf | 6 +-- conf/nginx_status.conf | 4 +- manifest.json | 2 +- scripts/_common.sh | 30 +++++++------- scripts/change_url | 76 +++++++++++----------------------- scripts/experimental_helper.sh | 55 +++++++++++++++++++++++- scripts/install | 18 ++++---- scripts/remove | 8 ++-- scripts/restore | 12 +++--- scripts/upgrade | 10 ++--- 11 files changed, 124 insertions(+), 99 deletions(-) diff --git a/conf/monitorix.conf b/conf/monitorix.conf index a148440..59407d4 100644 --- a/conf/monitorix.conf +++ b/conf/monitorix.conf @@ -470,7 +470,7 @@ secure_log_date_format = %b %e conn_type = socket list = /var/run/mysqld/mysqld.sock - /var/run/mysqld/mysqld.sock = 3306, __MYSQL_USER__, MYSQL_PASSWORD + /var/run/mysqld/mysqld.sock = 3306, __MYSQL_USER__, __MYSQL_PASSWORD__ rigid = 0, 2, 0, 0, 0, 0 limit = 100, 100, 100, 100, 100, 100 diff --git a/conf/nginx.conf b/conf/nginx.conf index a66a4d2..7c64fe4 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,8 +1,8 @@ -location __YNH_WWW_PATH__ { - proxy_pass http://127.0.0.1:__SERVICE_PORT____YNH_WWW_PATH__; +location __PATH__ { + proxy_pass http://127.0.0.1:__PORT____PATH__; allow 127.0.0.0/8; - location ~ ^__YNH_WWW_PATH__/(.+\.png)$ { + location ~ ^__PATH__/(.+\.png)$ { alias /var/lib/monitorix/www/$1; } diff --git a/conf/nginx_status.conf b/conf/nginx_status.conf index b0dc12f..e877c13 100644 --- a/conf/nginx_status.conf +++ b/conf/nginx_status.conf @@ -1,6 +1,6 @@ server { - listen PORT; - listen [::]:PORT; + listen __PORT__; + listen [::]:__PORT__; access_log /var/log/nginx/localhost-nginx_status_monitorix.lan-access.log; error_log /var/log/nginx/localhost-nginx_status_monitorix.lan-error.log; diff --git a/manifest.json b/manifest.json index 845056a..784699b 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,7 @@ "email": "josue@familletille.ch" }, "requirements": { - "yunohost": ">= 2.7.9" + "yunohost": ">= 2.7.12" }, "multi_instance": false, "services": [ diff --git a/scripts/_common.sh b/scripts/_common.sh index 1b2f847..cef47e9 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -30,28 +30,26 @@ get_install_source() { } config_nginx() { - nginx_conf=../conf/nginx.conf - ynh_replace_string __YNH_WWW_PATH__ $path $nginx_conf - ynh_replace_string __SERVICE_PORT__ $http_port $nginx_conf - cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf - - # Add special hostname for monitorix status - nginx_status_conf=../conf/nginx_status.conf - ynh_replace_string PORT $nginx_status_port $nginx_status_conf - cp $nginx_status_conf /etc/nginx/conf.d/monitorix_status.conf - - systemctl reload nginx.service + ynh_add_nginx_config + + # Add special hostname for monitorix status + nginx_status_conf="/etc/nginx/conf.d/monitorix_status.conf" + cp ../conf/nginx_status.conf $nginx_status_conf + ynh_replace_string __PORT__ $nginx_status_port $nginx_status_conf + + systemctl reload nginx } config_monitorix() { - monitorix_conf=../conf/monitorix.conf - ynh_replace_string __SERVICE_PORT__ $http_port $monitorix_conf + monitorix_conf=/etc/monitorix/monitorix.conf + cp ../conf/monitorix.conf $monitorix_conf + ynh_replace_string __SERVICE_PORT__ $port $monitorix_conf ynh_replace_string __YNH_DOMAIN__ $domain $monitorix_conf ynh_replace_string __NGINX_STATUS_PORT__ $nginx_status_port $monitorix_conf - ynh_replace_string __YNH_WWW_PATH__ $path $monitorix_conf + ynh_replace_string "__YNH_WWW_PATH__/" "${path_url%/}/" $monitorix_conf + ynh_replace_string __YNH_WWW_PATH__ $path_url $monitorix_conf ynh_replace_string __MYSQL_USER__ $dbuser $monitorix_conf - ynh_replace_string MYSQL_PASSWORD $dbpass $monitorix_conf - cp $monitorix_conf /etc/monitorix/monitorix.conf + ynh_replace_string __MYSQL_PASSWORD__ $dbpass $monitorix_conf } set_permission() { diff --git a/scripts/change_url b/scripts/change_url index 8015e50..083a45f 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -15,67 +15,41 @@ source ./experimental_helper.sh source ./_common.sh # Retrive arguments +path_url=$(ynh_normalize_url_path ${YNH_APP_NEW_PATH:-'/'}) old_domain=$YNH_APP_OLD_DOMAIN -old_path=$YNH_APP_OLD_PATH - -new_domain=$YNH_APP_NEW_DOMAIN -new_path=$YNH_APP_NEW_PATH - -test -n "$old_path" || old_path="/" -test -n "$new_path" || new_path="/" - -new_path=$(ynh_normalize_url_path $new_path) -old_path=$(ynh_normalize_url_path $old_path) - -http_port=$(ynh_app_setting_get "$app" http_port) - -# CHECK WHICH PARTS SHOULD BE CHANGED -change_domain=0 -if [ "$old_domain" != "$new_domain" ] -then - change_domain=1 -fi - -change_path=0 -if [ "$old_path" != "$new_path" ] -then - change_path=1 -fi +domain=$YNH_APP_NEW_DOMAIN +port=$(ynh_app_setting_get "$app" http_port) +nginx_status_port=$(ynh_app_setting_get "$app" nginx_status_port) +dbuser=$app +dbname=$app +dbpass=$(ynh_app_setting_get "$app" mysqlpwd) #================================================= # STANDARD MODIFICATIONS #================================================= -# MODIFY URL IN NGINX CONF -nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf - -# Change the path in the nginx config file -if [ $change_path -eq 1 ] +# Update nginx config +if [ "$old_domain" != "$domain" ] then - ynh_replace_string "location $old_path" "location $new_path" "$nginx_conf_path" - ynh_replace_string "http://127.0.0.1:$http_port$old_path;" "http://127.0.0.1:$http_port$new_path;" "$nginx_conf_path" - ynh_replace_string "location ~ ^$old_path/(.+\.png)$" "location ~ ^$new_path/(.+\.png)$" "$nginx_conf_path" + old_file_path="/etc/nginx/conf.d/$old_domain.d/$app.conf" + new_file_path="/etc/nginx/conf.d/$domain.d/$app.conf" + mv "$old_file_path" "$new_file_path" + + # Change the checksum setting name + checksum_setting_old_name=checksum_${old_file_path//[\/ ]/_} + checksum_setting_new_name=checksum_${new_file_path//[\/ ]/_} + checksum_value=$(ynh_app_setting_get $app $checksum_setting_old_name) + ynh_app_setting_set $app $checksum_setting_new_name $checksum_value fi +config_nginx -# Change the domain for nginx -if [ $change_domain -eq 1 ] -then - sudo mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf -fi - -# Update Monitorix Config -ynh_replace_string "base_url = $old_path" "base_url = $new_path" /etc/monitorix/monitorix.conf -ynh_replace_string "base_cgi = $old_path/cgi" "base_cgi = $new_path/cgi" /etc/monitorix/monitorix.conf -ynh_replace_string "url_prefix = http://127.0.0.1:$http_port$old_path" "url_prefix = http://127.0.0.1:$http_port$new_path" /etc/monitorix/monitorix.conf -ynh_replace_string "from_address = noreply@$old_domain" "from_address = noreply@$new_domain" /etc/monitorix/monitorix.conf - -# Reload services -sudo systemctl reload nginx.service +# Update monitorix configuration +config_monitorix # Reload monitorix -# While we call "restart" sometime for unknown reason monitorix don't cleanly restart. Probably on the stop instruction the webserver is not stoped before the new start instruction. +# While we stop monitorix sometime the built-in web server is not stopped cleanly. So are sure that everything is cleanly stoped by that # So this fix that -sudo systemctl stop monitorix.service +systemctl stop monitorix.service sleep 1 -sudo systemctl start monitorix.service -sleep 10 \ No newline at end of file +pkill -f "monitorix-httpd listening on" || true +ynh_check_starting ' - Ok, ready.' '/var/log/monitorix' \ No newline at end of file diff --git a/scripts/experimental_helper.sh b/scripts/experimental_helper.sh index a27aa01..fc6e9ff 100644 --- a/scripts/experimental_helper.sh +++ b/scripts/experimental_helper.sh @@ -39,4 +39,57 @@ ynh_app_package_version () { fi version_key=$(ynh_read_manifest "$manifest_path" "version") echo "${version_key/*~ynh/}" -} +} + +#!/bin/bash + +# Start or restart a service and follow its booting +# +# usage: ynh_check_starting "Line to match" [Log file] [Timeout] [Service name] +# +# | arg: Line to match - The line to find in the log to attest the service have finished to boot. +# | arg: Log file - The log file to watch +# | arg: Service name +# /var/log/$app/$app.log will be used if no other log is defined. +# | arg: Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds. +ynh_check_starting () { + local line_to_match="$1" + local service_name="${4:-$app}" + local app_log="${2:-/var/log/$service_name/$service_name.log}" + local timeout=${3:-300} + + ynh_clean_check_starting () { + # Stop the execution of tail. + kill -s 15 $pid_tail 2>&1 + ynh_secure_remove "$templog" 2>&1 + } + + echo "Starting of $service_name" >&2 + systemctl stop $service_name + local templog="$(mktemp)" + # Following the starting of the app in its log + tail -F -n0 "$app_log" > "$templog" & + # Get the PID of the tail command + local pid_tail=$! + systemctl start $service_name + + local i=0 + for i in `seq 1 $timeout` + do + # Read the log until the sentence is found, that means the app finished to start. Or run until the timeout + if grep --quiet "$line_to_match" "$templog" + then + echo "The service $service_name has correctly started." >&2 + break + fi + echo -n "." >&2 + sleep 1 + done + if [ $i -eq $timeout ] + then + echo "The service $service_name didn't fully started before the timeout." >&2 + fi + + echo "" + ynh_clean_check_starting +} diff --git a/scripts/install b/scripts/install index 142efb9..43cb030 100755 --- a/scripts/install +++ b/scripts/install @@ -16,17 +16,17 @@ source ./_common.sh # Retrieve arguments domain=$YNH_APP_ARG_DOMAIN -path=$(ynh_normalize_url_path $YNH_APP_ARG_PATH) +path_url=$(ynh_normalize_url_path $YNH_APP_ARG_PATH) # Check domain/path availability -test $(ynh_webpath_available $domain $path) == 'True' || ynh_die "$domain$path is not available, please use an other domain or path." -ynh_webpath_register $app $domain $path +test $(ynh_webpath_available $domain $path_url) == 'True' || ynh_die "$domain$path_url is not available, please use an other domain or path." +ynh_webpath_register $app $domain $path_url # Find a port for built-in monitorix HTTP server -http_port=$(ynh_find_port 8080) -nginx_status_port=$(ynh_find_port $(($http_port +1))) +port=$(ynh_find_port 8080) +nginx_status_port=$(ynh_find_port $(($port +1))) -ynh_app_setting_set $app http_port $http_port +ynh_app_setting_set $app http_port $port ynh_app_setting_set $app nginx_status_port $nginx_status_port #================================================= @@ -66,9 +66,9 @@ set_permission yunohost service add monitorix # Reload monitorix -# While we call "restart" sometime for unknown reason monitorix don't cleanly restart. Probably on the stop instruction the webserver is not stoped before the new start instruction. +# While we stop monitorix sometime the built-in web server is not stopped cleanly. So are sure that everything is cleanly stoped by that # So this fix that sudo systemctl stop monitorix.service sleep 1 -sudo systemctl start monitorix.service -sleep 10 +pkill -f "monitorix-httpd listening on" || true +ynh_check_starting ' - Ok, ready.' '/var/log/monitorix' \ No newline at end of file diff --git a/scripts/remove b/scripts/remove index f76ee9d..296064e 100755 --- a/scripts/remove +++ b/scripts/remove @@ -21,10 +21,6 @@ domain=$(ynh_app_setting_get "$app" domain) # Stop service systemctl stop monitorix.service -# Suppression de la configuration nginx -ynh_secure_remove "/etc/nginx/conf.d/$domain.d/$app.conf" -systemctl reload nginx.service - # Drop MySQL database and user dbname=$app dbuser=$app @@ -35,6 +31,10 @@ ynh_mysql_drop_user "$dbuser" || true ynh_secure_remove /var/lib/monitorix ynh_secure_remove /usr/share/yunohost/hooks/post_iptable_rules/50-$app +# Remove nginx config +ynh_secure_remove "/etc/nginx/conf.d/monitorix_status.conf" +ynh_remove_nginx_config + # Autoremove package ynh_remove_app_dependencies ynh_package_autoremove monitorix diff --git a/scripts/restore b/scripts/restore index 7d0d157..f0f2ca4 100755 --- a/scripts/restore +++ b/scripts/restore @@ -16,10 +16,10 @@ source ../settings/scripts/_common.sh # Retrieve old app settings domain=$(ynh_app_setting_get "$app" domain) -path=$(ynh_normalize_url_path $(ynh_app_setting_get "$app" path)) +path_url=$(ynh_normalize_url_path $(ynh_app_setting_get "$app" path)) # Check domain/path availability -ynh_webpath_available $domain $path || ynh_die "$domain/$path is not available, please use an other domain or path." +ynh_webpath_available $domain $path_url || ynh_die "$domain/$path_url is not available, please use an other domain or path." #================================================= # STANDARD RESTORATION STEPS @@ -52,9 +52,9 @@ set_permission systemctl reload nginx.service # Reload monitorix -# While we call "restart" sometime for unknown reason monitorix don't cleanly restart. Probably on the stop instruction the webserver is not stoped before the new start instruction. +# While we stop monitorix sometime the built-in web server is not stopped cleanly. So are sure that everything is cleanly stoped by that # So this fix that -sudo systemctl stop monitorix.service +systemctl stop monitorix.service sleep 1 -sudo systemctl start monitorix.service -sleep 10 \ No newline at end of file +pkill -f "monitorix-httpd listening on" || true +ynh_check_starting ' - Ok, ready.' '/var/log/monitorix' \ No newline at end of file diff --git a/scripts/upgrade b/scripts/upgrade index 2fdee74..c3ed308 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -16,8 +16,8 @@ source ./_common.sh # Retrieve app settings domain=$(ynh_app_setting_get "$app" domain) -path=$(ynh_normalize_url_path $(ynh_app_setting_get "$app" path)) -http_port=$(ynh_app_setting_get "$app" http_port) +path_url=$(ynh_normalize_url_path $(ynh_app_setting_get "$app" path)) +port=$(ynh_app_setting_get "$app" http_port) nginx_status_port=$(ynh_app_setting_get "$app" nginx_status_port) dbuser=$app dbname=$app @@ -60,9 +60,9 @@ cp ../conf/post_iptable_rules_hook /usr/share/yunohost/hooks/post_iptable_rules/ set_permission # Reload monitorix -# While we call "restart" sometime for unknown reason monitorix don't cleanly restart. Probably on the stop instruction the webserver is not stoped before the new start instruction. +# While we stop monitorix sometime the built-in web server is not stopped cleanly. So are sure that everything is cleanly stoped by that # So this fix that sudo systemctl stop monitorix.service sleep 1 -sudo systemctl start monitorix.service -sleep 10 +pkill -f "monitorix-httpd listening on" || true +ynh_check_starting ' - Ok, ready.' '/var/log/monitorix'