upgrade 11 KB

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