mirror of
https://github.com/YunoHost-Apps/dont-code_ynh.git
synced 2024-09-03 18:26:34 +02:00
295 lines
11 KiB
Bash
295 lines
11 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
source ynh_mongo_db__2
|
|
source ynh_install_java
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# LOAD SETTINGS
|
|
#=================================================
|
|
#REMOVEME? ynh_script_progression --message="Loading installation settings..." --weight=1
|
|
|
|
#REMOVEME? app=$YNH_APP_INSTANCE_NAME
|
|
|
|
#REMOVEME? domain=$(ynh_app_setting_get --app=$app --key=domain)
|
|
#REMOVEME? path=$(ynh_app_setting_get --app=$app --key=path)
|
|
#REMOVEME? port_ide=$(ynh_app_setting_get --app=$app --key=port_ide)
|
|
#REMOVEME? port_preview=$(ynh_app_setting_get --app=$app --key=port_preview)
|
|
#REMOVEME? port_project=$(ynh_app_setting_get --app=$app --key=port_project)
|
|
#REMOVEME? port_data=$(ynh_app_setting_get --app=$app --key=port_data)
|
|
#REMOVEME? db_user=$(ynh_app_setting_get --app=$app --key=db_user)
|
|
#REMOVEME? #REMOVEME? install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
|
|
#REMOVEME? document_path=$(ynh_app_setting_get --app=$app --key=document_path)
|
|
#REMOVEME? document_url=$(ynh_app_setting_get --app=$app --key=document_url)
|
|
#REMOVEME? html_path=$(ynh_app_setting_get --app=$app --key=html_path)
|
|
#REMOVEME? 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
|
|
#=================================================
|
|
#REMOVEME? ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=1
|
|
|
|
# Backup the current version of the app
|
|
#REMOVEME? ynh_backup_before_upgrade
|
|
#REMOVEME? ynh_clean_setup () {
|
|
# Restore it if the upgrade fails
|
|
#REMOVEME? ynh_restore_upgradebackup
|
|
}
|
|
# Exit if an error occurs during the execution of the script
|
|
#REMOVEME? 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
|
|
#REMOVEME? 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
|
|
#=================================================
|
|
#REMOVEME? ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
|
|
|
|
# Create a dedicated user (if not existing)
|
|
#REMOVEME? ynh_system_user_create --username=$app --home_dir="$install_dir" --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="$install_dir"
|
|
|
|
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 "$install_dir"
|
|
chmod -R o-rwx "$install_dir"
|
|
chown -R $app:$app "$install_dir"
|
|
|
|
#=================================================
|
|
# UPGRADE DEPENDENCIES
|
|
#=================================================
|
|
#REMOVEME? ynh_script_progression --message="Upgrading dependencies..." --weight=1
|
|
|
|
# Install the required version of Java
|
|
ynh_install_java --java_version=$java_version --jdk_type=jre
|
|
|
|
#REMOVEME? ynh_install_app_dependencies $pkg_dependencies
|
|
|
|
# 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
|
|
#REMOVEME? 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="$install_dir/.env"
|
|
ynh_replace_string --match_string="$old_doc_path" --replace_string="$document_path" --target_file="$install_dir/.env"
|
|
ynh_store_file_checksum --file="$install_dir/.env"
|
|
|
|
fi
|
|
|
|
# Regenerate the .env file if the document_url was incorrectly set before
|
|
correct_document_url=$(append_uri "https://${domain}${path}" "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="$install_dir/.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 "$install_dir/.env"
|
|
chown $app:$app "$install_dir/.env"
|
|
fi
|
|
|
|
if [ -n "$public_key" ] && [ ! -f "$install_dir/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="$install_dir/restart-services.sh"
|
|
|
|
# Enable root ownership to be able to call systemctl
|
|
chown $app:$app "$install_dir/restart-services.sh"
|
|
chmod o-rwx,gu=rwx "$install_dir/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="$install_dir/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 "$install_dir/some_config_file"
|
|
# chown $app:$app "$install_dir/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="$install_dir/some_config_file"
|
|
### ynh_store_file_checksum --file="$install_dir/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
|
|
#=================================================
|
|
#REMOVEME? ynh_script_progression --message="Reloading NGINX web server..." --weight=1
|
|
|
|
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Upgrade of $app completed" --last
|