1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/hotspot_ynh.git synced 2024-09-03 19:25:53 +02:00
hotspot_ynh/scripts/upgrade

210 lines
7 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping the hotspot service ... (this may take some time)"
yunohost service stop $service_name
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
if [ -d /var/www/wifiadmin/ ]; then
ynh_secure_remove /var/www/wifiadmin/
fi
if [ -e /etc/dnsmasq.d/$app.conf ]; then
ynh_secure_remove /etc/dnsmasq.d/$app.conf
fi
if [ ${firmware_nonfree:-} = "yes" ]; then
firmware_nonfree=1
ynh_app_setting_set --app=$app --key=firmware_nonfree --value=$firmware_nonfree
elif [ ${firmware_nonfree:-} = "no" ]; then
firmware_nonfree=0
ynh_app_setting_set --app=$app --key=firmware_nonfree --value=$firmware_nonfree
fi
if [ -z ${service_name:-} ]; then
service_name="ynh-$app"
ynh_app_setting_set --app=$app --key=service_name --value=$service_name
fi
if [[ -n "${multissid:-}" ]] && [[ "${multissid}" -gt 1 ]]; then
wifi_ssid=$(cut -d'|' -f 1 <<< ${wifi_ssid})
wifi_secure=$(cut -d'|' -f 1 <<< ${wifi_secure})
wifi_passphrase=$(cut -d'|' -f 1 <<< ${wifi_passphrase})
advanced=$(cut -d'|' -f 1 <<< ${advanced})
ip4_nat_prefix=$(cut -d'|' -f 1 <<< ${ip4_nat_prefix})
ip6_net=$(cut -d'|' -f 1 <<< ${ip6_net})
ip6_firewall=$(cut -d'|' -f 1 <<< ${ip6_firewall})
dns=$(cut -d'|' -f 1 <<< ${dns})
ynh_app_setting_set --app=$app --key=wifi_ssid --value="${wifi_ssid}"
ynh_app_setting_set --app=$app --key=wifi_secure --value="${wifi_secure}"
ynh_app_setting_set --app=$app --key=wifi_passphrase --value="${wifi_passphrase}"
ynh_app_setting_set --app=$app --key=ip4_nat_prefix --value="${ip4_nat_prefix}"
ynh_app_setting_set --app=$app --key=ip6_net --value="${ip6_net}"
ynh_app_setting_set --app=$app --key=ip6_firewall --value="${ip6_firewall}"
fi
if [[ -n "${dns:-}" ]]; then
ip6_dns=""
ip4_dns=""
for ip in $(echo "${dns}" | tr ',' ' '); do
if [[ "$ip" == *":"* ]]; then
ip6_dns+="[$ip],"
else
ip4_dns+="$ip,"
fi
done
# Remove trailing ,
ip6_dns="${ip6_dns%%,}"
ip4_dns="${ip4_dns%%,}"
if [[ -z "$(ynh_app_setting_get --app=$app --key=ip6_dns)" ]]; then
ynh_app_setting_set --app=$app --key=ip6_dns --value="${ip6_dns}"
fi
if [[ -z "$(ynh_app_setting_get --app=$app --key=ip4_dns)" ]]; then
ynh_app_setting_set --app=$app --key=ip4_dns --value="${ip4_dns}"
fi
ynh_app_setting_delete $app dns
fi
if [[ -n "${multissid:-}" ]]; then
ynh_app_setting_delete --app=$app --key=multissid
ynh_secure_remove --file="/etc/hostapd/hostapd.conf"
ynh_secure_remove --file="/etc/hostapd/hostapd.base.conf"
ynh_secure_remove --file="/etc/hostapd/hostapd.accesspoint.conf"
ynh_secure_remove --file="/etc/dnsmasq.dhcpd/"
fi
if [[ -z "${advanced:-}" ]]; then
ynh_app_setting_set --app=$app --key=advanced --value=0
fi
# Old stuff prior to 2.x
if [ "${ip6_net:-}" == "none" ]
then
ip6_net=""
ynh_app_setting_set --app=$app --key=ip6_net --value="$ip6_net"
fi
if [ -f /etc/nginx/conf.d/*.d/$app.conf ]; then
ynh_secure_remove /etc/nginx/conf.d/*.d/$app.conf
ynh_systemd_action --service_name=nginx --action=reload
fi
for php_path in $(ls /etc/php/*/fpm/pool.d/$app.conf 2> /dev/null); do
ynh_secure_remove $php_path
done
if [ -d /var/www/$app ]; then
ynh_secure_remove /var/www/$app
fi
[ -z "${domain:-}" ] || ynh_app_setting_delete $app domain
[ -z "${path:-}" ] || ynh_app_setting_delete $app path
[ -z "${install_dir:-}" ] || ynh_app_setting_delete $app install_dir
if [ -e "/etc/sudoers.d/${app}_ynh" ]; then
ynh_secure_remove "/etc/sudoers.d/${app}_ynh"
fi
if systemctl -q is-enabled hostapd
then
# Disable hostapd, we'll use hostapd@$app instead (for multissid support etc)
systemctl disable hostapd --quiet 2>&1
systemctl stop hostapd 2>&1
systemctl mask hostapd 2>&1
fi
#=================================================
# SPECIFIC UPGRADE
#=================================================
# COPY CONFIGS
#=================================================
ynh_script_progression --message="Copying configuration..."
hot_reload_usb_wifi_cards
if [[ -z "$wifi_device" ]] || ! grep -q -F "$wifi_device" <(unused_iw_devices); then
wifi_device="$(unused_iw_devices | head -n 1)"
ynh_app_setting_set --app=$app --key=wifi_device --value="${wifi_device}"
fi
mkdir -pm 0755 /etc/hostapd/$app/
chown root: /etc/hostapd/$app/
mkdir -pm 0755 /etc/dnsmasq.$app/
chown root: /etc/dnsmasq.$app/
if [[ -n "${wifi_device:-}" ]]; then
configure_hostapd
configure_dhcp
ynh_app_setting_set --app=$app --key=service_enabled --value=1
else
ynh_app_setting_set --app=$app --key=service_enabled --value=0
wifi_device=""
fi
# Copy init script
ynh_add_config --template="../conf/ynh-hotspot" --destination="/usr/local/bin/$service_name"
chmod 0755 "/usr/local/bin/$service_name"
# Copy openvpn scripts
mkdir -pm 0755 /etc/openvpn/scripts
mkdir -pm 0755 /etc/openvpn/scripts/route-up.d
mkdir -pm 0755 /etc/openvpn/scripts/route-down.d
ynh_add_config --template="../conf/openvpn_90-hotspot" --destination="/etc/openvpn/scripts/route-up.d/90-$service_name"
ynh_add_config --template="../conf/openvpn_90-hotspot" --destination="/etc/openvpn/scripts/route-down.d/90-$service_name"
chmod 0755 "/etc/openvpn/scripts/route-up.d/90-${service_name}"
chmod 0755 "/etc/openvpn/scripts/route-down.d/90-${service_name}"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..."
# Create a dedicated systemd config
ynh_add_systemd_config --service=$service_name
# Create custom systemd config for hostapd to handle multiple wifi devices
ynh_add_systemd_config --service="hostapd@$app" --template="../conf/systemd_hostapd.service"
yunohost service add $service_name --description "Creates a Wi-Fi access point" --test_status "systemctl is-active hostapd@$app" --need_lock
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting the hotspot service..."
# Start a systemd service if device is present
if [[ $wifi_device == "" ]]; then
echo "WARNING: Wifi Hotspot is not started because no wifi device was found (check the Hotspot configuration in the webadmin > Applications > Hotspot > the config panel)" >&2
else
yunohost service start $service_name
fi
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed"