diff --git a/scripts/.fonctions b/scripts/.fonctions index fcd5b76..c18e8d3 100755 --- a/scripts/.fonctions +++ b/scripts/.fonctions @@ -7,32 +7,33 @@ CHECK_VAR () { # Vérifie que la variable n'est pas vide. } EXIT_PROPERLY () { # Provoque l'arrêt du script en cas d'erreur. Et nettoye les résidus. - trap '' ERR + 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 - CLEAN_SETUP # Appel la fonction de nettoyage spécifique du script install. + 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 - exit 1 + ynh_die } TRAP_ON () { # Activate signal capture - trap EXIT_PROPERLY ERR # Capturing exit signals on error -} - -TRAP_OFF () { # Ignoring signal capture until TRAP_ON - # Pour une raison que j'ignore, la fonction TRAP_ON fonctionne très bien. - # Mais pas la fonction TRAP_OFF... - # Utiliser directement `trap '' ERR` dans le code pour l'utiliser, à la place de la fonction. - trap '' ERR # Ignoring exit signals + set -eu # Exit if a command fail, and if a variable is used unset. + trap EXIT_PROPERLY EXIT # Capturing exit signals on shell script } CHECK_USER () { # Vérifie la validité de l'user admin # $1 = Variable de l'user admin. - sudo yunohost user list --json | grep -q "\"username\": \"$1\"" || (echo "Wrong admin" >&2 && false) + 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. @@ -59,14 +60,16 @@ CHECK_FINALPATH () { # Vérifie que le dossier de destination n'est pas déjà u 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 - # Génère un mot de passe aléatoire. db_user=$1 - db_pwd=$(head -n20 /dev/urandom | tr -c -d 'A-Za-z0-9' | head -c20) + 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 - sudo yunohost app setting $app mysqlpwd -v $db_pwd + 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 @@ -91,7 +94,7 @@ SETUP_SOURCE () { # Télécharge la source, décompresse et copie dans $final_pa } ADD_SYS_USER () { # Créer un utilisateur système dédié à l'app - if grep -q "^$app:" /etc/passwd # Test l'existence de l'utilisateur + 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 @@ -113,13 +116,13 @@ POOL_FPM () { # Créer le fichier de configuration du pool php-fpm et le configu 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. - sudo yunohost app setting $app $1_file_md5 -v $(sudo md5sum "$2" | cut -d' ' -f1) + 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 [ "$(sudo yunohost app setting $app $1_file_md5)" != $(sudo md5sum "$2" | cut -d' ' -f1) ]; then + 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 } @@ -164,7 +167,7 @@ REMOVE_LOGROTATE_CONF () { # Suppression de la configuration de logrotate } SECURE_REMOVE () { # Suppression de dossier avec vérification des variables - chaine="$1" + 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 @@ -180,8 +183,10 @@ SECURE_REMOVE () { # Suppression de dossier avec vérification des variable done if [ "$no_var" -eq 1 ] then - echo "Delete directory $chaine" - sudo rm -r "$chaine" + if [ -e "$chaine" ]; then + echo "Delete directory $chaine" + sudo rm -r "$chaine" + fi return 0 else echo "No detected variable." >&2 @@ -201,7 +206,7 @@ REMOVE_BDD () { # Suppression de la base de donnée et de l'utilisateur associé } REMOVE_SYS_USER () { # Supprime l'utilisateur système dédié à l'app - if grep -q "^$app:" /etc/passwd # Test l'existence de l'utilisateur + if ynh_system_user_exists "$app" # Test l'existence de l'utilisateur then sudo userdel $app fi diff --git a/scripts/backup b/scripts/backup index cc1e51d..c2b8361 100644 --- a/scripts/backup +++ b/scripts/backup @@ -9,8 +9,8 @@ app=$2 # Source app helpers source /usr/share/yunohost/helpers -domain=$(sudo yunohost app setting $app domain) -final_path=$(sudo yunohost app setting $app final_path) +domain=$(ynh_app_setting_get $app domain) +final_path=$(ynh_app_setting_get $app final_path) # Copy the app files sudo mkdir -p ${backup_dir}/var/www diff --git a/scripts/install b/scripts/install index d247e5a..b121366 100644 --- a/scripts/install +++ b/scripts/install @@ -39,19 +39,19 @@ if [ "$path" == "/" ] && [ "$multisite" = "Yes" ]; then fi # Enregistre les infos dans la config YunoHost -sudo yunohost app setting $app domain -v $domain -sudo yunohost app setting $app path -v $path -sudo yunohost app setting $app admin -v $admin_wordpress -sudo yunohost app setting $app is_public -v $is_public -sudo yunohost app setting $app language -v $language -sudo yunohost app setting $app multisite -v $multisite +ynh_app_setting_set $app domain $domain +ynh_app_setting_set $app path $path +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 GENERATE_DB $app # Créer une base de données et un utilisateur dédié au nom de l'app. # Crée le repertoire de destination et stocke son emplacement. sudo mkdir "$final_path" -sudo yunohost app setting $app final_path -v $final_path +ynh_app_setting_set $app final_path $final_path SETUP_SOURCE "wordpress.tar.gz" # Télécharge la source, décompresse et copie dans $final_path @@ -93,7 +93,7 @@ done sudo chown -R www-data: $final_path # Donne un accès public pour curl -sudo yunohost app setting $app unprotected_uris -v "/" +ynh_app_setting_set $app unprotected_uris "/" # Recharge la configuration Nginx sudo service nginx reload # Régénère la configuration de SSOwat @@ -175,7 +175,7 @@ sudo chown root: $final_path/wp-config.php if [ "$is_public" = "No" ]; then # Retire l'accès public - sudo yunohost app setting $app unprotected_uris -d + ynh_app_setting_delete $app unprotected_uris sudo yunohost app ssowatconf fi diff --git a/scripts/remove b/scripts/remove index 7612727..21aaef0 100755 --- a/scripts/remove +++ b/scripts/remove @@ -7,7 +7,7 @@ source /usr/share/yunohost/helpers # Récupère les infos de l'application. app=$YNH_APP_INSTANCE_NAME -domain=$(sudo yunohost app setting $app domain) +domain=$(ynh_app_setting_get $app domain) REMOVE_BDD $app # Suppression de la base de donnée et de l'utilisateur associé. diff --git a/scripts/restore b/scripts/restore index a9beda4..c08116e 100644 --- a/scripts/restore +++ b/scripts/restore @@ -11,20 +11,18 @@ app=$2 source /usr/share/yunohost/helpers # Get old parameter of the app -domain=$(sudo yunohost app setting $app domain) -path=$(sudo yunohost app setting $app path) -is_public=$(sudo yunohost app setting $app is_public) -final_path=$(sudo yunohost app setting $app final_path) +domain=$(ynh_app_setting_get $app domain) +path=$(ynh_app_setting_get $app path) +is_public=$(ynh_app_setting_get $app is_public) +final_path=$(ynh_app_setting_get $app final_path) if [ -d $final_path ]; then - echo "There is already a directory: $final_path " | sudo tee /dev/stderr - exit 1 + ynh_die "There is already a directory: $final_path" fi conf=/etc/nginx/conf.d/$domain.d/$app.conf if [ -f $conf ]; then - echo "There is already a nginx conf file at this path: $conf " | sudo tee /dev/stderr - exit 1 + ynh_die "There is already a nginx conf file at this path: $conf" fi # Restore conf files sudo cp -a "${backup_dir}/conf/nginx.conf" $conf @@ -34,7 +32,7 @@ sudo service nginx reload sudo cp -a "${backup_dir}/var/www/$app" $final_path -db_pwd=$(sudo yunohost app setting $app mysqlpwd) +db_pwd=$(ynh_app_setting_get $app mysqlpwd) db_user=$app ynh_mysql_create_db $db_user $db_user $db_pwd sudo su -c "mysql -u $db_user -p$db_pwd $app < ${backup_dir}/db.sql" @@ -57,6 +55,6 @@ sudo service php5-fpm reload # Set ssowat config if [ "$is_public" = "No" ]; then - sudo yunohost app setting $app skipped_uris -d + ynh_app_setting_delete $app skipped_uris fi sudo yunohost app ssowatconf diff --git a/scripts/upgrade b/scripts/upgrade index 2aecb98..b7e71fd 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,20 +7,20 @@ source /usr/share/yunohost/helpers # Récupère les infos de l'application. app=$YNH_APP_INSTANCE_NAME -domain=$(sudo yunohost app setting $app domain) -path=$(sudo yunohost app setting $app path) -admin_wordpress=$(sudo yunohost app setting $app admin) -language=$(sudo yunohost app setting $app language) -is_public=$(sudo yunohost app setting $app is_public) -multisite=$(sudo yunohost app setting $app multisite) -final_path=$(sudo yunohost app setting $app final_path) +domain=$(ynh_app_setting_get $app domain) +path=$(ynh_app_setting_get $app path) +admin_wordpress=$(ynh_app_setting_get $app admin) +language=$(ynh_app_setting_get $app language) +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) CHECK_PATH # Vérifie et corrige la syntaxe du path. # Check if admin is not null if [[ "$admin_wordpress" = "" || "$is_public" = "" || "$language" = "" ]]; then echo "Unable to upgrade, please contact support" - exit 1 + ynh_die fi root_pwd=$(sudo cat /etc/yunohost/mysql) @@ -30,19 +30,19 @@ then mysql -u root -p$root_pwd $db_name -e "select MAX(user_login) from wp_users where user_status=0 INTO OUTFILE '/tmp/wordpressuser';" admin_wordpress=$(cat /tmp/wordpressuser) sudo rm -f /tmp/wordpressuser - sudo yunohost app setting $app admin -v $admin_wordpress + ynh_app_setting_set $app admin $admin_wordpress fi if [[ "$final_path" = "" ]]; then final_path=/var/www/$app - sudo yunohost app setting $app final_path -v $final_path + ynh_app_setting_set $app final_path $final_path fi if [[ "$language" = "" ]]; then language=$(sudo grep WPLANG $final_path/wp-config.php | cut -d"'" -f4) - sudo yunohost app setting $app language -v $language + ynh_app_setting_set $app language $language fi # Copie le fichier de config nginx @@ -67,7 +67,7 @@ else sudo sed -i "s@//--PUBLIC--define@define@g" $final_path/wp-config.php fi fi -sudo yunohost app setting $app multisite -v $multisite +ynh_app_setting_set $app multisite $multisite STORE_MD5_CONFIG "wp-config.php" "$final_path/wp-config.php" # Réenregistre la somme de contrôle du fichier de config # Configure les droits d'accès au fichiers @@ -77,11 +77,11 @@ sudo chown -R www-data: $final_path sudo chown root: $final_path/wp-config.php -sudo yunohost app setting $app skipped_uris -d # Retire le skipped_uris si il existe encore. +ynh_app_setting_delete $app skipped_uris # Retire le skipped_uris si il existe encore. if [ "$is_public" = "No" ]; then # Retire l'accès public - sudo yunohost app setting $app unprotected_uris -d + ynh_app_setting_delete $app unprotected_uris else # Ou remplace le skipped_uris par unprotected_uris le cas échéant. - sudo yunohost app setting $app unprotected_uris -v "/" + ynh_app_setting_set $app unprotected_uris "/" fi sudo yunohost app ssowatconf