upgrade 7.8 KB

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