install 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/bin/bash
  2. #=================================================
  3. # IMPORT GENERIC HELPERS
  4. #=================================================
  5. source _common.sh
  6. source /usr/share/yunohost/helpers
  7. #=================================================
  8. # INITIALIZE AND STORE SETTINGS
  9. #=================================================
  10. ynh_app_setting_set --app="$app" --key="overwrite_setupvars" --value=1
  11. ynh_app_setting_set --app="$app" --key="overwrite_ftl" --value=1
  12. #=================================================
  13. # CHECK AVAILABLE PORT
  14. #=================================================
  15. _configure_ports
  16. #=================================================
  17. # DOWNLOAD, CHECK AND UNPACK SOURCE
  18. #=================================================
  19. ynh_script_progression --message="Setting up source files..." --weight=4
  20. # Download, check integrity, uncompress and patch the source from app.src
  21. ynh_setup_source --dest_dir="$PI_HOLE_LOCAL_REPO"
  22. ynh_setup_source --source_id="pi-hole_web" --dest_dir="$install_dir/web"
  23. ynh_setup_source --source_id="pi-hole_ftl" --dest_dir="$install_dir/ftl"
  24. chmod -R o-rwx "$install_dir"
  25. chown -R "$app:www-data" "$install_dir"
  26. touch /var/log/{pihole,pihole-FTL}.log
  27. chmod 644 /var/log/{pihole,pihole-FTL}.log
  28. chown "$dnsmasq_user:root" /var/log/{pihole,pihole-FTL}.log
  29. #=================================================
  30. # INSTALLATION OF PIHOLE-FTL
  31. #=================================================
  32. ynh_script_progression --message="Building PiHole-FTL..." --weight=30
  33. # -------------------------------------------------
  34. # Build & install mbedTLS >= 3.x (required for FTL v6)
  35. # -------------------------------------------------
  36. mbedtls_dir="$install_dir/mbedtls"
  37. ynh_setup_source --source_id="mbedtls" --dest_dir="$mbedtls_dir"
  38. # Enable pthread support
  39. sed -i '/#define MBEDTLS_THREADING_C/s@^//@@' "$mbedtls_dir/include/mbedtls/mbedtls_config.h"
  40. sed -i '/#define MBEDTLS_THREADING_PTHREAD/s@^//@@' "$mbedtls_dir/include/mbedtls/mbedtls_config.h"
  41. mkdir -p "$mbedtls_dir/build"
  42. pushd "$mbedtls_dir/build"
  43. ynh_exec_warn_less cmake .. \
  44. -DENABLE_TESTING=OFF \
  45. -DENABLE_PROGRAMS=OFF
  46. ynh_exec_warn_less make -j"$(nproc)"
  47. ynh_exec_warn_less make install
  48. popd
  49. ldconfig
  50. #=================================================
  51. # INSTALLATION OF PIHOLE-FTL (precompiled binary)
  52. #=================================================
  53. ynh_script_progression --message="Installing PiHole-FTL (binary)..." --weight=30
  54. ftl_bin_dir="$install_dir/ftl_bin"
  55. ynh_setup_source --source_id="pi-hole_ftl" --dest_dir="$ftl_bin_dir"
  56. # The downloaded asset is the binary itself (not a tarball)
  57. install -m 0755 "$ftl_bin_dir" /usr/bin/pihole-FTL
  58. # Optional but usually needed: allow binding to low ports (53)
  59. setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/pihole-FTL || true
  60. ynh_secure_remove --file="$ftl_bin_dir"
  61. ynh_secure_remove --file="$mbedtls_dir"
  62. #=================================================
  63. # INSTALL THE SCRIPTS
  64. #=================================================
  65. ynh_script_progression --message="Installing Pihole..." --weight=1
  66. install -o "$app" -Dm755 -d "$PI_HOLE_INSTALL_DIR"
  67. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/gravity.sh"
  68. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/advanced/Scripts"/*.sh
  69. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/advanced/Scripts/COL_TABLE"
  70. install -Dm644 -t /etc/bash_completion.d/ "$PI_HOLE_LOCAL_REPO/advanced/bash-completion/pihole"
  71. install -o "$app" -Dm755 -t "$PI_HOLE_BIN_DIR" "$PI_HOLE_LOCAL_REPO/pihole"
  72. #=================================================
  73. # INSTALL THE CONFIGS
  74. #=================================================
  75. ynh_script_progression --message="Installing $app's configuration files..." --weight=1
  76. install -d -m 0755 "$PI_HOLE_CONFIG_DIR"
  77. ynh_add_config --template="dns-servers.conf" --destination="$PI_HOLE_CONFIG_DIR/dns-servers.conf"
  78. ynh_add_config --template="pihole-FTL.conf" --destination="$PI_HOLE_CONFIG_DIR/pihole-FTL.conf"
  79. ynh_add_config --template="setupVars.conf" --destination="$PI_HOLE_CONFIG_DIR/setupVars.conf"
  80. chmod 644 "${PI_HOLE_CONFIG_DIR}/dns-servers.conf"
  81. #=================================================
  82. # SET VERSIONS FOR THE FOOTER OF THE WEB INTERFACE
  83. #=================================================
  84. ynh_script_progression --message="Setting versions for the footer of the web interface..." --weight=1
  85. echo "master master master" > "$PI_HOLE_CONFIG_DIR/localbranches"
  86. echo "$(ynh_app_upstream_version) $pihole_adminlte_version $pihole_flt_version" \
  87. | tee "$PI_HOLE_CONFIG_DIR/"{GitHubVersions,localversions} > /dev/null
  88. #=================================================
  89. # BUILD THE LISTS WITH GRAVITY
  90. #=================================================
  91. ynh_script_progression --message="Building the lists with Gravity..." --weight=7
  92. ynh_add_config --template="adlists.default" --destination="$PI_HOLE_CONFIG_DIR/adlists.list"
  93. ynh_exec_warn_less "$PI_HOLE_INSTALL_DIR/gravity.sh" --force
  94. #=================================================
  95. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  96. #=================================================
  97. ynh_script_progression --message="Configuring DNS for the local domains..." --weight=7
  98. # List all YunoHost domains
  99. while read -r perdomain; do
  100. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  101. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  102. # And add a resolution on the local IP instead
  103. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  104. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  105. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  106. #=================================================
  107. # DISABLING DNSMASQ
  108. #=================================================
  109. ynh_script_progression --message="Disabling Dnsmasq in system and yunohost..." --weight=1
  110. # Stop dnsmasq to replace it by pihole-FTL
  111. ynh_systemd_action --service_name=dnsmasq --action=stop
  112. # Replace the service dnsmasq by pihole-FTL
  113. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  114. #ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  115. systemctl mask dnsmasq.service
  116. # Reload systemd config
  117. systemctl daemon-reload
  118. # Workaround for strings to not be replaced
  119. a_range="__A_RANGE__"
  120. b_range="__B_RANGE__"
  121. gateway="__GATEWAY__"
  122. ynh_add_config --template="dnsmasq_regenconf_hook" --destination="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  123. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  124. #=================================================
  125. # SYSTEM CONFIGURATION
  126. #=================================================
  127. ynh_script_progression --message="Adding system configurations related to $app..." --weight=1
  128. # Create a dedicated PHP-FPM config
  129. ynh_add_fpm_config
  130. # Create a dedicated NGINX config
  131. ynh_add_nginx_config
  132. # Create sudoers config
  133. _add_sudoers_config
  134. _add_cron_jobs
  135. _add_logrotate_config
  136. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
  137. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL-prestart.sh" "${PI_HOLE_INSTALL_DIR}/pihole-FTL-prestart.sh"
  138. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL-poststop.sh" "${PI_HOLE_INSTALL_DIR}/pihole-FTL-poststop.sh"
  139. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  140. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log" --needs_exposed_ports 53 67
  141. #=================================================
  142. # START SYSTEMD SERVICE
  143. #=================================================
  144. ynh_script_progression --message="Starting $app's systemd service..." --weight=2
  145. ynh_systemd_action --service_name="pihole-FTL" --action=restart --log_path="/var/log/pihole-FTL.log"
  146. #=================================================
  147. # END OF SCRIPT
  148. #=================================================
  149. ynh_script_progression --message="Installation of $app completed" --last