From 91b4966afdc8c429a09bf98ad7194d56b3e26243 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Thu, 5 May 2022 02:45:58 +0200 Subject: [PATCH 1/2] Switch to nodejs --- check_process | 3 +- conf/{supervisor.conf => supervisor.service} | 2 +- manifest.json | 2 +- scripts/_common.sh | 1 + scripts/backup | 2 +- scripts/install | 40 ++++- scripts/remove | 22 ++- scripts/restore | 25 ++- scripts/upgrade | 43 ++++- scripts/ynh_supervisor | 167 +++++++++++++++++++ 10 files changed, 285 insertions(+), 22 deletions(-) rename conf/{supervisor.conf => supervisor.service} (68%) create mode 100644 scripts/ynh_supervisor diff --git a/check_process b/check_process index 32cab1f..c696543 100644 --- a/check_process +++ b/check_process @@ -11,7 +11,8 @@ setup_private=0 setup_public=0 upgrade=1 - #upgrade=1 from_commit=CommitHash + # 2.0~ynh2 + upgrade=1 from_commit=1319c98657f9b743cd345185aafc51c7a7da342f backup_restore=1 multi_instance=1 port_already_use=0 diff --git a/conf/supervisor.conf b/conf/supervisor.service similarity index 68% rename from conf/supervisor.conf rename to conf/supervisor.service index a636eb5..76281de 100644 --- a/conf/supervisor.conf +++ b/conf/supervisor.service @@ -1,6 +1,6 @@ [program:__APP__] directory=__FINALPATH__/app/server/ -command=python index.py --port __PORT__ +command=__YNH_NODE__ index.js --port __PORT__ autostart=true autorestart=true user=__APP__ diff --git a/manifest.json b/manifest.json index 6c2cd06..342ea02 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "A drop-in, low-overhead monitoring web dashboard for GNU/linux machine.", "fr": "Tableau de bord léger pour le monitoring d’un serveur GNU/linux." }, - "version": "2.0~ynh2", + "version": "2.0~ynh3", "url": "https://github.com/afaqurk/linux-dash", "upstream": { "license": "MIT", diff --git a/scripts/_common.sh b/scripts/_common.sh index ea2aadd..591bf13 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -6,6 +6,7 @@ # dependencies used by the app pkg_dependencies="supervisor" +nodejs_version=12 #================================================= # PERSONAL HELPERS diff --git a/scripts/backup b/scripts/backup index b084c19..0b4de3b 100755 --- a/scripts/backup +++ b/scripts/backup @@ -47,7 +47,7 @@ ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # SPECIFIC BACKUP #================================================= -# BACKUP SUPERVISOR +# BACKUP SYSTEMD #================================================= ynh_backup --src_path="/etc/supervisor/conf.d/$app.conf" diff --git a/scripts/install b/scripts/install index da884e5..3997f17 100755 --- a/scripts/install +++ b/scripts/install @@ -7,12 +7,16 @@ #================================================= source _common.sh +source ynh_supervisor source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= +ynh_clean_setup () { + ynh_clean_check_starting +} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -63,6 +67,7 @@ ynh_app_setting_set --app=$app --key=port --value=$port ynh_script_progression --message="Installing dependencies..." ynh_install_app_dependencies $pkg_dependencies +ynh_install_nodejs --nodejs_version=$nodejs_version #================================================= # CREATE DEDICATED USER @@ -98,20 +103,45 @@ ynh_add_nginx_config #================================================= # REMOVE GOOGLE !!! #================================================= +ynh_script_progression --message="Remobing Google..." sed --in-place "/googlecode\|googleapis/d" "$final_path/index.html" #================================================= -# SETUP SUPERVISOR +# BUILD APP #================================================= -ynh_script_progression --message="Configuring supervisor..." +ynh_script_progression --message="Building app..." -ynh_add_config --template="../conf/supervisor.conf" --destination="/etc/supervisor/conf.d/$app.conf" -supervisorctl update -supervisorctl restart $app +pushd "$final_path" + ynh_use_nodejs + ynh_npm install --production +popd + +#================================================= +# SETUP SYSTEMD +#================================================= +ynh_script_progression --message="Configuring a systemd service..." + +# Create a dedicated systemd config +ynh_add_supervisor_config #================================================= # GENERIC FINALIZATION +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." + +yunohost service add $app --log="/var/log/$app/$app.log" + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." + +# Start a systemd service +ynh_supervisor_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + #================================================= # SETUP SSOWAT #================================================= diff --git a/scripts/remove b/scripts/remove index 499b3c8..dfd1ca8 100755 --- a/scripts/remove +++ b/scripts/remove @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_supervisor source /usr/share/yunohost/helpers #================================================= @@ -23,13 +24,23 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= # STANDARD REMOVE #================================================= -# REMOVE SUPERVISOR +# REMOVE SERVICE INTEGRATION IN YUNOHOST #================================================= -ynh_script_progression --message="Removing supervisor configuration..." -supervisorctl stop $app -ynh_secure_remove --file="/etc/supervisor/conf.d/$app.conf" -supervisorctl update +# Remove the service from the list of services known by YunoHost (added from `yunohost service add`) +if ynh_exec_warn_less yunohost service status $app >/dev/null +then + ynh_script_progression --message="Removing $app service integration..." + yunohost service remove $app +fi + +#================================================= +# STOP AND REMOVE SERVICE +#================================================= +ynh_script_progression --message="Stopping and removing the systemd service..." + +# Remove the dedicated systemd config +ynh_remove_supervisor_config #================================================= # REMOVE APP MAIN DIR @@ -53,6 +64,7 @@ ynh_remove_nginx_config ynh_script_progression --message="Removing dependencies..." # Remove metapackage and its dependencies +ynh_remove_nodejs ynh_remove_app_dependencies #================================================= diff --git a/scripts/restore b/scripts/restore index b5286da..25c90fa 100755 --- a/scripts/restore +++ b/scripts/restore @@ -8,12 +8,16 @@ # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh +source ../settings/scripts/ynh_supervisor source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= +ynh_clean_setup () { + ynh_clean_check_starting +} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -73,15 +77,30 @@ ynh_script_progression --message="Reinstalling dependencies..." # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies +ynh_install_nodejs --nodejs_version=$nodejs_version #================================================= -# RESTORE SUPERVISOR +# RESTORE SYSTEMD #================================================= -ynh_script_progression --message="Restoring the supervisor configuration..." +ynh_script_progression --message="Restoring the systemd configuration..." ynh_restore_file --origin_path="/etc/supervisor/conf.d/$app.conf" +supervisorctl reread supervisorctl update -supervisorctl restart $app + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." + +yunohost service add $app --log="/var/log/$app/$app.log" + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." + +ynh_supervisor_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" #================================================= # GENERIC FINALIZATION diff --git a/scripts/upgrade b/scripts/upgrade index f341038..f136a3e 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_supervisor source /usr/share/yunohost/helpers #================================================= @@ -37,6 +38,7 @@ ynh_script_progression --message="Backing up the app before upgrading (may take # 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 } @@ -45,6 +47,13 @@ ynh_abort_if_errors #================================================= # STANDARD UPGRADE STEPS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." + +ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" + #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= @@ -87,6 +96,7 @@ ynh_add_nginx_config ynh_script_progression --message="Upgrading dependencies..." ynh_install_app_dependencies $pkg_dependencies +ynh_install_nodejs --nodejs_version=$nodejs_version #================================================= # SPECIFIC UPGRADE @@ -97,16 +107,39 @@ ynh_install_app_dependencies $pkg_dependencies sed --in-place "/googlecode\|googleapis/d" "$final_path/index.html" #================================================= -# SETUP SUPERVISOR +# BUILD APP #================================================= -ynh_script_progression --message="Configuring supervisor..." +ynh_script_progression --message="Building app..." -ynh_add_config --template="../conf/supervisor.conf" --destination="/etc/supervisor/conf.d/$app.conf" -supervisorctl update -supervisorctl restart $app +pushd "$final_path" + ynh_use_nodejs + ynh_npm install --production +popd + +#================================================= +# SETUP SYSTEMD +#================================================= +ynh_script_progression --message="Upgrading systemd configuration..." + +# Create a dedicated systemd config +ynh_add_supervisor_config #================================================= # GENERIC FINALIZATION +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." + +yunohost service add $app --log="/var/log/$app/$app.log" + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." + +ynh_supervisor_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + #================================================= # RELOAD NGINX #================================================= diff --git a/scripts/ynh_supervisor b/scripts/ynh_supervisor new file mode 100644 index 0000000..67bd2bd --- /dev/null +++ b/scripts/ynh_supervisor @@ -0,0 +1,167 @@ +#!/bin/bash + +# Create a dedicated supervisor config +# +# usage: ynh_add_supervisor_config [--service=service] [--template=template] +# | arg: -s, --service= - Service name (optionnal, `$app` by default) +# | arg: -t, --template= - Name of template file (optionnal, this is 'supervisor' by default, meaning ./conf/supervisor.service will be used as template) +# +# This will use the template `../conf/.service`. +# +# See the documentation of `ynh_add_config` for a description of the template +# format and how placeholders are replaced with actual variables. +# +# Requires YunoHost version 2.7.11 or higher. +ynh_add_supervisor_config () { + # Declare an array to define the options of this helper. + local legacy_args=stv + local -A args_array=( [s]=service= [t]=template= [v]=others_var=) + local service + local template + local others_var + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local service="${service:-$app}" + local template="${template:-supervisor.service}" + others_var="${others_var:-}" + + [[ -z "$others_var" ]] || ynh_print_warn --message="Packagers: using --others_var is unecessary since Yunohost 4.2" + + ynh_add_config --template="$YNH_APP_BASEDIR/conf/$template" --destination="/etc/supervisor/conf.d/$service.conf" + + supervisorctl reread + supervisorctl update +} + +# Remove the dedicated supervisor config +# +# usage: ynh_remove_supervisor_config [--service=service] +# | arg: -s, --service= - Service name (optionnal, $app by default) +# +# Requires YunoHost version 2.7.2 or higher. +ynh_remove_supervisor_config () { + # Declare an array to define the options of this helper. + local legacy_args=s + local -A args_array=( [s]=service= ) + local service + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local service="${service:-$app}" + + local finalsupervisorconf="/etc/supervisor/conf.d/$service.conf" + if [ -e "$finalsupervisorconf" ] + then + ynh_supervisor_action --service_name=$service --action=stop + ynh_secure_remove --file="$finalsupervisorconf" + supervisorctl reread + supervisorctl update + fi +} + +# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started +# +# usage: ynh_supervisor_action [--service_name=service_name] [--action=action] [ [--line_match="line to match"] [--log_path=log_path] [--timeout=300] [--length=20] ] +# | arg: -n, --service_name= - Name of the service to start. Default : `$app` +# | arg: -a, --action= - Action to perform with supervisorctl. 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 +# +# Requires YunoHost version 3.5.0 or higher. +ynh_supervisor_action() { + # Declare an array to define the options of this helper. + local legacy_args=nalpte + 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 "$@" + service_name="${service_name:-$app}" + action=${action:-start} + line_match=${line_match:-} + length=${length:-20} + log_path="${log_path:-/var/log/$service_name/$service_name.log}" + 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 supervisor journal + journalctl --unit=supervisor --follow --since=-0 --quiet > "$templog" & + # Get the PID of the journalctl command + local pid_tail=$! + else + # Read the specified log file + tail --follow=name --retry --lines=0 "$log_path" > "$templog" 2>&1 & + # Get the PID of the tail command + local pid_tail=$! + fi + fi + + # Use reload-or-restart instead of reload. So it wouldn't fail if the service isn't running. + if [ "$action" == "reload" ]; then + action="update" + fi + + # If the service fails to perform the action + if ! supervisorctl $action $service_name + then + # Show syslog for this service + ynh_exec_err journalctl --quiet --no-hostname --no-pager --lines=$length --unit=$service_name + # If a log is specified for this service, show also the content of this log + if [ -e "$log_path" ] + then + ynh_exec_err tail --lines=$length "$log_path" + fi + ynh_clean_check_starting + return 1 + fi + + # Start the timeout and try to find line_match + if [[ -n "${line_match:-}" ]] + then + set +x + 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 --extended-regexp --quiet "$line_match" "$templog" + then + ynh_print_info --message="The service $service_name has correctly executed the action ${action}." + break + fi + if [ $i -eq 3 ]; then + echo -n "Please wait, the service $service_name is ${action}ing" >&2 + fi + if [ $i -ge 3 ]; then + echo -n "." >&2 + fi + sleep 1 + done + set -x + if [ $i -ge 3 ]; then + echo "" >&2 + fi + if [ $i -eq $timeout ] + then + ynh_print_warn --message="The service $service_name didn't fully executed the action ${action} before the timeout." + ynh_print_warn --message="Please find here an extract of the end of the log of the service $service_name:" + ynh_exec_warn journalctl --quiet --no-hostname --no-pager --lines=$length --unit=$service_name + if [ -e "$log_path" ] + then + ynh_print_warn --message="\-\-\-" + ynh_exec_warn tail --lines=$length "$log_path" + fi + fi + ynh_clean_check_starting + fi +} From f58616d3fab9803e5c2a11024165fb7351da25fe Mon Sep 17 00:00:00 2001 From: yunohost-bot Date: Thu, 5 May 2022 00:46:03 +0000 Subject: [PATCH 2/2] Auto-update README --- README.md | 2 +- README_fr.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d90eeca..517a290 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ If you don't have YunoHost, please consult [the guide](https://yunohost.org/#/in A beautiful web dashboard for GNU/Linux which display CPU, RAM, memory, network and other usage. -**Shipped version:** 2.0~ynh2 +**Shipped version:** 2.0~ynh3 **Demo:** https://afaqurk.github.io/linux-dash/ diff --git a/README_fr.md b/README_fr.md index 12439bb..aab5823 100644 --- a/README_fr.md +++ b/README_fr.md @@ -14,7 +14,7 @@ Si vous n'avez pas YunoHost, regardez [ici](https://yunohost.org/#/install) pour A beautiful web dashboard for GNU/Linux which display CPU, RAM, memory, network and other usage. -**Version incluse :** 2.0~ynh2 +**Version incluse :** 2.0~ynh3 **Démo :** https://afaqurk.github.io/linux-dash/