From 12de58d4a22a73b3a728a90ba4a598f6b38571a7 Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 14:41:46 +0000 Subject: [PATCH 01/11] Create .functions --- scripts/.functions | 248 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 scripts/.functions diff --git a/scripts/.functions b/scripts/.functions new file mode 100644 index 0000000..a701564 --- /dev/null +++ b/scripts/.functions @@ -0,0 +1,248 @@ +Skip to content +This repository +Search +Pull requests +Issues +Gist + @polytan02 + Sign out + Unwatch 1 + Star 0 + Fork 6 polytan02/phpmyadmin_ynh +forked from YunoHost-Apps/phpmyadmin_ynh + Code Pull requests 0 Projects 0 Wiki Pulse Graphs Settings +Branch: master Find file Copy pathphpmyadmin_ynh/scripts/.fonctions +1a1a940 8 days ago +@polytan02 polytan02 Update on .fonctions file +1 contributor +RawBlameHistory +Executable File 228 lines (201 sloc) 8.08 KB +#!/bin/bash + +CHECK_VAR () { # Vérifie que la variable n'est pas vide. +# $1 = Variable à vérifier +# $2 = Texte à afficher en cas d'erreur + test -n "$1" || (echo "$2" >&2 && false) +} + +EXIT_PROPERLY () { # Provoque l'arrêt du script en cas d'erreur. Et nettoye les résidus. + exit_code=$? + if [ "$exit_code" -eq 0 ]; then + exit 0 # Quitte sans erreur si le script se termine correctement. + fi + trap '' EXIT + set +eu + echo -e "\e[91m \e[1m" # Shell in light red bold + echo -e "!!\n $app install's script has encountered an error. Installation was cancelled.\n!!" >&2 + + if type -t CLEAN_SETUP > /dev/null; then # Vérifie l'existance de la fonction avant de l'exécuter. + CLEAN_SETUP # Appel la fonction de nettoyage spécifique du script install. + fi + + # Compense le bug de ssowat qui ne supprime pas l'entrée de l'app en cas d'erreur d'installation. + sudo sed -i "\@\"$domain$path/\":@d" /etc/ssowat/conf.json + + ynh_die +} + +TRAP_ON () { # Activate signal capture + set -eu # Exit if a command fail, and if a variable is used unset. + trap EXIT_PROPERLY EXIT # Capturing exit signals on shell script +} + +# Ignore the yunohost-cli log to prevent errors with conditionals commands +# usage: NO_LOG COMMAND +# Simply duplicate the log, execute the yunohost command and replace the log without the result of this command +# It's a very badly hack... +# Petite copie perso à mon usage ;) +NO_LOG() { + ynh_cli_log=/var/log/yunohost/yunohost-cli.log + sudo cp -a ${ynh_cli_log} ${ynh_cli_log}-move + eval $@ + exit_code=$? + sudo mv ${ynh_cli_log}-move ${ynh_cli_log} + return $? +} + +CHECK_USER () { # Vérifie la validité de l'user admin +# $1 = Variable de l'user admin. + ynh_user_exists "$1" || (echo "Wrong admin" >&2 && false) +} + +CHECK_PATH () { # Vérifie la présence du / en début de path. Et son absence à la fin. + if [ "${path:0:1}" != "/" ]; then # Si le premier caractère n'est pas un / + path="/$path" # Ajoute un / en début de path + fi + if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then # Si le dernier caractère est un / et que ce n'est pas le seul caractère. + path="${path:0:${#path}-1}" # Supprime le dernier caractère + fi +} + +CHECK_DOMAINPATH () { # Vérifie la disponibilité du path et du domaine. + sudo yunohost app checkurl $domain$path -a $app +} + +CHECK_FINALPATH () { # Vérifie que le dossier de destination n'est pas déjà utilisé. + final_path=/var/www/$app + if [ -e "$final_path" ] + then + echo "This path already contains a folder" >&2 + false + fi +} + +GENERATE_DB () { # Créer une base de données et un utilisateur dédié au nom de l'app. +# $1 = Nom de la base de donnée + db_user=$1 + db_user=${db_user//-/_} # mariadb ne supporte pas les - dans les noms de base de données. Ils sont donc remplacé par des _ + # Génère un mot de passe aléatoire. +# db_pwd=$(head -n20 /dev/urandom | tr -c -d 'A-Za-z0-9' | head -c20) + db_pwd=$(ynh_string_random) + CHECK_VAR "$db_pwd" "db_pwd empty" + # Utilise '$app' comme nom d'utilisateur et de base de donnée + # Initialise la base de donnée et stocke le mot de passe mysql. + ynh_mysql_create_db "$db_user" "$db_user" $db_pwd + ynh_app_setting_set $app mysqlpwd $db_pwd +} + +SETUP_SOURCE () { # Télécharge la source, décompresse et copie dans $final_path +# $1 = Nom de l'archive téléchargée. + wget -nv -i ../sources/source_url -O $1 + # Vérifie la somme de contrôle de la source téléchargée. + md5sum -c ../sources/source_md5 --status || (echo "Corrupt source" >&2 && false) + # Décompresse la source + if [ "$(echo ${1##*.})" == "gz" ]; then + tar -x -f $1 + elif [ "$(echo ${1##*.})" == "zip" ]; then + unzip -q $1 + else + false # Format d'archive non pris en charge. + fi + # Copie les fichiers sources + sudo cp -a $(cat ../sources/source_dir)/. "$final_path" + # Copie les fichiers additionnels ou modifiés. + if test -e "../sources/ajouts"; then + sudo cp -a ../sources/ajouts/. "$final_path" + fi +} + +ADD_SYS_USER () { # Créer un utilisateur système dédié à l'app + if ! ynh_system_user_exists "$app" # Test l'existence de l'utilisateur + then + sudo useradd -d /var/www/$app --system --user-group $app --shell /usr/sbin/nologin || (echo "Unable to create $app system account" >&2 && false) + fi +} + +POOL_FPM () { # Créer le fichier de configuration du pool php-fpm et le configure. + sed -i "s@__NAMETOCHANGE__@$app@g" ../conf/php-fpm.conf + sed -i "s@__FINALPATH__@$final_path@g" ../conf/php-fpm.conf + sed -i "s@__USER__@$app@g" ../conf/php-fpm.conf + finalphpconf=/etc/php5/fpm/pool.d/$app.conf + sudo cp ../conf/php-fpm.conf $finalphpconf + sudo chown root: $finalphpconf + finalphpini=/etc/php5/fpm/conf.d/20-$app.ini + sudo cp ../conf/php-fpm.ini $finalphpini + sudo chown root: $finalphpini + sudo service php5-fpm reload +} + +STORE_MD5_CONFIG () { # Enregistre la somme de contrôle du fichier de config +# $1 = Nom du fichier de conf pour le stockage dans settings.yml +# $2 = Nom complet et chemin du fichier de conf. + ynh_app_setting_set $app $1_file_md5 $(sudo md5sum "$2" | cut -d' ' -f1) +} + +CHECK_MD5_CONFIG () { # Créé un backup du fichier de config si il a été modifié. +# $1 = Nom du fichier de conf pour le stockage dans settings.yml +# $2 = Nom complet et chemin du fichier de conf. + if [ "$(ynh_app_setting_get $app $1_file_md5)" != $(sudo md5sum "$2" | cut -d' ' -f1) ]; then + sudo cp -a "$2" "$2.backup.$(date '+%d.%m.%y_%Hh%M,%Ss')" # Si le fichier de config a été modifié, créer un backup. + fi +} + +FIND_PORT () { # Cherche un port libre. +# $1 = Numéro de port pour débuter la recherche. + port=$1 + while ! sudo yunohost app checkport $port ; do + port=$((port+1)) + done + CHECK_VAR "$port" "port empty" +} + + +### REMOVE SCRIPT + +REMOVE_NGINX_CONF () { # Suppression de la configuration nginx + if [ -e "/etc/nginx/conf.d/$domain.d/$app.conf" ]; then # Delete nginx config + echo "Delete nginx config" + sudo rm "/etc/nginx/conf.d/$domain.d/$app.conf" +# sudo service nginx reload + fi +} + +REMOVE_FPM_CONF () { # Suppression de la configuration du pool php-fpm + if [ -e "/etc/php5/fpm/pool.d/$app.conf" ]; then # Delete fpm config + echo "Delete fpm config" + sudo rm "/etc/php5/fpm/pool.d/$app.conf" + fi + if [ -e "/etc/php5/fpm/conf.d/20-$app.ini" ]; then # Delete php config + echo "Delete php config" + sudo rm "/etc/php5/fpm/conf.d/20-$app.ini" + fi +# sudo service php5-fpm reload +} + +REMOVE_LOGROTATE_CONF () { # Suppression de la configuration de logrotate + if [ -e "/etc/logrotate.d/$app" ]; then + echo "Delete logrotate config" + sudo rm "/etc/logrotate.d/$app" + fi +} + +SECURE_REMOVE () { # Suppression de dossier avec vérification des variables + chaine="$1" # L'argument doit être donné entre quotes simple '', pour éviter d'interpréter les variables. + no_var=0 + while (echo "$chaine" | grep -q '\$') # Boucle tant qu'il y a des $ dans la chaine + do + no_var=1 + global_var=$(echo "$chaine" | cut -d '$' -f 2) # Isole la première variable trouvée. + only_var=\$$(expr "$global_var" : '\([A-Za-z0-9_]*\)') # Isole complètement la variable en ajoutant le $ au début et en gardant uniquement le nom de la variable. Se débarrasse surtout du / et d'un éventuel chemin derrière. + real_var=$(eval "echo ${only_var}") # `eval "echo ${var}` permet d'interpréter une variable contenue dans une variable. + if test -z "$real_var" || [ "$real_var" = "/" ]; then + echo "Variable $only_var is empty, suppression of $chaine cancelled." >&2 + return 1 + fi + chaine=$(echo "$chaine" | sed "s@$only_var@$real_var@") # remplace la variable par sa valeur dans la chaine. + done + if [ "$no_var" -eq 1 ] + then + if [ -e "$chaine" ]; then + echo "Delete directory $chaine" + sudo rm -r "$chaine" + fi + return 0 + else + echo "No detected variable." >&2 + return 1 + fi +} + +REMOVE_BDD () { # Suppression de la base de donnée et de l'utilisateur associé. +# $1 = Nom de la base de donnée + # Utilise '$app' comme nom d'utilisateur et de base de donnée + db_user=$1 + if mysqlshow -u root -p$(sudo cat $MYSQL_ROOT_PWD_FILE) | grep -q "^| $db_user"; then + echo "Delete db" + ynh_mysql_drop_db $db_user + ynh_mysql_drop_user $db_user + fi +} + +REMOVE_SYS_USER () { # Supprime l'utilisateur système dédié à l'app + if ynh_system_user_exists "$app" # Test l'existence de l'utilisateur + then + sudo userdel $app + fi +} +Contact GitHub API Training Shop Blog About +© 2017 GitHub, Inc. Terms Privacy Security Status Help From a3707289af6ed248bb0fc1ac92c1c13a0e7a9f6f Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 14:47:46 +0000 Subject: [PATCH 02/11] Use of extra helpers Make sure $path is correct (the {YNH_PATH%/} doesn't work in every cases --- scripts/install | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/scripts/install b/scripts/install index 837b0fd..eb8f389 100644 --- a/scripts/install +++ b/scripts/install @@ -1,7 +1,10 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +# Load extra functions +source .fonctions + +# Activate TRAP to stop the script if an error is detected +TRAP_ON # This is a multi-instance app, meaning it can be installed several times independently # The id of the app as stated in the manifest is available as $YNH_APP_ID @@ -17,7 +20,7 @@ app=$YNH_APP_INSTANCE_NAME # Retrieve arguments domain=$YNH_APP_ARG_DOMAIN -path=${YNH_APP_ARG_PATH%/} +path=$YNH_APP_ARG_PATH admin=$YNH_APP_ARG_ADMIN title=$YNH_APP_ARG_TITLE is_public=$YNH_APP_ARG_IS_PUBLIC @@ -27,14 +30,27 @@ final_path=$www_path/$app # Source YunoHost helpers$ source /usr/share/yunohost/helpers +# Check variables are not empty +CHECK_VAR "$app" "app name not set" + +# Check validity of admin user +CHECK_USER "$admin" + +# Check and correct path syntax +CHECK_PATH + +# Check availibility of path and domain +CHECK_DOMAINPATH + # Save app settings ynh_app_setting_set "$app" admin "$admin" ynh_app_setting_set "$app" is_public "$is_public" ynh_app_setting_set "$app" path "$path" -# Check domain/path availability -sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" + +# Check final_path availibility. Installation stops if it already exists +CHECK_FINALPATH +sudo mkdir "$final_path" tmp=/tmp/flaruminstall sudo rm -rf $tmp From 08a6dfc92abe0fa8cea7ed3b69fa1db00049c202 Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 14:56:18 +0000 Subject: [PATCH 03/11] Store composer in /opt No need to "hide" composer in /var/www/.composer ==> You can place it in /opt for example Easier to remember and to clean afterwards. Other adjustments may be required in your script ! --- scripts/install | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/install b/scripts/install index 837b0fd..94680c8 100644 --- a/scripts/install +++ b/scripts/install @@ -43,9 +43,9 @@ sudo chown -R www-data:www-data $tmp sudo chmod -R 755 $tmp # Prepare composer and cache directories -sudo mkdir -p /var/www/.composer/cache -sudo chown -R www-data:www-data /var/www/.composer -sudo chmod -R 755 /var/www/.composer +sudo mkdir -p /opt/flarum_composer/cache +sudo chown -R www-data:www-data /opt/flarum_composer +sudo chmod -R 755 /opt/flarum_composer ### composer ### if ! type "composer" > /dev/null; then @@ -55,7 +55,7 @@ if ! type "composer" > /dev/null; then ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', '$tmp/composer-setup.php');") if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ] then - sudo su - root -c "grep -q -F 'env[COMPOSER_HOME]= /var/www/.composer' /etc/php5/fpm/php-fpm.conf || sudo echo 'env[COMPOSER_HOME]= /var/www/.composer' >> /etc/php5/fpm/php-fpm.conf" + sudo su - root -c "grep -q -F 'env[COMPOSER_HOME]= /opt/flarum_composer' /etc/php5/fpm/php-fpm.conf || sudo echo 'env[COMPOSER_HOME]= /opt/flarum_composer' >> /etc/php5/fpm/php-fpm.conf" sudo service php5-fpm reload sudo su - www-data -s /bin/bash -c "php $tmp/composer-setup.php --install-dir=$tmp --filename=composer" sudo mv $tmp/composer /usr/local/bin From b093b2e1b5ce42da25713023af3e9ddd77143c93 Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 14:57:58 +0000 Subject: [PATCH 04/11] Remove composer as well --- scripts/remove | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/remove b/scripts/remove index e073f51..aae75d6 100644 --- a/scripts/remove +++ b/scripts/remove @@ -12,6 +12,8 @@ domain=$(ynh_app_setting_get "$app" domain) # Remove sources sudo rm -rf /tmp/composerinstall sudo rm -rf /var/www/$app +sudo rm -rf /opt/flarum_composer +sudo rm -rf /usr/local/bin/composer # Remove nginx configuration file sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf From 676fac82f3c88754c948f407a06a86adfa9e15aa Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 15:21:27 +0000 Subject: [PATCH 05/11] Update backup --- scripts/backup | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/backup b/scripts/backup index 575a599..f162882 100644 --- a/scripts/backup +++ b/scripts/backup @@ -13,6 +13,12 @@ source /usr/share/yunohost/helpers # Note: the last argument is where to save this path, see the restore script. ynh_backup "/var/www/${app}" "sources" +# Backup of composer +# +# TO BE VALIDATED AND TESTED +ynh_backup "/opt/flarum_composer" "flarum_composer" +ynh_backup "/usr/local/bin/composerr" "local_composer" + ### MySQL ### # If a MySQL database is used: # # Dump the database From f12a568a3179511ebf6244f8a5a49d474676c60f Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 15:23:34 +0000 Subject: [PATCH 06/11] Update restore --- scripts/restore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/restore b/scripts/restore index 2f532ee..23c300f 100644 --- a/scripts/restore +++ b/scripts/restore @@ -24,6 +24,13 @@ sudo yunohost app checkurl "${domain}${path}" -a "$app" \ src_path="/var/www/${app}" sudo cp -a ./sources "$src_path" +# Restore composer +# +# TO BE TESTED AND VALIDATED +# +cp -a ./flarum_composer /opt/flarum_composer +cp -a ./local_composer /usr/local/bin/composer + # Restore permissions to app files # you may need to make some file and/or directory writeable by www-data (nginx user) sudo chown -R root: "$src_path" From ee9bcedbe02869da07e97f9ce70bec8bfabf4c96 Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 15:24:00 +0000 Subject: [PATCH 07/11] Typo --- scripts/backup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/backup b/scripts/backup index f162882..5c5bc81 100644 --- a/scripts/backup +++ b/scripts/backup @@ -17,7 +17,7 @@ ynh_backup "/var/www/${app}" "sources" # # TO BE VALIDATED AND TESTED ynh_backup "/opt/flarum_composer" "flarum_composer" -ynh_backup "/usr/local/bin/composerr" "local_composer" +ynh_backup "/usr/local/bin/composer" "local_composer" ### MySQL ### # If a MySQL database is used: From bb025b1255f40cf96494e6eb5aca652686c2c9e6 Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 15:50:16 +0000 Subject: [PATCH 08/11] path is / for root --- scripts/install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/install b/scripts/install index eb8f389..c4ca3bf 100644 --- a/scripts/install +++ b/scripts/install @@ -105,7 +105,7 @@ ynh_app_setting_set "$app" mysqlpwd "$dbpass" ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" ### Nginx ### -if [ $path = "" ]; then +if [ $path = "/" ]; then nginx_conf=../conf/nginx_root.conf else nginx_conf=../conf/nginx.conf From 4a5717daa3254735ce5fad96fe8fa0512ecd9eae Mon Sep 17 00:00:00 2001 From: polytan02 Date: Wed, 22 Feb 2017 15:51:47 +0000 Subject: [PATCH 09/11] Adjust upgrade to path variable --- scripts/upgrade | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scripts/upgrade b/scripts/upgrade index 4547608..b6c6395 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -3,6 +3,8 @@ # Exit on command errors and treat unset variables as an error set -eu +source .functions + # See comments in install script app=$YNH_APP_INSTANCE_NAME @@ -16,8 +18,8 @@ admin=$(ynh_app_setting_get "$app" admin) is_public=$(ynh_app_setting_get "$app" is_public) language=$(ynh_app_setting_get "$app" language) -# Remove trailing "/" for next commands -path=${path%/} +# Check path and correct if required +CHECK_PATH # Copy source files src_path=/var/www/$app @@ -29,9 +31,17 @@ sudo cp -a ../sources/. $src_path sudo chown -R root: $src_path # Modify Nginx configuration file and copy it to Nginx conf directory +### Nginx ### +if [ $path = "/" ]; then +nginx_conf=../conf/nginx_root.conf +else nginx_conf=../conf/nginx.conf +fi sed -i "s@YNH_WWW_PATH@$path@g" $nginx_conf -sed -i "s@YNH_WWW_ALIAS@$src_path/@g" $nginx_conf +sed -i "s@YNH_WWW_ALIAS@$app/@g" $nginx_conf +sed -i "s@YNH_WWW_APP@$app@g" $nginx_conf +sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf + # If a dedicated php-fpm process is used: # sed -i "s@YNH_WWW_APP@$app@g" $nginx_conf From 3b632a12264ff7bd68ba22f46c39c28002da7550 Mon Sep 17 00:00:00 2001 From: Titus PiJean Date: Wed, 22 Feb 2017 18:21:36 +0100 Subject: [PATCH 10/11] Update .functions Copy/paste clean up --- scripts/.functions | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/scripts/.functions b/scripts/.functions index a701564..2eaaa56 100644 --- a/scripts/.functions +++ b/scripts/.functions @@ -1,22 +1,3 @@ -Skip to content -This repository -Search -Pull requests -Issues -Gist - @polytan02 - Sign out - Unwatch 1 - Star 0 - Fork 6 polytan02/phpmyadmin_ynh -forked from YunoHost-Apps/phpmyadmin_ynh - Code Pull requests 0 Projects 0 Wiki Pulse Graphs Settings -Branch: master Find file Copy pathphpmyadmin_ynh/scripts/.fonctions -1a1a940 8 days ago -@polytan02 polytan02 Update on .fonctions file -1 contributor -RawBlameHistory -Executable File 228 lines (201 sloc) 8.08 KB #!/bin/bash CHECK_VAR () { # Vérifie que la variable n'est pas vide. @@ -244,5 +225,3 @@ REMOVE_SYS_USER () { # Supprime l'utilisateur système dédié à l'app sudo userdel $app fi } -Contact GitHub API Training Shop Blog About -© 2017 GitHub, Inc. Terms Privacy Security Status Help From a7d162ca06acb498f32a0c3fb22e1875a3b158db Mon Sep 17 00:00:00 2001 From: Titus PiJean Date: Wed, 22 Feb 2017 18:24:42 +0100 Subject: [PATCH 11/11] Update install Frenchie typo --- scripts/install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/install b/scripts/install index c4ca3bf..67a6de5 100644 --- a/scripts/install +++ b/scripts/install @@ -1,7 +1,7 @@ #!/bin/bash # Load extra functions -source .fonctions +source .functions # Activate TRAP to stop the script if an error is detected TRAP_ON