From 115b7cb7fb56a654580bb7d8ed12f8a5bdd51cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Sun, 10 Jul 2016 16:24:09 +0200 Subject: [PATCH] [enh] Make use of YunoHost helpers coming with v2.4 --- manifest.json | 3 ++ scripts/install | 91 ++++++++++++++++++++++--------------------------- scripts/remove | 29 +++++++++++----- scripts/upgrade | 65 +++++++++++++++++------------------ 4 files changed, 95 insertions(+), 93 deletions(-) diff --git a/manifest.json b/manifest.json index e882bb5..3751739 100644 --- a/manifest.json +++ b/manifest.json @@ -17,6 +17,9 @@ "mysql", "metronome" ], + "requirements": { + "yunohost": ">= 2.4.0" + }, "multi_instance": "false", "arguments": { "install" : [ diff --git a/scripts/install b/scripts/install index 0e8479a..cdd7739 100644 --- a/scripts/install +++ b/scripts/install @@ -1,9 +1,13 @@ #!/bin/bash -# + +# Exit on command errors and treat unset variables as an error +set -eu + +app="movim" # Retrieve arguments domain=$1 -path=$2 +path=${2:-/} admin=$3 password=$4 language=$5 @@ -11,73 +15,60 @@ ssoenabled=$6 port=$7 timezone=`cat /etc/timezone` +# Source YunoHost helpers +source /usr/share/yunohost/helpers + # Check domain/path availability -sudo yunohost app checkurl $domain$path -a movim -if [[ ! $? -eq 0 ]]; then - exit 1 -fi +sudo yunohost app checkurl "${domain}${path}" -a "$app" \ + || exit 1 # Check port availability -sudo yunohost app checkport $port -if [[ ! $? -eq 0 ]]; then - exit 1 -fi +sudo yunohost app checkport "$port" \ + || exit 1 # Check user availability -sudo yunohost user info $admin -if [[ ! $? -eq 0 ]]; then -exit 1 -fi +ynh_user_exists "$admin" \ + || ynh_die "The chosen admin user does not exist." # Check password not empty -if [[ -z "$password" ]]; then -printf "Error empty admin password, installation aborted" - exit 1 -fi +[[ -n "$password" ]] \ + || ynh_die "You must set an admin password." # Check timezone -if [[ -z "$timezone" ]]; then -printf "Could not detect Timezone, please check /etc/timezone. Installation aborted" - exit 1 -else -printf "Detected Timezone $timezone \n" -fi +[[ -n "$timezone" ]] \ + || ynh_die "Could not detect timezone, please check /etc/timezone." \ + && echo "Detected timezone: $timezone" -# Check path not empty -if [[ -z "$path" ]]; then -printf "Error path is empty, installation aborted" - exit 1 -fi +# Save app settings +ynh_app_setting_set "$app" admin "$admin" +ynh_app_setting_set "$app" ssoenabled "$ssoenabled" +ynh_app_setting_set "$app" port "$port" +ynh_app_setting_set "$app" path "$path" -# Save parameters -sudo yunohost app setting movim admin -v $admin -sudo yunohost app setting movim ssoenabled -v $ssoenabled -sudo yunohost app setting movim port -v $port -sudo yunohost app setting movim path -v $path - -# Generate random password -db_pwd=$(dd if=/dev/urandom bs=1 count=30 2> /dev/null | tr -c -d '[[:alnum:]]') +# Generate and save random MySQL password +db_pwd=$(ynh_string_random 12) +ynh_app_setting_set "$app" mysqlpwd "$db_pwd" # Use 'movim' as database name and user db_user=movim +db_name=movim -# Initialize database and store mysql password for upgrade -sudo yunohost app initdb $db_user -p $db_pwd -sudo yunohost app setting movim mysqlpwd -v $db_pwd +# Create MySQL database +ynh_mysql_create_db "$db_name" "$db_user" "$db_pwd" # Install packages -sudo apt-get install php5-gd php5-curl php5-imagick php5-cli -y +ynh_package_install php5-gd php5-curl php5-imagick php5-cli -# Install Movim files +# Copy Movim source files final_path=/var/www/movim -sudo mkdir -p $final_path -sudo cp -a ../sources/* $final_path -sudo cp $final_path/config/db.example.inc.php $final_path/config/db.inc.php +sudo cp -a ../sources "$final_path" +sudo cp "${final_path}/config/"{db.example.inc.php,db.inc.php} # Create movim system user and set permissions sudo useradd -d /var/www/movim -s /bin/sh movim -sudo chown -R movim:www-data $final_path -sudo chmod -R 750 $final_path +sudo chown -R movim:www-data "$final_path" +sudo find "${final_path}/" -type f -print0 | sudo xargs -0 chmod 0640 +sudo find "${final_path}/" -type d -print0 | sudo xargs -0 chmod 0750 # Install PHP dependencies sudo su -c "curl -sS https://getcomposer.org/installer | php -- --install-dir=$final_path" movim @@ -126,14 +117,12 @@ sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/movim.conf # SSOwat Configuration if [ $ssoenabled = "No" ]; then - sudo yunohost app setting movim skipped_uris -v "/" - sudo yunohost app ssowatconf + ynh_app_setting_set "$app" skipped_uris "/" sudo su -c "cd $final_path && php mud.php config --xmppwhitelist=$domain" movim else - sudo yunohost app setting movim unprotected_uris -v "/" + ynh_app_setting_set "$app" unprotected_uris "/" # Next line is to avoid SSO to auto-connect just after disconnect sudo sed -i "s@redirect('login')@redirect('')@g" $final_path/app/controllers/DisconnectController.php - sudo yunohost app ssowatconf fi # Start Movim diff --git a/scripts/remove b/scripts/remove index 6672861..9a7dd2c 100644 --- a/scripts/remove +++ b/scripts/remove @@ -1,10 +1,14 @@ #!/bin/bash -db_user=movim -db_name=movim -root_pwd=$(sudo cat /etc/yunohost/mysql) +app="movim" -#Stop service and remove init +# Source YunoHost helpers +source /usr/share/yunohost/helpers + +# Retrieve app settings +domain=$(ynh_app_setting_get "$app" domain) + +# Stop service and remove init sudo service php5-fpm stop sudo /etc/init.d/movim stop sudo systemctl stop movim.service @@ -13,13 +17,20 @@ sudo update-rc.d -f movim remove sudo rm -f /etc/init.d/movim sudo rm -f /lib/systemd/system/movim.service -#Drop database, movim files and user. -mysql -u root -p$root_pwd -e "DROP DATABASE $db_name ; DROP USER $db_user@localhost ;" -sudo userdel movim +# Drop MySQL database and user +db_user=movim +db_name=movim +ynh_mysql_drop_db "$db_name" || true +ynh_mysql_drop_user "$db_user" || true + +# Remove Movim files and user +ynh_system_user_exists movim \ + && sudo userdel movim sudo rm -rf /var/www/movim -domain=$(sudo yunohost app setting movim domain) + +# Remove nginx and php-fpm configuration files sudo rm -f /etc/nginx/conf.d/$domain.d/movim.conf -sudo rm /etc/php5/fpm/pool.d/movim.conf +sudo rm -f /etc/php5/fpm/pool.d/movim.conf # Restarting services sudo service nginx reload diff --git a/scripts/upgrade b/scripts/upgrade index 58a4107..9bf5168 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,35 +1,36 @@ #!/bin/bash -# -# Retrieve arguments -path=$(sudo yunohost app setting movim path) -domain=$(sudo yunohost app setting movim domain) -port=$(sudo yunohost app setting movim port) -public_site=$(sudo yunohost app setting movim public_site) -ssoenabled=$(sudo yunohost app setting movim ssoenabled) +# Exit on command errors and treat unset variables as an error +set -eu + +app="movim" + +# Source YunoHost helpers +source /usr/share/yunohost/helpers + +# Retrieve app settings +domain=$(ynh_app_setting_get "$app" domain) +path=$(ynh_app_setting_get "$app" path) +port=$(ynh_app_setting_get "$app" port) +ssoenabled=$(ynh_app_setting_get "$app" ssoenabled) +public_site=$(ynh_app_setting_get "$app" public_site) timezone=`cat /etc/timezone` # Check timezone -if [[ -z "$timezone" ]]; then -printf "Could not detect Timezone, please check /etc/timezone. Installation aborted" - exit 1 -else -printf "Detected Timezone $timezone \n" -fi +[[ -n "$timezone" ]] \ + || ynh_die "Could not detect timezone, please check /etc/timezone." \ + && echo "Detected timezone: $timezone" -# Install Movim files +# Copy Movim source files final_path=/var/www/movim -sudo cp -a ../sources/* $final_path +sudo cp -a ../sources "$final_path" # Create movim user if not exists and set permissions -if grep -q movim /etc/passwd; then - sudo chown -R movim:www-data $final_path - sudo chmod -R 750 $final_path -else - sudo useradd -s /bin/sh -d $final_path movim - sudo chown -R movim:www-data $final_path - sudo chmod -R 750 $final_path -fi +ynh_system_user_exists movim \ + || sudo useradd -d /var/www/movim -s /bin/sh movim +sudo chown -R movim:www-data "$final_path" +sudo find "${final_path}/" -type f -print0 | sudo xargs -0 chmod 0640 +sudo find "${final_path}/" -type d -print0 | sudo xargs -0 chmod 0750 # Update PHP dependencies sudo su -c "cd $final_path && php composer.phar config --global discard-changes true" movim @@ -42,32 +43,30 @@ sudo sed -i "s@/ws/@$path/ws/@g" $final_path/app/assets/js/movim_websocket.js sudo su -c "cd $final_path && php mud.php db --set" movim # Reset SSO parameters -sudo yunohost app setting movim protected_uris -d -sudo yunohost app setting movim skipped_uris -d +ynh_app_setting_delete "$app" protected_uris +ynh_app_setting_delete "$app" skipped_uris sudo yunohost app clearaccess movim # Replace old public_site variable (if exists) by ssoenabled if [ ! -z "$public_site" ] && [ $public_site = "Yes" ]; then - sudo yunohost app setting movim public_site -d - sudo yunohost app setting movim ssoenabled -v "No" + ynh_app_setting_delete "$app" public_site + ynh_app_setting_set "$app" ssoenabled "No" elif [ ! -z "$public_site" ] && [ $public_site = "No" ]; then - sudo yunohost app setting movim public_site -d - sudo yunohost app setting movim ssoenabled -v "Yes" + ynh_app_setting_delete "$app" public_site + ynh_app_setting_set "$app" ssoenabled "Yes" fi # SSOwat Configuration if [ $ssoenabled = "No" ]; then - sudo yunohost app setting movim skipped_uris -v "/" - sudo yunohost app ssowatconf + ynh_app_setting_set "$app" skipped_uris "/" sudo su -c "cd $final_path && php mud.php config --xmppwhitelist=$domain" movim else - sudo yunohost app setting movim unprotected_uris -v "/" + ynh_app_setting_set "$app" unprotected_uris "/" # Next line is to avoid SSO to auto-connect just after disconnect sudo sed -i "s@redirect('login')@redirect('')@g" $final_path/app/controllers/DisconnectController.php - sudo yunohost app ssowatconf fi # Update init scripts