1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pyinventory_ynh.git synced 2024-09-03 20:16:09 +02:00
pyinventory_ynh/scripts/upgrade
JensDiemer ba39fc8bf7 init
2020-12-08 09:06:25 +01:00

250 lines
No EOL
8.9 KiB
Bash
Executable file

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."
path_url=$(ynh_app_setting_get --app="$app" --key=path)
is_public=$(ynh_app_setting_get --app="$app" --key=is_public)
public_path=$(ynh_app_setting_get --app="$app" --key=public_path)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
db_name=$(ynh_app_setting_get --app="$app" --key=db_name)
db_user=$db_name
domain=$(ynh_app_setting_get --app="$app" --key=domain)
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
admin=$(ynh_app_setting_get --app="$app" --key=admin)
admin_mail=$(ynh_user_get_info "$admin" mail)
key=$(ynh_string_random 50)
#redis_db=$(ynh_app_setting_get --app="$app" --key=redis_db)
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=40
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
# Fix is_public as a boolean value
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set --app=$app --key=is_public --value=1
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set --app=$app --key=is_public --value=0
is_public=0
fi
if [[ -d "$final_path/bin/" ]]
then
ynh_secure_remove --file="$final_path/bin/"
fi
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping systemd services..." --weight=5
ynh_systemd_action --service_name="$app" --action="stop"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading nginx web server configuration..."
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# SPECIFIC UPGRADE
#=================================================
# Update dependencies
#=================================================
ynh_script_progression --message="Upgrading dependencies..."
ynh_exec_warn_less ynh_install_app_dependencies "$pkg_dependencies"
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..."
# Create a system user
ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..."
ynh_add_systemd_config --service="$app" --template="pyinventory.service"
#=================================================
# UPGRADE PYINVENTORY
#=================================================
upgrade() {
new_version=$1
settings_template=$2
#=================================================
# PIP INSTALLATION
#=================================================
ynh_script_progression --message="Install pyinventory using PIP..." --weight=15
virtualenv --python=python3 "${final_path}/venv"
chown -R "$app": "$final_path/venv"
#run source in a 'sub shell'
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
sudo -u $app $final_path/venv/bin/pip install --upgrade pip
sudo -u $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary
sudo -u $app $final_path/venv/bin/pip install --upgrade pyinventory=="$new_version"
)
#=================================================
# MODIFY A CONFIG FILE
#=================================================
ynh_script_progression --message="Create pyinventory configuration file..."
ynh_backup_if_checksum_is_different --file="$final_path/wsgi.py"
cp ../conf/wsgi.py "$final_path/wsgi.py"
ynh_backup_if_checksum_is_different --file="$final_path/manage.py"
cp ../conf/wsgi.py "$final_path/manage.py"
chmod +x "$final_path/manage.py"
# save old settings file
settings="$final_path/ynh_pyinventory_settings.py"
ynh_backup_if_checksum_is_different --file="$settings"
cp "$settings_template" "$settings"
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$settings"
ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$settings"
ynh_replace_string --match_string="__ADMIN__" --replace_string="$admin" --target_file="$settings"
ynh_replace_string --match_string="__ADMINMAIL__" --replace_string="$admin_mail" --target_file="$settings"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$settings"
ynh_replace_string --match_string="__KEY__" --replace_string="$key" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_WWW_PATH__" --replace_string="$public_path" --target_file="$settings"
ynh_replace_string --match_string="__PYTHON_VERSION__" --replace_string="$python_version" --target_file="$settings"
# remove last "/" of $path_url
ynh_replace_string --match_string="__PATHURL__" --replace_string="${path_url%/}" --target_file="$settings"
# Recalculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$settings"
touch "$final_path/local_settings.py"
#=================================================
# MIGRATE PYINVENTORY
#=================================================
ynh_script_progression --message="Run migration scripts..." --weight=10
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
cd "${final_path}"
./manage.py migrate --noinput
./manage.py collectstatic --noinput
# Check the configuration
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
if [ "$new_version" == "$pyinventory_version" ]; then
./manage.py check --deploy || true
fi
)
}
file_version="${final_path}/venv/lib/python$python_version/site-packages/pyinventory/__init__.py"
if [ -e $file_version ]
then
current_version=$(cat $file_version | grep "^VERSION = " | grep -o "[0-9].[0-9]" | head -n1 | cut -d"." -f1)
else
current_version=3
fi
upgrade $pyinventory_version "../conf/ynh_pyinventory_settings.py"
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..."
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add "$app" --log "/var/log/$app/pyinventory.log"
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set right permissions for curl installation
chown -R "$app": "$final_path"
#=================================================
# START SYSTEMD SERVICES
#=================================================
ynh_script_progression --message="Starting systemd services..." --weight=5
ynh_systemd_action --service_name="$app" --action="start"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Upgrading SSOwat configuration..."
# Make app public if necessary
if [ "$is_public" -eq 1 ]
then
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/"
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last