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
2018-04-07 10:13:07 +02:00

302 lines
9 KiB
Bash
Executable file

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
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)
key=$(ynh_string_random)
#=================================================
# Get previous version number
#=================================================
(
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==||")
previous_version_file="../conf/settings_history/settings.$previous_version.py"
test -e "$previous_version_file" || ynh_die "Previous version unknown: $previous_version"
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
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
# (<2.18) handle python3 migration (because of django 2.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_py2to3=true
else
settings="$final_path/venv/lib/python3.4/site-packages/weblate/settings.py"
migrate_py2to3=false
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
# (<2.18) migrade old uwsgi files if existing
if [ -e "/etc/systemd/system/$app.service" ]
then
systemctl stop "$app.service"
systemctl disable "$app.service"
yunohost service remove "$app.service"
ynh_secure_remove "$final_path/uwsgi.ini"
ynh_secure_remove "/etc/systemd/system/$app.service"
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
#=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
path_url=$(ynh_normalize_url_path "$path_url")
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# 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
#=================================================
# CREATE DEDICATED USER
#=================================================
# Create a system user
ynh_system_user_create "$app" "/home/$app"
chsh --shell /bin/bash "$app"
#=================================================
# SPECIFIC UPGRADE
#=================================================
# Update dependencies
#=================================================
ynh_install_app_dependencies libxml2-dev libxslt-dev libfreetype6-dev \
libjpeg-dev libz-dev libyaml-dev python3-dev python3-pip python3-virtualenv python-virtualenv \
postgresql libpq-dev uwsgi uwsgi-plugin-python3 memcached
#=================================================
# SPECIFIC SETUP uwsgi
#=================================================
ynh_add_uwsgi_service
#=================================================
# PIP INSTALLATION
#=================================================
# save old settings file
cp "$settings" "$final_path/settings.$previous_version.old.py"
# (<2.18) handle python3 migration
if [[ "$migrate_py2to3" = true ]]
then
ynh_secure_remove "$final_path/venv"
virtualenv --python=python3 "${final_path}/venv"
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
pip install --upgrade pip
pip install Weblate=="$current_version"
settings="$final_path/venv/lib/python3.4/site-packages/weblate/settings.py"
cp "$final_path/settings.$previous_version.old.py" "$settings"
ynh_store_file_checksum "$settings"
fi
old_settings="./settings.$previous_version.old.py"
settings_diff="$final_path/settings.${previous_version}_${current_version}.diff"
(
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"
# prevent error "ImportError: No module named 'social'"
pip install -r "$final_path/venv/lib/python3.4/site-packages/weblate/requirements.txt"
pip install pytz python-bidi PyYaML Babel pyuca pylibravatar py3dns psycopg2 python-memcached phply
# specific to YunoHost package:
pip install django_sendmail_backend
)
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_file" "$old_settings"
weblate_fill_settings "$old_settings"
# store diff between defaults and local settings
diff --unified "$old_settings" "$settings" > "$settings_diff"
# generate new defaults settings
cp "../conf/settings_history/settings.$current_version.py" "$settings"
weblate_fill_settings "$settings"
# send diff to the server administrator
mail_message="
Weblate was updated from version $previous_version to $current_version
A new settings.py has been created in:
$settings
You may have changed your defaults settings.
To help you to apply it again, here is a diff file with every changes you did.
Diff has been created in:
$settings_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."
# generate new defaults settings
cp "../conf/settings_history/settings.$current_version.py" "$settings"
weblate_fill_settings "$settings"
fi
#=================================================
# Run migration scripts
#=================================================
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
export DJANGO_SETTINGS_MODULE="weblate.settings"
cd "${final_path}"
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
if [[ "$migrate_py2to3" = true ]]
then
weblate rebuild_index --clean --all
fi
)
# Recalculate and store the config file checksum into the app settings
ynh_store_file_checksum "$final_path/venv/lib/python3.4/site-packages/weblate/settings.py"
#=================================================
# GENERIC FINALIZATION
#=================================================
#=================================================
# SETUP SSOWAT
#=================================================
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
#=================================================
# RELOAD NGINX
#=================================================
systemctl reload nginx