diff --git a/conf/php-fpm.conf b/conf/php-fpm.conf index 26e1603..336c925 100644 --- a/conf/php-fpm.conf +++ b/conf/php-fpm.conf @@ -195,7 +195,7 @@ rlimit_core = 0 ; Chdir to this directory at the start. ; Note: relative path can be used. ; Default Value: current directory or / when chroot -chdir = /var/www/FOLDERTOCHANGE +chdir = FOLDERTOCHANGE ; Redirect worker stdout and stderr into main error log. If not set, stdout and ; stderr will be redirected to /dev/null according to FastCGI specs. diff --git a/scripts/backup b/scripts/backup new file mode 100644 index 0000000..a2cae69 --- /dev/null +++ b/scripts/backup @@ -0,0 +1,39 @@ +#!/bin/bash + +# Exit on command errors and treat unset variables as an error +set -eu + +# See comments in install script +app=$YNH_APP_INSTANCE_NAME + +# Source YunoHost helpers +source /usr/share/yunohost/helpers + +final_path=$(ynh_app_setting_get $app final_path) +path=$(ynh_app_setting_get $app path) +domain=$(ynh_app_setting_get $app domain) +is_public=$(ynh_app_setting_get $app is_public) +finalnginxconf=$(ynh_app_setting_get $app finalnginxconf) +finalphpconf=$(ynh_app_setting_get $app finalphpconf) + + +# Backup sources & data +# Note: the last argument is where to save this path, see the restore script. +ynh_backup "$final_path" "sources" + +### MySQL (remove if not used) ### +# If a MySQL database is used: +# # Dump the database +# dbname=$app +# dbuser=$app +# dbpass=$(ynh_app_setting_get "$app" mysqlpwd) +# mysqldump -u "$dbuser" -p"$dbpass" --no-create-db "$dbname" > ./dump.sql +### MySQL end ### + +# Copy NGINX configuration +ynh_backup "$finalnginxconf" "nginx.conf" + +### PHP (remove if not used) ### +# If a dedicated php-fpm process is used: +# # Copy PHP-FPM pool configuration +ynh_backup "$finalphpconf" "php-fpm.conf" diff --git a/scripts/fonctions b/scripts/fonctions new file mode 100644 index 0000000..73587c1 --- /dev/null +++ b/scripts/fonctions @@ -0,0 +1,211 @@ +#!/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. +# trap '' ERR +# 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. +# +# # 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 + trap EXIT_PROPERLY ERR # Capturing ex it 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 ex it signals +} + +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 + # 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 +} diff --git a/scripts/install b/scripts/install new file mode 100644 index 0000000..d8046e2 --- /dev/null +++ b/scripts/install @@ -0,0 +1,84 @@ +#!/bin/bash +set -eu + +# Charge les fonctions génériques habituellement utilisées dans le script +source fonctions + +# Active trap pour arrêter le script si une erreur est détectée. +TRAP_ON + +# Source app helpers +source /usr/share/yunohost/helpers + +# Retrieve arguments +app=$YNH_APP_INSTANCE_NAME +domain=$YNH_APP_ARG_DOMAIN +path=$YNH_APP_ARG_PATH +is_public=$YNH_APP_ARG_IS_PUBLIC +usergroup="owncloud:owncloud" + +# We check variables are not empty +CHECK_VAR "$app" "app name not set" + +# Check the path value and correct it (adds / at begining and removes it at the end) +CHECK_PATH + +# Check domain and path availibility +CHECK_DOMAINPATH +# Check destination folder is not used already +CHECK_FINALPATH + +final_path=/var/www/$app + +# Define variables and Save app settings +ynh_app_setting_set "$app" usergroup "$usergroup" +ynh_app_setting_set "$app" domain "$domain" +ynh_app_setting_set "$app" is_public "$is_public" +ynh_app_setting_set "$app" final_path "$final_path" + +finalnginxconf="/etc/nginx/conf.d/${domain}.d/${app}.conf" +ynh_app_setting_set "$app" finalnginxconf "$finalnginxconf" + +finalphpconf="/etc/php5/fpm/pool.d/${app}.conf" +ynh_app_setting_set "$app" finalphpconf "$finalphpconf" + + +# Creation of folder +#sudo rm -rf $final_path +sudo mkdir -p $final_path + +# Base site +#sed -i "s@DOMAINTOCHANGE@$domain@g" ../sources/index.html +#sed -i "s@SITETOCHANGE@$sitename@g" ../sources/index.html +#sudo cp ../sources/* $final_path/ + +# Set permissions +sudo chmod 775 -R $final_path +sudo chown -hR $usergroup $final_path + +# Modify Nginx configuration file and copy it to Nginx conf directory +sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf +sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf +sed -i "s@NAMETOCHANGE@$app@g" ../conf/nginx.conf +sudo cp ../conf/nginx.conf $finalnginxconf + + +sed -i "s@NAMETOCHANGE@$app@g" ../conf/php-fpm.conf +sed -i "s@FOLDERTOCHANGE@$finalpath@g" ../conf/php-fpm.conf +sed -i "s@USERTOCHANGE@$usergroup@g" ../conf/php-fpm.conf + +sudo cp ../conf/php-fpm.conf $finalphpconf +sudo chown root: $finalphpconf +sudo chmod 644 $finalphpconf + +# Make app public if necessary +is_public=$(ynh_app_setting_get $app is_public) +if [ "$is_public" = "Yes" ]; +then + ynh_app_setting_set $app skipped_uris "/" +fi + +# Reload Nginx and regenerate SSOwat conf +sudo service php5-fpm reload +sudo service nginx reload +sudo yunohost app ssowatconf diff --git a/scripts/remove b/scripts/remove new file mode 100644 index 0000000..2e2f64c --- /dev/null +++ b/scripts/remove @@ -0,0 +1,40 @@ +#!/bin/bash +set -eu + +# Charge les fonctions génériques habituellement utilisées dans le script +source fonctions + +# Active trap pour arrêter le script si une erreur est détectée. +TRAP_ON + +# Source app helpers +source /usr/share/yunohost/helpers + +# We retrieve app parameters +app=$YNH_APP_INSTANCE_NAME + +# We check variables are not empty +CHECK_VAR "$app" "app name not set" + +path=$(ynh_app_setting_get $app path) +domain=$(ynh_app_setting_get $app domain) +final_path=$(ynh_app_setting_get $app final_path) +sitename=$(ynh_app_setting_get $app sitename) +finalnginxconf=$(ynh_app_setting_get $app finalnginxconf) +finalphpconf=$(ynh_app_setting_get $app finalphpconf) + +# Suppression du dossier de la webapp +sudo rm -rf $final_path + +# Suppression de la config nginx de la webapp +sudo rm -f $finalnginxconf +sudo rm -f $finalphpconf + +# We remove old files in /var/www/webapp_multi +sudo rmdir --ignore-fail-on-non-empty /var/www/webapp_multi/$domain +sudo rmdir --ignore-fail-on-non-empty /var/www/webapp_multi + + +sudo service php5-fpm reload +sudo service nginx reload +sudo yunohost app ssowatconf diff --git a/scripts/restore b/scripts/restore new file mode 100644 index 0000000..499180c --- /dev/null +++ b/scripts/restore @@ -0,0 +1,68 @@ +#!/bin/bash + +# Note: each files and directories you've saved using the ynh_backup helper +# will be located in the current directory, regarding the last argument. + +# Exit on command errors and treat unset variables as an error +set -eu + +# See comments in install script +app=$YNH_APP_INSTANCE_NAME + +# Source YunoHost helpers +source /usr/share/yunohost/helpers + +# Retrieve old app settings +final_path=$(ynh_app_setting_get $app final_path) +path=$(ynh_app_setting_get $app path) +sitename=$(ynh_app_setting_get $app sitename) +domain=$(ynh_app_setting_get $app domain) +is_public=$(ynh_app_setting_get $app is_public) +finalnginxconf=$(ynh_app_setting_get $app finalnginxconf) +finalphpconf=$(ynh_app_setting_get $app finalphpconf) +usergroup=$(ynh_app_setting_get $app usergroup) + +# Check domain/path availability +#sudo yunohost app checkurl "${domain}${path}" -a "$app" \ +# || ynh_die "Path not available: ${domain}${path}" + +# Restore sources & data +sudo mkdir -p $final_path +sudo cp -a ./sources/* $final_path/ + +# Restore permissions to app files +# you may need to make some file and/or directory writeable by www-data (nginx user) +sudo chown -R $usergroup $final_path + +### MySQL (remove if not used) ### +# If a MySQL database is used: +# # Create and restore the database +# dbname=$app +# dbuser=$app +# dbpass=$(ynh_app_setting_get "$app" mysqlpwd) +# ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" +# ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./dump.sql +### MySQL end ### + +# Restore NGINX configuration +sudo cp -a ./nginx.conf $finalnginxconf + +### PHP (remove if not used) ### +# If a dedicated php-fpm process is used: +# # Copy PHP-FPM pool configuration and reload the service +sudo cp -a ./php-fpm.conf $finalphpconf +### PHP end ### + +# Make app public if necessary +is_public=$(ynh_app_setting_get $app is_public) +if [ "$is_public" = "Yes" ]; +then + ynh_app_setting_set $app skipped_uris "/" +fi + + +# Restart webserver +sudo service nginx reload +sudo service php5-fpm reload +sudo yunohost app ssowatconf + diff --git a/scripts/upgrade b/scripts/upgrade new file mode 100644 index 0000000..75711a5 --- /dev/null +++ b/scripts/upgrade @@ -0,0 +1,60 @@ +#!/bin/bash +set -eu + +# Charge les fonctions génériques habituellement utilisées dans le script +source fonctions + +# Active trap pour arrêter le script si une erreur est détectée. +TRAP_ON + +# Source app helpers +source /usr/share/yunohost/helpers + +# We retrieve app parameters +app=$YNH_APP_INSTANCE_NAME + +# We check variables are not empty +CHECK_VAR "$app" "app name not set" + +path=$(ynh_app_setting_get $app path) +domain=$(ynh_app_setting_get $app domain) +final_path=$(ynh_app_setting_get $app final_path) +sitename=$(ynh_app_setting_get $app sitename) +finalnginxconf=$(ynh_app_setting_get $app finalnginxconf) +finalphpconf=$(ynh_app_setting_get $app finalphpconf) +usergroup=$(ynh_app_setting_get $app usergroup) + +# Modify Nginx configuration file and copy it to Nginx conf directory +sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf +sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf +sed -i "s@NAMETOCHANGE@$app@g" ../conf/nginx.conf +sudo cp ../conf/nginx.conf $finalnginxconf + + +sed -i "s@NAMETOCHANGE@$app@g" ../conf/php-fpm.conf +sed -i "s@FOLDERTOCHANGE@$finalpath@g" ../conf/php-fpm.conf +sed -i "s@USERTOCHANGE@$usergroup@g" ../conf/php-fpm.conf + +sudo cp ../conf/php-fpm.conf $finalphpconf +sudo chown root: $finalphpconf +sudo chmod 644 $finalphpconf + + +# We adjust permissions +sudo chmod 775 -R $final_path +sudo chown -hR $usergroup $final_path + + +# Make app public if necessary +is_public=$(ynh_app_setting_get $app is_public) +if [ "$is_public" = "Yes" ]; +then + ynh_app_setting_set $app skipped_uris "/" +else + ynh_app_setting_set $app protected_uris "/" +fi + +# Reload Nginx and regenerate SSOwat conf +sudo service php5-fpm reload +sudo service nginx reload +sudo yunohost app ssowatconf