1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/dont-code_ynh.git synced 2024-09-03 18:26:34 +02:00
dont-code_ynh/scripts/upgrade
2023-01-08 17:24:20 +01:00

297 lines
11 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source ynh_mongo_db__2
source ynh_java
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
port_ide=$(ynh_app_setting_get --app=$app --key=port_ide)
port_preview=$(ynh_app_setting_get --app=$app --key=port_preview)
port_project=$(ynh_app_setting_get --app=$app --key=port_project)
port_data=$(ynh_app_setting_get --app=$app --key=port_data)
db_user=$(ynh_app_setting_get --app=$app --key=db_user)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
document_path=$(ynh_app_setting_get --app=$app --key=document_path)
document_url=$(ynh_app_setting_get --app=$app --key=document_url)
html_path=$(ynh_app_setting_get --app=$app --key=html_path)
public_key=$(ynh_app_setting_get --app=$app --key=public_key)
PORT_LIST=($port_ide $port_preview $port_project $port_data)
#=================================================
# CHECK VERSION
#=================================================
### This helper will compare the version of the currently installed app and the version of the upstream package.
### $upgrade_type can have 2 different values
### - UPGRADE_APP if the upstream app version has changed
### - UPGRADE_PACKAGE if only the YunoHost package has changed
### ynh_check_app_version_changed will stop the upgrade if the app is up to date.
### UPGRADE_APP should be used to upgrade the core app only if there's an upgrade to do.
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --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
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICES
#=================================================
ynh_script_progression --message="Stopping systemd services..." --weight=1
for service_name in "${SERVICES_LIST[@]}"
do
ynh_systemd_action --service_name="${app}-${service_name}" --action="stop" --log_path="/var/log/$app/$app.log"
done
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
# Create html_path if needed
if [ -z "$html_path" ]; then
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
html_path=/usr/share/nginx/html/$app
ynh_app_setting_set --app=$app --key=html_path --value=$html_path
mkdir --parents $html_path
chmod 750 "$html_path"
chmod -R o-rwx "$html_path"
chown -R $app:www-data "$html_path"
fi
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir="$final_path" --use_shell --groups="ssh.app"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --weight=1
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
fi
# FIXME: this should be managed by the core in the future
# Here, as a packager, you may have to tweak the ownerhsip/permissions
# such that the appropriate users (e.g. maybe www-data) can access
# files in some cases.
# But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder -
# this will be treated as a security issue.
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path"
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=1
# Install the required version of Java
ynh_install_java --java_version=$java_version --jdk_type=jre
ynh_install_app_dependencies $pkg_dependencies
ynh_print_info --message="Debian release is $(ynh_get_debian_release)"
# Install the required version of Mongo
ynh_install_mongo --mongo_version=$mongo_version
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1
# Do we need to upgrade the status index ? Do it now as html_path variable is needed for nginx file
# Always Upgrade the status page
ynh_add_config --template="index.html" --destination="$html_path/index.html"
chmod 750 "$html_path/index.html"
chmod o-rwx "$html_path/index.html"
chown $app:www-data "$html_path/index.html"
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC UPGRADE
#=================================================
# Create document storage
#=================================================
# The document path has changed => Copy all the files
if [ "$document_path" != "/usr/share/nginx/html/$app/docs" ]; then
ynh_script_progression --message="Moving the document path to its new dir" --weight=8
old_doc_path=$document_path
document_path=/usr/share/nginx/html/$app/docs
test ! -e "$document_path" || ynh_die --message="This path already contains a folder"
mkdir --parents $document_path
mv $old_doc_path/* $document_path/*
ynh_secure_remove --file=$old_doc_path
ynh_app_setting_set --app=$app --key=document_path --value=$document_path
chmod 750 "$document_path"
chmod -R o-rwx "$document_path"
chown -R $app:www-data "$document_path"
# Update the path in .env file
ynh_backup_if_checksum_is_different --file="$final_path/.env"
ynh_replace_string --match_string="$old_doc_path" --replace_string="$document_path" --target_file="$final_path/.env"
ynh_store_file_checksum --file="$final_path/.env"
fi
# Regenerate the .env file if the document_url was incorrectly set before
correct_document_url=$(append_uri "https://${domain}${path_url}" "docs")
if [ "$correct_document_url" != "$document_url" ]; then
ynh_script_progression --message="Updading url for documents" --weight=1
old_doc_url=$document_url
document_url=$correct_document_url
ynh_app_setting_set --app=$app --key=document_url --value=$document_url
ynh_add_config --template=".env" --destination="$final_path/.env"
# FIXME: this should be handled by the core in the future
# You may need to use chmod 600 instead of 400,
# for example if the app is expected to be able to modify its own config
chmod 400 "$final_path/.env"
chown $app:$app "$final_path/.env"
fi
if [ -n "$public_key" ] && [ ! -f "$final_path/restart-services.sh" ];
then
ynh_script_progression --message="Upgrading ssh access for dev..." --weight=1
# Enable restarting of services from ssh
ynh_add_config --template="restart-services.sh" --destination="$final_path/restart-services.sh"
# Enable root ownership to be able to call systemctl
chown $app:$app "$final_path/restart-services.sh"
chmod o-rwx,gu=rwx "$final_path/restart-services.sh"
ynh_add_config --template="dont-code-sudoers" --destination="/etc/sudoers.d/$app-sudoers"
chown root:root "/etc/sudoers.d/$app-sudoers"
chmod o-rwx,gu=r "/etc/sudoers.d/$app-sudoers"
fi
#=================================================
# UPDATE A CONFIG FILE
#=================================================
# ynh_script_progression --message="Updating a configuration file..." --weight=1
### Same as during install
###
### The file will automatically be backed-up if it's found to be manually modified (because
### ynh_add_config keeps track of the file's checksum)
# ynh_add_config --template="some_config_file" --destination="$final_path/some_config_file"
# FIXME: this should be handled by the core in the future
# You may need to use chmod 600 instead of 400,
# for example if the app is expected to be able to modify its own config
# chmod 400 "$final_path/some_config_file"
# chown $app:$app "$final_path/some_config_file"
### For more complex cases where you want to replace stuff using regexes,
### you shoud rely on ynh_replace_string (which is basically a wrapper for sed)
### When doing so, you also need to manually call ynh_store_file_checksum
###
### ynh_replace_string --match_string="match_string" --replace_string="replace_string" --target_file="$final_path/some_config_file"
### ynh_store_file_checksum --file="$final_path/some_config_file"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
# Create a dedicated systemd config for each service
service_list_length=${#SERVICES_LIST[@]}
for (( j=0; j<${service_list_length}; j++ ))
do
service_name=${SERVICES_LIST[$j]}
port=${PORT_LIST[$j]}
ynh_add_systemd_config --service="${app}-${service_name}"
done
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1
# 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..." --weight=1
for service_name in "${SERVICES_LIST[@]}"
do
yunohost service add "${app}-${service_name}" --description="Dont-code platform ${service_name} service" --log="/var/log/${app}/${service_name}-${app}.log"
done
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting systemd services..." --weight=1
for service_name in "${SERVICES_LIST[@]}"
do
ynh_systemd_action --service_name="${app}-${service_name}" --action="start" --log_path="/var/log/$app/$app.log"
done
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last