upgrade 8.4 KB

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