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

213 lines
8.1 KiB
Text
Raw Normal View History

2020-12-07 16:20:37 +01:00
#!/bin/bash
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2020-12-08 09:31:10 +01:00
2020-12-07 16:20:37 +01:00
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."
2020-12-08 09:31:10 +01:00
admin=$(ynh_app_setting_get --app="$app" --key=admin)
2020-12-07 16:20:37 +01:00
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)
2020-12-08 09:31:10 +01:00
log_path=$(ynh_app_setting_get --app="$app" --key=log_path)
domain=$(ynh_app_setting_get --app="$app" --key=domain)
path_url=$(ynh_app_setting_get --app="$app" --key=path)
port=$(ynh_app_setting_get --app="$app" --key=port)
2020-12-07 16:20:37 +01:00
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
admin_mail=$(ynh_user_get_info "$admin" mail)
2020-12-08 09:31:10 +01:00
redis_db=$(ynh_app_setting_get --app="$app" --key=redis_db)
2020-12-07 16:20:37 +01:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=15
2020-12-07 16:20:37 +01:00
# 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
#=================================================
# 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
2020-12-08 09:31:10 +01:00
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/nginx
ynh_add_nginx_config "public_path" "port"
2020-12-07 16:20:37 +01:00
#=================================================
# 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"
#=================================================
2020-12-29 13:58:40 +01:00
# UPGRADE VENV
2020-12-07 16:20:37 +01:00
#=================================================
setup_venv # function defined in _common.sh
2020-12-08 09:31:10 +01:00
#=================================================
# copy config files
# ================================================
2020-12-29 13:58:40 +01:00
ynh_script_progression --message="Create project configuration files..."
2020-12-08 09:31:10 +01:00
gunicorn_conf="$final_path/gunicorn.conf.py"
ynh_backup_if_checksum_is_different --file="$gunicorn_conf"
cp "../conf/gunicorn.conf.py" "$gunicorn_conf"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$gunicorn_conf"
ynh_replace_string --match_string="__LOG_FILE__" --replace_string="$log_file" --target_file="$gunicorn_conf"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$gunicorn_conf"
ynh_store_file_checksum --file="$gunicorn_conf"
ynh_backup_if_checksum_is_different --file="$final_path/manage.py"
cp ../conf/manage.py "$final_path/manage.py"
chmod +x "$final_path/manage.py"
# save old settings file
2020-12-29 13:58:40 +01:00
settings="$final_path/settings.py"
2020-12-08 09:31:10 +01:00
ynh_backup_if_checksum_is_different --file="$settings"
2020-12-29 13:58:40 +01:00
cp "../conf/settings.py" "$settings"
2020-12-08 09:31:10 +01:00
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="__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="__LOG_FILE__" --replace_string="$log_file" --target_file="$settings"
ynh_replace_string --match_string="__REDIS_DB__" --replace_string="$redis_db" --target_file="$settings"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$settings"
ynh_replace_string --match_string="__PATH_URL__" --replace_string="$path_url" --target_file="$settings"
2020-12-08 09:31:10 +01:00
# Recalculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$settings"
2020-12-29 13:58:40 +01:00
ynh_backup_if_checksum_is_different --file="$final_path/setup_user.py"
cp ../conf/setup_user.py "$final_path/setup_user.py"
ynh_backup_if_checksum_is_different --file="$final_path/urls.py"
cp ../conf/urls.py "$final_path/urls.py"
2020-12-08 09:31:10 +01:00
2020-12-29 13:58:40 +01:00
ynh_backup_if_checksum_is_different --file="$final_path/wsgi.py"
cp ../conf/wsgi.py "$final_path/wsgi.py"
touch "$final_path/local_settings.py"
2020-12-08 09:31:10 +01:00
#=================================================
# MIGRATE PYINVENTORY
#=================================================
ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight=10
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
cd "${final_path}"
# Just for debugging:
./manage.py diffsettings
./manage.py migrate --no-input
./manage.py collectstatic --no-input
2020-12-29 13:58:40 +01:00
# Create/update Django superuser (set unusable password, because auth done via SSOwat):
./manage.py create_superuser --username="$admin" --email="$admin_mail"
2020-12-08 09:31:10 +01:00
# Check the configuration
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
./manage.py check --deploy || true
)
2020-12-07 16:20:37 +01:00
#=================================================
# 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..."
2020-12-09 17:58:19 +01:00
yunohost service add $app --description="Web based management to catalog things" --log="${log_file}"
2020-12-07 16:20:37 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2020-12-08 09:31:10 +01:00
# Set permissions to app files
2021-10-10 12:07:30 +02:00
chown -R "$app:" "$log_path"
chown -R "$app:www-data" "$public_path"
2021-10-10 12:07:30 +02:00
chown -R "$app:" "$final_path"
2020-12-07 16:20:37 +01:00
chmod o-rwx "$log_path"
chmod o-rwx "$public_path"
chmod o-rwx "$final_path"
2020-12-08 09:31:10 +01:00
#=================================================
# Start pyinventory via systemd
#=================================================
ynh_script_progression --message="Starting PyInventory's services..." --weight=5
ynh_systemd_action --service_name="$app" --action="start"
2020-12-07 16:20:37 +01:00
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2020-12-09 17:49:11 +01:00
ynh_script_progression --message="Upgrade of $app completed" --last