2022-12-27 17:59:56 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC START
|
|
|
|
#=================================================
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
source _common.sh
|
2023-01-02 19:03:27 +01:00
|
|
|
source ynh_mongo_db__2
|
|
|
|
source ynh_java
|
2022-12-27 17:59:56 +01:00
|
|
|
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)
|
2023-01-02 19:03:27 +01:00
|
|
|
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)
|
2022-12-27 17:59:56 +01:00
|
|
|
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
|
2023-01-02 19:03:27 +01:00
|
|
|
document_path=$(ynh_app_setting_get --app=$app --key=document_path)
|
2023-01-03 14:51:52 +01:00
|
|
|
html_path=$(ynh_app_setting_get --app=$app --key=html_path)
|
2023-01-02 19:03:27 +01:00
|
|
|
|
|
|
|
PORT_LIST=($port_ide $port_preview $port_project $port_data)
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
|
|
|
#=================================================
|
2023-01-02 19:03:27 +01:00
|
|
|
# STOP SYSTEMD SERVICES
|
2022-12-27 17:59:56 +01:00
|
|
|
#=================================================
|
2023-01-02 19:03:27 +01:00
|
|
|
ynh_script_progression --message="Stopping systemd services..." --weight=1
|
2022-12-27 17:59:56 +01:00
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
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
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
|
|
#=================================================
|
2023-01-03 14:51:52 +01:00
|
|
|
|
|
|
|
# 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
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CREATE DEDICATED USER
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
|
|
|
|
|
|
|
|
# Create a dedicated user (if not existing)
|
2023-01-02 19:03:27 +01:00
|
|
|
ynh_system_user_create --username=$app --home_dir="$final_path" --use_shell --groups="ssh.app"
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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"
|
2023-01-02 19:03:27 +01:00
|
|
|
|
|
|
|
# No need to upgrade the .ssh authorization files
|
2022-12-27 17:59:56 +01:00
|
|
|
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"
|
2023-01-02 19:03:27 +01:00
|
|
|
chown -R $app:$app "$final_path"
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# UPGRADE DEPENDENCIES
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Upgrading dependencies..." --weight=1
|
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
# Install the required version of Java
|
|
|
|
ynh_install_java --java_version=$java_version --jdk_type=jre
|
2022-12-27 17:59:56 +01:00
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
2022-12-27 17:59:56 +01:00
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
# Install the required version of Mongo
|
|
|
|
ynh_install_mongo --mongo_version=$mongo_version
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# NGINX CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Upgrading NGINX web server configuration..." --time --weight=1
|
|
|
|
|
2023-01-03 14:51:52 +01:00
|
|
|
# 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"
|
|
|
|
|
2022-12-27 17:59:56 +01:00
|
|
|
# Create a dedicated NGINX config
|
|
|
|
ynh_add_nginx_config
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SPECIFIC UPGRADE
|
|
|
|
#=================================================
|
2023-01-02 19:03:27 +01:00
|
|
|
# Create document storage
|
2022-12-27 17:59:56 +01:00
|
|
|
#=================================================
|
2023-01-02 19:03:27 +01:00
|
|
|
# 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" --time --weight=1
|
|
|
|
|
|
|
|
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_old_doc_path" --replace_string="$document_document_path" --target_file="$final_path/.env"
|
|
|
|
|
|
|
|
# Changed the document url as well
|
|
|
|
old_document_url=$document_url
|
|
|
|
document_url=https://${domain}${path_url}/docs
|
|
|
|
ynh_app_setting_set --app=$app --key=document_url --value=$document_url
|
|
|
|
|
|
|
|
ynh_replace_string --match_string="$old_document_url" --replace_string="$document_url" --target_file="$final_path/.env"
|
|
|
|
ynh_store_file_checksum --file="$final_path/.env"
|
|
|
|
|
|
|
|
fi
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# UPDATE A CONFIG FILE
|
|
|
|
#=================================================
|
2023-01-02 19:03:27 +01:00
|
|
|
# ynh_script_progression --message="Updating a configuration file..." --weight=1
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
### 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)
|
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
# ynh_add_config --template="some_config_file" --destination="$final_path/some_config_file"
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
# 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
|
2023-01-02 19:03:27 +01:00
|
|
|
# chmod 400 "$final_path/some_config_file"
|
|
|
|
# chown $app:$app "$final_path/some_config_file"
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
### 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
|
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
# 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
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
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
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# START SYSTEMD SERVICE
|
|
|
|
#=================================================
|
2023-01-02 19:03:27 +01:00
|
|
|
ynh_script_progression --message="Starting systemd services..." --weight=1
|
2022-12-27 17:59:56 +01:00
|
|
|
|
2023-01-02 19:03:27 +01:00
|
|
|
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
|
2022-12-27 17:59:56 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|