upgrade 17 KB

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