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/install

219 lines
8.1 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
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH #TODO: Check if possible with wireguard_ui to use sub path
2020-10-11 14:45:13 +02:00
admin=$YNH_APP_ARG_ADMIN
is_public=$YNH_APP_ARG_IS_PUBLIC # Forced to use it to pass root installation check as public
2020-10-11 14:45:13 +02:00
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1
2020-10-11 14:45:13 +02:00
final_path=/opt/yunohost/$app
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..." --weight=1
2020-10-11 14:45:13 +02:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=admin --value=$admin
ynh_app_setting_set --app=$app --key=is_public --value=$is_public # Forced to use it to pass root installation check as public
2020-10-11 14:45:13 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Configuring firewall..." --weight=1
2020-10-11 14:45:13 +02:00
# Find an available port for WireGuard
port_wg=$(ynh_find_port --port=8095)
ynh_app_setting_set --app=$app --key=port_wg --value=$port_wg
# Find an available port for WireGuard UI
port=$(ynh_find_port --port=8096)
2020-10-11 14:45:13 +02:00
ynh_app_setting_set --app=$app --key=port --value=$port
# Open the WireGuard port
ynh_exec_warn_less yunohost firewall allow --no-upnp UDP $port_wg
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=7
2020-10-11 14:45:13 +02:00
2021-01-11 23:16:53 +01:00
# Add buster-backports repo
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
ynh_pin_repo --package="wireguard*" --pin="origin deb http://deb.debian.org/debian buster-backports main" --priority=995 --name="$app"
# Update the list of package with the new repo
ynh_package_update
ynh_add_app_dependencies --package="$pkg_dependencies"
2021-01-11 23:16:53 +01:00
# Remove buster-backports repo and pin-priority
ynh_remove_extra_repo --name=$app
2020-10-11 14:45:13 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=1
2020-10-11 14:45:13 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path" --source_id="$(ynh_detect_arch)"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring nginx web server..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --weight=1
2020-10-11 14:45:13 +02:00
# Create a system user
ynh_system_user_create --username=$app
2021-01-11 22:20:30 +01:00
# Ensure the system user has enough permissions
install -b -o root -g root -m 0440 ../conf/sudoers.conf /etc/sudoers.d/${app}_ynh
ynh_replace_string "__USER__" "${app}" /etc/sudoers.d/${app}_ynh
2020-10-11 14:45:13 +02:00
#=================================================
# SPECIFIC SETUP
#=================================================
# CONFIGURING WIREGUARD
#=================================================
# Create db directory for securing it later
mkdir -p $final_path/db/server
# Add interface configuration file for the Web UI
cp ../conf/interfaces.json $final_path/db/server/interfaces.json
ynh_replace_string --match_string="__PORT_WG__" --replace_string="$port_wg" --target_file="$final_path/db/server/interfaces.json"
2021-01-01 21:49:12 +01:00
# Create WireGuard configuration directory
2020-10-11 14:45:13 +02:00
mkdir -p /etc/wireguard
# Add interace configuration file for WireGuard
cp ../conf/wg0.conf /etc/wireguard/wg0.conf
ynh_replace_string --match_string="__PORT_WG__" --replace_string="$port_wg" --target_file="/etc/wireguard/wg0.conf"
ynh_replace_string --match_string="__PRIVATE_KEY__" --replace_string="$(wg genkey)" --target_file="/etc/wireguard/wg0.conf"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --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 --others_var="port"
# Create a dedicated systemd config for monitoring WireGuard's configuration
cp ../conf/wireguard.path /etc/systemd/system/wireguard.path
systemctl enable --quiet wireguard.path
2020-10-11 14:45:13 +02:00
# Create a dedicated systemd config for restarting WireGuard
ynh_add_systemd_config --service=wireguard --template=wireguard.service --others_var="port_wg"
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions to app files
chown -R root: $final_path
chown -R $app: $final_path/db
chmod -R 750 $final_path/db
chown -R $app:$app /etc/wireguard
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=1
2020-10-11 14:45:13 +02:00
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2020-10-11 14:45:13 +02:00
2021-01-01 13:54:05 +01:00
yunohost service add wireguard --description "WireGuard" --needs_exposed_ports $port_wg --test_status "wg show | grep wg0"
2020-10-11 14:45:13 +02:00
yunohost service add wireguard_ui --description "WireGuard UI" --log "/var/log/$app/ui.log"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
2020-10-11 14:45:13 +02:00
# Start a systemd service
ynh_systemd_action --service_name=wireguard_ui --action="start" --log_path="/var/log/$app/ui.log"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring permissions..." --weight=1
2020-10-11 14:45:13 +02:00
# Make app public if necessary to pass root installation check
if [ $is_public -eq 1 ]
then
ynh_permission_update --permission "main" --add visitors
else
ynh_permission_update --permission "main" --remove "all_users" --add "$admin"
fi
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="Installation of $app completed" --last