upgrade 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/bin/bash
  2. #=================================================
  3. # IMPORT GENERIC HELPERS
  4. #=================================================
  5. source _common.sh
  6. source /usr/share/yunohost/helpers
  7. #=================================================
  8. # INITIALIZE AND STORE SETTINGS
  9. #=================================================
  10. # Get the default network interface
  11. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  12. # Get the dnsmasq user to set log files permissions
  13. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  14. # Find the IP associated to the network interface
  15. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  16. if [ "$query_logging" -eq 1 ]; then
  17. query_logging_str=true
  18. else
  19. query_logging_str=false
  20. fi
  21. #=================================================
  22. # ACTIVATE MAINTENANCE MODE
  23. #=================================================
  24. ynh_script_progression --message="Activating maintenance mode..." --weight=1
  25. ynh_maintenance_mode_ON
  26. #=================================================
  27. # STOP SYSTEMD SERVICE
  28. #=================================================
  29. ynh_script_progression --message="Stopping a systemd service..." --weight=1
  30. ynh_systemd_action --service_name=pihole-FTL --action="stop" --log_path="/var/log/pihole-FTL.log"
  31. #=================================================
  32. # ENSURE DOWNWARD COMPATIBILITY
  33. #=================================================
  34. ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
  35. # If overwrite_setupvars doesn't exist, create it
  36. if [ -z "${overwrite_setupvars:-}" ]; then
  37. overwrite_setupvars=1
  38. ynh_app_setting_set --app="$app" --key="overwrite_setupvars" --value="$overwrite_setupvars"
  39. fi
  40. # If overwrite_ftl doesn't exist, create it
  41. if [ -z "${overwrite_ftl:-}" ]; then
  42. overwrite_ftl=1
  43. ynh_app_setting_set --app="$app" --key="overwrite_ftl" --value="$overwrite_ftl"
  44. fi
  45. # If pihole_version doesn't exist, create it
  46. if [ -z "${pihole_version:-}" ]; then
  47. pihole_version="Last 3.X"
  48. ynh_app_setting_set --app="$app" --key="pihole_version" --value=""$pihole_version""
  49. fi
  50. #=================================================
  51. # DOWNLOAD, CHECK AND UNPACK SOURCE
  52. #=================================================
  53. ynh_script_progression --message="Upgrading source files..." --weight=4
  54. # Download, check integrity, uncompress and patch the source from app.src
  55. ynh_setup_source --source_id="pi-hole_core" --dest_dir="$PI_HOLE_LOCAL_REPO"
  56. ynh_setup_source --source_id="pi-hole_web" --dest_dir="$install_dir/web"
  57. ynh_setup_source --source_id="pi-hole_ftl" --dest_dir="$install_dir/ftl"
  58. chmod -R o-rwx "$install_dir"
  59. chown -R "$app:www-data" "$install_dir"
  60. touch /var/log/{pihole,pihole-FTL}.log
  61. chmod 644 /var/log/{pihole,pihole-FTL}.log
  62. chown "$dnsmasq_user:root" /var/log/{pihole,pihole-FTL}.log
  63. #=================================================
  64. # UPGRADE OF PIHOLE-FTL
  65. #=================================================
  66. ynh_script_progression --message="Rebuilding PiHole-FTL..." --weight=30
  67. # Instead of downloading a binary file, we're going to compile it
  68. pushd "$install_dir/ftl"
  69. ynh_exec_warn_less cmake .
  70. ynh_exec_warn_less make
  71. ynh_exec_warn_less make install
  72. popd
  73. ynh_secure_remove --file="$install_dir/ftl"
  74. #=================================================
  75. # UPDATE THE SCRIPTS
  76. #=================================================
  77. ynh_script_progression --message="Upgrading Pihole..." --weight=1
  78. install -o "$app" -Dm755 -d "$PI_HOLE_INSTALL_DIR"
  79. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/gravity.sh"
  80. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/advanced/Scripts"/*.sh
  81. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/advanced/Scripts/COL_TABLE"
  82. install -Dm644 -t /etc/bash_completion.d/pihole "$PI_HOLE_LOCAL_REPO/advanced/bash-completion/pihole"
  83. install -o "$app" -Dm755 -t "$PI_HOLE_BIN_DIR" "$PI_HOLE_LOCAL_REPO/pihole"
  84. #=================================================
  85. # UPDATE THE CONFIGS
  86. #=================================================
  87. ynh_script_progression --message="Updating $app's configuration files..." --weight=1
  88. install -d -m 0755 "$PI_HOLE_CONFIG_DIR"
  89. ynh_add_config --template="dns-servers.conf" --destination="$PI_HOLE_CONFIG_DIR/dns-servers.conf"
  90. # Overwrite pihole-FTL config file only if it's allowed
  91. if [ "$overwrite_ftl" -eq 1 ]; then
  92. ynh_add_config --template="pihole-FTL.conf" --destination="$PI_HOLE_CONFIG_DIR/pihole-FTL.conf"
  93. fi
  94. # Overwrite the setupVars config file only if it's allowed
  95. if [ "$overwrite_setupvars" -eq 1 ]; then
  96. ynh_add_config --template="setupVars.conf" --destination="$PI_HOLE_CONFIG_DIR/setupVars.conf"
  97. fi
  98. chmod 644 "${PI_HOLE_CONFIG_DIR}/dns-servers.conf"
  99. #=================================================
  100. # SET VERSIONS FOR THE FOOTER OF THE WEB INTERFACE
  101. #=================================================
  102. ynh_script_progression --message="Setting versions for the footer of the web interface..." --weight=1
  103. echo "master master master" > "$PI_HOLE_CONFIG_DIR/localbranches"
  104. echo "$(ynh_app_upstream_version) $pihole_adminlte_version $pihole_flt_version" \
  105. | tee "$PI_HOLE_CONFIG_DIR/"{GitHubVersions,localversions} > /dev/null
  106. #=================================================
  107. # BUILD THE LISTS WITH GRAVITY
  108. #=================================================
  109. ynh_script_progression --message="Building the lists with Gravity..." --weight=7
  110. ynh_add_config --template="adlists.default" --destination="$PI_HOLE_CONFIG_DIR/adlists.list"
  111. ynh_exec_warn_less "$PI_HOLE_INSTALL_DIR/gravity.sh" --force
  112. #=================================================
  113. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  114. #=================================================
  115. ynh_script_progression --message="Configuring DNS for the local domains..." --weight=7
  116. # List all YunoHost domains
  117. while read -r perdomain; do
  118. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  119. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  120. # And add a resolution on the local IP instead
  121. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  122. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  123. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  124. #=================================================
  125. # DISABLING DNSMASQ
  126. #=================================================
  127. ynh_script_progression --message="Disabling Dnsmasq in system and yunohost..." --weight=1
  128. # Stop dnsmasq to replace it by pihole-FTL
  129. ynh_systemd_action --service_name=dnsmasq --action=stop
  130. # Disable the real dnsmasq service
  131. #ynh_exec_warn_less systemctl disable dnsmasq --quiet
  132. # Replace the service dnsmasq by pihole-FTL
  133. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  134. #ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  135. systemctl mask dnsmasq.service
  136. # Reload systemd config
  137. systemctl daemon-reload
  138. # Workaround for strings to not be replaced
  139. a_range="__A_RANGE__"
  140. b_range="__B_RANGE__"
  141. gateway="__GATEWAY__"
  142. ynh_add_config --template="dnsmasq_regenconf_hook" --destination="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  143. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  144. #=================================================
  145. # REAPPLY SYSTEM CONFIGURATIONS
  146. #=================================================
  147. ynh_script_progression --message="Upgrading system configurations related to $app..." --weight=1
  148. # Create a dedicated PHP-FPM config
  149. ynh_add_fpm_config
  150. # Create a dedicated NGINX config
  151. ynh_add_nginx_config
  152. _add_sudoers_config
  153. _add_cron_jobs
  154. _add_logrotate_config
  155. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
  156. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  157. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log" --needs_exposed_ports 53 67
  158. #=================================================
  159. # START SYSTEMD SERVICE
  160. #=================================================
  161. ynh_script_progression --message="Starting $app's systemd service..." --weight=2
  162. ynh_systemd_action --service_name=pihole-FTL --action=restart --log_path="/var/log/pihole-FTL.log"
  163. #=================================================
  164. # DEACTIVE MAINTENANCE MODE
  165. #=================================================
  166. ynh_script_progression --message="Disabling maintenance mode..." --weight=5
  167. ynh_maintenance_mode_OFF
  168. #=================================================
  169. # END OF SCRIPT
  170. #=================================================
  171. ynh_script_progression --message="Upgrade of $app completed" --last