upgrade 14 KB

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