upgrade 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. port=$(ynh_app_setting_get $app port)
  21. overwrite_setupvars=$(ynh_app_setting_get $app overwrite_setupvars)
  22. overwrite_ftl=$(ynh_app_setting_get $app overwrite_ftl)
  23. overwrite_nginx=$(ynh_app_setting_get $app overwrite_nginx)
  24. overwrite_phpfpm=$(ynh_app_setting_get $app overwrite_phpfpm)
  25. #=================================================
  26. # CHECK VERSION
  27. #=================================================
  28. upgrade_type=$(ynh_check_app_version_changed)
  29. #=================================================
  30. # ENSURE DOWNWARD COMPATIBILITY
  31. #=================================================
  32. # If overwrite_setupvars doesn't exist, create it
  33. if [ -z "$overwrite_setupvars" ]; then
  34. overwrite_setupvars=1
  35. ynh_app_setting_set $app overwrite_setupvars $overwrite_setupvars
  36. fi
  37. # If overwrite_ftl doesn't exist, create it
  38. if [ -z "$overwrite_ftl" ]; then
  39. overwrite_ftl=1
  40. ynh_app_setting_set $app overwrite_ftl $overwrite_ftl
  41. fi
  42. # If overwrite_nginx doesn't exist, create it
  43. if [ -z "$overwrite_nginx" ]; then
  44. overwrite_nginx=1
  45. ynh_app_setting_set $app overwrite_nginx $overwrite_nginx
  46. fi
  47. # If overwrite_phpfpm doesn't exist, create it
  48. if [ -z "$overwrite_phpfpm" ]; then
  49. overwrite_phpfpm=1
  50. ynh_app_setting_set $app overwrite_phpfpm $overwrite_phpfpm
  51. fi
  52. #=================================================
  53. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  54. #=================================================
  55. # Backup the current version of the app
  56. ynh_backup_before_upgrade
  57. ynh_clean_setup () {
  58. # restore it if the upgrade fails
  59. ynh_restore_upgradebackup
  60. }
  61. # Exit if an error occurs during the execution of the script
  62. ynh_abort_if_errors
  63. #=================================================
  64. # CHECK THE PATH
  65. #=================================================
  66. # Normalize the URL path syntax
  67. path_url=$(ynh_normalize_url_path $path_url)
  68. #=================================================
  69. # ACTIVATE MAINTENANCE MODE
  70. #=================================================
  71. ynh_maintenance_mode_ON
  72. #=================================================
  73. # STANDARD UPGRADE STEPS
  74. #=================================================
  75. # INSTALL DEPENDENCIES
  76. #=================================================
  77. ynh_install_app_dependencies $app_depencencies
  78. #=================================================
  79. # DOWNLOAD, CHECK AND UNPACK SOURCE
  80. #=================================================
  81. pihole_local_repo="/etc/.pihole"
  82. if [ "$upgrade_type" == "UPGRADE_APP" ]
  83. then
  84. # Update the local copy pihole repository (for Gravity)
  85. ynh_setup_source "$pihole_local_repo"
  86. # Update admin dashboard
  87. ynh_setup_source "$final_path" admin_dashboard
  88. fi
  89. #=================================================
  90. # NGINX CONFIGURATION
  91. #=================================================
  92. # Overwrite the nginx configuration only if it's allowed
  93. if [ $overwrite_nginx -eq 1 ]
  94. then
  95. # Create a dedicated nginx config
  96. ynh_add_nginx_config
  97. fi
  98. #=================================================
  99. # CREATE DEDICATED USER
  100. #=================================================
  101. # Create a dedicated user (if not existing)
  102. ynh_system_user_create $app
  103. #=================================================
  104. # PHP-FPM CONFIGURATION
  105. #=================================================
  106. # Overwrite the php-fpm configuration only if it's allowed
  107. if [ $overwrite_phpfpm -eq 1 ]
  108. then
  109. # Create a dedicated php-fpm config
  110. ynh_add_fpm_config
  111. fi
  112. #=================================================
  113. # SPECIFIC UPGRADE
  114. #=================================================
  115. # UPDATE PI-HOLE SCRIPTS
  116. #=================================================
  117. pihole_dir="/opt/pihole"
  118. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  119. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  120. # And copy this fucking COL_TABLE file...
  121. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  122. #=================================================
  123. # COPY PI-HOLE MAIN SCRIPT
  124. #=================================================
  125. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  126. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  127. #=================================================
  128. # CREATE SUDOER FILE
  129. #=================================================
  130. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  131. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  132. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  133. chmod 0440 /etc/sudoers.d/pihole
  134. #=================================================
  135. # UPDATE LOGROTATE SCRIPT FOR PI-HOLE
  136. #=================================================
  137. pihole_storage="/etc/pihole"
  138. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  139. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  140. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  141. #=================================================
  142. # UPDATE PIHOLE-FTL
  143. #=================================================
  144. ynh_systemd_action --action=stop --service_name=pihole-FTL
  145. if [ "$upgrade_type" == "UPGRADE_APP" ]
  146. then
  147. # Get the source of Pi-Hole-FTL
  148. FTL_temp_path=$(mktemp -d)
  149. ynh_setup_source "$FTL_temp_path" FTL
  150. # Instead of downloading a binary file, we're going to compile it
  151. ( cd "$FTL_temp_path"
  152. ynh_exec_warn_less make
  153. ynh_exec_warn_less make install )
  154. ynh_secure_remove "$FTL_temp_path"
  155. fi
  156. # Overwrite pihole-FTL config file only if it's allowed
  157. if [ $overwrite_ftl -eq 1 ]
  158. then
  159. # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
  160. ynh_backup_if_checksum_is_different "$pihole_storage/pihole-FTL.conf"
  161. cp "../conf/pihole-FTL.conf" "$pihole_storage"
  162. # Recalculate and store the checksum of the file for the next upgrade.
  163. ynh_store_file_checksum "$pihole_storage/pihole-FTL.conf"
  164. fi
  165. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  166. chmod +x /etc/init.d/pihole-FTL
  167. ynh_exec_warn_less systemctl enable pihole-FTL
  168. #=================================================
  169. # BUILD VARIABLES FILE
  170. #=================================================
  171. setupVars="$pihole_storage/setupVars.conf"
  172. # Overwrite the setupVars config file only if it's allowed
  173. if [ $overwrite_setupvars -eq 1 ]
  174. then
  175. # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
  176. ynh_backup_if_checksum_is_different "$setupVars"
  177. # Get the default network interface
  178. main_iface=$(ip route | grep default | awk '{print $5;}')
  179. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  180. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  181. echo "IPV6_ADDRESS=" >> $setupVars
  182. echo "PIHOLE_DNS_1=" >> $setupVars
  183. echo "PIHOLE_DNS_2=" >> $setupVars
  184. if [ $query_logging -eq 1 ]; then
  185. query_logging=true
  186. else
  187. query_logging=false
  188. fi
  189. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  190. echo "INSTALL_WEB=true" >> $setupVars
  191. # Recalculate and store the checksum of the file for the next upgrade.
  192. ynh_store_file_checksum "$setupVars"
  193. fi
  194. #=================================================
  195. # UPDATE CRON JOB
  196. #=================================================
  197. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  198. # Remove git usage for version. Which fails because we use here a release instead of master.
  199. ynh_replace_string ".*updatechecker.*" "#&" /etc/cron.d/pihole
  200. #=================================================
  201. # START PIHOLE-FTL
  202. #=================================================
  203. ynh_systemd_action --action=restart --service_name=pihole-FTL
  204. #=================================================
  205. # UPDATE CONF_REGEN HOOK
  206. #=================================================
  207. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  208. #=================================================
  209. # RELOAD NGINX
  210. #=================================================
  211. ynh_systemd_action --action=reload --service_name=nginx
  212. #=================================================
  213. # DEACTIVE MAINTENANCE MODE
  214. #=================================================
  215. ynh_maintenance_mode_OFF