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
#=================================================
2020-12-30 20:26:03 +01:00
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)
2020-12-30 11:21:59 +01:00
port=$(ynh_app_setting_get --app=$app --key=port)
2020-12-29 18:07:35 +01:00
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)
2021-06-20 01:33:15 +02:00
#=================================================
# 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
#=================================================
2020-12-30 20:26:03 +01:00
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 ]
2020-12-30 12:48:17 +01:00
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)))
2020-12-30 12:48:17 +01:00
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
2020-12-30 12:48:17 +01:00
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-01-09 08:45:54 +01:00
# Downgrade linux-image-$arch if updated to the buster-backports version
2021-12-12 17:59:22 +01:00
linuximage_version=$(ynh_package_version --package=linux-image-$architecture)
2021-01-09 08:45:54 +01:00
if [[ $linuximage_version == *"bpo10"* ]]
then
2021-01-10 10:57:05 +01:00
# Downgrading using ynh_package_install apt command without "--no-remove" and with "--allow-downgrades"
2021-01-09 11:18:28 +01:00
# It will remove wireguard-ynh-deps and wireguard but they will be reinstalled throught upgrade process
2021-01-09 11:24:27 +01:00
ynh_apt --allow-downgrades --option Dpkg::Options::=--force-confdef \
2021-12-12 17:59:22 +01:00
--option Dpkg::Options::=--force-confold install linux-image-$architecture/stable
2021-01-10 10:58:55 +01:00
#Remove backports kernel if running on it and send a mail to the admin to ask him to reboot
2021-01-10 11:17:34 +01:00
linuxkernel_version=$(uname -r)
2021-01-10 10:58:55 +01:00
if [[ $linuxkernel_version == *"bpo"* ]]
then
2021-01-10 11:17:34 +01:00
ynh_package_remove linux-image-$linuxkernel_version
2021-01-10 10:58:55 +01:00
echo "You've been using an experimental version of wireguard_ynh, which was using the backports version of the linux kernel.
Now wireguard_ynh use a DKMS module allowing itself to be used with the stable kernel, then the backports one was removed and a reboot is needed to go back to the stable one." > mail_to_send
ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="admin" --type=upgrade
fi
2021-01-09 08:45:54 +01:00
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"
yunohost service remove wireguard_ui_conf
2021-03-21 14:35:36 +01:00
fi
2020-10-11 14:45:13 +02:00
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
2020-12-30 20:26:03 +01:00
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
2021-12-22 10:37:22 +01:00
ynh_systemd_action --service_name=wireguard@wg0 --action="stop"
2020-10-11 14:45:13 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-30 20:26:03 +01:00
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
#=================================================
# 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
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Upgrading dependencies..." --weight=7
2020-10-11 14:45:13 +02:00
2020-12-22 22:06:35 +01:00
#TODO: remove buster-backports kernel
2021-03-20 18:36:54 +01:00
# Add buster-backports gpg key
ynh_install_repo_gpg --key="https://ftp-master.debian.org/keys/archive-key-10.asc" --name="$app"
2021-01-11 23:16:53 +01:00
# Add buster-backports repo
2020-12-22 22:06:35 +01:00
ynh_add_repo --uri="http://deb.debian.org/debian" --suite="buster-backports" --component="main" --name="$app"
2021-01-11 23:16:53 +01:00
# Add pin-priority for wireguard packages
2020-12-23 17:13:58 +01:00
ynh_pin_repo --package="wireguard*" --pin="origin deb http://deb.debian.org/debian buster-backports main" --priority=995 --name="$app"
2020-12-22 22:06:35 +01:00
# Update the list of package with the new repo
ynh_package_update
2021-12-22 17:24:56 +01:00
ynh_add_app_dependencies --package="$pkg_dependencies"
2020-12-22 22:06:35 +01:00
2021-01-11 23:16:53 +01:00
# Remove buster-backports repo and pin-priority
2020-12-22 22:06:35 +01:00
ynh_remove_extra_repo --name=$app
2020-10-11 14:45:13 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2020-12-30 20:26:03 +01:00
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
#=================================================
2020-12-30 20:26:03 +01:00
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
2021-06-20 01:52:05 +02:00
ynh_add_systemd_config --service=wireguard_ui --template=wireguard_ui.service
2020-10-11 14:45:13 +02:00
# Create a dedicated systemd config for monitoring WireGuard's configuration
2021-12-22 10:37:22 +01:00
cp ../conf/wireguard@.path /etc/systemd/system/wireguard@.path
systemctl enable --quiet wireguard@wg0.path
2020-10-11 14:45:13 +02:00
2021-06-20 01:52:05 +02:00
# Create a dedicated systemd config for restarting WireGuard when its configuration changes
2021-12-22 10:37:22 +01:00
cp ../conf/wireguard@.service /etc/systemd/system/wireguard@.service
systemctl enable --quiet wireguard@wg0.service
2021-06-20 01:52:05 +02:00
2020-10-11 14:45:13 +02:00
#=================================================
# CONFIGURING WIREGUARD
#=================================================
#=================================================
# 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
#=================================================
2020-12-30 20:26:03 +01:00
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2020-10-11 14:45:13 +02:00
2021-12-22 10:37:22 +01:00
yunohost service add wireguard@wg0 --description="WireGuard VPN" --needs_exposed_ports="$port_wg" --test_status="wg show | grep wg0"
2021-09-09 20:39:53 +02:00
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
#=================================================
2020-12-30 20:26:03 +01:00
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
#=================================================
2021-03-22 17:02:49 +01:00
ynh_script_progression --message="Upgrade of $app completed. You may need to reboot your server before being able to start the WireGuard service." --last