1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/wireguard_ynh.git synced 2024-09-03 20:35:58 +02:00
wireguard_ynh/scripts/upgrade

213 lines
7.7 KiB
Text
Raw Normal View History

2020-10-11 14:45:13 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1
2020-10-11 14:45:13 +02:00
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=$(ynh_app_setting_get --app=$app --key=port)
port_wg=$(ynh_app_setting_get --app=$app --key=port_wg)
2020-10-11 14:45:13 +02:00
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
2021-12-09 12:31:02 +01:00
architecture=$YNH_ARCH
2020-10-11 14:45:13 +02:00
#=================================================
# CHECK VERSION
#=================================================
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
2020-10-11 14:45:13 +02:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
2020-10-11 14:45:13 +02:00
# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
final_path=/opt/yunohost/$app
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
fi
2021-01-21 19:39:11 +01:00
# Change port if WireGuard port is the same as WireGuard UI's
if [ $port -eq $port_wg ]
then
ynh_app_setting_delete --app=$app --key=port
2021-01-21 19:39:11 +01:00
port=$(ynh_find_port --port=$(($port_wg+1)))
ynh_app_setting_set --app=$app --key=port --value=$port
2021-01-21 19:55:16 +01:00
# Let's remove the unused is_public key too
ynh_app_setting_delete --app=$app --key=is_public
fi
2021-01-21 21:35:22 +01:00
# WireGuard UI should be private, really.
2021-01-21 22:04:04 +01:00
if ynh_permission_has_user --permission=main --user=visitors
then
2021-01-21 21:35:22 +01:00
ynh_permission_update --permission=main --remove=visitors
fi
2021-12-21 17:48:41 +01:00
# Drop sudoers file if present
2021-01-11 22:20:30 +01:00
if [ -f "/etc/sudoers.d/${app}_ynh" ]; then
2021-12-21 17:48:41 +01:00
ynh_secure_remove /etc/sudoers.d/${app}_ynh
2021-01-11 22:20:30 +01:00
fi
2021-03-21 14:35:36 +01:00
# Remove deprecated services
2021-12-22 10:37:22 +01:00
if systemctl list-units --full -all | grep -Fq "wireguard_ui_conf.path"; then
systemctl disable --now --quiet wireguard_ui_conf.path
ynh_secure_remove --file="/etc/systemd/system/wireguard_ui_conf.path"
fi
if systemctl list-units --full -all | grep -Fq "wireguard_ui_conf.service"; then
systemctl disable --now --quiet wireguard_ui_conf.service
ynh_secure_remove --file="/etc/systemd/system/wireguard_ui_conf.service"
2021-03-21 14:35:36 +01:00
fi
2022-02-20 19:13:48 +01:00
# Remove the service integration from an older version
if ynh_exec_warn_less yunohost service status wireguard@wg0 >/dev/null
then
yunohost service remove wireguard@wg0
fi
2020-10-11 14:45:13 +02:00
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=1
2020-10-11 14:45:13 +02:00
2021-01-21 22:15:43 +01:00
ynh_systemd_action --service_name=wireguard_ui --action="stop" --line_match="Stopped WireGuard UI" --log_path="systemd" --timeout=30
2022-02-20 19:13:48 +01:00
ynh_systemd_action --service_name=wg-quick@wg0 --action="stop"
2020-10-11 14:45:13 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --weight=1
2020-10-11 14:45:13 +02:00
# Download, check integrity, uncompress and patch the source from app.src
2021-12-09 12:31:02 +01:00
ynh_setup_source --dest_dir="$final_path" --source_id="$architecture"
2020-10-11 14:45:13 +02:00
fi
2022-01-29 12:57:32 +01:00
#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a configuration file..." --weight=1
# Update configuration for the Web UI
ynh_add_config --template="../conf/wireguard-ui.env" --destination="$final_path/wireguard-ui.env"
2020-10-11 14:45:13 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2021-12-09 12:31:02 +01:00
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=7
2020-10-11 14:45:13 +02:00
ynh_install_app_dependencies "$pkg_dependencies"
2020-10-11 14:45:13 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a dedicated systemd config for the web UI
ynh_add_systemd_config --service=wireguard_ui --template=wireguard_ui.service
2020-10-11 14:45:13 +02:00
2021-12-26 11:51:25 +01:00
# Create dedicated systemd configs for starting and monitoring WireGuard's configuration
2021-12-22 10:37:22 +01:00
cp ../conf/wireguard@.service /etc/systemd/system/wireguard@.service
2021-12-26 11:51:25 +01:00
cp ../conf/wireguard@.path /etc/systemd/system/wireguard@.path
systemctl daemon-reload
2021-12-22 10:37:22 +01:00
systemctl enable --quiet wireguard@wg0.service
2021-12-26 11:51:25 +01:00
systemctl enable --quiet --now wireguard@wg0.path
2020-10-11 14:45:13 +02:00
#=================================================
2021-12-30 17:06:32 +01:00
# ENABLE PORT FORWARDING
2020-10-11 14:45:13 +02:00
#=================================================
2021-12-30 17:06:32 +01:00
ynh_script_progression --message="Enabling port forwarding..." --weight=1
ynh_add_config --template="../conf/sysctl.conf" --destination="/etc/sysctl.d/$app.conf"
sysctl -p /etc/sysctl.d/$app.conf
2020-10-11 14:45:13 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2021-07-04 21:43:22 +02:00
# Set permissions to app files
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app: "$final_path"
chmod 750 /etc/wireguard
chmod -R o-rwx /etc/wireguard
chown -R $app: /etc/wireguard
2020-10-11 14:45:13 +02:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2020-10-11 14:45:13 +02:00
2022-02-20 19:13:48 +01:00
yunohost service add wg-quick@wg0 --description="WireGuard VPN" --needs_exposed_ports="$port_wg" --test_status="wg show | grep wg0"
yunohost service add wireguard_ui --description="WireGuard UI"
2020-10-11 14:45:13 +02:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
2021-12-21 17:57:39 +01:00
ynh_script_progression --message="Starting the systemd service for the UI..." --weight=1
2020-10-11 14:45:13 +02:00
# Start a systemd service
2021-01-21 22:15:43 +01:00
ynh_systemd_action --service_name=wireguard_ui --action="start" --line_match="http server started" --log_path="systemd" --timeout=30
2021-12-21 17:57:39 +01:00
2020-10-11 14:45:13 +02:00
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --weight=1
2020-10-11 14:45:13 +02:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed. You may need to reboot your server before being able to start the WireGuard service." --last