upgrade 17 KB

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