upgrade 16 KB

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