install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. # MANAGE SCRIPT FAILURE
  11. #=================================================
  12. ynh_clean_setup () {
  13. true
  14. }
  15. # Exit if an error occurs during the execution of the script
  16. ynh_abort_if_errors
  17. #=================================================
  18. # RETRIEVE ARGUMENTS FROM THE MANIFEST
  19. #=================================================
  20. domain=$YNH_APP_ARG_DOMAIN
  21. path_url=$YNH_APP_ARG_PATH
  22. admin=$YNH_APP_ARG_ADMIN
  23. query_logging=$YNH_APP_ARG_QUERY_LOGGING
  24. enable_dhcp=$YNH_APP_ARG_ENABLE_DHCP
  25. app=$YNH_APP_INSTANCE_NAME
  26. #=================================================
  27. # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
  28. #=================================================
  29. ynh_script_progression --message="Validating installation parameters..." --weight=2
  30. final_path=/var/www/$app
  31. test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
  32. # Register (book) web path
  33. ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
  34. #=================================================
  35. # STORE SETTINGS FROM MANIFEST
  36. #=================================================
  37. ynh_script_progression --message="Storing installation settings..." --weight=3
  38. ynh_app_setting_set --app=$app --key=domain --value=$domain
  39. ynh_app_setting_set --app=$app --key=path --value=$path_url
  40. ynh_app_setting_set --app=$app --key=admin --value=$admin
  41. ynh_app_setting_set --app=$app --key=query_logging --value=$query_logging
  42. ynh_app_setting_set --app=$app --key=enable_dhcp --value=$enable_dhcp
  43. ynh_app_setting_set --app=$app --key=overwrite_setupvars --value=1
  44. ynh_app_setting_set --app=$app --key=overwrite_ftl --value=1
  45. ynh_app_setting_set --app=$app --key=overwrite_nginx --value=1
  46. ynh_app_setting_set --app=$app --key=overwrite_phpfpm --value=1
  47. ynh_app_setting_set --app=$app --key=admin_mail_html --value=1
  48. #=================================================
  49. # STANDARD MODIFICATIONS
  50. #=================================================
  51. # FIND AND OPEN A PORT
  52. #=================================================
  53. ynh_script_progression --message="Finding an available port..." --weight=12
  54. # Find an available port
  55. port=$(ynh_find_port --port=4711)
  56. if [ $port -gt 4720 ]
  57. then
  58. ynh_die --message="The ports 4711 to 4720 are already in use. Pi-hole can't work on another port. Please try to free one of these ports."
  59. fi
  60. ynh_app_setting_set --app=$app --key=port --value=$port
  61. # Disable the port 53 for upnp
  62. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  63. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  64. #=================================================
  65. # INSTALL DEPENDENCIES
  66. #=================================================
  67. ynh_script_progression --message="Installing dependencies..." --weight=12
  68. ynh_install_app_dependencies $pkg_dependencies
  69. #=================================================
  70. # CREATE DEDICATED USER
  71. #=================================================
  72. ynh_script_progression --message="Configuring system user..." --weight=2
  73. # Create a system user
  74. ynh_system_user_create --username=$app --home_dir="$final_path"
  75. #=================================================
  76. # DOWNLOAD, CHECK AND UNPACK SOURCE
  77. #=================================================
  78. ynh_script_progression --message="Setting up source files..." --weight=4
  79. ynh_app_setting_set --app=$app --key=final_path --value=$final_path
  80. # Download, check integrity, uncompress and patch the source from app.src
  81. ynh_setup_source --dest_dir="$PI_HOLE_LOCAL_REPO" --source_id="pi-hole_Core"
  82. ynh_setup_source --dest_dir="$final_path" --source_id=pi-hole_AdminLTE
  83. FTL_temp_path=$(mktemp -d)
  84. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id="pi-hole_FTL"
  85. chmod 750 "$final_path"
  86. chmod -R o-rwx "$final_path"
  87. chown -R $app:www-data "$final_path"
  88. #=================================================
  89. # PHP-FPM CONFIGURATION
  90. #=================================================
  91. ynh_script_progression --message="Configuring PHP-FPM..." --weight=2
  92. # Create a dedicated PHP-FPM config
  93. ynh_add_fpm_config --usage=low --footprint=low --dedicated_service
  94. #=================================================
  95. # NGINX CONFIGURATION
  96. #=================================================
  97. ynh_script_progression --message="Configuring NGINX web server..." --weight=2
  98. # Create a dedicated NGINX config
  99. ynh_add_nginx_config
  100. #=================================================
  101. # SPECIFIC SETUP
  102. #=================================================
  103. # INSTALLATION OF PIHOLE-FTL
  104. #=================================================
  105. ynh_script_progression --message="Installing PiHole-FTL..." --weight=30
  106. # Instead of downloading a binary file, we're going to compile it
  107. (
  108. cd "$FTL_temp_path"
  109. ynh_exec_warn_less cmake .
  110. ynh_exec_warn_less make
  111. ynh_exec_warn_less make install
  112. )
  113. ynh_secure_remove --file="$FTL_temp_path"
  114. #=================================================
  115. # INSTALL THE SCRIPTS
  116. #=================================================
  117. ynh_script_progression --message="Installing the scripts..." --weight=1
  118. pushd "${PI_HOLE_LOCAL_REPO}"
  119. install -o "${app}" -Dm755 -d "${PI_HOLE_INSTALL_DIR}"
  120. install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" gravity.sh
  121. install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" ./advanced/Scripts/*.sh
  122. install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" ./advanced/Scripts/COL_TABLE
  123. install -o "${app}" -Dm755 -t "${PI_HOLE_BIN_DIR}" pihole
  124. install -Dm644 ./advanced/bash-completion/pihole /etc/bash_completion.d/pihole
  125. popd
  126. #=================================================
  127. # INSTALL THE CONFIGS
  128. #=================================================
  129. ynh_script_progression --message="Installing the configs..." --weight=1
  130. install -d -m 0755 ${PI_HOLE_CONFIG_DIR}
  131. cp "../conf/dns-servers.conf" "$PI_HOLE_CONFIG_DIR/dns-servers.conf"
  132. chmod 644 "${PI_HOLE_CONFIG_DIR}/dns-servers.conf"
  133. ynh_add_config --template="../conf/pihole-FTL.conf" --destination="$PI_HOLE_CONFIG_DIR/pihole-FTL.conf"
  134. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
  135. #=================================================
  136. # INSTALL SUDOER FILE
  137. #=================================================
  138. ynh_script_progression --message="Installing sudoer file..." --weight=1
  139. install -m 0640 ${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole.sudo /etc/sudoers.d/pihole
  140. echo "$app ALL=NOPASSWD: ${PI_HOLE_BIN_DIR}/pihole" >> /etc/sudoers.d/pihole
  141. #=================================================
  142. # INSTALL A CRON JOB
  143. #=================================================
  144. ynh_script_progression --message="Installing a cron job..." --weight=1
  145. install -D -m 644 -T -o root -g root ${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole.cron /etc/cron.d/pihole
  146. # Randomize gravity update time
  147. ynh_replace_string --match_string="59 1 " --replace_string="$((1 + RANDOM % 58)) $((3 + RANDOM % 2)) " --target_file="/etc/cron.d/pihole"
  148. # Randomize update checker time
  149. ynh_replace_string --match_string="59 17" --replace_string="$((1 + RANDOM % 58)) $((12 + RANDOM % 8))" --target_file="/etc/cron.d/pihole"
  150. # Remove git usage for version. Which fails because we use here a release instead of master.
  151. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
  152. #=================================================
  153. # INSTALL LOGROTATE SCRIPT FOR PI-HOLE
  154. #=================================================
  155. ynh_script_progression --message="Installing logrotate script for PI-HOLE..." --weight=1
  156. install -D -m 644 -T "${PI_HOLE_LOCAL_REPO}"/advanced/Templates/logrotate "$PI_HOLE_CONFIG_DIR/logrotate"
  157. sed -i "/# su #/d;" "$PI_HOLE_CONFIG_DIR/logrotate"
  158. #=================================================
  159. # DISABLING DNSMASQ
  160. #=================================================
  161. ynh_script_progression --message="Disabling DNSMASQ..." --weight=1
  162. # Last version available
  163. # Stopped dnsmasq to replace it by pihole-FTL
  164. ynh_systemd_action --service_name=dnsmasq --action=stop
  165. # Disable the real dnsmasq service
  166. #ynh_exec_warn_less systemctl disable dnsmasq --quiet
  167. #=================================================
  168. # FINAL EXPORTS
  169. #=================================================
  170. ynh_script_progression --message="Final exports..." --weight=1
  171. setupVars="$PI_HOLE_CONFIG_DIR/setupVars.conf"
  172. # Get the default network interface
  173. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  174. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  175. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  176. echo "IPV6_ADDRESS=::1" >> $setupVars
  177. echo "PIHOLE_DNS_1=" >> $setupVars
  178. echo "PIHOLE_DNS_2=" >> $setupVars
  179. if [ $query_logging -eq 1 ]; then
  180. query_logging=true
  181. else
  182. query_logging=false
  183. fi
  184. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  185. echo "INSTALL_WEB=true" >> $setupVars
  186. # Calculate and store the config file checksum into the app settings
  187. ynh_store_file_checksum --file="$setupVars"
  188. #=================================================
  189. # ENABLING FTL
  190. #=================================================
  191. ynh_script_progression --message="Enable FTL..." --weight=1
  192. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  193. # Replace the service dnsmasq by pihole-FTL
  194. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  195. #ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  196. systemctl mask dnsmasq.service
  197. # Reload systemd config
  198. systemctl daemon-reload
  199. #=================================================
  200. # CREATE LOG FILES
  201. #=================================================
  202. ynh_script_progression --message="Creating log files..." --weight=1
  203. touch /var/log/{pihole,pihole-FTL}.log
  204. chmod 644 /var/log/{pihole,pihole-FTL}.log
  205. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  206. chown $dnsmasq_user:root /var/log/{pihole,pihole-FTL}.log
  207. #=================================================
  208. # BUILD THE LISTS WITH GRAVITY
  209. #=================================================
  210. ynh_script_progression --message="Building the lists with Gravity..." --weight=7
  211. cp "../conf/adlists.default" "$PI_HOLE_CONFIG_DIR/adlists.list"
  212. ynh_exec_warn_less $PI_HOLE_INSTALL_DIR/gravity.sh --force
  213. #=================================================
  214. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  215. #=================================================
  216. ynh_script_progression --message="Configuring DNS for the local domains..." --weight=7
  217. # Find the IP associated to the network interface
  218. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  219. # List all YunoHost domains
  220. while read perdomain
  221. do
  222. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  223. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  224. # And add a resolution on the local IP instead
  225. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  226. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  227. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  228. #=================================================
  229. # ENABLE DHCP SERVER
  230. #=================================================
  231. ynh_script_progression --message="Enabling DHCP server..." --weight=1
  232. # Open the UDP port 67 for dhcp
  233. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  234. #=================================================
  235. # SET VERSIONS FOR THE FOOTER OF THE WEB INTERFACE
  236. #=================================================
  237. ynh_script_progression --message="Setting versions for the footer of the web interface..." --weight=1
  238. echo "master master master" > $PI_HOLE_CONFIG_DIR/localbranches
  239. echo "$(ynh_app_upstream_version) $pihole_adminlte_version $pihole_flt_version" | tee $PI_HOLE_CONFIG_DIR/{GitHubVersions,localversions} > /dev/null
  240. #=================================================
  241. # SET UP CONF_REGEN HOOK
  242. #=================================================
  243. ynh_script_progression --message="Setting up conf_regen hook..." --weight=1
  244. ynh_add_config --template="../conf/dnsmasq_regenconf_hook" --destination="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  245. systemctl daemon-reload
  246. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  247. #=================================================
  248. # GENERIC FINALISATION
  249. #=================================================
  250. # INTEGRATE SERVICE IN YUNOHOST
  251. #=================================================
  252. ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  253. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log" --needs_exposed_ports 53 67
  254. #=================================================
  255. # START SYSTEMD SERVICE
  256. #=================================================
  257. ynh_script_progression --message="Starting a systemd service..." --weight=2
  258. ynh_systemd_action --service_name=pihole-FTL --action=restart --log_path="/var/log/pihole-FTL.log"
  259. #=================================================
  260. # SETUP SSOWAT
  261. #=================================================
  262. ynh_script_progression --message="Configuring permissions..." --weight=2
  263. ynh_permission_update --permission="main" --add="$admin" --remove="all_users"
  264. #=================================================
  265. # RELOAD NGINX
  266. #=================================================
  267. ynh_script_progression --message="Reloading NGINX web server..." --weight=3
  268. ynh_systemd_action --service_name=nginx --action=reload
  269. #=================================================
  270. # SEND A README FOR THE ADMIN
  271. #=================================================
  272. # Get main domain and buid the url of the admin panel of the app.
  273. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  274. if [ $enable_dhcp -eq 1 ]
  275. then
  276. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  277. You should really read the __URL_TAG1__documentation about that__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md__URL_TAG3__
  278. "
  279. else
  280. dhcp_alert=""
  281. fi
  282. 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__.
  283. 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__.
  284. 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__." > mail_to_send
  285. ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=install
  286. #=================================================
  287. # END OF SCRIPT
  288. #=================================================
  289. ynh_script_progression --message="Installation of $app completed" --last