upgrade 12 KB

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