upgrade 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint)
  27. fpm_usage=$(ynh_app_setting_get --app=$app --key=fpm_usage)
  28. #=================================================
  29. # CHECK VERSION
  30. #=================================================
  31. ynh_script_progression --message="Checking version..." --weight=1
  32. upgrade_type=$(ynh_check_app_version_changed)
  33. #=================================================
  34. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  35. #=================================================
  36. ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=7
  37. # Backup the current version of the app
  38. ynh_backup_before_upgrade
  39. ynh_clean_setup () {
  40. # Restore it if the upgrade fails
  41. ynh_restore_upgradebackup
  42. }
  43. # Exit if an error occurs during the execution of the script
  44. ynh_abort_if_errors
  45. #=================================================
  46. # ACTIVATE MAINTENANCE MODE
  47. #=================================================
  48. ynh_script_progression --message="Activating maintenance mode..." --weight=1
  49. ynh_maintenance_mode_ON
  50. #=================================================
  51. # STANDARD UPGRADE STEPS
  52. #=================================================
  53. # ENSURE DOWNWARD COMPATIBILITY
  54. #=================================================
  55. ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
  56. # If overwrite_setupvars doesn't exist, create it
  57. if [ -z "$overwrite_setupvars" ]; then
  58. overwrite_setupvars=1
  59. ynh_app_setting_set --app=$app --key=overwrite_setupvars --value=$overwrite_setupvars
  60. fi
  61. # If overwrite_ftl doesn't exist, create it
  62. if [ -z "$overwrite_ftl" ]; then
  63. overwrite_ftl=1
  64. ynh_app_setting_set --app=$app --key=overwrite_ftl --value=$overwrite_ftl
  65. fi
  66. # If overwrite_nginx doesn't exist, create it
  67. if [ -z "$overwrite_nginx" ]; then
  68. overwrite_nginx=1
  69. ynh_app_setting_set --app=$app --key=overwrite_nginx --value=$overwrite_nginx
  70. fi
  71. # If overwrite_phpfpm doesn't exist, create it
  72. if [ -z "$overwrite_phpfpm" ]; then
  73. overwrite_phpfpm=1
  74. ynh_app_setting_set --app=$app --key=overwrite_phpfpm --value=$overwrite_phpfpm
  75. fi
  76. # If admin_mail_html doesn't exist, create it
  77. if [ -z "$admin_mail_html" ]; then
  78. admin_mail_html=1
  79. ynh_app_setting_set --app=$app --key=admin_mail_html --value=$admin_mail_html
  80. fi
  81. # If fpm_footprint doesn't exist, create it
  82. if [ -z "$fpm_footprint" ]; then
  83. fpm_footprint=low
  84. ynh_app_setting_set --app=$app --key=fpm_footprint --value=$fpm_footprint
  85. fi
  86. # If fpm_usage doesn't exist, create it
  87. if [ -z "$fpm_usage" ]; then
  88. fpm_usage=low
  89. ynh_app_setting_set --app=$app --key=fpm_usage --value=$fpm_usage
  90. fi
  91. # If pihole_version doesn't exist, create it
  92. if [ -z "$pihole_version" ]; then
  93. pihole_version="Last 3.X"
  94. ynh_app_setting_set --app=$app --key=pihole_version --value="$pihole_version"
  95. fi
  96. #=================================================
  97. # CREATE DEDICATED USER
  98. #=================================================
  99. ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
  100. # Create a dedicated user (if not existing)
  101. ynh_system_user_create --username=$app --home_dir="$final_path"
  102. #=================================================
  103. # DOWNLOAD, CHECK AND UNPACK SOURCE
  104. #=================================================
  105. if [ "$upgrade_type" == "UPGRADE_APP" ]
  106. then
  107. ynh_script_progression --message="Upgrading source files..." --weight=4
  108. ynh_setup_source --dest_dir="$PI_HOLE_LOCAL_REPO" --source_id="pi-hole_Core"
  109. ynh_setup_source --dest_dir="$final_path" --source_id=pi-hole_AdminLTE
  110. FTL_temp_path=$(mktemp -d)
  111. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id="pi-hole_FTL"
  112. fi
  113. chmod 750 "$final_path"
  114. chmod -R o-rwx "$final_path"
  115. chown -R $app:www-data "$final_path"
  116. #=================================================
  117. # UPGRADE DEPENDENCIES
  118. #=================================================
  119. ynh_script_progression --message="Upgrading dependencies..." --weight=6
  120. ynh_install_app_dependencies $pkg_dependencies
  121. #=================================================
  122. # PHP-FPM CONFIGURATION
  123. #=================================================
  124. # Overwrite the PHP-FPM configuration only if it's allowed
  125. if [ $overwrite_phpfpm -eq 1 ]
  126. then
  127. ynh_script_progression --message="Upgrading PHP-FPM configuration..." --weight=3
  128. # Create a dedicated PHP-FPM config
  129. ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --dedicated_service
  130. fi
  131. #=================================================
  132. # NGINX CONFIGURATION
  133. #=================================================
  134. # Overwrite the NGINX configuration only if it's allowed
  135. if [ $overwrite_nginx -eq 1 ]
  136. then
  137. ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2
  138. # Create a dedicated NGINX config
  139. ynh_add_nginx_config
  140. fi
  141. #=================================================
  142. # SPECIFIC UPGRADE
  143. #=================================================
  144. # STOP SYSTEMD SERVICE
  145. #=================================================
  146. ynh_script_progression --message="Stopping a systemd service..." --weight=1
  147. ynh_systemd_action --service_name=pihole-FTL --action="stop" --log_path="/var/log/pihole-FTL.log"
  148. #=================================================
  149. # UPDATE PIHOLE-FTL
  150. #=================================================
  151. if [ "$upgrade_type" == "UPGRADE_APP" ]
  152. then
  153. ynh_script_progression --message="Upgrading PiHole-FTL..." --weight=35
  154. # Instead of downloading a binary file, we're going to compile it
  155. (
  156. cd "$FTL_temp_path"
  157. ynh_exec_warn_less cmake .
  158. ynh_exec_warn_less make
  159. ynh_exec_warn_less make install
  160. )
  161. ynh_secure_remove --file="$FTL_temp_path"
  162. fi
  163. #=================================================
  164. # UPDATE THE SCRIPTS
  165. #=================================================
  166. ynh_script_progression --message="Updating the scripts..." --weight=1
  167. pushd "${PI_HOLE_LOCAL_REPO}"
  168. install -o "${app}" -Dm755 -d "${PI_HOLE_INSTALL_DIR}"
  169. install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" gravity.sh
  170. install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" ./advanced/Scripts/*.sh
  171. install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" ./advanced/Scripts/COL_TABLE
  172. install -o "${app}" -Dm755 -t "${PI_HOLE_BIN_DIR}" pihole
  173. install -Dm644 ./advanced/bash-completion/pihole /etc/bash_completion.d/pihole
  174. popd
  175. #=================================================
  176. # UPDATE THE CONFIGS
  177. #=================================================
  178. ynh_script_progression --message="Updating the configs..." --weight=1
  179. install -d -m 0755 ${PI_HOLE_CONFIG_DIR}
  180. cp -f "../conf/dns-servers.conf" "$PI_HOLE_CONFIG_DIR/dns-servers.conf"
  181. chmod 644 "${PI_HOLE_CONFIG_DIR}/dns-servers.conf"
  182. # Overwrite pihole-FTL config file only if it's allowed
  183. if [ $overwrite_ftl -eq 1 ]
  184. then
  185. ynh_add_config --template="../conf/pihole-FTL.conf" --destination="$PI_HOLE_CONFIG_DIR/pihole-FTL.conf"
  186. fi
  187. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
  188. #=================================================
  189. # INSTALL SUDOER FILE
  190. #=================================================
  191. ynh_script_progression --message="Installing sudoer file..." --weight=1
  192. install -m 0640 ${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole.sudo /etc/sudoers.d/pihole
  193. echo "$app ALL=NOPASSWD: ${PI_HOLE_BIN_DIR}/pihole" >> /etc/sudoers.d/pihole
  194. #=================================================
  195. # UPDATE A CRON JOB
  196. #=================================================
  197. ynh_script_progression --message="Updating a cron job..." --weight=1
  198. install -D -m 644 -T -o root -g root ${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole.cron /etc/cron.d/pihole
  199. # Randomize gravity update time
  200. ynh_replace_string --match_string="59 1 " --replace_string="$((1 + RANDOM % 58)) $((3 + RANDOM % 2)) " --target_file="/etc/cron.d/pihole"
  201. # Randomize update checker time
  202. ynh_replace_string --match_string="59 17" --replace_string="$((1 + RANDOM % 58)) $((12 + RANDOM % 8))" --target_file="/etc/cron.d/pihole"
  203. # Remove Git usage for version. Which fails because we use here a release instead of master.
  204. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
  205. #=================================================
  206. # UPDATE LOGROTATE SCRIPT FOR PI-HOLE
  207. #=================================================
  208. ynh_script_progression --message="Updating logrotate script for PI-HOLE..." --weight=1
  209. install -D -m 644 -T "${PI_HOLE_LOCAL_REPO}"/advanced/Templates/logrotate "$PI_HOLE_CONFIG_DIR/logrotate"
  210. sed -i "/# su #/d;" "$PI_HOLE_CONFIG_DIR/logrotate"
  211. #=================================================
  212. # DISABLING DNSMASQ
  213. #=================================================
  214. ynh_script_progression --message="Disabling DNSMASQ..." --weight=1
  215. # Last version available
  216. # Stopped dnsmasq to replace it by pihole-FTL
  217. ynh_systemd_action --service_name=dnsmasq --action=stop
  218. # Disable the real dnsmasq service
  219. #ynh_exec_warn_less systemctl disable dnsmasq --quiet
  220. #=================================================
  221. # FINAL EXPORTS
  222. #=================================================
  223. setupVars="$PI_HOLE_CONFIG_DIR/setupVars.conf"
  224. # Overwrite the setupVars config file only if it's allowed
  225. if [ $overwrite_setupvars -eq 1 ]
  226. then
  227. ynh_script_progression --message="Final exports..." --weight=1
  228. # Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
  229. ynh_backup_if_checksum_is_different --file="$setupVars"
  230. # Get the default network interface
  231. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  232. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  233. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  234. echo "IPV6_ADDRESS=::1" >> $setupVars
  235. echo "PIHOLE_DNS_1=" >> $setupVars
  236. echo "PIHOLE_DNS_2=" >> $setupVars
  237. if [ $query_logging -eq 1 ]; then
  238. query_logging=true
  239. else
  240. query_logging=false
  241. fi
  242. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  243. echo "INSTALL_WEB=true" >> $setupVars
  244. echo "BLOCKING_ENABLED=true" >> $setupVars
  245. # Recalculate and store the checksum of the file for the next upgrade.
  246. ynh_store_file_checksum --file="$setupVars"
  247. fi
  248. #=================================================
  249. # ENABLING FTL
  250. #=================================================
  251. ynh_script_progression --message="Enable FTL..." --weight=1
  252. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  253. # Replace the service dnsmasq by pihole-FTL
  254. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  255. #ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  256. systemctl mask dnsmasq.service
  257. # Reload systemd config
  258. systemctl daemon-reload
  259. #=================================================
  260. # CREATE LOG FILES
  261. #=================================================
  262. ynh_script_progression --message="Creating log files..." --weight=1
  263. touch /var/log/{pihole,pihole-FTL}.log
  264. chmod 644 /var/log/{pihole,pihole-FTL}.log
  265. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  266. chown $dnsmasq_user:root /var/log/{pihole,pihole-FTL}.log
  267. #=================================================
  268. # BUILD THE LISTS WITH GRAVITY
  269. #=================================================
  270. ynh_script_progression --message="Building the lists with Gravity..." --weight=7
  271. cp -f "../conf/adlists.default" "$PI_HOLE_CONFIG_DIR/adlists.list"
  272. ynh_exec_warn_less $PI_HOLE_INSTALL_DIR/gravity.sh --force
  273. #=================================================
  274. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  275. #=================================================
  276. ynh_script_progression --message="Configuring DNS for the local domains..." --weight=7
  277. # Find the IP associated to the network interface
  278. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  279. # List all YunoHost domains
  280. while read perdomain
  281. do
  282. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  283. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  284. # And add a resolution on the local IP instead
  285. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  286. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  287. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  288. #=================================================
  289. # SET VERSIONS FOR THE FOOTER OF THE WEB INTERFACE
  290. #=================================================
  291. ynh_script_progression --message="Setting versions for the footer of the web interface..." --weight=1
  292. echo "master master master" > $PI_HOLE_CONFIG_DIR/localbranches
  293. echo "$(ynh_app_upstream_version) $pihole_adminlte_version $pihole_flt_version" | tee $PI_HOLE_CONFIG_DIR/{GitHubVersions,localversions} > /dev/null
  294. #=================================================
  295. # UPDATE CONF_REGEN HOOK
  296. #=================================================
  297. ynh_script_progression --message="Updating conf_regen hook..." --weight=1
  298. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  299. ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  300. systemctl daemon-reload
  301. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  302. #=================================================
  303. # GENERIC FINALIZATION
  304. #=================================================
  305. # INTEGRATE SERVICE IN YUNOHOST
  306. #=================================================
  307. ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  308. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log" --needs_exposed_ports 53 67
  309. #=================================================
  310. # START SYSTEMD SERVICE
  311. #=================================================
  312. ynh_script_progression --message="Starting a systemd service..." --weight=2
  313. ynh_systemd_action --service_name=pihole-FTL --action=restart --log_path="/var/log/pihole-FTL.log"
  314. #=================================================
  315. # RELOAD NGINX
  316. #=================================================
  317. ynh_script_progression --message="Reloading NGINX web server..." --weight=1
  318. ynh_systemd_action --service_name=nginx --action=reload
  319. #=================================================
  320. # DEACTIVE MAINTENANCE MODE
  321. #=================================================
  322. ynh_script_progression --message="Disabling maintenance mode..." --weight=5
  323. ynh_maintenance_mode_OFF
  324. #=================================================
  325. # SEND A README FOR THE ADMIN
  326. #=================================================
  327. # Get main domain and buid the url of the admin panel of the app.
  328. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  329. if [ $enable_dhcp -eq 1 ]
  330. then
  331. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with Pi-hole.
  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