upgrade 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. # Load common variables for all scripts.
  10. source _variables
  11. #=================================================
  12. # LOAD SETTINGS
  13. #=================================================
  14. app=$YNH_APP_INSTANCE_NAME
  15. domain=$(ynh_app_setting_get $app domain)
  16. path_url=$(ynh_app_setting_get $app path)
  17. admin=$(ynh_app_setting_get $app admin)
  18. query_logging=$(ynh_app_setting_get $app query_logging)
  19. final_path=$(ynh_app_setting_get $app final_path)
  20. enable_dhcp=$(ynh_app_setting_get $app enable_dhcp)
  21. port=$(ynh_app_setting_get $app port)
  22. overwrite_setupvars=$(ynh_app_setting_get $app overwrite_setupvars)
  23. overwrite_ftl=$(ynh_app_setting_get $app overwrite_ftl)
  24. overwrite_nginx=$(ynh_app_setting_get $app overwrite_nginx)
  25. overwrite_phpfpm=$(ynh_app_setting_get $app overwrite_phpfpm)
  26. #=================================================
  27. # CHECK VERSION
  28. #=================================================
  29. upgrade_type=$(ynh_check_app_version_changed)
  30. #=================================================
  31. # ENSURE DOWNWARD COMPATIBILITY
  32. #=================================================
  33. # If overwrite_setupvars doesn't exist, create it
  34. if [ -z "$overwrite_setupvars" ]; then
  35. overwrite_setupvars=1
  36. ynh_app_setting_set $app overwrite_setupvars $overwrite_setupvars
  37. fi
  38. # If overwrite_ftl doesn't exist, create it
  39. if [ -z "$overwrite_ftl" ]; then
  40. overwrite_ftl=1
  41. ynh_app_setting_set $app overwrite_ftl $overwrite_ftl
  42. fi
  43. # If overwrite_nginx doesn't exist, create it
  44. if [ -z "$overwrite_nginx" ]; then
  45. overwrite_nginx=1
  46. ynh_app_setting_set $app overwrite_nginx $overwrite_nginx
  47. fi
  48. # If overwrite_phpfpm doesn't exist, create it
  49. if [ -z "$overwrite_phpfpm" ]; then
  50. overwrite_phpfpm=1
  51. ynh_app_setting_set $app overwrite_phpfpm $overwrite_phpfpm
  52. fi
  53. #=================================================
  54. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  55. #=================================================
  56. # Backup the current version of the app
  57. ynh_backup_before_upgrade
  58. ynh_clean_setup () {
  59. # restore it if the upgrade fails
  60. ynh_restore_upgradebackup
  61. }
  62. # Exit if an error occurs during the execution of the script
  63. ynh_abort_if_errors
  64. #=================================================
  65. # CHECK THE PATH
  66. #=================================================
  67. # Normalize the URL path syntax
  68. path_url=$(ynh_normalize_url_path $path_url)
  69. #=================================================
  70. # ACTIVATE MAINTENANCE MODE
  71. #=================================================
  72. ynh_maintenance_mode_ON
  73. #=================================================
  74. # STANDARD UPGRADE STEPS
  75. #=================================================
  76. # INSTALL DEPENDENCIES
  77. #=================================================
  78. ynh_install_app_dependencies $app_depencencies
  79. #=================================================
  80. # DOWNLOAD, CHECK AND UNPACK SOURCE
  81. #=================================================
  82. pihole_local_repo="/etc/.pihole"
  83. if [ "$upgrade_type" == "UPGRADE_APP" ]
  84. then
  85. # Update the local copy pihole repository (for Gravity)
  86. ynh_setup_source "$pihole_local_repo"
  87. # Update admin dashboard
  88. ynh_setup_source "$final_path" admin_dashboard
  89. fi
  90. #=================================================
  91. # NGINX CONFIGURATION
  92. #=================================================
  93. # Overwrite the nginx configuration only if it's allowed
  94. if [ $overwrite_nginx -eq 1 ]
  95. then
  96. # Create a dedicated nginx config
  97. ynh_add_nginx_config
  98. fi
  99. #=================================================
  100. # CREATE DEDICATED USER
  101. #=================================================
  102. # Create a dedicated user (if not existing)
  103. ynh_system_user_create $app
  104. #=================================================
  105. # PHP-FPM CONFIGURATION
  106. #=================================================
  107. # Overwrite the php-fpm configuration only if it's allowed
  108. if [ $overwrite_phpfpm -eq 1 ]
  109. then
  110. # Create a dedicated php-fpm config
  111. ynh_add_fpm_config
  112. fi
  113. #=================================================
  114. # SPECIFIC UPGRADE
  115. #=================================================
  116. # UPDATE PI-HOLE SCRIPTS
  117. #=================================================
  118. pihole_dir="/opt/pihole"
  119. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  120. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  121. # And copy this fucking COL_TABLE file...
  122. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  123. #=================================================
  124. # COPY PI-HOLE MAIN SCRIPT
  125. #=================================================
  126. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  127. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  128. #=================================================
  129. # CREATE SUDOER FILE
  130. #=================================================
  131. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  132. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  133. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  134. chmod 0440 /etc/sudoers.d/pihole
  135. #=================================================
  136. # UPDATE LOGROTATE SCRIPT FOR PI-HOLE
  137. #=================================================
  138. pihole_storage="/etc/pihole"
  139. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  140. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  141. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  142. #=================================================
  143. # UPDATE PIHOLE-FTL
  144. #=================================================
  145. ynh_systemd_action --action=stop --service_name=pihole-FTL
  146. if [ "$upgrade_type" == "UPGRADE_APP" ]
  147. then
  148. # Get the source of Pi-Hole-FTL
  149. FTL_temp_path=$(mktemp -d)
  150. ynh_setup_source "$FTL_temp_path" FTL
  151. # Instead of downloading a binary file, we're going to compile it
  152. ( cd "$FTL_temp_path"
  153. ynh_exec_warn_less make
  154. ynh_exec_warn_less make install )
  155. ynh_secure_remove "$FTL_temp_path"
  156. fi
  157. # Overwrite pihole-FTL config file only if it's allowed
  158. if [ $overwrite_ftl -eq 1 ]
  159. then
  160. # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
  161. ynh_backup_if_checksum_is_different "$pihole_storage/pihole-FTL.conf"
  162. cp "../conf/pihole-FTL.conf" "$pihole_storage"
  163. # Recalculate and store the checksum of the file for the next upgrade.
  164. ynh_store_file_checksum "$pihole_storage/pihole-FTL.conf"
  165. fi
  166. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  167. chmod +x /etc/init.d/pihole-FTL
  168. ynh_exec_warn_less systemctl enable pihole-FTL
  169. #=================================================
  170. # BUILD VARIABLES FILE
  171. #=================================================
  172. setupVars="$pihole_storage/setupVars.conf"
  173. # Overwrite the setupVars config file only if it's allowed
  174. if [ $overwrite_setupvars -eq 1 ]
  175. then
  176. # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
  177. ynh_backup_if_checksum_is_different "$setupVars"
  178. # Get the default network interface
  179. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  180. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  181. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  182. echo "IPV6_ADDRESS=" >> $setupVars
  183. echo "PIHOLE_DNS_1=" >> $setupVars
  184. echo "PIHOLE_DNS_2=" >> $setupVars
  185. if [ $query_logging -eq 1 ]; then
  186. query_logging=true
  187. else
  188. query_logging=false
  189. fi
  190. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  191. echo "INSTALL_WEB=true" >> $setupVars
  192. # Recalculate and store the checksum of the file for the next upgrade.
  193. ynh_store_file_checksum "$setupVars"
  194. fi
  195. #=================================================
  196. # UPDATE CRON JOB
  197. #=================================================
  198. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  199. # Remove git usage for version. Which fails because we use here a release instead of master.
  200. ynh_replace_string ".*updatechecker.*" "#&" /etc/cron.d/pihole
  201. #=================================================
  202. # START PIHOLE-FTL
  203. #=================================================
  204. ynh_systemd_action --action=restart --service_name=pihole-FTL
  205. #=================================================
  206. # UPDATE CONF_REGEN HOOK
  207. #=================================================
  208. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  209. #=================================================
  210. # RELOAD NGINX
  211. #=================================================
  212. ynh_systemd_action --action=reload --service_name=nginx
  213. #=================================================
  214. # DEACTIVE MAINTENANCE MODE
  215. #=================================================
  216. ynh_maintenance_mode_OFF
  217. #=================================================
  218. # SEND A README FOR THE ADMIN
  219. #=================================================
  220. # Get main domain and buid the url of the admin panel of the app.
  221. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  222. if [ $enable_dhcp -eq 1 ]
  223. then
  224. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  225. You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
  226. "
  227. else
  228. dhcp_alert=""
  229. fi
  230. message="${dhcp_alert}You can configure this app easily by using the experimental config-panel feature: $admin_panel/config-panel.
  231. You can also find some specific actions for this app by using the experimental action feature: $admin_panel/actions.
  232. If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/pihole_ynh"
  233. ynh_send_readme_to_admin --app_message="$message" --recipients="$admin" --type="upgrade"