upgrade 15 KB

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