From d37d87fdeba3cfa93f36f9891089479bda1d8e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Sat, 14 May 2016 21:59:14 +0200 Subject: [PATCH] [enh] Make use of ynh_die and YNH_APP_INSTANCE_NAME env var --- scripts/_common.sh | 15 ++++----------- scripts/backup | 13 ++++++------- scripts/install | 21 +++++++++++---------- scripts/remove | 4 +++- scripts/restore | 24 +++++++++--------------- scripts/upgrade | 17 +++++++++-------- 6 files changed, 42 insertions(+), 52 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index a4ff0bb..13390e1 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -23,13 +23,6 @@ PKGDIR=$(cd ../; pwd) # Common helpers # -# Print a message to stderr and exit -# usage: die msg [retcode] -die() { - printf "%s" "$1" 1>&2 - exit "${2:-1}" -} - # Download and extract ownCloud sources to the given directory # usage: extract_owncloud DESTDIR [AS_USER] extract_owncloud() { @@ -40,18 +33,18 @@ extract_owncloud() { oc_tarball="/tmp/owncloud.tar.bz2" rm -f "$oc_tarball" wget -q -O "$oc_tarball" "$OWNCLOUD_SOURCE_URL" \ - || die "Unable to download ownCloud tarball" + || ynh_die "Unable to download ownCloud tarball" echo "$OWNCLOUD_SOUCE_SHA256 $oc_tarball" | sha256sum -c >/dev/null \ - || die "Invalid checksum of downloaded tarball" + || ynh_die "Invalid checksum of downloaded tarball" exec_as "$AS_USER" tar xjf "$oc_tarball" -C "$DESTDIR" --strip-components 1 \ - || die "Unable to extract ownCloud tarball" + || ynh_die "Unable to extract ownCloud tarball" rm -f "$oc_tarball" # apply patches (cd "$DESTDIR" \ && for p in ${PKGDIR}/patches/*.patch; do \ exec_as "$AS_USER" patch -p1 < $p; done) \ - || die "Unable to apply patches to ownCloud" + || ynh_die "Unable to apply patches to ownCloud" } # Execute a command as another user diff --git a/scripts/backup b/scripts/backup index 3483700..aa06538 100755 --- a/scripts/backup +++ b/scripts/backup @@ -1,10 +1,9 @@ #!/bin/bash -set -e +set -eu -# Retrieve arguments -backup_dir=$1 -app=${!#} +# Get multi-instances specific variables +app=$YNH_APP_INSTANCE_NAME # Set app specific variables dbname=$app @@ -14,9 +13,9 @@ dbuser=$app . /usr/share/yunohost/helpers # Retrieve app settings -domain=$(ynh_app_setting_get $app domain) -path=$(ynh_app_setting_get $app path) -dbpass=$(ynh_app_setting_get $app mysqlpwd) +domain=$(ynh_app_setting_get "$app" domain) +path=$(ynh_app_setting_get "$app" path) +dbpass=$(ynh_app_setting_get "$app" mysqlpwd) # Copy the app source files DESTDIR="/var/www/$app" diff --git a/scripts/install b/scripts/install index 324c123..8bc885e 100755 --- a/scripts/install +++ b/scripts/install @@ -1,13 +1,14 @@ #!/bin/bash -set -e -set -u +set -eu + +# Get multi-instances specific variables +app=$YNH_APP_INSTANCE_NAME # Retrieve arguments domain=$1 path=${2%/} admin=$3 -app=${!#} # Load common variables . ./_common.sh @@ -21,16 +22,16 @@ dbuser=$app # TODO: Check domain/path availability with app helper sudo yunohost app checkurl $domain$path -a $app \ - || die "The path ${domain}${path} is not available for app installation." + || ynh_die "The path ${domain}${path} is not available for app installation." # Check user parameter ynh_user_exists "$admin" \ - || die "The chosen admin user does not exist." + || ynh_die "The chosen admin user does not exist." ynh_app_setting_set $app admin_user $admin # Check destination directory DESTDIR="/var/www/$app" -[[ -d $DESTDIR ]] && die \ +[[ -d $DESTDIR ]] && ynh_die \ "The destination directory '$DESTDIR' already exists.\ You should safely delete it before installing this app." @@ -39,7 +40,7 @@ DATADIR="/home/yunohost.app/${app}/data" # Install dependencies ynh_package_install_from_equivs ../conf/${DEPS_PKG_NAME}.control \ - || die "Unable to install dependencies" + || ynh_die "Unable to install dependencies" # Generate random password dbpass=$(ynh_string_random) @@ -51,7 +52,7 @@ ynh_mysql_create_db $dbname $dbuser $dbpass # Create a system account for ownCloud sudo useradd -c "$app system account" \ -d /var/lib/$app --system --user-group $app \ - || die "Unable to create $app system account" + || ynh_die "Unable to create $app system account" # Set system group in hooks sed -i "s@#GROUP#@${app}@g" ../hooks/post_user_create @@ -103,7 +104,7 @@ _exec_occ maintenance:install \ --database-user "$dbuser" --database-pass "$dbpass" \ --admin-user "admin" --admin-pass "$(ynh_string_random 6)" \ --data-dir "$DATADIR" \ - || die "Unable to install ownCloud" + || ynh_die "Unable to install ownCloud" # Enable plugins and set ownCloud configuration _exec_occ app:enable user_ldap @@ -113,7 +114,7 @@ sudo rm -f "$oc_conf" # Check LDAP configuratio to see if everything worked well _exec_occ ldap:test-config \'\' \ - || die "An error occured during LDAP configuration" + || ynh_die "An error occured during LDAP configuration" # Enable External Storage and create local mount _exec_occ app:enable files_external diff --git a/scripts/remove b/scripts/remove index 75e6b5f..0e02fba 100755 --- a/scripts/remove +++ b/scripts/remove @@ -1,7 +1,9 @@ #!/bin/bash +# Get multi-instances specific variables +app=$YNH_APP_INSTANCE_NAME + # Set app specific variables -app=${!#} dbname=$app dbuser=$app diff --git a/scripts/restore b/scripts/restore index d012ef0..aee8529 100755 --- a/scripts/restore +++ b/scripts/restore @@ -1,15 +1,9 @@ #!/bin/bash -# Retrieve arguments -backup_dir=$1 -app=${!#} +set -eu -# TODO: Put a simple die function in app helpers, redeclare it since -# _common.sh cannot be easily sourced -die() { - printf "%s" "$1" 1>&2 - exit "${2:-1}" -} +# Get multi-instances specific variables +app=$YNH_APP_INSTANCE_NAME # Set app specific variables dbname=$app @@ -25,11 +19,11 @@ dbpass=$(ynh_app_setting_get $app mysqlpwd) # TODO: Check domain/path availability with app helper sudo yunohost app checkurl $domain$path -a $app \ - || die "The path ${domain}${path} is not available for app installation." + || ynh_die "The path ${domain}${path} is not available for app installation." # Check destination directory DESTDIR="/var/www/$app" -[[ -d $DESTDIR ]] && die \ +[[ -d $DESTDIR ]] && ynh_die \ "The destination directory '$DESTDIR' already exists.\ You should safely delete it before restoring this app." @@ -38,22 +32,22 @@ DATADIR="/home/yunohost.app/${app}/data" # Check configuration files nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" -[[ -f $nginx_conf ]] && die \ +[[ -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 ]] && die \ +[[ -f $phpfpm_conf ]] && ynh_die \ "The PHP FPM configuration already exists at '${phpfpm_conf}'. You should safely delete it before restoring this app." # Install dependencies ynh_package_install_from_equivs ./owncloud-deps.control \ - || die "Unable to install dependencies" + || ynh_die "Unable to install dependencies" # Create a system account for ownCloud sudo useradd -c "$app system account" \ -d /var/lib/$app --system --user-group $app \ - || die "Unable to create $app system account" + || ynh_die "Unable to create $app system account" # Restore the app files sudo cp -a ./www "$DESTDIR" diff --git a/scripts/upgrade b/scripts/upgrade index f758276..a5dcee8 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,13 +1,14 @@ #!/bin/bash -set -e -set -u +set -eu + +# Get multi-instances specific variables +app=$YNH_APP_INSTANCE_NAME # Load common variables and helpers . ./_common.sh -# Retrieve arguments -app=${!#} +# Set app specific variables dbname=$app dbuser=$app @@ -22,19 +23,19 @@ dbpass=$(ynh_app_setting_get $app mysqlpwd) # Check destination directory DESTDIR="/var/www/$app" -[[ ! -d $DESTDIR ]] && die \ +[[ ! -d $DESTDIR ]] && ynh_die \ "The destination directory '$DESTDIR' does not exist.\ The app is not correctly installed, you should remove it first." # Check app's data directory DATADIR="/home/yunohost.app/${app}/data" -[[ ! -d $DATADIR ]] && die \ +[[ ! -d $DATADIR ]] && ynh_die \ "The data directory '$DATADIR' does not exist.\ The app is not correctly installed, you should remove it first." # Upgrade dependencies ynh_package_install_from_equivs ../conf/${DEPS_PKG_NAME}.control \ - || die "Unable to upgrade dependencies" + || ynh_die "Unable to upgrade dependencies" # Copy and set nginx configuration nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" @@ -96,7 +97,7 @@ sudo chown -R $app: "$DESTDIR" "$DATADIR" # TODO: Restore old directory in case of failure? _exec_occ maintenance:mode --off _exec_occ upgrade \ - || ([[ $? -eq 3 ]] || die "Unable to upgrade ownCloud") + || ([[ $? -eq 3 ]] || ynh_die "Unable to upgrade ownCloud") # Enable plugins and set ownCloud configuration _exec_occ app:enable user_ldap