From 3be1f1c8f48cf789e8336091cbb530a4817304cd Mon Sep 17 00:00:00 2001 From: Garwinch Date: Sun, 4 Dec 2016 18:18:23 +0100 Subject: [PATCH] first set disign from https://github.com/YunoHost-Apps/my_webapp_ynh --- scripts/backup | 44 +++++++------- scripts/install | 153 ++++++++++++++++++++++++++---------------------- scripts/remove | 50 +++++++++------- scripts/restore | 97 +++++++++++++++++++----------- scripts/upgrade | 78 ++++++++++++------------ 5 files changed, 231 insertions(+), 191 deletions(-) diff --git a/scripts/backup b/scripts/backup index af8ccd9..63f13fb 100755 --- a/scripts/backup +++ b/scripts/backup @@ -3,31 +3,29 @@ # Exit on command errors and treat unset variables as an error set -eu -# See comments in install script +# Get multi-instances specific variables app=$YNH_APP_INSTANCE_NAME -# Source YunoHost helpers -source /usr/share/yunohost/helpers +# Source app helpers +. /usr/share/yunohost/helpers -# Backup sources & data -# Note: the last argument is where to save this path, see the restore script. -ynh_backup "/var/www/${app}" "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 +# Retrieve app settings domain=$(ynh_app_setting_get "$app" domain) -ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf" +path=$(ynh_app_setting_get "$app" path) +with_mysql=$(ynh_app_setting_get "$app" with_mysql) -### PHP (remove if not used) ### -# If a dedicated php-fpm process is used: -# # Copy PHP-FPM pool configuration -# ynh_backup "/etc/php5/fpm/pool.d/${app}.conf" "php-fpm.conf" -### PHP end ### +# Copy the app files +DESTDIR="/var/www/${app}" +ynh_backup "$DESTDIR" "sources" 1 + +# Copy the conf files +ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "conf/nginx.conf" +ynh_backup "/etc/php5/fpm/pool.d/${app}.conf" "conf/php-fpm.conf" + +# Dump the database +if [[ $with_mysql -eq 1 ]]; then + dbname=$app + dbuser=$app + dbpass=$(ynh_app_setting_get "$app" mysqlpwd) + mysqldump -u "$dbuser" -p"$dbpass" --no-create-db "$dbname" > ./dump.sql +fi diff --git a/scripts/install b/scripts/install index f1b2bf4..5f60c90 100755 --- a/scripts/install +++ b/scripts/install @@ -3,90 +3,103 @@ # Exit on command errors and treat unset variables as an error set -eu -# 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 -# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...) -# The app instance name is available as $YNH_APP_INSTANCE_NAME -# - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample -# - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2 -# - ynhexample__{N} for the subsequent installations, with N=3,4, ... -# The app instance name is probably what you are interested the most, since this is -# guaranteed to be unique. This is a good unique identifier to define installation path, -# db names, ... +# Get instances specific variables app=$YNH_APP_INSTANCE_NAME + # Retrieve arguments -domain=$YNH_APP_ARG_DOMAIN -path=$YNH_APP_ARG_PATH -admin=$YNH_APP_ARG_ADMIN -is_public=$YNH_APP_ARG_IS_PUBLIC -language=$YNH_APP_ARG_LANGUAGE +domain=$1 +path=${2%/} +password=$3 +is_public=$4 +with_mysql=$5 -# Source YunoHost helpers -source /usr/share/yunohost/helpers - -# Save app settings -ynh_app_setting_set "$app" admin "$admin" -ynh_app_setting_set "$app" is_public "$is_public" -ynh_app_setting_set "$app" language "$language" +# Source app helpers +. /usr/share/yunohost/helpers # Check domain/path availability sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" + || exit 1 -# Copy source files -src_path=/var/www/$app -sudo mkdir -p $src_path -sudo cp -a ../sources/. $src_path +# Check password strength +[[ ${#password} -gt 5 ]] || ynh_die \ +"The password is too weak, it must be longer than 5 characters" -# Set 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 +# Check destination directory +DESTDIR="/var/www/${app}" +[[ -d "$DESTDIR" ]] && ynh_die \ +"The destination directory '${DESTDIR}' already exists.\ + You should safely delete it before installing this app." -### MySQL (can be removed if not used) ### -# If your app use a MySQL database you can use these lines to bootstrap -# a database, an associated user and save the password in app settings. -# -# # Generate MySQL password and create database -# dbuser=$app -# dbname=$app -# dbpass=$(ynh_string_random 12) -# ynh_app_setting_set "$app" mysqlpwd "$dbpass" -# ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" -# -# # Load initial SQL into the new database -# ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" \ -# < "../sources/sql/mysql.init.sql" -### MySQL end ### +# Save app settings +user="webapp${app_nb}" +ynh_app_setting_set "$app" is_public "$is_public" +ynh_app_setting_set "$app" with_mysql "$with_mysql" +ynh_app_setting_set "$app" password "$password" +ynh_app_setting_set "$app" user "$user" -# Modify Nginx configuration file and copy it to Nginx conf directory -nginx_conf=../conf/nginx.conf -sed -i "s@YNH_WWW_PATH@$path@g" $nginx_conf -sed -i "s@YNH_WWW_ALIAS@$src_path/@g" $nginx_conf -# If a dedicated php-fpm process is used: -# Don't forget to modify ../conf/nginx.conf accordingly or your app will not work! -# sed -i "s@YNH_WWW_APP@$app@g" $nginx_conf -sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf +# Create the user account +sudo useradd -c "${app} user account" \ + -d "$DESTDIR" -M -g www-data "$user" \ + || ynh_die "Unable to create user account" +sudo chpasswd <<< "${user}:${password}" -### PHP (can be removed if not used) ### -# If a dedicated php-fpm process is used: -# Don't forget to modify ../conf/php-fpm.conf accordingly or your app will not work! -# -# # Modify PHP-FPM pool configuration and copy it to the pool directory -# sed -i "s@YNH_WWW_APP@$app@g" ../conf/php-fpm.conf -# sed -i "s@YNH_WWW_ALIAS@$src_path/@g" ../conf/php-fpm.conf -# finalphpconf=/etc/php5/fpm/pool.d/$app.conf -# sudo cp ../conf/php-fpm.conf $finalphpconf -# sudo chown root: $finalphpconf -# sudo chmod 644 $finalphpconf -# sudo service php5-fpm reload -### PHP end ### +# Harden SSH connection for the user +echo "##-> ${app} +# Hardening user connection +Match User ${user} + ChrootDirectory %h + ForceCommand internal-sftp + AllowTcpForwarding no + PermitTunnel no + X11Forwarding no +##<- ${app}" | sudo tee -a /etc/ssh/sshd_config >/dev/null -# If app is public, add url to SSOWat conf as skipped_uris -if [[ $is_public -eq 1 ]]; then - # unprotected_uris allows SSO credentials to be passed anyway. - ynh_app_setting_set "$app" unprotected_uris "/" +# Specify the user and the domain in the home page +sed -i "s@{DOMAIN}@${domain}@g" ../sources/www/index.html +sed -i "s@{USER}@${user}@g" ../sources/www/index.html + +# Initialize database as needed +if [[ $with_mysql -eq 1 ]]; then + dbname=$app + dbuser=$app + dbpass=$(ynh_string_random) + ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" + + # Store the database access + echo -e "# MySQL Database +name: ${dbname}\nuser: ${dbuser}\npass: ${dbpass}" > ../sources/db_access.txt + ynh_app_setting_set "$app" mysqlpwd "$dbpass" fi +# Copy files to the right place and set permissions +sudo cp -r ../sources "$DESTDIR" +sudo chown -hR "${user}:" "$DESTDIR" + +# Home directory of the user need to be owned by root to allow +# SFTP connections +sudo chown root: "$DESTDIR" + +# Set SSOwat rules +[[ $is_public -eq 1 ]] \ + && ynh_app_setting_set "$app" skipped_uris "/" + +# Copy and set nginx configuration +nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" +sed -i "s@{PATH}@${path}@g" ../conf/nginx.conf +sed -i "s@{LOCATION}@${path:-/}@g" ../conf/nginx.conf +sed -i "s@{DESTDIR}@${DESTDIR}@g" ../conf/nginx.conf +sed -i "s@{POOLNAME}@${app}@g" ../conf/nginx.conf +sudo cp ../conf/nginx.conf "$nginx_conf" + +# Copy and set php-fpm configuration +phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf" +sed -i "s@{USER}@${user}@g" ../conf/php-fpm.conf +sed -i "s@{POOLNAME}@${app}@g" ../conf/php-fpm.conf +sed -i "s@{DESTDIR}@${DESTDIR}@g" ../conf/php-fpm.conf +sudo cp ../conf/php-fpm.conf "$phpfpm_conf" + # Reload services +sudo service php5-fpm reload sudo service nginx reload +sudo service sshd reload diff --git a/scripts/remove b/scripts/remove index 59ef331..021afa4 100755 --- a/scripts/remove +++ b/scripts/remove @@ -1,34 +1,40 @@ #!/bin/bash -# See comments in install script +# Get multi-instances specific variables app=$YNH_APP_INSTANCE_NAME +app_nb=$YNH_APP_INSTANCE_NUMBER -# Source YunoHost helpers -source /usr/share/yunohost/helpers +# Source app helpers +. /usr/share/yunohost/helpers # Retrieve app settings domain=$(ynh_app_setting_get "$app" domain) +with_mysql=$(ynh_app_setting_get "$app" with_mysql) +user=$(ynh_app_setting_get "$app" user) -# Remove sources -sudo rm -rf /var/www/$app +# Drop MySQL database and user as needed +if [[ $with_mysql -eq 1 ]]; then + dbname=$app + dbuser=$app + dbpass=$(ynh_app_setting_get "$app" mysqlpwd) + ynh_mysql_drop_db $dbname || true + ynh_mysql_drop_user $dbuser || true +fi -# Remove nginx configuration file -sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf +# Delete app directory and configurations +sudo rm -rf "/var/www/${app}" +sudo rm -f "/etc/php5/fpm/pool.d/${app}.conf" +[[ -n $domain ]] && sudo rm -f "/etc/nginx/conf.d/${domain}.d/${app}.conf" -### PHP (remove if not used) ### -# If a dedicated php-fpm process is used: -# sudo rm -f /etc/php5/fpm/pool.d/$app.conf -# sudo service php5-fpm reload -### PHP end ### +# Remove custom SSH configuration +sudo sed -i "/##-> ${app}/,/##<- ${app}/d" /etc/ssh/sshd_config -### MySQL (remove if not used) ### -# If a MySQL database is used: -# # Drop MySQL database and user -# dbname=$app -# dbuser=$app -# ynh_mysql_drop_db "$dbname" || true -# ynh_mysql_drop_user "$dbuser" || true -### MySQL end ### +# Reload services +sudo service php5-fpm restart || true +sudo service nginx reload || true +sudo service sshd reload -# Reload nginx service -sudo service nginx reload +# Remove the user account +id "$user" >/dev/null 2>&1 \ + && sudo deluser --quiet --force "$user" >/dev/null \ + || true diff --git a/scripts/restore b/scripts/restore index a83fa6d..eb5a6d6 100755 --- a/scripts/restore +++ b/scripts/restore @@ -1,52 +1,81 @@ #!/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. +set -e -# Exit on command errors and treat unset variables as an error -set -eu - -# See comments in install script +# Get multi-instances specific variables app=$YNH_APP_INSTANCE_NAME -# Source YunoHost helpers -source /usr/share/yunohost/helpers +# Source app helpers +. /usr/share/yunohost/helpers # Retrieve old app settings domain=$(ynh_app_setting_get "$app" domain) path=$(ynh_app_setting_get "$app" path) +with_mysql=$(ynh_app_setting_get "$app" with_mysql) +password=$(ynh_app_setting_get "$app" password) +user=$(ynh_app_setting_get "$app" user) # Check domain/path availability sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" + || exit 1 -# Restore sources & data -src_path="/var/www/${app}" -sudo cp -a ./sources "$src_path" +# Check destination directory +DESTDIR="/var/www/$app" +[[ -d $DESTDIR ]] && ynh_die \ +"The destination directory '$DESTDIR' already exists.\ + You should safely delete it before restoring this app." -# 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" +# Check configuration files +nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" +[[ -f $nginx_conf ]] && ynh_die \ +"The NGINX configuration already exists at '${nginx_conf}'. + You should safely delete it before restoring this app." +phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf" +[[ -f $phpfpm_conf ]] && ynh_die \ +"The PHP FPM configuration already exists at '${phpfpm_conf}'. + You should safely delete it before restoring this app." -### 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 ### +# Create the user account +sudo useradd -c "${app} user account" \ + -d "$DESTDIR" -M -g www-data "$user" \ + || ynh_die "Unable to create user account" +sudo chpasswd <<< "${user}:${password}" -# Restore NGINX configuration -sudo cp -a ./nginx.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf" +# Harden SSH connection for the user +echo "##-> ${app} +# Hardening user connection +Match User ${user} + ChrootDirectory %h + ForceCommand internal-sftp + AllowTcpForwarding no + PermitTunnel no + X11Forwarding no +##<- ${app}" | sudo tee -a /etc/ssh/sshd_config >/dev/null -### 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 "/etc/php5/fpm/pool.d/${app}.conf" -# sudo service php5-fpm reload -### PHP end ### +# Restore the app files +sudo cp -a ./sources "$DESTDIR" +sudo chown -hR "${user}:" "$DESTDIR" -# Restart webserver -sudo service nginx reload +# Home directory of the user need to be owned by root to allow +# SFTP connections +sudo chown root: "$DESTDIR" + +# Create and restore the database as needed +if [[ $with_mysql -eq 1 ]]; then + dbname=$app + dbuser=$app + dbpass=$(ynh_app_setting_get "$app" mysqlpwd) + ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" + [[ -f ./dump.sql ]] \ + && ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./dump.sql \ + || echo "No MySQL dump has been found" >&2 +fi + +# Restore configuration files +sudo cp -a ./conf/nginx.conf "$nginx_conf" +sudo cp -a ./conf/php-fpm.conf "$phpfpm_conf" + +# Reload services +sudo service php5-fpm reload || true +sudo service nginx reload || true +sudo service sshd reload diff --git a/scripts/upgrade b/scripts/upgrade index 1decc77..6181f54 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,59 +1,53 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error set -eu -# See comments in install script +# Get multi-instances specific variables app=$YNH_APP_INSTANCE_NAME -# Source YunoHost helpers -source /usr/share/yunohost/helpers +# Source app helpers +. /usr/share/yunohost/helpers # Retrieve app settings domain=$(ynh_app_setting_get "$app" domain) path=$(ynh_app_setting_get "$app" path) -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%/} +is_public=$(ynh_app_setting_get "$app" is_public) +with_mysql=$(ynh_app_setting_get "$app" with_mysql) +password=$(ynh_app_setting_get "$app" password) +user=$(ynh_app_setting_get "$app" user) -# Copy source files -src_path=/var/www/$app -sudo mkdir -p $src_path -sudo cp -a ../sources/. $src_path +([[ -n "$with_mysql" ]] && [[ -n "$password" ]] && [[ -n "$user" ]]) \ + || ynh_die "The app changed and can not be automatically upgraded. \ +You will have to manually upgrade it following those instructions: \ +https://github.com/garwinch/Yuno_app_cagette#upgrade" -# Set 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 +# Check destination directory +DESTDIR="/var/www/$app" +[[ ! -d $DESTDIR ]] && ynh_die \ +"The destination directory '$DESTDIR' does not exist.\ + The app is not correctly installed, you should remove it first." -# Modify Nginx configuration file and copy it to Nginx conf directory -nginx_conf=../conf/nginx.conf -sed -i "s@YNH_WWW_PATH@$path@g" $nginx_conf -sed -i "s@YNH_WWW_ALIAS@$src_path/@g" $nginx_conf -# If a dedicated php-fpm process is used: -# -# sed -i "s@YNH_WWW_APP@$app@g" $nginx_conf -sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf +# Harden SSH connection for the user +sudo sed -i "/##-> ${app}/,/##<- ${app}/d" /etc/ssh/sshd_config +echo "##-> ${app} +# Hardening user connection +Match User ${user} + ChrootDirectory %h + ForceCommand internal-sftp + AllowTcpForwarding no + PermitTunnel no + X11Forwarding no +##<- ${app}" | sudo tee -a /etc/ssh/sshd_config >/dev/null -### PHP (remove if not used) ### -# If a dedicated php-fpm process is used: -# # Modify PHP-FPM pool configuration and copy it to the pool directory -# sed -i "s@YNH_WWW_APP@$app@g" ../conf/php-fpm.conf -# sed -i "s@YNH_WWW_ALIAS@$src_path/@g" ../conf/php-fpm.conf -# finalphpconf=/etc/php5/fpm/pool.d/$app.conf -# sudo cp ../conf/php-fpm.conf $finalphpconf -# sudo chown root: $finalphpconf -# sudo chmod 644 $finalphpconf -# sudo service php5-fpm restart -### PHP end ### +# Fix permissions +sudo chown -hR "${user}:" "$DESTDIR" -# If app is public, add url to SSOWat conf as skipped_uris -if [[ $is_public -eq 1 ]]; then - # See install script - ynh_app_setting_set "$app" unprotected_uris "/" -fi +# Home directory of the user need to be owned by root to allow +# SFTP connections +sudo chown root: "$DESTDIR" + +# Set SSOwat rules +[[ $is_public -eq 1 ]] \ + && ynh_app_setting_set "$app" skipped_uris "/" -# Reload nginx service -sudo service nginx reload