1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/weblate_ynh.git synced 2024-10-01 13:35:04 +02:00
weblate_ynh/scripts/upgrade
Jean-Baptiste Holcroft 6872223e5a add logrotate
2019-08-23 08:32:02 +02:00

462 lines
15 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..." --time --weight=1
app=$YNH_APP_INSTANCE_NAME
path_url=$(ynh_app_setting_get "$app" path)
is_public=$(ynh_app_setting_get "$app" is_public)
final_path=$(ynh_app_setting_get "$app" final_path)
db_name=$(ynh_app_setting_get "$app" db_name)
domain=$(ynh_app_setting_get "$app" domain)
db_pwd=$(ynh_app_setting_get "$app" psqlpwd)
admin=$(ynh_app_setting_get "$app" admin)
admin_mail=$(ynh_user_get_info "$admin" mail)
memc_port=$(ynh_app_setting_get "$app" memc_port)
github_account=$(ynh_app_setting_get "$app" github_account)
migration311=$(ynh_app_setting_get "$app" migration311)
key=$(ynh_string_random 24)$(ynh_string_random 24)$(ynh_string_random 2)
redis_db=$(ynh_app_setting_get "$app" redis_db)
#=================================================
# Get previous version number
#=================================================
ynh_script_progression --message="Get previous version number..." --time --weight=1
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
pip install --upgrade pip
pip freeze --local > freeze.pip
)
previous_version=$(cat freeze.pip | grep "Weblate==" | sed "s|Weblate==||")
ynh_secure_remove freeze.pip
previous_version_template="../conf/settings_history/settings.$previous_version.py"
test -e "$previous_version_template" || ynh_die "Previous version unknown: $previous_version"
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --time --weight=1
# 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..." --time --weight=1
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set "$app" is_public 1 # Fix is_public as a boolean value
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set "$app" is_public 0
is_public=0
fi
# (<2.17)
if [ -z "$db_name" ]; then # If db_name doesn't exist, create it
db_name=$(ynh_sanitize_dbid "$app")
ynh_app_setting_set "$app" db_name "$db_name"
fi
# (<3.2)
migrate_to_python3=0
if [ -e "$final_path/venv/lib/python2.7/site-packages/weblate/settings.py" ]; then
settings="$final_path/venv/lib/python2.7/site-packages/weblate/settings.py"
migrate_to_python3=1
cp "$settings" "$final_path/settings.py"
else
settings="$final_path/venv/lib/python3.5/site-packages/weblate/settings.py"
fi
# (<2.17) save memc_port if it doesn't exist
if [[ -z "$memc_port" ]]
then
memc_port=$(cat "$settings" \
| grep "'LOCATION': '127.0.0.1:" \
| sed "s|.*:\\(.*\\)'.*|\\1|")
ynh_app_setting_set "$app" memc_port "$memc_port"
fi
# Weblate requires an update to 3.0 before 3.1
# the upgrade hook will launch this script again to make sure it works
if [[ -z "$migration311" && $previous_version = "2.20" ]]
then
# $migration311 is not set, version is <3.0
migration311="two_steps_upgrade_3.0to3.1-needed"
mig_type=$(cat "/etc/yunohost/apps/$YNH_APP_ID/status.json" | sed -e 's/.*"type":.*"\(.\+\)".*/\1/gi')
if [ "$mig_type" = "file" ]
then
mig_parm=$(cat "/etc/yunohost/apps/$YNH_APP_ID/status.json" | sed -e 's/.*"path": "\(.\+\)",.*/\1/gi')
else
mig_parm=$(cat "/etc/yunohost/apps/$YNH_APP_ID/status.json" | sed -e 's/.*"url": "\(.\+\)", "type".*/\1/gi')
fi
ynh_app_setting_set "$app" migration311 "$migration311"
ynh_app_setting_set "$app" migration311_type "$mig_type"
ynh_app_setting_set "$app" migration311_parm "$mig_parm"
current_version="3.0.1"
fi
# (<3.2)
if [ -z "$redis_db" ]; then
redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set "$app" redis_db "$redis_db"
fi
# (<2.20) remove complex uwsgi systemd template
if [ -e "/etc/systemd/system/uwsgi-app@.service" ]
then
systemctl stop "uwsgi-app@$app"
# if somehow the service doesn't exist, add it
yunohost service add "uwsgi-app@$app.service" --log "/var/log/uwsgi/app/$app"
yunohost service remove "uwsgi-app@$app.service"
ynh_secure_remove "/etc/systemd/system/uwsgi-app@.socket"
ynh_secure_remove "/etc/systemd/system/uwsgi-app@.service"
# remove old log file (used by the new service file)
ynh_secure_remove "/var/log/uwsgi/app/weblate"
systemctl daemon-reload
fi
# (<2.18) move hub to the correct folder
if [ -e "$final_path/bin/hub" ]
then
mv "$final_path/bin/hub" /usr/bin/
chown root:root /usr/bin/hub
fi
if [[ -d "$final_path/bin/" ]]
then
ynh_secure_remove "$final_path/bin/"
fi
# Make sure the uwsgi service is stoped
if [[ -e "/var/run/$app/socket" ]]
then
systemctl stop "$app"
fi
if [[ -e "/var/run/$app-celery/$app-w1.pid" ]]
then
systemctl stop "$app-celery"
fi
#=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
path_url=$(ynh_normalize_url_path "$path_url")
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping systemd services..." --time --weight=1
ynh_systemd_action --service_name="$app" --action="stop"
ynh_systemd_action --service_name="$app-celery" --action="stop"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading nginx web server configuration..." --time --weight=1
# Create a dedicated nginx config
ynh_add_nginx_config
if [ "$path_url" == "/" ]
then
# $finalnginxconf comes from ynh_add_nginx_config
ynh_replace_string "location //" "location /" "$finalnginxconf"
# ynh panel is only comptable with non-root installation
ynh_replace_string " include conf.d/" " #include conf.d/" "$finalnginxconf"
ynh_store_file_checksum "$finalnginxconf"
fi
#=================================================
# SPECIFIC UPGRADE
#=================================================
# Update dependencies
#=================================================
ynh_install_app_dependencies "$pkg_dependencies"
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --time --weight=1
# Create a system user
ynh_system_user_create "$app" "/home/$app"
chsh --shell /bin/bash "$app"
#=================================================
# SPECIFIC SETUP uwsgi
#=================================================
ynh_script_progression --message="Configure uwsgi..." --time --weight=1
finaluwsgiini="/etc/uwsgi/apps-available/$app.ini"
cp ../conf/uwsgi.ini "$finaluwsgiini"
ynh_replace_string "__FINALPATH__" "$final_path" "$finaluwsgiini"
ynh_replace_string "__PATH__" "$path_url" "$finaluwsgiini"
ynh_replace_string "__APP__" "$app" "$finaluwsgiini"
# root install doesn't require uwsgi to handle script names
if [ "$path_url" == "/" ]
then
ynh_replace_string "manage-script-name = true" "manage-script-name = false" "$finaluwsgiini"
fi
ynh_add_systemd_config "$app" "weblate.service"
ynh_store_file_checksum "$finaluwsgiini"
#=================================================
# PIP INSTALLATION
#=================================================
if [ "$migrate_to_python3" -eq 1 ]
then
ynh_script_progression --message="Rebuild the virtualenv with Python3..." --time --weight=1
ynh_secure_remove "${final_path}/venv"
virtualenv --python=python3 "${final_path}/venv"
fi
(
ynh_script_progression --message="Install weblate using PIP..." --time --weight=10
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
pip install --upgrade pip
# prevent error: "command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers"
pip install --upgrade setuptools
pip install Weblate=="$current_version"
pip install pytz python-bidi PyYaML Babel pyuca pylibravatar py3dns psycopg2-binary phply django-redis hiredis aeidon
# specific to YunoHost package:
pip install django_sendmail_backend
)
if [ "$migrate_to_python3" -eq 1 ]
then
mv "$final_path/settings.py" "$final_path/venv/lib/python3.5/site-packages/weblate/settings.py"
settings="$final_path/venv/lib/python3.5/site-packages/weblate/settings.py"
fi
#=================================================
# CONFIG FILE UPGRADE
#=================================================
ynh_script_progression --message="Create weblate configuration file..." --time --weight=1
# save old settings file
old_settings="$final_path/settings.$previous_version.old.py"
cp "$settings" "$old_settings"
check=$(ynh_check_if_checksum_is_different "$settings")
if [[ "$check" -eq 1 ]]
then
echo "Settings.py was modified localy, running diff before using the new default file for $current_version."
# generate previous defaults settings
cp "$previous_version_template" "$old_settings"
weblate_fill_settings "$old_settings"
# store diff between defaults and local settings
set +eu
diff --unified "$old_settings" "$settings" > "$final_path/settings.${previous_version}_${current_version}.diff"
set -eu
# send diff to the server administrator
mail_message="
Weblate was updated from version $previous_version to $current_version
Please read:
https://docs.weblate.org/en/latest/admin/upgrade.html
A new settings.py has been created:
$settings
You may have changed your defaults settings.
To help you, here is a diff file with every changes you did.
Diff has been created in:
$final_path/settings.${previous_version}_${current_version}.diff
Please note secret key is updated, this is normal.
For any issue, please file a bug in: https://github.com/YunoHost-Apps/weblate_ynh
"
ynh_send_readme_to_admin "$mail_message" root "$admin_mail"
else
echo "Settings.py was not modified, using the new default file for $current_version."
fi
# generate new defaults settings
cp "../conf/settings_history/settings.$current_version.py" "$settings"
weblate_fill_settings "$settings"
ynh_secure_remove "$old_settings"
#=================================================
# ACTIVATE CELERY
#=================================================
ynh_script_progression --message="Configure celery..." --time --weight=1
celeryconf="$final_path/celery-weblate"
cp ../conf/celery-weblate "$celeryconf"
ynh_replace_string "__APP__" "$app" "$celeryconf"
ynh_replace_string "__FINALPATH__" "$final_path" "$celeryconf"
ynh_add_systemd_config "$app-celery" "celery-weblate.service"
#=================================================
# Run migration scripts
#=================================================
ynh_script_progression --message="Run migration scripts..." --time --weight=1
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
export DJANGO_SETTINGS_MODULE="weblate.settings"
cd "${final_path}"
# https://docs.weblate.org/en/latest/admin/upgrade.html#upgrade-3
# https://github.com/WeblateOrg/docker/blob/386aa8d98bb57dfec3707680827d4e4f4d79e3fd/start#L81-L88
if [[ $current_version = "3.0.1" ]]
then
weblate showmigrations --plan > /tmp/migrations.txt
if grep -Fq '[X] auth.0001_initial' /tmp/migrations.txt && grep -Fq '[ ] weblate_auth.0001_initial' /tmp/migrations.txt ; then
ynh_replace_string "AUTH_USER_MODEL" "#AUTH_USER_MODEL" "$settings"
weblate migrate weblate_auth 0001
ynh_replace_string "#AUTH_USER_MODEL" "AUTH_USER_MODEL" "$settings"
fi
ynh_secure_remove /tmp/migrations.txt
fi
weblate migrate --noinput
weblate collectstatic --noinput
weblate setuplang
weblate setupgroups
if [[ $previous_version = "2.16" ]] || \
[[ $previous_version = "2.17.1" ]] || \
[[ $previous_version = "2.18" ]]
then
weblate loadpo --all --lang dsb
weblate loadpo --all --lang he
weblate loadpo --all --lang hsb
weblate loadpo --all --lang kw
weblate loadpo --all --lang lt
weblate loadpo --all --lang lv
fi
)
# Recalculate and store the config file checksum into the app settings
ynh_store_file_checksum "$final_path/venv/lib/python3.5/site-packages/weblate/settings.py"
#=================================================
# SETUP CRON
#=================================================
ynh_script_progression --message="Configure cron file..." --time --weight=1
cp ../conf/cron "/etc/cron.d/$app"
ynh_replace_string "__APP__" "$app" "/etc/cron.d/$app"
ynh_replace_string "__FINALPATH__" "$final_path/" "/etc/cron.d/$app"
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --time --weight=1
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --logfile "/var/log/$app-celery" --non-append
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set right permissions for curl installation
chown -R root:root "$final_path"
chown -R "$app": "$final_path/data"
mkdir -p "$final_path/avatar-cache"
chown -R "$app": "$final_path/avatar-cache"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring SSOwat..." --time --weight=1
if [ $is_public -eq 0 ]
then # Remove the public access
ynh_app_setting_delete "$app" skipped_uris
fi
# 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" unprotected_uris "/"
# ynh panel is not needed
ynh_replace_string " include conf.d/" " #include conf.d/" "$finalnginxconf"
ynh_store_file_checksum "$finalnginxconf"
fi
#=================================================
# Restart weblate
#=================================================
ynh_script_progression --message="Starting weblate's services..." --time --weight=1
ynh_systemd_action --service_name="$app" --action="start"
ynh_systemd_action --service_name="$app-celery" --action="start"
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --time --weight=1
ynh_systemd_action --service_name="nginx" --action="reload"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --time --last