#!/bin/bash #================================================= # GENERIC STARTING #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source /usr/share/yunohost/helpers source _ynh_add_fpm_config # Load common variables for all scripts. source _variables #================================================= # LOAD SETTINGS #================================================= ynh_script_progression --message="Load settings" --weight=3 app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get $app domain) path_url=$(ynh_app_setting_get $app path) admin=$(ynh_app_setting_get $app admin) query_logging=$(ynh_app_setting_get $app query_logging) final_path=$(ynh_app_setting_get $app final_path) enable_dhcp=$(ynh_app_setting_get $app enable_dhcp) port=$(ynh_app_setting_get $app port) overwrite_setupvars=$(ynh_app_setting_get $app overwrite_setupvars) overwrite_ftl=$(ynh_app_setting_get $app overwrite_ftl) overwrite_nginx=$(ynh_app_setting_get $app overwrite_nginx) overwrite_phpfpm=$(ynh_app_setting_get $app overwrite_phpfpm) fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint) fpm_usage=$(ynh_app_setting_get --app=$app --key=fpm_usage) #================================================= # CHECK VERSION #================================================= upgrade_type=$(ynh_check_app_version_changed) #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= ynh_script_progression --message="Ensure downward compatibility" # If overwrite_setupvars doesn't exist, create it if [ -z "$overwrite_setupvars" ]; then overwrite_setupvars=1 ynh_app_setting_set $app overwrite_setupvars $overwrite_setupvars fi # If overwrite_ftl doesn't exist, create it if [ -z "$overwrite_ftl" ]; then overwrite_ftl=1 ynh_app_setting_set $app overwrite_ftl $overwrite_ftl fi # If overwrite_nginx doesn't exist, create it if [ -z "$overwrite_nginx" ]; then overwrite_nginx=1 ynh_app_setting_set $app overwrite_nginx $overwrite_nginx fi # If overwrite_phpfpm doesn't exist, create it if [ -z "$overwrite_phpfpm" ]; then overwrite_phpfpm=1 ynh_app_setting_set $app overwrite_phpfpm $overwrite_phpfpm # If fpm_footprint doesn't exist, create it if [ -z "$fpm_footprint" ]; then fpm_footprint=low ynh_app_setting_set --app=$app --key=fpm_footprint --value=$fpm_footprint fi # If fpm_usage doesn't exist, create it if [ -z "$fpm_usage" ]; then fpm_usage=low ynh_app_setting_set --app=$app --key=fpm_usage --value=$fpm_usage fi #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= ynh_script_progression --message="Backup the app before upgrading" --weight=7 # 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 #================================================= # CHECK THE PATH #================================================= # Normalize the URL path syntax path_url=$(ynh_normalize_url_path $path_url) #================================================= # ACTIVATE MAINTENANCE MODE #================================================= ynh_script_progression --message="Activate maintenance mode" ynh_maintenance_mode_ON #================================================= # STANDARD UPGRADE STEPS #================================================= # INSTALL DEPENDENCIES #================================================= ynh_script_progression --message="Upgrade dependencies" --weight=6 ynh_install_app_dependencies $app_depencencies #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= pihole_local_repo="/etc/.pihole" if [ "$upgrade_type" == "UPGRADE_APP" ] then ynh_script_progression --message="Download, check and unpack source" --weight=4 # Update the local copy pihole repository (for Gravity) ynh_setup_source "$pihole_local_repo" # Update admin dashboard ynh_setup_source "$final_path" admin_dashboard fi #================================================= # NGINX CONFIGURATION #================================================= # Overwrite the nginx configuration only if it's allowed if [ $overwrite_nginx -eq 1 ] then ynh_script_progression --message="Reconfigure nginx" --weight=2 # Create a dedicated nginx config ynh_add_nginx_config fi #================================================= # CREATE DEDICATED USER #================================================= ynh_script_progression --message="Create a dedicated user" # Create a dedicated user (if not existing) ynh_system_user_create $app #================================================= # PHP-FPM CONFIGURATION #================================================= # Overwrite the php-fpm configuration only if it's allowed if [ $overwrite_phpfpm -eq 1 ] then ynh_script_progression --message="Reconfigure php-fpm" --weight=3 # Create a dedicated php-fpm config ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint fi #================================================= # SPECIFIC UPGRADE #================================================= # UPDATE PI-HOLE SCRIPTS #================================================= pihole_dir="/opt/pihole" cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/" cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/" # And copy this fucking COL_TABLE file... cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/" #================================================= # COPY PI-HOLE MAIN SCRIPT #================================================= ynh_script_progression --message="Copy Pi-Hole main script" cp -a "$pihole_local_repo/pihole" /usr/local/bin/ cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole #================================================= # CREATE SUDOER FILE #================================================= # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole chmod 0440 /etc/sudoers.d/pihole #================================================= # UPDATE LOGROTATE SCRIPT FOR PI-HOLE #================================================= pihole_storage="/etc/pihole" cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate" dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2) sed -i "/# su #/d;" "$pihole_storage/logrotate" #================================================= # UPDATE PIHOLE-FTL #================================================= ynh_script_progression --message="Upgrade PiHole-FTL" --weight=35 ynh_systemd_action --action=stop --service_name=pihole-FTL if [ "$upgrade_type" == "UPGRADE_APP" ] then # Get the source of Pi-Hole-FTL FTL_temp_path=$(mktemp -d) ynh_setup_source "$FTL_temp_path" FTL # Instead of downloading a binary file, we're going to compile it ( cd "$FTL_temp_path" ynh_exec_warn_less make ynh_exec_warn_less make install ) ynh_secure_remove "$FTL_temp_path" fi # Overwrite pihole-FTL config file only if it's allowed if [ $overwrite_ftl -eq 1 ] then # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ynh_backup_if_checksum_is_different "$pihole_storage/pihole-FTL.conf" cp "../conf/pihole-FTL.conf" "$pihole_storage" # Recalculate and store the checksum of the file for the next upgrade. ynh_store_file_checksum "$pihole_storage/pihole-FTL.conf" fi cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL chmod +x /etc/init.d/pihole-FTL ynh_exec_warn_less systemctl enable pihole-FTL #================================================= # BUILD VARIABLES FILE #================================================= setupVars="$pihole_storage/setupVars.conf" # Overwrite the setupVars config file only if it's allowed if [ $overwrite_setupvars -eq 1 ] then # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ynh_backup_if_checksum_is_different "$setupVars" # Get the default network interface main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}') echo "PIHOLE_INTERFACE=$main_iface" > $setupVars echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars echo "IPV6_ADDRESS=" >> $setupVars echo "PIHOLE_DNS_1=" >> $setupVars echo "PIHOLE_DNS_2=" >> $setupVars if [ $query_logging -eq 1 ]; then query_logging=true else query_logging=false fi echo "QUERY_LOGGING=$query_logging" >> $setupVars echo "INSTALL_WEB=true" >> $setupVars # Recalculate and store the checksum of the file for the next upgrade. ynh_store_file_checksum "$setupVars" fi #================================================= # UPDATE CRON JOB #================================================= cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole # Remove git usage for version. Which fails because we use here a release instead of master. ynh_replace_string ".*updatechecker.*" "#&" /etc/cron.d/pihole #================================================= # START PIHOLE-FTL #================================================= ynh_script_progression --message="Restart PiHole-FTL" --weight=2 ynh_systemd_action --action=restart --service_name=pihole-FTL #================================================= # UPDATE CONF_REGEN HOOK #================================================= cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app #================================================= # RELOAD NGINX #================================================= ynh_script_progression --message="Reload nginx" ynh_systemd_action --action=reload --service_name=nginx #================================================= # DEACTIVE MAINTENANCE MODE #================================================= ynh_script_progression --message="Disable maintenance mode" --weight=5 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" # Build the changelog # Get the value of admin_mail_html admin_mail_html=$(ynh_app_setting_get $app admin_mail_html) admin_mail_html="${admin_mail_html:-0}" # If a html email is required. Apply html to the changelog. if [ "$admin_mail_html" -eq 1 ]; then format=html else format=plain fi ynh_app_changelog --format=$format 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 documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md " 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__. --- Changelog since your last upgrade: $(cat changelog)" > mail_to_send ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type="upgrade" #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Upgrade completed" --last