2016-12-21 18:46:36 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-01-17 21:27:58 +01:00
|
|
|
# We retrieve app parameters
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
2016-12-21 18:46:36 +01:00
|
|
|
|
2017-01-17 21:27:58 +01:00
|
|
|
# Backup the current version of the app, restore it if the upgrade fails
|
2017-01-18 00:13:02 +01:00
|
|
|
#if sudo yunohost backup list | grep -q ${app}-before-upgrade > /dev/null 2>&1; then # Supprime l'ancienne archive seulement si e$
|
|
|
|
# sudo yunohost backup delete ${app}-before-upgrade
|
|
|
|
#fi
|
|
|
|
#sudo yunohost backup create --ignore-hooks --apps $app --name ${app}-before-upgrade
|
2017-01-17 21:27:58 +01:00
|
|
|
EXIT_PROPERLY () {
|
2017-01-17 23:03:10 +01:00
|
|
|
exit_code=$?
|
|
|
|
if [ "$exit_code" -eq 0 ]; then
|
|
|
|
exit 0 # Quitte sans erreur si le script se termine correctement.
|
|
|
|
fi
|
|
|
|
trap '' EXIT
|
|
|
|
set +eu
|
|
|
|
sudo yunohost app remove $app # Supprime l'application avant de la restaurer.
|
|
|
|
sudo yunohost backup restore --ignore-hooks ${app}-before-upgrade --apps $app --force # Restore the backup if upgrade fail$
|
|
|
|
ynh_die "Upgrade failed. The app was restored to the way it was before the failed upgrade."
|
2017-01-17 21:27:58 +01:00
|
|
|
}
|
2017-01-18 00:13:02 +01:00
|
|
|
#set -eu
|
|
|
|
#trap EXIT_PROPERLY EXIT
|
2016-12-21 18:46:36 +01:00
|
|
|
|
|
|
|
# Source app helpers
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
|
|
|
# We check variables are not empty
|
2016-12-23 23:55:35 +01:00
|
|
|
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)
|
|
|
|
}
|
2016-12-21 18:46:36 +01:00
|
|
|
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)
|
|
|
|
finalnginxconf=$(ynh_app_setting_get $app finalnginxconf)
|
|
|
|
finalphpconf=$(ynh_app_setting_get $app finalphpconf)
|
2016-12-23 23:02:50 +01:00
|
|
|
calibre=$(ynh_app_setting_get $app calibre)
|
2016-12-22 13:01:20 +01:00
|
|
|
basicauthcreate=$(ynh_app_setting_get $app basicauthcreate)
|
2016-12-22 03:42:11 +01:00
|
|
|
|
2016-12-23 23:02:50 +01:00
|
|
|
# We check that calibre path is correct
|
|
|
|
CHECK_CALIBRE () { # Vérifie la présence du / en début de path. Et son absence à la fin.
|
|
|
|
if [ "${calibre:0:1}" != "/" ]; then # Si le premier caractère n'est pas un /
|
|
|
|
calibre="/$calibre" # Ajoute un / en début de path
|
|
|
|
fi
|
|
|
|
if [ "${calibre:${#calibre}-1}" == "/" ] && [ ${#calibre} -gt 1 ]; then # Si le dernier caractère est un / $
|
|
|
|
calibre="${calibre:0:${#calibre}-1}" # Supprime le dernier caractère
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_CALIBRE
|
2016-12-22 14:19:29 +01:00
|
|
|
|
2016-12-23 21:24:09 +01:00
|
|
|
# We install dependencies
|
2017-02-24 14:43:49 +01:00
|
|
|
#sudo apt-get update -y
|
|
|
|
#sudo apt-get install php5-gd php5-sqlite php5-json php5-intl -y
|
|
|
|
|
|
|
|
# Install dependencies using Helpers
|
|
|
|
ynh_package_install_from_equivs ../conf/cops-deps.control \
|
|
|
|
|| ynh_die "Unable to install dependencies"
|
2016-12-23 21:24:09 +01:00
|
|
|
|
|
|
|
# Removal of old folder and restart from fresh
|
|
|
|
sudo rm -rf $final_path
|
|
|
|
sudo mkdir -p $final_path
|
|
|
|
|
|
|
|
# We download the sources and check the md5sum
|
|
|
|
cops_file=`sudo cat ../sources/source_file`;
|
2016-12-23 21:59:20 +01:00
|
|
|
sudo wget -nv -i ../sources/source_url -O $cops_file
|
2016-12-23 21:24:09 +01:00
|
|
|
sudo md5sum -c ../sources/source_md5 --status || (echo "Corrupt source" >&2 && false)
|
2016-12-23 21:56:43 +01:00
|
|
|
sudo unzip ${cops_file} -d $final_path
|
2016-12-23 21:24:09 +01:00
|
|
|
|
|
|
|
# Site adjustments
|
|
|
|
sed -i "s@CALIBRETOCHANGE@$calibrepath@g" ../conf/config_local.php
|
|
|
|
timezone=`sudo cat /etc/timezone`;
|
|
|
|
sed -i "s@TIMEZONETOCHANGE@$timezone@g" ../conf/config_local.php
|
|
|
|
|
|
|
|
sudo cp ../conf/config_local.php $final_path
|
|
|
|
sudo cp ../conf/robots.txt $final_path
|
|
|
|
|
2017-02-24 17:06:06 +01:00
|
|
|
# Create cops user and join nextcloud/owncloud/www-data groups
|
|
|
|
runninguser="${app}-ynh"
|
|
|
|
# 1. Create the user
|
2017-02-24 17:47:43 +01:00
|
|
|
# Create a system account for COPS if it doesn't already exists
|
2017-02-24 18:20:38 +01:00
|
|
|
test="$(sudo grep ^${runninguser}: /etc/passwd)"
|
2017-02-24 18:12:32 +01:00
|
|
|
if [ -n "${test}" ]; then
|
|
|
|
echo "The user $runninguser does not exist, we can create it"
|
2017-02-24 17:47:43 +01:00
|
|
|
sudo useradd -c "$runninguser system account" \
|
|
|
|
-d $final_path --system --user-group $runninguser \
|
|
|
|
|| ynh_die "Unable to create $runninguser system account"
|
2017-02-24 18:12:32 +01:00
|
|
|
else
|
|
|
|
echo "The user $runninguser exists, no need to create it"
|
2017-02-24 17:47:43 +01:00
|
|
|
fi
|
2017-02-24 17:06:06 +01:00
|
|
|
ynh_app_setting_set "$app" runninguser "$runninguser"
|
|
|
|
|
|
|
|
# 2. Add cops-ynh to groups www-data and nextcloud/owncloud if they exist
|
|
|
|
sudo usermod -a -G www-data $runninguser
|
|
|
|
for filesharing in "nextcloud" "owncloud"; do
|
|
|
|
app_id=$(sudo yunohost app list --installed -f "$filesharing" \
|
|
|
|
--output-as json | grep -Po '"id":[ ]?"\K.*?(?=")' | head -1)
|
|
|
|
[[ -z "$app_id" ]] || {
|
|
|
|
sudo usermod -a -G $filesharing $runninguser
|
|
|
|
}
|
|
|
|
done
|
|
|
|
|
2016-12-23 21:24:09 +01:00
|
|
|
# Set permissions
|
2017-02-24 17:06:06 +01:00
|
|
|
sudo chmod ug+rw -R $final_path
|
2016-12-23 21:24:09 +01:00
|
|
|
sudo chown -hR $runninguser:$runninguser $final_path
|
|
|
|
|
2016-12-22 13:01:20 +01:00
|
|
|
# Add basic auth if requested
|
|
|
|
if [ "$basicauthcreate" = "Yes" ];
|
|
|
|
then
|
2016-12-23 23:55:35 +01:00
|
|
|
basicauthname=$(ynh_app_setting_get $app basicauthname)
|
2016-12-22 13:01:20 +01:00
|
|
|
basicauthpass=$(ynh_app_setting_get $app basicauthpass)
|
|
|
|
|
|
|
|
# Generation of the htpasswd file according https://www.nginx.com/resources/wiki/community/faq/
|
|
|
|
SALT="$(openssl rand -base64 3)"
|
|
|
|
(SHA1=$(printf "$basicauthpass$SALT" |
|
|
|
|
openssl dgst -binary -sha1 | xxd -ps |
|
|
|
|
sed 's#$#'"`echo -n $SALT | xxd -ps`"'#' |
|
|
|
|
xxd -r -ps |
|
2016-12-23 23:55:35 +01:00
|
|
|
base64);printf "$basicauthname:{SSHA}$SHA1\n" >> ../sources/htpasswd)
|
2016-12-23 21:24:09 +01:00
|
|
|
sudo cp ../sources/htpasswd $final_path
|
|
|
|
sudo chmod 440 $final_path/htpasswd
|
|
|
|
sudo chown www-data:www-data $final_path/htpasswd
|
2016-12-22 13:01:20 +01:00
|
|
|
|
|
|
|
# Modif nginx
|
2016-12-23 21:24:09 +01:00
|
|
|
sed -i "s|^.*\bauth_basic\b.*$| auth_basic \"Private Library\";|" ../conf/nginx.conf;
|
|
|
|
sed -i "s|^.*\bauth_basic_user_file\b.*$| auth_basic_user_file $final_path/htpasswd;|" ../conf/nginx.conf;
|
2016-12-22 13:01:20 +01:00
|
|
|
else
|
|
|
|
echo "No basic auth";
|
|
|
|
fi
|
2016-12-22 03:42:11 +01:00
|
|
|
|
2016-12-23 21:24:09 +01:00
|
|
|
# Modify Nginx configuration file and copy it to Nginx conf.d directory
|
2016-12-21 18:46:36 +01:00
|
|
|
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
|
|
|
|
|
2016-12-23 21:24:09 +01:00
|
|
|
# Modify php-fpm configuration file and copy it to php-fpm pool.d directory
|
2016-12-21 18:46:36 +01:00
|
|
|
sed -i "s@NAMETOCHANGE@$app@g" ../conf/php-fpm.conf
|
2016-12-22 01:08:35 +01:00
|
|
|
sed -i "s@FOLDERTOCHANGE@$final_path@g" ../conf/php-fpm.conf
|
2016-12-22 01:44:07 +01:00
|
|
|
sed -i "s@USERTOCHANGE@$runninguser@g" ../conf/php-fpm.conf
|
2016-12-21 18:46:36 +01:00
|
|
|
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 "/"
|
|
|
|
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
|