From 5caceb55060b40741ca7de4de987c4d597a5bfc9 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 19:17:36 +0100 Subject: [PATCH 01/10] set seervice type as simpe --- conf/lstu.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/lstu.service b/conf/lstu.service index 12f3aac..ffd4941 100644 --- a/conf/lstu.service +++ b/conf/lstu.service @@ -5,7 +5,7 @@ Requires=network.target After=network.target [Service] -Type=forking +Type=simple User=www-data RemainAfterExit=yes WorkingDirectory=__FINALPATH__ From 1172d4f9241f345a21685190c673da8375fc307a Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 19:31:09 +0100 Subject: [PATCH 02/10] Fix old db_pwd --- scripts/upgrade | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/upgrade b/scripts/upgrade index 63c311c..dc36d3d 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -53,6 +53,12 @@ then # Si final_path n'est pas renseigné dans la config yunohost, cas d'ancien final_path=/var/www/$app fi +if [ -z "$db_pwd" ]; then + db_pwd=$(ynh_app_setting_get $app db_pwd) # Fix old db_pwd + ynh_app_setting_delete $app db_pwd + ynh_app_setting_set $app psqlpwd $db_pwd +fi + #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= From 001599224e31a4cd0fde59758fdda4054f413932 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 20:03:25 +0100 Subject: [PATCH 03/10] if no db then create it --- scripts/install | 2 -- scripts/upgrade | 13 +++++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/scripts/install b/scripts/install index 832deac..b0d42d7 100644 --- a/scripts/install +++ b/scripts/install @@ -101,8 +101,6 @@ ynh_setup_source "$final_path" # NGINX CONFIGURATION #================================================= -### `ynh_add_nginx_config` will use the file conf/nginx.conf - # Create a dedicated nginx config ynh_add_nginx_config diff --git a/scripts/upgrade b/scripts/upgrade index dc36d3d..8c86f0a 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -55,8 +55,17 @@ fi if [ -z "$db_pwd" ]; then db_pwd=$(ynh_app_setting_get $app db_pwd) # Fix old db_pwd - ynh_app_setting_delete $app db_pwd - ynh_app_setting_set $app psqlpwd $db_pwd + if [ -z "$db_pwd" ]; then + db_name=$(ynh_sanitize_dbid "$app") + db_user=$db_name + # Initialize database and store postgres password for upgrade + ynh_psql_setup_db "$db_name" "$db_user" + ynh_app_setting_set "$app" db_name "$db_name" + db_pwd=$(ynh_app_setting_get $app psqlpwd) # Password created in ynh_psql_setup_db function + else + ynh_app_setting_delete $app db_pwd + ynh_app_setting_set $app psqlpwd $db_pwd + fi fi #================================================= From c110f30c74744bfa9eeb8e3e20cfa720c88b9661 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 20:04:16 +0100 Subject: [PATCH 04/10] Inc manifest --- manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manifest.json b/manifest.json index b0ca202..9e6a229 100644 --- a/manifest.json +++ b/manifest.json @@ -2,9 +2,9 @@ "name": "Lstu", "id": "lstu", "packaging_format": 1, - "version": "0.21-4~ynh1", + "version": "0.21-4~ynh2", "requirements": { - "yunohost": ">= 2.4" + "yunohost": ">= 3.0" }, "description": { "en": "URL Shortener", From 1906204764a65a9ea9bc25dfb12799322d752514 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 20:44:59 +0100 Subject: [PATCH 05/10] store secret --- scripts/install | 1 + scripts/upgrade | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/install b/scripts/install index b0d42d7..23bedcb 100644 --- a/scripts/install +++ b/scripts/install @@ -124,6 +124,7 @@ ynh_replace_string "__DB_USER__" "$db_user" "${final_path}/lstu.conf" ynh_replace_string "__DB_PWD__" "$db_pwd" "${final_path}/lstu.conf" secret=$(ynh_string_random 24) +ynh_app_setting_set $app secret $secret ynh_replace_string "__SECRET__" "$secret" "${final_path}/lstu.conf" ynh_store_file_checksum "${final_path}/lstu.conf" diff --git a/scripts/upgrade b/scripts/upgrade index 8c86f0a..36ec1ea 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -98,7 +98,6 @@ ynh_replace_string "__DB_NAME__" "$db_name" "${final_path}/lstu.conf" ynh_replace_string "__DB_USER__" "$db_user" "${final_path}/lstu.conf" ynh_replace_string "__DB_PWD__" "$db_pwd" "${final_path}/lstu.conf" -secret=$(ynh_string_random 24) ynh_replace_string "__SECRET__" "$secret" "${final_path}/lstu.conf" ynh_store_file_checksum "${final_path}/lstu.conf" From 30e7c9878c89bf7c3cf313833e54002fa0862f11 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 21:24:02 +0100 Subject: [PATCH 06/10] start correctly the service --- scripts/_common.sh | 91 ++++++++++++++++++++++++++++++++++++++++++++-- scripts/install | 4 +- scripts/restore | 2 +- scripts/upgrade | 2 +- 4 files changed, 91 insertions(+), 8 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index a6b1ce9..8078143 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,7 +1,90 @@ #!/bin/bash -CHECK_VAR () { # Verifies that the variable is not empty. - # $1 = Variable to be checked - # $2 = Display text on error - test -n "$1" || (echo "$2" >&2 && false) +# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started +# +# usage: ynh_systemd_action [-n service_name] [-a action] [ [-l "line to match"] [-p log_path] [-t timeout] [-e length] ] +# | arg: -n, --service_name= - Name of the service to reload. Default : $app +# | arg: -a, --action= - Action to perform with systemctl. Default: start +# | arg: -l, --line_match= - Line to match - The line to find in the log to attest the service have finished to boot. +# If not defined it don't wait until the service is completely started. +# | arg: -p, --log_path= - Log file - Path to the log file. Default : /var/log/$app/$app.log +# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Default : 300 seconds. +# | arg: -e, --length= - Length of the error log : Default : 20 +ynh_systemd_action() { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [n]=service_name= [a]=action= [l]=line_match= [p]=log_path= [t]=timeout= [e]=length= ) + local service_name + local action + local line_match + local length + local log_path + local timeout + + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local service_name="${service_name:-$app}" + local action=${action:-start} + local log_path="${log_path:-/var/log/$service_name/$service_name.log}" + local length=${length:-20} + local timeout=${timeout:-300} + + # Start to read the log + if [[ -n "${line_match:-}" ]] + then + local templog="$(mktemp)" + # Following the starting of the app in its log + if [ "$log_path" == "systemd" ] ; then + # Read the systemd journal + journalctl -u $service_name -f --since=-45 > "$templog" & + else + # Read the specified log file + tail -F -n0 "$log_path" > "$templog" & + fi + # Get the PID of the tail command + local pid_tail=$! + fi + + echo "${action^} the service $service_name" >&2 + systemctl $action $service_name \ + || ( journalctl --lines=$length -u $service_name >&2 \ + ; test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 \ + ; false ) + + # Start the timeout and try to find line_match + if [[ -n "${line_match:-}" ]] + then + 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_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 + echo "Please find here an extract of the end of the log of the service $service_name:" + journalctl --lines=$length -u $service_name >&2 + test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 + fi + + echo "" + ynh_clean_check_starting + fi } + +# Clean temporary process and file used by ynh_check_starting +# (usually used in ynh_clean_setup scripts) +# +# usage: ynh_clean_check_starting +ynh_clean_check_starting () { + # Stop the execution of tail. + kill -s 15 $pid_tail 2>&1 + ynh_secure_remove "$templog" 2>&1 +} \ No newline at end of file diff --git a/scripts/install b/scripts/install index 23bedcb..9a54996 100644 --- a/scripts/install +++ b/scripts/install @@ -183,11 +183,11 @@ sudo chown -R www-data $final_path #================================================= systemctl enable $app.service -systemctl start $app +ynh_systemd_action -n $app -a start -l "Server available at" -p "systemd" #================================================= # RELOAD NGINX #================================================= # Reload Nginx -sudo service nginx reload +systemctl reload nginx diff --git a/scripts/restore b/scripts/restore index 02f5892..17bb378 100644 --- a/scripts/restore +++ b/scripts/restore @@ -77,7 +77,7 @@ ynh_install_app_dependencies build-essential libssl-dev zlib1g-dev libpng-dev li ynh_restore_file "/etc/systemd/system/$app.service" systemctl enable $app.service -systemctl start $app +ynh_systemd_action -n $app -a start -l "Server available at" -p "systemd" #================================================= # ADVERTISE SERVICE IN ADMIN PANEL diff --git a/scripts/upgrade b/scripts/upgrade index 36ec1ea..04c96c8 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -139,7 +139,7 @@ yunohost service add $app --log "/var/log/$app.log" # RESTART LSTU #================================================= -systemctl reload $app +ynh_systemd_action -n $app -a reload -l "Starting hot deployment for Hypnotoad server" -p "systemd" #================================================= # SETUP SSOWAT From f53406b066077fef79968f18a7cb619349f3c8b3 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 21:26:07 +0100 Subject: [PATCH 07/10] keep CHECK_VAR... For now --- scripts/_common.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/_common.sh b/scripts/_common.sh index 8078143..423ac1d 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,5 +1,12 @@ #!/bin/bash +CHECK_VAR () { # Verifies that the variable is not empty. + # $1 = Variable to be checked + # $2 = Display text on error + test -n "$1" || (echo "$2" >&2 && false) +} + + # Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started # # usage: ynh_systemd_action [-n service_name] [-a action] [ [-l "line to match"] [-p log_path] [-t timeout] [-e length] ] From fb343532e76c276efbd68e1de205d8c419680b82 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 21:39:45 +0100 Subject: [PATCH 08/10] reinstall dependencies before the app --- scripts/restore | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/restore b/scripts/restore index 17bb378..66f2aab 100644 --- a/scripts/restore +++ b/scripts/restore @@ -42,6 +42,13 @@ test ! -d $final_path \ #================================================= # STANDARD RESTORATION STEPS +#================================================= +# REINSTALL DEPENDENCIES +#================================================= + +# Define and install dependencies +ynh_install_app_dependencies build-essential libssl-dev zlib1g-dev libpng-dev libpq-dev memcached postgresql + #================================================= # RESTORE THE NGINX CONFIGURATION #================================================= @@ -64,13 +71,6 @@ ynh_psql_connect_as $db_name $db_pwd $db_name < ./db.sql #================================================= # SPECIFIC RESTORATION -#================================================= -# REINSTALL DEPENDENCIES -#================================================= - -# Define and install dependencies -ynh_install_app_dependencies build-essential libssl-dev zlib1g-dev libpng-dev libpq-dev memcached postgresql - #================================================= # RESTORE SYSTEMD #================================================= From 5b8c219706c304fd7628053ea0e744d3ef973bd8 Mon Sep 17 00:00:00 2001 From: Kayou Date: Sun, 3 Feb 2019 22:39:25 +0100 Subject: [PATCH 09/10] Update restore --- scripts/restore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/restore b/scripts/restore index 66f2aab..4014a81 100644 --- a/scripts/restore +++ b/scripts/restore @@ -67,7 +67,7 @@ ynh_restore_file "$final_path" db_pwd=$(ynh_app_setting_get $app psqlpwd) ynh_psql_setup_db $db_name $db_name $db_pwd -ynh_psql_connect_as $db_name $db_pwd $db_name < ./db.sql +ynh_psql_connect_as $db_name $db_pwd < ./db.sql #================================================= # SPECIFIC RESTORATION From 7f7625433f5f1f8342762171a85a8c32afb8919d Mon Sep 17 00:00:00 2001 From: Kayou Date: Mon, 4 Feb 2019 00:51:23 +0100 Subject: [PATCH 10/10] Trying to fix the restore script --- scripts/restore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/restore b/scripts/restore index 4014a81..f444bdc 100644 --- a/scripts/restore +++ b/scripts/restore @@ -66,8 +66,9 @@ ynh_restore_file "$final_path" #================================================= db_pwd=$(ynh_app_setting_get $app psqlpwd) +ynh_psql_test_if_first_run ynh_psql_setup_db $db_name $db_name $db_pwd -ynh_psql_connect_as $db_name $db_pwd < ./db.sql +ynh_psql_connect_as $db_name $db_pwd $db_name < ./db.sql #================================================= # SPECIFIC RESTORATION