From 4cfac67dcdb1293bef8e28bcdcad0fa80b4a9959 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Sat, 29 Sep 2018 21:08:14 +0200 Subject: [PATCH] Panel-config + actions fully tested --- actions.json | 36 +++++++------- config_panel.json | 37 ++++++++++++++ scripts/_common.sh | 18 +++++++ scripts/actions/public_private | 18 +++---- scripts/config | 88 ++++++++++++++++++++++++++++++++++ scripts/install | 2 + scripts/upgrade | 36 +++++++++++--- 7 files changed, 199 insertions(+), 36 deletions(-) create mode 100644 config_panel.json create mode 100644 scripts/config diff --git a/actions.json b/actions.json index 1a9d9b1..e506b49 100644 --- a/actions.json +++ b/actions.json @@ -1,20 +1,20 @@ [{ - "id": "public_private", - "name": "Move to public or private", - "command": "/bin/bash scripts/actions/public_private", - "user": "root", - "accepted_return_codes": [0], - "description": { - "en": "Change the public access of the app." - }, - "arguments": [ - { - "name": "is_public", - "type": "boolean", - "ask": { - "en": "Is it a public app ? (1/0) (1=yes; 0=no)" - }, - "default": true - } - ] + "id": "public_private", + "name": "Move to public or private", + "command": "/bin/bash scripts/actions/public_private", + "user": "root", + "accepted_return_codes": [0], + "description": { + "en": "Change the public access of the app." + }, + "arguments": [ + { + "name": "is_public", + "type": "boolean", + "ask": { + "en": "Is it a public app ?" + }, + "default": true + } + ] }] diff --git a/config_panel.json b/config_panel.json new file mode 100644 index 0000000..9976222 --- /dev/null +++ b/config_panel.json @@ -0,0 +1,37 @@ +{ + "name": "Wordpress configuration panel", + "version": "0.1", + "panel": [{ + "name": "Wordpress configuration", + "id": "main", + "sections": [{ + "name": "Public access", + "id": "is_public", + "options": [{ + "name": "Is it a public app ?", + "id": "is_public", + "type": "bool", + "default": true + }] + }, + { + "name": "Overwriting config files", + "id": "overwrite_files", + "options": [{ + "name": "Overwrite the nginx config file ?", + "help": "If the file is overwritten, a backup will be created.", + "id": "overwrite_nginx", + "type": "bool", + "default": true + }, + { + "name": "Overwrite the php-fpm config file ?", + "help": "If the file is overwritten, a backup will be created.", + "id": "overwrite_phpfpm", + "type": "bool", + "default": true + }] + }] + } +] +} diff --git a/scripts/_common.sh b/scripts/_common.sh index b23810b..ddee27c 100755 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -34,6 +34,24 @@ CHECK_SIZE () { # Vérifie avant chaque backup que l'espace est suffisant fi } +#================================================= +# BOOLEAN CONVERTER +#================================================= + +bool_to_01 () { + local var="$1" + [ "$var" = "true" ] && var=1 + [ "$var" = "false" ] && var=0 + echo "$var" +} + +bool_to_true_false () { + local var="$1" + [ "$var" = "1" ] && var=true + [ "$var" = "0" ] && var=false + echo "$var" +} + #================================================= #============= FUTURE YUNOHOST HELPER ============ diff --git a/scripts/actions/public_private b/scripts/actions/public_private index ab3d3f4..19c538c 100755 --- a/scripts/actions/public_private +++ b/scripts/actions/public_private @@ -12,14 +12,10 @@ source /usr/share/yunohost/helpers # RETRIEVE ARGUMENTS #================================================= -# Get is_public and set as lowercase -is_public=${YNH_ACTION_IS_PUBLIC,,} +# Get is_public +is_public=${YNH_ACTION_IS_PUBLIC} -app=$YNH_APP_ID - -#================================================= -# CHECK IF ARGUMENTS ARE CORRECT -#================================================= +app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} #================================================= # CHECK IF AN ACTION HAS TO BE DONE @@ -29,7 +25,7 @@ is_public_old=$(ynh_app_setting_get $app is_public) if [ $is_public -eq $is_public_old ] then - ynh_die "is_public is already set as $is_public." 0 + ynh_die "is_public is already set as $is_public." 0 fi #================================================= @@ -38,11 +34,11 @@ fi # MOVE TO PUBLIC OR PRIVATE #================================================= -if [ $is_public -eq 0 ]; +if [ $is_public -eq 0 ] then - ynh_app_setting_delete $app unprotected_uris + ynh_app_setting_delete $app unprotected_uris else - ynh_app_setting_set $app unprotected_uris "/" + ynh_app_setting_set $app unprotected_uris "/" fi # Regen ssowat configuration diff --git a/scripts/config b/scripts/config new file mode 100644 index 0000000..4777fe9 --- /dev/null +++ b/scripts/config @@ -0,0 +1,88 @@ +#!/bin/bash + +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# RETRIEVE ARGUMENTS +#================================================= + +app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID} + +#================================================= +# SPECIFIC CODE +#================================================= +# LOAD VALUES +#================================================= + +# Load the real value from the app config or elsewhere. +# Then get the value from the form. +# If the form has a value for a variable, take the value from the form, +# Otherwise, keep the value from the app config. + +# is_public +old_is_public="$(ynh_app_setting_get $app is_public)" +old_is_public=$(bool_to_true_false $old_is_public) +is_public="${YNH_CONFIG_MAIN_IS_PUBLIC_IS_PUBLIC:-$old_is_public}" + +# Overwrite nginx configuration +old_overwrite_nginx="$(ynh_app_setting_get $app overwrite_nginx)" +old_overwrite_nginx=$(bool_to_true_false $old_overwrite_nginx) +overwrite_nginx="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_NGINX:-$old_overwrite_nginx}" + +# Overwrite php-fpm configuration +old_overwrite_phpfpm="$(ynh_app_setting_get $app overwrite_phpfpm)" +old_overwrite_phpfpm=$(bool_to_true_false $old_overwrite_phpfpm) +overwrite_phpfpm="${YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PHPFPM:-$old_overwrite_phpfpm}" + +#================================================= +# SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND +#================================================= + +show_config() { + # here you are supposed to read some config file/database/other then print the values + # echo "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value" + + echo "YNH_CONFIG_MAIN_IS_PUBLIC_IS_PUBLIC=$is_public" + + echo "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_NGINX=$overwrite_nginx" + echo "YNH_CONFIG_MAIN_OVERWRITE_FILES_OVERWRITE_PHPFPM=$overwrite_phpfpm" +} + +#================================================= +# MODIFY THE CONFIGURATION +#================================================= + +apply_config() { + # Change public accessibility + if [ "$is_public" = "true" ] + then + yunohost app action run $app public_private --args is_public=1 + else + yunohost app action run $app public_private --args is_public=0 + fi + + # Set overwrite_nginx + overwrite_nginx=$(bool_to_01 $overwrite_nginx) + ynh_app_setting_set $app overwrite_nginx "$overwrite_nginx" + # Set overwrite_phpfpm + overwrite_phpfpm=$(bool_to_01 $overwrite_phpfpm) + ynh_app_setting_set $app overwrite_phpfpm "$overwrite_phpfpm" +} + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SELECT THE ACTION FOLLOWING THE GIVEN ARGUMENT +#================================================= + +case $1 in + show) show_config;; + apply) apply_config;; +esac diff --git a/scripts/install b/scripts/install index 4ab1252..8a8a427 100644 --- a/scripts/install +++ b/scripts/install @@ -58,6 +58,8 @@ ynh_app_setting_set $app admin $admin_wordpress ynh_app_setting_set $app is_public $is_public ynh_app_setting_set $app language $language ynh_app_setting_set $app multisite $multisite +ynh_app_setting_set $app overwrite_nginx "1" +ynh_app_setting_set $app overwrite_phpfpm "1" #================================================= # STANDARD MODIFICATIONS diff --git a/scripts/upgrade b/scripts/upgrade index 8fe8b2d..6a86acb 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -23,6 +23,8 @@ is_public=$(ynh_app_setting_get $app is_public) multisite=$(ynh_app_setting_get $app multisite) final_path=$(ynh_app_setting_get $app final_path) db_name=$(ynh_app_setting_get $app db_name) +overwrite_nginx=$(ynh_app_setting_get $app overwrite_nginx) +overwrite_phpfpm=$(ynh_app_setting_get $app overwrite_phpfpm) #================================================= # CHECK VERSION @@ -67,7 +69,7 @@ elif [ "${multisite,,}" = "no" ]; then multisite=0 fi -if [ -z $db_name ]; then # Si db_name n'est pas renseigné dans app setting +if [ -z "$db_name" ]; then # Si db_name n'est pas renseigné dans app setting db_name=$(ynh_sanitize_dbid $app) ynh_app_setting_set $app db_name $db_name fi @@ -76,6 +78,18 @@ if grep add_filter.*auto_update $final_path/wp-config.php; then # Si des add_fil sed --in-place '/add_filter.*auto_update/d' $final_path/wp-config.php fi +# If overwrite_nginx doesn't exist, create it +if [ -z "$overwrite_nginx" ]; then + overwrite_nginx=1 + ynh_app_setting_set $app overwrite_nginx $overwrite_nginx +fi + +# If overwrite_phpfpm doesn't exist, create it +if [ -z "$overwrite_phpfpm" ]; then + overwrite_phpfpm=1 + ynh_app_setting_set $app overwrite_phpfpm $overwrite_phpfpm +fi + #================================================= # STANDARD UPGRADE STEPS #================================================= @@ -102,12 +116,16 @@ path_url=$(ynh_normalize_url_path $path_url) # NGINX CONFIGURATION #================================================= -# Create a dedicated nginx config -if [ "$path_url" != "/" ] +# Overwrite the nginx configuration only if it's allowed +if [ $overwrite_nginx -eq 1 ] then - ynh_replace_string "^#sub_path_only" "" "../conf/nginx.conf" + # Create a dedicated nginx config + if [ "$path_url" != "/" ] + then + ynh_replace_string "^#sub_path_only" "" "../conf/nginx.conf" + fi + ynh_add_nginx_config fi -ynh_add_nginx_config #================================================= # CREATE DEDICATED USER @@ -120,8 +138,12 @@ ynh_system_user_create $app # PHP-FPM CONFIGURATION #================================================= -# Create a dedicated php-fpm config -ynh_add_fpm_config +# Overwrite the php-fpm configuration only if it's allowed +if [ $overwrite_phpfpm -eq 1 ] +then + # Create a dedicated php-fpm config + ynh_add_fpm_config +fi #================================================= # SPECIFIC UPGRADE