upgrade 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. # Load common variables for all scripts.
  8. source _variables
  9. source _common.sh
  10. source /usr/share/yunohost/helpers
  11. source _ynh_add_fpm_config.sh
  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. pihole_version="$(ynh_app_setting_get --app=$app --key=pihole_version)"
  25. overwrite_setupvars=$(ynh_app_setting_get --app=$app --key=overwrite_setupvars)
  26. overwrite_ftl=$(ynh_app_setting_get --app=$app --key=overwrite_ftl)
  27. overwrite_nginx=$(ynh_app_setting_get --app=$app --key=overwrite_nginx)
  28. overwrite_phpfpm=$(ynh_app_setting_get --app=$app --key=overwrite_phpfpm)
  29. fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint)
  30. fpm_usage=$(ynh_app_setting_get --app=$app --key=fpm_usage)
  31. #=================================================
  32. # CHECK VERSION
  33. #=================================================
  34. upgrade_type=$(ynh_check_app_version_changed)
  35. #=================================================
  36. # ENSURE DOWNWARD COMPATIBILITY
  37. #=================================================
  38. ynh_script_progression --message="Ensuring downward compatibility..."
  39. # If overwrite_setupvars doesn't exist, create it
  40. if [ -z "$overwrite_setupvars" ]; then
  41. overwrite_setupvars=1
  42. ynh_app_setting_set --app=$app --key=overwrite_setupvars --value=$overwrite_setupvars
  43. fi
  44. # If overwrite_ftl doesn't exist, create it
  45. if [ -z "$overwrite_ftl" ]; then
  46. overwrite_ftl=1
  47. ynh_app_setting_set --app=$app --key=overwrite_ftl --value=$overwrite_ftl
  48. fi
  49. # If overwrite_nginx doesn't exist, create it
  50. if [ -z "$overwrite_nginx" ]; then
  51. overwrite_nginx=1
  52. ynh_app_setting_set --app=$app --key=overwrite_nginx --value=$overwrite_nginx
  53. fi
  54. # If overwrite_phpfpm doesn't exist, create it
  55. if [ -z "$overwrite_phpfpm" ]; then
  56. overwrite_phpfpm=1
  57. ynh_app_setting_set --app=$app --key=overwrite_phpfpm --value=$overwrite_phpfpm
  58. fi
  59. # If admin_mail_html doesn't exist, create it
  60. if [ -z "$admin_mail_html" ]; then
  61. admin_mail_html=1
  62. ynh_app_setting_set --app=$app --key=admin_mail_html --value=$admin_mail_html
  63. fi
  64. # If fpm_footprint doesn't exist, create it
  65. if [ -z "$fpm_footprint" ]; then
  66. fpm_footprint=low
  67. ynh_app_setting_set --app=$app --key=fpm_footprint --value=$fpm_footprint
  68. fi
  69. # If fpm_usage doesn't exist, create it
  70. if [ -z "$fpm_usage" ]; then
  71. fpm_usage=low
  72. ynh_app_setting_set --app=$app --key=fpm_usage --value=$fpm_usage
  73. fi
  74. # If pihole_version doesn't exist, create it
  75. if [ -z "$pihole_version" ]; then
  76. pihole_version="Last 3.X"
  77. ynh_app_setting_set --app=$app --key=pihole_version --value="$pihole_version"
  78. fi
  79. #=================================================
  80. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  81. #=================================================
  82. ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=7
  83. # Backup the current version of the app
  84. ynh_backup_before_upgrade
  85. ynh_clean_setup () {
  86. # restore it if the upgrade fails
  87. ynh_restore_upgradebackup
  88. }
  89. # Exit if an error occurs during the execution of the script
  90. ynh_abort_if_errors
  91. #=================================================
  92. # CHECK THE PATH
  93. #=================================================
  94. # Normalize the URL path syntax
  95. path_url=$(ynh_normalize_url_path --path_url=$path_url)
  96. #=================================================
  97. # ACTIVATE MAINTENANCE MODE
  98. #=================================================
  99. ynh_script_progression --message="Activating maintenance mode..."
  100. ynh_maintenance_mode_ON
  101. #=================================================
  102. # STANDARD UPGRADE STEPS
  103. #=================================================
  104. # INSTALL DEPENDENCIES
  105. #=================================================
  106. ynh_script_progression --message="Upgrading dependencies..." --weight=6
  107. ynh_install_app_dependencies $app_depencencies
  108. #=================================================
  109. # CREATE DEDICATED USER
  110. #=================================================
  111. ynh_script_progression --message="Making sure dedicated system user exists..."
  112. # Create a dedicated user (if not existing)
  113. ynh_system_user_create --username=$app
  114. #=================================================
  115. # DOWNLOAD, CHECK AND UNPACK SOURCE
  116. #=================================================
  117. pihole_local_repo="/etc/.pihole"
  118. if [ "$upgrade_type" == "UPGRADE_APP" ]
  119. then
  120. ynh_script_progression --message="Upgrading source files..." --weight=4
  121. if [ "$pihole_version" == "Last 3.X" ]
  122. then
  123. # Update the version 3.X
  124. ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_3
  125. # Update admin dashboard
  126. ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_3
  127. else
  128. # Update the last version available
  129. ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_last
  130. # Update admin dashboard
  131. ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_last
  132. fi
  133. fi
  134. chown $app:www-data "$final_path"
  135. #=================================================
  136. # NGINX CONFIGURATION
  137. #=================================================
  138. # Overwrite the nginx configuration only if it's allowed
  139. if [ $overwrite_nginx -eq 1 ]
  140. then
  141. ynh_script_progression --message="Upgrading nginx web server configuration..." --weight=2
  142. # Create a dedicated nginx config
  143. ynh_add_nginx_config
  144. fi
  145. #=================================================
  146. # PHP-FPM CONFIGURATION
  147. #=================================================
  148. # Overwrite the php-fpm configuration only if it's allowed
  149. if [ $overwrite_phpfpm -eq 1 ]
  150. then
  151. ynh_script_progression --message="Upgrading php-fpm configuration..." --weight=3
  152. # Create a dedicated php-fpm config
  153. ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --dedicated_service
  154. fi
  155. #=================================================
  156. # SPECIFIC UPGRADE
  157. #=================================================
  158. # UPDATE PI-HOLE SCRIPTS
  159. #=================================================
  160. pihole_dir="/opt/pihole"
  161. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  162. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  163. # And copy this fucking COL_TABLE file...
  164. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  165. #=================================================
  166. # COPY PI-HOLE MAIN SCRIPT
  167. #=================================================
  168. ynh_script_progression --message="Copying Pi-Hole main script..."
  169. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  170. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  171. #=================================================
  172. # CREATE SUDOER FILE
  173. #=================================================
  174. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  175. if [ "$pihole_version" == "Last 3.X" ]
  176. then
  177. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  178. else
  179. cp "$pihole_local_repo/advanced/Templates/pihole.sudo" /etc/sudoers.d/pihole
  180. fi
  181. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  182. chmod 0440 /etc/sudoers.d/pihole
  183. #=================================================
  184. # UPDATE LOGROTATE SCRIPT FOR PI-HOLE
  185. #=================================================
  186. pihole_storage="/etc/pihole"
  187. if [ "$pihole_version" == "Last 3.X" ]
  188. then
  189. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  190. else
  191. cp "$pihole_local_repo/advanced/Templates/logrotate" "$pihole_storage/logrotate"
  192. fi
  193. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  194. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  195. #=================================================
  196. # UPDATE PIHOLE-FTL
  197. #=================================================
  198. ynh_script_progression --message="Upgrading PiHole-FTL..." --weight=35
  199. ynh_systemd_action --action=stop --service_name=pihole-FTL
  200. if [ "$upgrade_type" == "UPGRADE_APP" ]
  201. then
  202. # Get the source of Pi-Hole-FTL
  203. FTL_temp_path=$(mktemp -d)
  204. if [ "$pihole_version" == "Last 3.X" ]
  205. then
  206. # Install the version 3.3.1
  207. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_3
  208. else
  209. # Install the last version available
  210. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_last
  211. fi
  212. # Instead of downloading a binary file, we're going to compile it
  213. ( cd "$FTL_temp_path"
  214. if [ "$pihole_version" == "Last available" ]
  215. then
  216. ynh_exec_warn_less cmake .
  217. fi
  218. ynh_exec_warn_less make
  219. ynh_exec_warn_less make install )
  220. ynh_secure_remove --file="$FTL_temp_path"
  221. fi
  222. # Overwrite pihole-FTL config file only if it's allowed
  223. if [ $overwrite_ftl -eq 1 ]
  224. then
  225. # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
  226. ynh_backup_if_checksum_is_different --file="$pihole_storage/pihole-FTL.conf"
  227. cp "../conf/pihole-FTL.conf" "$pihole_storage"
  228. # Recalculate and store the checksum of the file for the next upgrade.
  229. ynh_store_file_checksum --file="$pihole_storage/pihole-FTL.conf"
  230. fi
  231. if [ "$pihole_version" == "Last 3.X" ]
  232. then
  233. # Version 3.3.1
  234. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  235. chmod +x /etc/init.d/pihole-FTL
  236. ynh_exec_warn_less systemctl enable pihole-FTL
  237. else
  238. # Last version available
  239. # Stopped dnsmasq to replace it by pihole-FTL
  240. ynh_systemd_action --action=stop --service_name=dnsmasq
  241. # Disable the real dnsmasq service
  242. ynh_exec_warn_less systemctl disable dnsmasq
  243. # And move the files that make the service available in systemd to really disable it
  244. mv /lib/systemd/system/dnsmasq.service /lib/systemd/system/.dnsmasq.service.backup_by_pihole
  245. mv /etc/init.d/dnsmasq /etc/init.d/.dnsmasq.backup_by_pihole
  246. # Move dnsmasq to preserve the current binary
  247. mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.backup_by_pihole
  248. # Replace dnsmasq by pihole-FTL
  249. # NOTE: pihole-FTL is actually a modified version of dnsmasq
  250. # https://github.com/pi-hole/FTL/tree/master/dnsmasq
  251. ln -s /usr/bin/pihole-FTL /usr/sbin/dnsmasq
  252. cp -a $pihole_local_repo/advanced/Templates/pihole-FTL.service /etc/init.d/pihole-FTL
  253. chmod +x /etc/init.d/pihole-FTL
  254. ynh_exec_warn_less systemctl enable pihole-FTL
  255. # Replace the service dnsmasq by pihole-FTL
  256. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  257. ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/multi-user.target.wants/dnsmasq.service
  258. # Reload systemd config
  259. systemctl daemon-reload
  260. fi
  261. #=================================================
  262. # BUILD VARIABLES FILE
  263. #=================================================
  264. setupVars="$pihole_storage/setupVars.conf"
  265. # Overwrite the setupVars config file only if it's allowed
  266. if [ $overwrite_setupvars -eq 1 ]
  267. then
  268. # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
  269. ynh_backup_if_checksum_is_different --file="$setupVars"
  270. # Get the default network interface
  271. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  272. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  273. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  274. echo "IPV6_ADDRESS=::1" >> $setupVars
  275. echo "PIHOLE_DNS_1=" >> $setupVars
  276. echo "PIHOLE_DNS_2=" >> $setupVars
  277. if [ $query_logging -eq 1 ]; then
  278. query_logging=true
  279. else
  280. query_logging=false
  281. fi
  282. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  283. echo "INSTALL_WEB=true" >> $setupVars
  284. # Recalculate and store the checksum of the file for the next upgrade.
  285. ynh_store_file_checksum --file="$setupVars"
  286. fi
  287. #=================================================
  288. # UPDATE CRON JOB
  289. #=================================================
  290. if [ "$pihole_version" == "Last 3.X" ]
  291. then
  292. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  293. else
  294. cp $pihole_local_repo/advanced/Templates/pihole.cron /etc/cron.d/pihole
  295. fi
  296. # Remove git usage for version. Which fails because we use here a release instead of master.
  297. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
  298. #=================================================
  299. # START PIHOLE-FTL
  300. #=================================================
  301. ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
  302. ynh_systemd_action --action=restart --service_name=pihole-FTL
  303. #=================================================
  304. # UPDATE CONF_REGEN HOOK
  305. #=================================================
  306. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  307. #=================================================
  308. # RELOAD NGINX
  309. #=================================================
  310. ynh_script_progression --message="Reloading nginx web server..."
  311. ynh_systemd_action --action=reload --service_name=nginx
  312. #=================================================
  313. # DEACTIVE MAINTENANCE MODE
  314. #=================================================
  315. ynh_script_progression --message="Disabling maintenance mode..." --weight=5
  316. ynh_maintenance_mode_OFF
  317. #=================================================
  318. # SEND A README FOR THE ADMIN
  319. #=================================================
  320. # Get main domain and buid the url of the admin panel of the app.
  321. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  322. # If a html email is required. Apply html to the changelog.
  323. if [ "$admin_mail_html" -eq 1 ]; then
  324. format=html
  325. else
  326. format=plain
  327. fi
  328. ynh_app_changelog --format=$format
  329. if [ $enable_dhcp -eq 1 ]
  330. then
  331. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  332. You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
  333. "
  334. else
  335. dhcp_alert=""
  336. fi
  337. 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__.
  338. 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__.
  339. 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__.
  340. ---
  341. Changelog since your last upgrade:
  342. $(cat changelog)" > mail_to_send
  343. ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=upgrade
  344. #=================================================
  345. # END OF SCRIPT
  346. #=================================================
  347. ynh_script_progression --message="Upgrade of $app completed" --last