upgrade 12 KB

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