mirror of
https://github.com/YunoHost-Apps/pihole_ynh.git
synced 2024-09-03 20:05:58 +02:00
301 lines
12 KiB
Bash
301 lines
12 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
# Load common variables for all scripts.
|
|
source ../settings/scripts/_variables
|
|
|
|
source ../settings/scripts/_common.sh
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# MANAGE SCRIPT FAILURE
|
|
#=================================================
|
|
|
|
# Exit if an error occurs during the execution of the script
|
|
ynh_abort_if_errors
|
|
|
|
#=================================================
|
|
# LOAD SETTINGS
|
|
#=================================================
|
|
ynh_script_progression --message="Loading settings..." --weight=2
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
domain=$(ynh_app_setting_get --app=$app --key=domain)
|
|
path_url=$(ynh_app_setting_get --app=$app --key=path)
|
|
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
|
|
enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
|
|
admin=$(ynh_app_setting_get --app=$app --key=admin)
|
|
pihole_version="$(ynh_app_setting_get --app=$app --key=pihole_version)"
|
|
|
|
# Get variables from ynh_add_fpm_config
|
|
fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint)
|
|
fpm_usage=$(ynh_app_setting_get --app=$app --key=fpm_usage)
|
|
fpm_config_dir=$(ynh_app_setting_get --app=$app --key=fpm_config_dir)
|
|
fpm_service=$(ynh_app_setting_get --app=$app --key=fpm_service)
|
|
|
|
#=================================================
|
|
# CHECK IF THE APP CAN BE RESTORED
|
|
#=================================================
|
|
ynh_script_progression --message="Validating restoration parameters..."
|
|
|
|
ynh_webpath_available --domain=$domain --path_url=$path_url \
|
|
|| ynh_die --message="Path not available: ${domain}${path_url}"
|
|
test ! -d $final_path \
|
|
|| ynh_die --message="There is already a directory: $final_path "
|
|
|
|
#=================================================
|
|
# ACTIVATE MAINTENANCE MODE
|
|
#=================================================
|
|
ynh_script_progression --message="Activating maintenance mode..." --weight=2
|
|
|
|
ynh_maintenance_mode_ON
|
|
|
|
#=================================================
|
|
# STANDARD RESTORE STEPS
|
|
#=================================================
|
|
# RESTORE THE NGINX CONFIGURATION
|
|
#=================================================
|
|
|
|
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
|
|
|
|
#=================================================
|
|
# RESTORE THE MAIN DIRECTORIES OF THE APP
|
|
#=================================================
|
|
ynh_script_progression --message="Restoring the app main directory..."
|
|
|
|
ynh_restore_file --origin_path="$final_path"
|
|
|
|
ynh_restore_file --origin_path="/etc/.pihole"
|
|
|
|
ynh_restore_file --origin_path="/etc/pihole"
|
|
|
|
ynh_restore_file --origin_path="/opt/pihole"
|
|
|
|
#=================================================
|
|
# RECREATE THE DEDICATED USER
|
|
#=================================================
|
|
ynh_script_progression --message="Recreating the dedicated system user..." --weight=2
|
|
|
|
# Create the dedicated user (if not existing)
|
|
ynh_system_user_create --username=$app
|
|
|
|
#=================================================
|
|
# RESTORE USER RIGHTS
|
|
#=================================================
|
|
|
|
# Restore permissions on app files
|
|
chown $app: -R "/etc/pihole"
|
|
# /etc/pihole/logrotate have to belong to root, otherwise logrotate will failed silently...
|
|
chown root: -R "/etc/pihole/logrotate"
|
|
|
|
#=================================================
|
|
# RESTORE THE PHP-FPM CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Reconfiguring PHP-FPM..." --weight=7
|
|
|
|
# Restore the file first, so it can have a backup if different
|
|
ynh_restore_file --origin_path="$fpm_config_dir/php-fpm-$app.conf"
|
|
ynh_restore_file --origin_path="$fpm_config_dir/pool.d/$app.conf"
|
|
|
|
# Recreate a dedicated php-fpm config
|
|
ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --dedicated_service
|
|
|
|
#=================================================
|
|
# SPECIFIC RESTORE
|
|
#=================================================
|
|
# REINSTALL DEPENDENCIES
|
|
#=================================================
|
|
ynh_script_progression --message="Reinstalling dependencies..." --weight=12
|
|
|
|
ynh_install_app_dependencies $app_depencencies
|
|
|
|
#=================================================
|
|
# ADVERTISE SERVICE IN ADMIN PANEL
|
|
#=================================================
|
|
|
|
yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log"
|
|
|
|
#=================================================
|
|
# RESTORE THE CRON FILE
|
|
#=================================================
|
|
|
|
ynh_restore_file --origin_path="/etc/cron.d/pihole"
|
|
|
|
#=================================================
|
|
# RECREATE LOG FILES
|
|
#=================================================
|
|
|
|
touch /var/log/pihole.log
|
|
chmod 644 /var/log/pihole.log
|
|
dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
|
|
chown $dnsmasq_user:root /var/log/pihole.log
|
|
|
|
#=================================================
|
|
# RESTORE SPECIFIC FILES
|
|
#=================================================
|
|
ynh_script_progression --message="Restoring specific files..."
|
|
|
|
ynh_restore_file --origin_path="/usr/local/bin/pihole"
|
|
ynh_restore_file --origin_path="/etc/bash_completion.d/pihole"
|
|
|
|
ynh_restore_file --origin_path="/etc/sudoers.d/pihole"
|
|
|
|
ynh_restore_file --origin_path="/etc/init.d/pihole-FTL"
|
|
ynh_restore_file --origin_path="/usr/bin/pihole-FTL"
|
|
|
|
ynh_restore_file --origin_path="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
|
|
|
|
#=================================================
|
|
# REPLACE THE DEFAULT DNSMASQ BY PIHOLE-FTL
|
|
#=================================================
|
|
|
|
if [ "$pihole_version" == "Last available" ]
|
|
then
|
|
# Last version available
|
|
# Stopped dnsmasq to replace it by pihole-FTL
|
|
ynh_systemd_action --action=stop --service_name=dnsmasq
|
|
|
|
# Disable the real dnsmasq service
|
|
ynh_exec_warn_less systemctl disable dnsmasq --quiet
|
|
|
|
# And move the files that make the service available in systemd to really disable it
|
|
mv /lib/systemd/system/dnsmasq.service /lib/systemd/system/.dnsmasq.service.backup_by_pihole
|
|
mv /etc/init.d/dnsmasq /etc/init.d/.dnsmasq.backup_by_pihole
|
|
|
|
# Move dnsmasq to preserve the current binary
|
|
mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.backup_by_pihole
|
|
# Replace dnsmasq by pihole-FTL
|
|
# NOTE: pihole-FTL is actually a modified version of dnsmasq
|
|
# https://github.com/pi-hole/FTL/tree/master/dnsmasq
|
|
ln -s /usr/bin/pihole-FTL /usr/sbin/dnsmasq
|
|
|
|
pihole_local_repo="/etc/.pihole"
|
|
cp -a $pihole_local_repo/advanced/Templates/pihole-FTL.service /etc/init.d/pihole-FTL
|
|
chmod +x /etc/init.d/pihole-FTL
|
|
ynh_exec_warn_less systemctl enable pihole-FTL --quiet
|
|
|
|
# Replace the service dnsmasq by pihole-FTL
|
|
# That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
|
|
ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
|
|
|
|
# Reload systemd config
|
|
systemctl daemon-reload
|
|
fi
|
|
|
|
#=================================================
|
|
# RESTORE DNSMASQ CONFIG
|
|
#=================================================
|
|
ynh_script_progression --message="Restoring Dnsmasq config..."
|
|
|
|
test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \
|
|
ynh_restore_file --origin_path="/etc/dnsmasq.d/03-pihole-wildcard.conf"
|
|
|
|
systemctl daemon-reload
|
|
ynh_exec_warn_less yunohost tools regen-conf dnsmasq
|
|
|
|
#=================================================
|
|
# CONFIGURE DNS FOR THE LOCAL DOMAINS
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring dns for the local domains..." --weight=2
|
|
|
|
# Get the default network interface
|
|
main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
|
|
# Find the IP associated to the network interface
|
|
localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
|
|
|
|
# List all YunoHost domains
|
|
while read perdomain
|
|
do
|
|
# Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
|
|
ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
|
|
|
|
# And add a resolution on the local IP instead
|
|
grep -q "^$localipv4.*$perdomain" /etc/hosts || \
|
|
echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
|
|
done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
|
|
|
|
#=================================================
|
|
# RESTART DNSMASQ
|
|
#=================================================
|
|
|
|
# Restart dnsmasq only for the version 3.X, otherwise we're going to restart it twice.
|
|
if [ "$pihole_version" == "Last 3.X" ]
|
|
then
|
|
ynh_script_progression --message="Restarting Dnsmasq..."
|
|
|
|
ynh_systemd_action --action=restart --service_name=dnsmasq
|
|
fi
|
|
|
|
#=================================================
|
|
# UPDATE VARIABLES FILE
|
|
#=================================================
|
|
|
|
setupVars="/etc/pihole/setupVars.conf"
|
|
|
|
echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
|
|
ynh_replace_string --match_string="^PIHOLE_INTERFACE=.*" --replace_string="PIHOLE_INTERFACE=$main_iface" --target_file=$setupVars
|
|
ynh_replace_string --match_string="^IPV4_ADDRESS=.*" --replace_string="IPV4_ADDRESS=127.0.0.1" --target_file=$setupVars
|
|
|
|
# Recalculate and store the checksum of the file for the next upgrade.
|
|
ynh_store_file_checksum --file="$setupVars"
|
|
|
|
#=================================================
|
|
# START PIHOLE-FTL
|
|
#=================================================
|
|
ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
|
|
|
|
ynh_exec_warn_less systemctl enable pihole-FTL --quiet
|
|
ynh_systemd_action --action=restart --service_name=pihole-FTL
|
|
|
|
#=================================================
|
|
# GENERIC FINALISATION
|
|
#=================================================
|
|
# RELOAD NGINX AND PHP-FPM
|
|
#=================================================
|
|
ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..."
|
|
|
|
ynh_systemd_action --service_name=$fpm_service --action=reload
|
|
ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
#=================================================
|
|
# DEACTIVE MAINTENANCE MODE
|
|
#=================================================
|
|
ynh_script_progression --message="Disabling maintenance mode..." --weight=4
|
|
|
|
ynh_maintenance_mode_OFF
|
|
|
|
#=================================================
|
|
# SEND A README FOR THE ADMIN
|
|
#=================================================
|
|
|
|
# Get main domain and buid the url of the admin panel of the app.
|
|
admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
|
|
|
|
if [ $enable_dhcp -eq 1 ]
|
|
then
|
|
dhcp_alert="You asked to use the internal DHCP server of Dnsmasq with PiHole.
|
|
You should really read the __URL_TAG1__documentation about that__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md__URL_TAG3__
|
|
|
|
"
|
|
else
|
|
dhcp_alert=""
|
|
fi
|
|
|
|
echo "${dhcp_alert}You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
|
|
You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.
|
|
|
|
If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh__URL_TAG3__." > mail_to_send
|
|
|
|
ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=restore
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Restoration completed for $app" --last
|