From 8171698c07fc28da1708b432a759243ffef04538 Mon Sep 17 00:00:00 2001 From: ericgaspar Date: Thu, 5 Nov 2020 13:34:17 +0100 Subject: [PATCH] Repackage --- conf/nginx.conf | 16 ++++--- manifest.json | 21 ++++++++- scripts/_common.sh | 17 ++++++++ scripts/backup | 69 ++++++++++++++++++++++++----- scripts/install | 105 ++++++++++++++++++++++++++++++++++++--------- scripts/remove | 47 +++++++++++++++----- scripts/restore | 87 ++++++++++++++++++++++++++++++++----- scripts/upgrade | 100 ++++++++++++++++++++++++++++++++++-------- 8 files changed, 384 insertions(+), 78 deletions(-) create mode 100644 scripts/_common.sh diff --git a/conf/nginx.conf b/conf/nginx.conf index 55e2999..51c7570 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,16 +1,20 @@ -location PATHTOCHANGE { +#sub_path_only rewrite ^__PATH__$ __PATH__/ permanent; +location __PATH__/ { - alias WWWPATH ; + # Path to source + alias __FINALPATH__/ ; + + index index.php; - index index.html index.php ; try_files $uri $uri/ index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; - fastcgi_pass unix:/var/run/php5-fpm.sock; + fastcgi_pass unix:/var/run/php/php__PHPVERSION__-fpm-__NAME__.sock; + fastcgi_index index.php; include fastcgi_params; - fastcgi_param REMOTE_USER $remote_user; - fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param REMOTE_USER $remote_user; + fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $request_filename; } diff --git a/manifest.json b/manifest.json index e6968ca..f3f8e7c 100644 --- a/manifest.json +++ b/manifest.json @@ -1,18 +1,24 @@ { "name": "ChtickyNotes", "id": "chtickynotes", + "packaging_format": 1, "description": { "en": "Generic sticky notes for all purpose", "fr": "Des post-its génériques à tout faire" }, + "version": "1.0~ynh1", "license": "MIT", "developer": { "name": "chtixof" }, - "multi_instance": "false", + "requirements": { + "yunohost": ">= 3.8.1" + }, + "multi_instance": false, "arguments": { "install" : [ { "name": "domain", + "type": "domain", "ask": { "en": "Choose a domain for ChtickyNotes", "fr": "Choisissez un domaine pour ChtickyNotes" @@ -20,12 +26,23 @@ "example": "domain.org" },{ "name": "path", + "type": "path", "ask": { "en": "Choose a path for ChtickyNotes", "fr": "Choisissez un chemin pour ChtickyNotes" }, "example": "/chtickynotes", "default": "/chtickynotes" - } ] + }, + { + "name": "is_public", + "type": "boolean", + "ask": { + "en": "Is it a public application?", + "fr": "Est-ce une application publique ?" + }, + "default": true + } + ] } } diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..944a65e --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +#================================================= +# COMMON VARIABLES +#================================================= + +#================================================= +# PERSONAL HELPERS +#================================================= + +#================================================= +# EXPERIMENTAL HELPERS +#================================================= + +#================================================= +# FUTURE OFFICIAL HELPERS +#================================================= diff --git a/scripts/backup b/scripts/backup index 6d552f2..0e8f238 100644 --- a/scripts/backup +++ b/scripts/backup @@ -1,15 +1,62 @@ #!/bin/bash -app=chtickynotes -# The parameter $1 is the backup directory location -# which will be compressed afterward -backup_dir=$1/apps/$app -mkdir -p $backup_dir +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Backup sources & data -sudo cp -a /var/www/$app/. $backup_dir/sources +#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 -# Copy Nginx and YunoHost parameters to make the script "standalone" -sudo cp -a /etc/yunohost/apps/$app/. $backup_dir/yunohost -domain=$(sudo yunohost app setting $app domain) -sudo cp -a /etc/nginx/conf.d/$domain.d/$app.conf $backup_dir/nginx.conf \ No newline at end of file +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +ynh_clean_setup () { + ### Remove this function if there's nothing to clean before calling the remove script. + true +} +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= +ynh_print_info --message="Loading installation settings..." + +app=$YNH_APP_INSTANCE_NAME + +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +domain=$(ynh_app_setting_get --app=$app --key=domain) +phpversion=$(ynh_app_setting_get --app=$app --key=phpversion) + +#================================================= +# DECLARE DATA AND CONF FILES TO BACKUP +#================================================= +ynh_print_info --message="Declaring files to be backed up..." + +#================================================= +# BACKUP THE APP MAIN DIR +#================================================= + +ynh_backup --src_path="$final_path" + +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= + +ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# BACKUP THE PHP-FPM CONFIGURATION +#================================================= + +ynh_backup --src_path="/etc/php/$phpversion/fpm/pool.d/$app.conf" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)." diff --git a/scripts/install b/scripts/install index 32d6cf2..cde1675 100644 --- a/scripts/install +++ b/scripts/install @@ -1,28 +1,91 @@ #!/bin/bash -# Installation de ChtickyNotes dans Yunohost -app=chtickynotes -# Retrieve arguments -domain=$1 -path=$2 +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Check domain/path availability -sudo yunohost app checkurl $domain$path -a $app -if [[ ! $? -eq 0 ]]; then - exit 1 -fi +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +ynh_abort_if_errors + +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= + +domain=$YNH_APP_ARG_DOMAIN +path_url=$YNH_APP_ARG_PATH +is_public=$YNH_APP_ARG_IS_PUBLIC + +app=$YNH_APP_INSTANCE_NAME + +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS +#================================================= +ynh_script_progression --message="Validating installation parameters..." --weight=1 -# Copy files to the right place with the right permissions final_path=/var/www/$app -sudo mkdir -p $final_path -sudo cp -a ../sources/* $final_path -sudo chown -R www-data: $final_path +test ! -e "$final_path" || ynh_die --message="This path already contains a folder" -# Modify Nginx configuration file and copy it to Nginx conf directory -sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf -sed -i "s@WWWPATH@$final_path@g" ../conf/nginx.conf -sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf +# Register (book) web path +ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url -# Reload nginx and regenerate SSOwat conf -sudo service nginx reload -sudo yunohost app ssowatconf \ No newline at end of file +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_script_progression --message="Storing installation settings..." --weight=1 + +ynh_app_setting_set --app=$app --key=domain --value=$domain +ynh_app_setting_set --app=$app --key=path --value=$path_url +ynh_app_setting_set --app=$app --key=is_public --value=$is_public + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= +ynh_script_progression --message="Setting up source files..." --weight=7 + +ynh_app_setting_set --app=$app --key=final_path --value=$final_path + +mkdir -p $final_path +cp -a ../sources/* $final_path + +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Configuring NGINX web server..." --weight=2 + +# Create a dedicated NGINX config +ynh_add_nginx_config + +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= + +chown -R www-data: $final_path + +#================================================= +# SETUP SSOWAT +#================================================= +ynh_script_progression --message="Configuring SSOwat..." --weight=1 + +# Make app public if necessary or protect it +[ $is_public -eq 0 ] || ynh_permission_update --permission "main" --add "visitors" + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --weight=1 + +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/remove b/scripts/remove index 9c7bddc..6ddb24d 100644 --- a/scripts/remove +++ b/scripts/remove @@ -1,15 +1,42 @@ #!/bin/bash -app=chtickynotes -# Retrieve arguments -domain=$(sudo yunohost app setting $app domain) +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Remove sources -sudo rm -rf /var/www/$app +source _common.sh +source /usr/share/yunohost/helpers -# Remove configuration files -sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --weight=1 -# Restart services -sudo service nginx reload -sudo yunohost app ssowatconf +app=$YNH_APP_INSTANCE_NAME + +domain=$(ynh_app_setting_get --app=$app --key=domain) +final_path=$(ynh_app_setting_get --app=$app --key=final_path) + +#================================================= +# REMOVE APP MAIN DIR +#================================================= +ynh_script_progression --message="Removing app main directory..." --weight=2 + +# Remove the app directory securely +ynh_secure_remove --file="$final_path" + +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Removing NGINX web server configuration..." --weight=1 + +# Remove the dedicated NGINX config +ynh_remove_nginx_config + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Removal of $app completed" --last diff --git a/scripts/restore b/scripts/restore index 8223942..bc45fee 100644 --- a/scripts/restore +++ b/scripts/restore @@ -1,16 +1,81 @@ #!/bin/bash -app=chtickynotes -# The parameter $1 is the uncompressed restore directory location -backup_dir=$1/apps/$app +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Restore sources & data -sudo cp -a $backup_dir/sources/. /var/www/$app +source ../settings/scripts/_common.sh +source /usr/share/yunohost/helpers -# Restore Nginx and YunoHost parameters -sudo cp -a $backup_dir/yunohost/. /etc/yunohost/apps/$app -domain=$(sudo yunohost app setting $app domain) -sudo cp -a $backup_dir/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= -# Restart webserver -sudo service nginx reload \ No newline at end of file +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --weight=1 + +app=$YNH_APP_INSTANCE_NAME + +domain=$(ynh_app_setting_get --app=$app --key=domain) +path_url=$(ynh_app_setting_get --app=$app --key=path) +final_path=$(ynh_app_setting_get --app=$app --key=final_path) + +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= +ynh_script_progression --message="Validating restoration parameters..." --weight=2 + +ynh_webpath_available --domain=$domain --path_url=$path_url \ + || ynh_die --message="Path not available: ${domain}${path_url}" +test ! -d $final_path \ + || ynh_die --message="There is already a directory: $final_path " + +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= + +ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= +ynh_script_progression --message="Restoring the app main directory..." --weight=2 + +ynh_restore_file --origin_path="$final_path" + +#================================================= +# RESTORE USER RIGHTS +#================================================= + +# Restore permissions on app files +chown -R www-data: $final_path + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 + +yunohost service add $app --description "A short description of the app" --log "/var/log/$app/$app.log" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --weight=1 + +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Restoration completed for $app" --last diff --git a/scripts/upgrade b/scripts/upgrade index f5559ec..a6177aa 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,24 +1,90 @@ #!/bin/bash -app=chtickynotes -# Retrieve arguments -domain=$(sudo yunohost app setting $app domain) -path=$(sudo yunohost app setting $app path) +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Remove trailing "/" for next commands -path=${path%/} +source _common.sh +source /usr/share/yunohost/helpers -# Copy source files -final_path=/var/www/$app -sudo mkdir -p $final_path -sudo cp -a ../sources/* $final_path +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --weight=1 -# Modify Nginx configuration file and copy it to Nginx conf directory -sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf -sed -i "s@WWWPATH@$final_path@g" ../conf/nginx.conf -sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf +app=$YNH_APP_INSTANCE_NAME -# Restart services -sudo service nginx reload -sudo yunohost app ssowatconf +domain=$(ynh_app_setting_get --app=$app --key=domain) +path_url=$(ynh_app_setting_get --app=$app --key=path) +final_path=$(ynh_app_setting_get --app=$app --key=final_path) +#================================================= +# CHECK VERSION +#================================================= + +upgrade_type=$(ynh_check_app_version_changed) + +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= +ynh_script_progression --message="Ensuring downward compatibility..." --weight=2 + +# Fix is_public as a boolean value +if [ "$is_public" = "Yes" ]; then + ynh_app_setting_set --app=$app --key=is_public --value=1 + is_public=1 +elif [ "$is_public" = "No" ]; then + ynh_app_setting_set --app=$app --key=is_public --value=0 + is_public=0 +fi + +# If final_path doesn't exist, create it +if [ -z "$final_path" ]; then + final_path=/var/www/$app + ynh_app_setting_set --app=$app --key=final_path --value=$final_path +fi + +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= +ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=1 + +# Backup the current version of the app +ynh_backup_before_upgrade +ynh_clean_setup () { + # restore it if the upgrade fails + ynh_restore_upgradebackup +} +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= + +mkdir -p $final_path +cp -a ../sources/* $final_path +chown -R www-data: $final_path + +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2 + +# Create a dedicated nginx config +ynh_add_nginx_config + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --weight=1 + +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Upgrade of $app completed" --last