1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/lufi_ynh.git synced 2024-09-03 19:36:28 +02:00
lufi_ynh/scripts/upgrade
2019-03-03 20:26:09 +01:00

209 lines
6.7 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info "Loading installation 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)
final_path=$(ynh_app_setting_get $app final_path)
secret=$(ynh_app_setting_get $app secret)
db_name=$(ynh_app_setting_get $app db_name)
db_user=$db_name
db_pwd=$(ynh_app_setting_get $app psqlpwd)
max_file_size=$(ynh_app_setting_get $app max_file_size)
#=================================================
# 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
fi
if [ -z "$db_pwd" ]; then
# Create postgresql database
ynh_psql_test_if_first_run
db_name=$(ynh_sanitize_dbid "$app")
db_user=$db_name
ynh_app_setting_set "$app" db_name "$db_name"
# Initialize database and store postgres password for upgrade
ynh_psql_setup_db "$db_name" "$db_user"
db_pwd=$(ynh_app_setting_get $app psqlpwd) # Password created in ynh_psql_setup_db function
fi
if [ -z "$max_file_size" ]; then
max_file_size=100 # 100 Mo
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_print_info "Backing up the app before upgrading (may take a while)..."
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
ynh_clean_check_starting
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_print_info "Upgrading source files..."
ynh_install_app_dependencies build-essential libssl-dev libio-socket-ssl-perl liblwp-protocol-https-perl libpq-dev postgresql cpanminus
ynh_setup_source "$final_path"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_print_info "Upgrading nginx web server configuration..."
# Create a dedicated nginx config
ynh_add_nginx_config max_file_size
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_print_info "Making sure dedicated system user exists..."
# Create a dedicated user (if not existing)
ynh_system_user_create $app
#=================================================
# SPECIFIC UPGRADE
#=================================================
# SETUP LUFI
#=================================================
ynh_print_info "Configuring lufi..."
cp ../conf/lufi.conf.template "${final_path}/lufi.conf"
ynh_replace_string "__DOMAIN__" "$domain" "${final_path}/lufi.conf"
ynh_replace_string "__PATH__" "$path_url" "${final_path}/lufi.conf"
ynh_replace_string "__PORT__" "$port" "${final_path}/lufi.conf"
ynh_replace_string "__DB_NAME__" "$db_name" "${final_path}/lufi.conf"
ynh_replace_string "__DB_USER__" "$db_user" "${final_path}/lufi.conf"
ynh_replace_string "__DB_PWD__" "$db_pwd" "${final_path}/lufi.conf"
ynh_replace_string "__MAX_FILE_SIZE__" "$max_file_size" "${final_path}/lufi.conf"
if [ $max_file_size -eq 0 ]; then # Comment the limitation line if no limit
ynh_replace_string "max_file_size" "#max_file_size" "${final_path}/lufi.conf"
fi
ynh_replace_string "__SECRET__" "$secret" "${final_path}/lufi.conf"
if [ $is_public -eq 0 ];
then
ynh_replace_string "__IS_PUBLIC__" "" "${final_path}/lufi.conf"
else
ynh_replace_string "__IS_PUBLIC__" "#" "${final_path}/lufi.conf"
fi
ynh_store_file_checksum "${final_path}/lufi.conf"
#=================================================
# SETUP CRON
#=================================================
cp ../conf/cron_lufi /etc/cron.d/$app
ynh_replace_string "__FINALPATH__" "$final_path/" "/etc/cron.d/$app"
chmod +x $final_path/script/lufi
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
chown -R $app:$app "$final_path"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_print_info "Upgrading systemd configuration..."
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# Install lufi's dependencies via carton
#=================================================
pushd $final_path
carton install --deployment --without=sqlite --without=mysql
popd
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_print_info "Upgrading logrotate configuration..."
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add $app --log "$final_path/log/production.log"
#=================================================
# RESTART LUFI
#=================================================
ynh_systemd_action -n $app -a reload -l "Creating process id file" -p "$final_path/log/production.log"
ln -sf "$final_path/log/production.log" "/var/log/$app/production.log"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_print_info "Upgrading SSOwat configuration..."
# Make app public or private
ynh_app_setting_set $app unprotected_uris "/"
if [ $is_public -eq 0 ];
then
ynh_app_setting_set $app protected_regex "/stats$","/manifest.webapp$","/$","/d/.*$","/m/.*$"
else
ynh_app_setting_delete $app protected_regex
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_print_info "Reloading nginx web server..."
systemctl reload nginx
yunohost app ssowatconf
#=================================================
# END OF SCRIPT
#=================================================
ynh_print_info "Upgrade of $app completed"