mirror of
https://github.com/YunoHost-Apps/wireguard_ynh.git
synced 2024-09-03 20:35:58 +02:00
212 lines
7.9 KiB
Bash
212 lines
7.9 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
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=$(ynh_app_setting_get --app=$app --key=port)
|
|
#REMOVEME? port_wg=$(ynh_app_setting_get --app=$app --key=port_wg)
|
|
#REMOVEME? #REMOVEME? install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
|
|
architecture=$YNH_ARCH
|
|
|
|
#=================================================
|
|
# CHECK VERSION
|
|
#=================================================
|
|
|
|
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
|
|
|
|
#=================================================
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
#=================================================
|
|
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
|
|
|
|
# If install_dir doesn't exist, create it
|
|
if [ -z "$install_dir" ]; then
|
|
#REMOVEME? install_dir=/opt/yunohost/$app
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=install_dir --value=$install_dir
|
|
fi
|
|
|
|
# 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
|
|
#REMOVEME? port=$(ynh_find_port --port=$(($port_wg+1)))
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=port --value=$port
|
|
# Let's remove the unused is_public key too
|
|
ynh_app_setting_delete --app=$app --key=is_public
|
|
fi
|
|
|
|
# WireGuard UI should be private, really.
|
|
if ynh_permission_has_user --permission=main --user=visitors
|
|
then
|
|
#REMOVEME? ynh_permission_update --permission=main --remove=visitors
|
|
fi
|
|
|
|
# Drop sudoers file if present
|
|
if [ -f "/etc/sudoers.d/${app}_ynh" ]; then
|
|
ynh_secure_remove /etc/sudoers.d/${app}_ynh
|
|
fi
|
|
|
|
# Remove deprecated services
|
|
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"
|
|
fi
|
|
|
|
# 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
|
|
|
|
#=================================================
|
|
# STANDARD UPGRADE STEPS
|
|
#=================================================
|
|
# STOP SYSTEMD SERVICE
|
|
#=================================================
|
|
ynh_script_progression --message="Stopping a systemd service..." --weight=1
|
|
|
|
ynh_systemd_action --service_name=wireguard_ui --action="stop" --line_match="Stopped WireGuard UI" --log_path="systemd" --timeout=30
|
|
ynh_systemd_action --service_name=wg-quick@wg0 --action="stop"
|
|
|
|
#=================================================
|
|
# 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
|
|
|
|
#=================================================
|
|
# 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="$install_dir/wireguard-ui.env"
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1
|
|
|
|
# Create a dedicated nginx config
|
|
ynh_add_nginx_config
|
|
|
|
#=================================================
|
|
# UPGRADE DEPENDENCIES
|
|
#=================================================
|
|
#REMOVEME? ynh_script_progression --message="Upgrading dependencies..." --weight=7
|
|
|
|
#REMOVEME? ynh_install_app_dependencies "$pkg_dependencies"
|
|
|
|
#=================================================
|
|
# 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
|
|
|
|
#=================================================
|
|
# SETUP SYSTEMD
|
|
#=================================================
|
|
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
|
|
|
|
# Create a dedicated systemd config for the web UI
|
|
ynh_add_systemd_config --service=wireguard_ui --template=wireguard_ui.service
|
|
|
|
# Create dedicated systemd configs for starting and monitoring WireGuard's configuration
|
|
cp ../conf/wireguard@.service /etc/systemd/system/wireguard@.service
|
|
cp ../conf/wireguard@.path /etc/systemd/system/wireguard@.path
|
|
systemctl daemon-reload
|
|
systemctl enable --quiet wireguard@wg0.service
|
|
systemctl enable --quiet --now wireguard@wg0.path
|
|
|
|
#=================================================
|
|
# ENABLE PORT FORWARDING
|
|
#=================================================
|
|
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
|
|
|
|
#=================================================
|
|
# GENERIC FINALIZATION
|
|
#=================================================
|
|
# SECURE FILES AND DIRECTORIES
|
|
#=================================================
|
|
|
|
# Set permissions to app files
|
|
chmod 750 "$install_dir"
|
|
chmod -R o-rwx "$install_dir"
|
|
chown -R $app: "$install_dir"
|
|
|
|
chmod 750 /etc/wireguard
|
|
chmod -R o-rwx /etc/wireguard
|
|
chown -R $app: /etc/wireguard
|
|
|
|
#=================================================
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
#=================================================
|
|
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
|
|
|
|
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"
|
|
|
|
#=================================================
|
|
# START SYSTEMD SERVICE
|
|
#=================================================
|
|
ynh_script_progression --message="Starting the systemd service for the UI..." --weight=1
|
|
|
|
# Start a systemd service
|
|
ynh_systemd_action --service_name=wireguard_ui --action="start" --line_match="http server started" --log_path="systemd" --timeout=30
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
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. You may need to reboot your server before being able to start the WireGuard service." --last
|