From eed0f9e00390e8b7405b922070be459772f704c8 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Thu, 27 Jan 2022 00:17:52 +0100 Subject: [PATCH] Apply last example_ynh --- conf/nginx.conf | 22 ++++++------- conf/systemd.service | 2 ++ scripts/backup | 3 +- scripts/change_url | 15 +++++---- scripts/install | 42 ++++++++++++++---------- scripts/remove | 24 +++++++------- scripts/restore | 12 ++++--- scripts/upgrade | 76 +++++++++++++++++++++++--------------------- 8 files changed, 107 insertions(+), 89 deletions(-) diff --git a/conf/nginx.conf b/conf/nginx.conf index aa0fa5f..9ac97f6 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,16 +1,16 @@ location ^~ __PATH__/ { - rewrite ^__PATH__/admin$ https://$host__PATH__/admin/ permanent; - rewrite ^__PATH__/ui$ https://$host__PATH__/ui/ permanent; + rewrite ^__PATH__/admin$ https://$host__PATH__/admin/ permanent; + rewrite ^__PATH__/ui$ https://$host__PATH__/ui/ permanent; - proxy_pass http://localhost:__PORT__/; - proxy_set_header Host $host; - proxy_redirect http:// https://; - proxy_http_version 1.1; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; + proxy_pass http://localhost:__PORT__/; + proxy_set_header Host $host; + proxy_redirect http:// https://; + proxy_http_version 1.1; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; - # Include SSOWAT user panel. - include conf.d/yunohost_panel.conf.inc; + # Include SSOWAT user panel. + include conf.d/yunohost_panel.conf.inc; } diff --git a/conf/systemd.service b/conf/systemd.service index 56ac7ee..4ec774b 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -13,6 +13,8 @@ StandardOutput=syslog StandardError=inherit # Sandboxing options to harden security +# Depending on specificities of your service/app, you may need to tweak these +# .. but this should be a good baseline # Details for these options: https://www.freedesktop.org/software/systemd/man/systemd.exec.html NoNewPrivileges=yes PrivateTmp=yes diff --git a/scripts/backup b/scripts/backup index 4a572dc..e6d0aa0 100755 --- a/scripts/backup +++ b/scripts/backup @@ -6,6 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= +# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers @@ -55,4 +56,4 @@ ynh_backup --src_path="/etc/systemd/system/$app.service" # END OF SCRIPT #================================================= -ynh_print_info --message="Backup script completed for Node-RED. (YunoHost will then actually copy those files to the archive)." +ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)." diff --git a/scripts/change_url b/scripts/change_url index 5d39860..673347a 100755 --- a/scripts/change_url +++ b/scripts/change_url @@ -39,7 +39,7 @@ else fi #================================================= -# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +# BACKUP BEFORE CHANGE URL THEN ACTIVE TRAP #================================================= ynh_script_progression --message="Backing up the app before changing its URL (may take a while)..." --weight=1 @@ -87,19 +87,19 @@ ynh_script_progression --message="Updating NGINX web server configuration..." -- nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf -# Change the path in the nginx config file +# Change the path in the NGINX config file if [ $change_path -eq 1 ] then - # Make a backup of the original nginx config file if modified + # Make a backup of the original NGINX config file if modified ynh_backup_if_checksum_is_different --file="$nginx_conf_path" - # Set global variables for nginx helper + # Set global variables for NGINX helper domain="$old_domain" path_url="$new_path" - # Create a dedicated nginx config + # Create a dedicated NGINX config ynh_add_nginx_config fi -# Change the domain for nginx +# Change the domain for NGINX if [ $change_domain -eq 1 ] then # Delete file checksum for the old conf file location @@ -127,6 +127,7 @@ chown -R $app: "$final_path/data/settings.js" #================================================= ynh_script_progression --message="Starting a systemd service..." --weight=1 +# Start a systemd service ynh_systemd_action --service_name=$app --action="start" #================================================= @@ -140,4 +141,4 @@ ynh_systemd_action --service_name=nginx --action=reload # END OF SCRIPT #================================================= -ynh_script_progression --message="Change of URL completed for Node-RED" --last +ynh_script_progression --message="Change of URL completed for $app" --last diff --git a/scripts/install b/scripts/install index 645985c..61707ed 100755 --- a/scripts/install +++ b/scripts/install @@ -24,6 +24,7 @@ domain=$YNH_APP_ARG_DOMAIN path_url=$YNH_APP_ARG_PATH admin=$YNH_APP_ARG_ADMIN is_public=$YNH_APP_ARG_IS_PUBLIC + app=$YNH_APP_INSTANCE_NAME #================================================= @@ -52,16 +53,25 @@ ynh_app_setting_set --app=$app --key=path --value=$path_url #================================================= ynh_script_progression --message="Finding an available port..." --weight=1 -# Find a free port +# Find an available port port=$(ynh_find_port --port=1880) ynh_app_setting_set --app=$app --key=port --value=$port #================================================= -# INSTALL NODEJS +# INSTALL DEPENDENCIES #================================================= +ynh_script_progression --message="Installing dependencies..." ynh_install_nodejs --nodejs_version=$nodejs_version +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Configuring system user..." --weight=1 + +# Create a system user +ynh_system_user_create --username=$app --home_dir=$final_path + #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= @@ -72,13 +82,15 @@ ynh_app_setting_set --app=$app --key=final_path --value=$final_path ynh_setup_source --dest_dir="$final_path" #================================================= -# CREATE DEDICATED USER +# NGINX CONFIGURATION #================================================= -ynh_script_progression --message="Configuring system user..." --weight=1 +ynh_script_progression --message="Configuring NGINX web server..." --weight=1 -# Create a system user -ynh_system_user_create --username=$app --home_dir=$final_path +# Create a dedicated NGINX config +ynh_add_nginx_config +#================================================= +# SPECIFIC SETUP #================================================= # Install through npm #================================================= @@ -93,16 +105,9 @@ pushd $final_path popd #================================================= -# NGINX CONFIGURATION -#================================================= -ynh_script_progression --message="Configuring NGINX web server..." --weight=1 - -# Create a dedicated NGINX config -ynh_add_nginx_config - -#================================================= -# SPECIFIC SETUP +# ADD A CONFIGURATION #================================================= +ynh_script_progression --message="Adding a configuration file..." # Set up the settings file mkdir -p $final_path/data @@ -133,8 +138,11 @@ ynh_script_progression --message="Configuring a systemd service..." --weight=1 ynh_add_systemd_config #================================================= -# ADVERTISE SERVICE IN ADMIN PANEL +# GENERIC FINALIZATION #================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." yunohost service add $app --description="Low-code programming for event-driven applications" @@ -184,4 +192,4 @@ ynh_systemd_action --service_name=nginx --action=reload # END OF SCRIPT #================================================= -ynh_script_progression --message="Installation of Node-RED completed" --last +ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/remove b/scripts/remove index f8743af..7b4b312 100755 --- a/scripts/remove +++ b/scripts/remove @@ -23,13 +23,13 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= # STANDARD REMOVE #================================================= -# REMOVE SERVICE FROM ADMIN PANEL +# REMOVE SERVICE INTEGRATION IN YUNOHOST #================================================= -# Remove a service from the admin panel, added by `yunohost service add` +# 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 Node-RED service..." --weight=1 + ynh_script_progression --message="Removing $app service..." --weight=1 yunohost service remove $app fi @@ -41,17 +41,10 @@ ynh_script_progression --message="Stopping and removing the systemd service..." # Remove the dedicated systemd config ynh_remove_systemd_config -#================================================= -# REMOVE NODEJS -#================================================= -ynh_script_progression --message="Removing NodeJS version requirement..." --weight=4 - -ynh_remove_nodejs - #================================================= # REMOVE APP MAIN DIR #================================================= -ynh_script_progression --message="Removing Node-RED main directory..." --weight=2 +ynh_script_progression --message="Removing app main directory..." --weight=2 # Remove the app directory securely ynh_secure_remove --file="$final_path" @@ -64,6 +57,13 @@ ynh_script_progression --message="Removing NGINX web server configuration..." -- # Remove the dedicated NGINX config ynh_remove_nginx_config +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_script_progression --message="Removing dependencies..." --weight=4 + +ynh_remove_nodejs + #================================================= # SPECIFIC REMOVE #================================================= @@ -82,4 +82,4 @@ ynh_system_user_delete --username=$app # END OF SCRIPT #================================================= -ynh_script_progression --message="Removal of Node-RED completed" --last +ynh_script_progression --message="Removal of $app completed" --last diff --git a/scripts/restore b/scripts/restore index 67b913f..a6eebd2 100755 --- a/scripts/restore +++ b/scripts/restore @@ -6,6 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= +# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers @@ -40,6 +41,7 @@ test ! -d $final_path \ #================================================= # RESTORE THE NGINX CONFIGURATION #================================================= +ynh_script_progression --message="Restoring the NGINX web server configuration..." ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" @@ -54,7 +56,7 @@ ynh_system_user_create --username=$app --home_dir=$final_path #================================================= # RESTORE THE APP MAIN DIR #================================================= -ynh_script_progression --message="Restoring Node-RED main directory..." --weight=5 +ynh_script_progression --message="Restoring the app main directory..." --weight=5 ynh_restore_file --origin_path="$final_path" @@ -69,8 +71,9 @@ chown -R $app: /var/log/$app #================================================= # SPECIFIC RESTORATION #================================================= -# INSTALL NODEJS +# REINSTALL DEPENDENCIES #================================================= +ynh_script_progression --message="Reinstalling dependencies..." ynh_install_nodejs --nodejs_version=$nodejs_version @@ -83,8 +86,9 @@ ynh_restore_file --origin_path="/etc/systemd/system/$app.service" systemctl enable $app.service --quiet #================================================= -# ADVERTISE SERVICE IN ADMIN PANEL +# INTEGRATE SERVICE IN YUNOHOST #================================================= +ynh_script_progression --message="Integrating service in YunoHost..." yunohost service add $app --description="Low-code programming for event-driven applications" @@ -108,4 +112,4 @@ ynh_systemd_action --service_name=nginx --action=reload # END OF SCRIPT #================================================= -ynh_script_progression --message="Restoration completed for Node-RED" --last +ynh_script_progression --message="Restoration completed for $app" --last diff --git a/scripts/upgrade b/scripts/upgrade index c53213a..3af0dc7 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -24,23 +24,33 @@ port=$(ynh_app_setting_get --app=$app --key=port) #================================================= # CHECK VERSION #================================================= +ynh_script_progression --message="Checking version..." upgrade_type=$(ynh_check_app_version_changed) #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= -ynh_script_progression --message="Backing up Node-RED before upgrading (may take a while)..." --weight=5 +ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=5 # Backup the current version of the app ynh_backup_before_upgrade ynh_clean_setup () { - # restore it if the upgrade fails + # Restore it if the upgrade fails ynh_restore_upgradebackup } # Exit if an error occurs during the execution of the script ynh_abort_if_errors +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="stop" + #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= @@ -103,10 +113,6 @@ if [[ -d "/etc/logrotate.d/$app" ]]; then ynh_secure_remove --file="/var/log/$app" fi -#================================================= -# CLOSING PORT -#================================================= - # In older versions of the package, the port serving the webui was opened to anyone, # allowing direct access to Node-RED... let's close it. if yunohost firewall list | grep -q "\- $port$" @@ -116,22 +122,12 @@ then fi #================================================= -# STANDARD UPGRADE STEPS +# CREATE DEDICATED USER #================================================= -# STOP SYSTEMD SERVICE -#================================================= -ynh_script_progression --message="Stopping a systemd service..." --weight=1 +ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1 -ynh_systemd_action --service_name=$app --action="stop" - -#================================================= -# INSTALL NODEJS -#================================================= - -if [ $(ynh_app_setting_get --app=$app --key=nodejs_version) != $nodejs_version ]; then - ynh_remove_nodejs - ynh_install_nodejs --nodejs_version=$nodejs_version -fi +# Create a dedicated user (if not existing) +ynh_system_user_create --username=$app --home_dir=$final_path #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -146,13 +142,25 @@ then fi #================================================= -# CREATE DEDICATED USER +# NGINX CONFIGURATION #================================================= -ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1 +ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1 -# Create a dedicated user (if not existing) -ynh_system_user_create --username=$app --home_dir=$final_path +# Create a dedicated NGINX config +ynh_add_nginx_config +#================================================= +# UPGRADE DEPENDENCIES +#================================================= +ynh_script_progression --message="Upgrading dependencies..." + +if [ $(ynh_app_setting_get --app=$app --key=nodejs_version) != $nodejs_version ]; then + ynh_remove_nodejs + ynh_install_nodejs --nodejs_version=$nodejs_version +fi + +#================================================= +# SPECIFIC UPGRADE #================================================= # Install through npm #================================================= @@ -168,16 +176,9 @@ pushd $final_path popd #================================================= -# NGINX CONFIGURATION -#================================================= -ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1 - -# Create a dedicated NGINX config -ynh_add_nginx_config - -#================================================= -# SPECIFIC UPGRADE +# UPDATE A CONFIG FILE #================================================= +ynh_script_progression --message="Updating a configuration file..." # Set up the settings file ynh_add_config --template="../conf/settings.js" --destination="$final_path/data/settings.js" @@ -198,8 +199,6 @@ ynh_script_progression --message="Upgrading systemd configuration..." --weight=1 # Create a dedicated systemd config ynh_add_systemd_config -#================================================= -# GENERIC FINALIZATION #================================================= # SECURE FILES AND DIRECTORIES #================================================= @@ -212,8 +211,11 @@ chown -R $app: "$final_path" chmod a-w "$final_path/data/settings.js" #================================================= -# ADVERTISE SERVICE IN ADMIN PANEL +# GENERIC FINALIZATION #================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." yunohost service add $app --description="Low-code programming for event-driven applications" @@ -235,4 +237,4 @@ ynh_systemd_action --service_name=nginx --action=reload # END OF SCRIPT #================================================= -ynh_script_progression --message="Upgrade of Node-RED completed" --last +ynh_script_progression --message="Upgrade of $app completed" --last