1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/lutim_ynh.git synced 2024-09-03 19:36:24 +02:00
lutim_ynh/scripts/upgrade
2017-05-24 16:48:26 +02:00

177 lines
6.1 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get $app domain)
path_url=$(ynh_app_setting_get $app path)
is_public=$(ynh_app_setting_get $app is_public)
port=$(ynh_app_setting_get $app port)
always_encrypt=$(ynh_app_setting_get $app always_encrypt)
final_path=$(ynh_app_setting_get $app final_path)
#=================================================
# FIX OLD THINGS
#=================================================
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set $app is_public 1 # Fixe is_public en booléen
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set $app is_public 0
is_public=0
fi
if [ "${#final_path}" -eq 0 ]
then # Si final_path n'est pas renseigné dans la config yunohost, cas d'ancien script, code final_path en dur
final_path=/var/www/$app
ynh_app_setting_set $app final_path $final_path
fi
if [ "$always_encrypt" = "Yes" ]; then
ynh_app_setting_set $app always_encrypt 1 # Fixe always_encrypt en booléen
always_encrypt=1
elif [ "$always_encrypt" = "No" ]; then
ynh_app_setting_set $app always_encrypt 0
always_encrypt=0
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
BACKUP_BEFORE_UPGRADE # Backup the current version of the app
ynh_clean_setup () {
BACKUP_FAIL_UPGRADE # restore it if the upgrade fails
}
ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée.
#=================================================
# CHECK THE PATH
#=================================================
path_url=$(ynh_normalize_url_path $path_url) # Vérifie et corrige la syntaxe du path.
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_setup_source "$final_path" # Télécharge la source, décompresse et copie dans $final_path
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_system_user_create $app # Create the dedicated user, if not exist
#=================================================
# SPECIFIC UPGRADE
#=================================================
# SETUP LUTIM
#=================================================
## Copie et configuration du fichier de conf.
ynh_backup_if_checksum_is_different "$final_path/lutim.conf" # Créé un backup du fichier de config si il a été modifié.
sudo cp ../conf/lutim.conf.template "$final_path/lutim.conf"
ynh_replace_string "__DOMAIN__" "$domain" "$final_path/lutim.conf"
ynh_replace_string "__PATH__" "$path_url" "$final_path/lutim.conf"
ynh_replace_string "__PORT__" "$port" "$final_path/lutim.conf"
ynh_replace_string "__ENCRYPT__" "$always_encrypt" "$final_path/lutim.conf"
ynh_store_file_checksum "$final_path/lutim.conf" # Réenregistre la somme de contrôle du fichier de config
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_systemd_config
#=================================================
# SETUP CRON
#=================================================
sudo cp ../conf/cron_lutim /etc/cron.d/$app
ynh_replace_string "__FINALPATH__" "$final_path/" /etc/cron.d/$app
#=================================================
# UPDATE LUTIM WITH CARTON
#=================================================
pushd $final_path # cd avec une stack pour revenir en arrière
sudo carton install 2>&1 | sudo tee -a "/var/log/$app/setup_carton.log"
popd # Revient au dossier courant avant pushd
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
sudo chown -R $app: $final_path
#=================================================
# GENERIC FINALISATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_use_logrotate
sudo chown $app: -R /var/log/$app
#=================================================
# SETUP SSOWAT
#=================================================
ynh_app_setting_set $app skipped_uris "/"
if [ $is_public -eq 0 ]
then # Si l'app est privée, seul le visionnage des images reste public
# Modifie le domaine pour qu'il passe dans une regex
domain_regex=$(echo "$domain" | sed 's@-@.@g')
ynh_app_setting_set $app protected_regex "$domain_regex$path_url/stats$","$domain_regex$path_url/manifest.webapp$","$domain_regex$path_url/$","$domain_regex$path_url/d/.*$","$domain_regex$path_url/m/.*$"
fi
#=================================================
# RELOAD NGINX
#=================================================
sudo systemctl reload nginx
#=================================================
# START AND CHECK LUTIM BOOTING
#=================================================
tempfile="$(mktemp)"
tail -f -n1 /var/log/$app/production.log > "$tempfile" & # Suit le démarrage dans le log
PID_TAIL=$! # Récupère le PID de la commande tail, qui est passée en arrière plan.
sudo systemctl restart $app # Démarre lutim
for i in `seq 1 60`
do # La boucle attend le démarrage de lutim. Ou 1 minute. Cette boucle évite simplement un 502 au début, car le démarrage est parfois long...
if grep -q "Manager.*started" "$tempfile"; then
cat "$tempfile"
WARNING echo "Le service $app a démarré correctement."
break # Si le log annonce le démarrage de lutim, sort de la boucle.
fi
WARNING echo -n "."
sleep 1
done
echo ""
QUIET kill -s 15 $PID_TAIL # Arrête l'exécution de tail.
ynh_secure_remove "$tempfile"