install 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. # Get the default network interface
  13. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  14. # Get the dnsmasq user to set log files permissions
  15. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  16. if [ "$query_logging" -eq 1 ]; then
  17. query_logging_str=true
  18. else
  19. query_logging_str=false
  20. fi
  21. #=================================================
  22. # CHECK AVAILABLE PORT
  23. #=================================================
  24. _configure_ports
  25. #=================================================
  26. # DOWNLOAD, CHECK AND UNPACK SOURCE
  27. #=================================================
  28. ynh_script_progression --message="Setting up source files..." --weight=4
  29. # Download, check integrity, uncompress and patch the source from app.src
  30. ynh_setup_source --source_id="pi-hole_core" --dest_dir="$PI_HOLE_LOCAL_REPO"
  31. ynh_setup_source --source_id="pi-hole_web" --dest_dir="$install_dir/web"
  32. ynh_setup_source --source_id="pi-hole_ftl" --dest_dir="$install_dir/ftl"
  33. chmod -R o-rwx "$install_dir"
  34. chown -R "$app:www-data" "$install_dir"
  35. touch /var/log/{pihole,pihole-FTL}.log
  36. chmod 644 /var/log/{pihole,pihole-FTL}.log
  37. chown "$dnsmasq_user:root" /var/log/{pihole,pihole-FTL}.log
  38. #=================================================
  39. # INSTALLATION OF PIHOLE-FTL
  40. #=================================================
  41. ynh_script_progression --message="Building PiHole-FTL..." --weight=30
  42. # Instead of downloading a binary file, we're going to compile it
  43. pushd "$install_dir/ftl"
  44. ynh_exec_warn_less cmake .
  45. ynh_exec_warn_less make
  46. ynh_exec_warn_less make install
  47. popd
  48. ynh_secure_remove --file="$install_dir/ftl"
  49. #=================================================
  50. # INSTALL THE SCRIPTS
  51. #=================================================
  52. ynh_script_progression --message="Installing Pihole..." --weight=1
  53. install -o "$app" -Dm755 -d "$PI_HOLE_INSTALL_DIR"
  54. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/gravity.sh"
  55. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/advanced/Scripts"/*.sh
  56. install -o "$app" -Dm755 -t "$PI_HOLE_INSTALL_DIR" "$PI_HOLE_LOCAL_REPO/advanced/Scripts/COL_TABLE"
  57. install -Dm644 -t /etc/bash_completion.d/pihole "$PI_HOLE_LOCAL_REPO/advanced/bash-completion/pihole"
  58. install -o "$app" -Dm755 -t "$PI_HOLE_BIN_DIR" "$PI_HOLE_LOCAL_REPO/pihole"
  59. #=================================================
  60. # INSTALL THE CONFIGS
  61. #=================================================
  62. ynh_script_progression --message="Installing $app's configuration files..." --weight=1
  63. install -d -m 0755 "$PI_HOLE_CONFIG_DIR"
  64. ynh_add_config --template="dns-servers.conf" --destination="$PI_HOLE_CONFIG_DIR/dns-servers.conf"
  65. ynh_add_config --template="pihole-FTL.conf" --destination="$PI_HOLE_CONFIG_DIR/pihole-FTL.conf"
  66. chmod 644 "${PI_HOLE_CONFIG_DIR}/dns-servers.conf"
  67. {
  68. echo "PIHOLE_INTERFACE=$main_iface"
  69. echo "IPV4_ADDRESS=127.0.0.1"
  70. echo "IPV6_ADDRESS=::1"
  71. echo "PIHOLE_DNS_1="
  72. echo "PIHOLE_DNS_2="
  73. echo "QUERY_LOGGING=$query_logging_str"
  74. echo "INSTALL_WEB=true"
  75. echo "BLOCKING_ENABLED=true"
  76. } > "$PI_HOLE_CONFIG_DIR/setupVars.conf"
  77. ynh_store_file_checksum --file="$PI_HOLE_CONFIG_DIR/setupVars.conf"
  78. #=================================================
  79. # SET VERSIONS FOR THE FOOTER OF THE WEB INTERFACE
  80. #=================================================
  81. ynh_script_progression --message="Setting versions for the footer of the web interface..." --weight=1
  82. echo "master master master" > "$PI_HOLE_CONFIG_DIR/localbranches"
  83. echo "$(ynh_app_upstream_version) $pihole_adminlte_version $pihole_flt_version" \
  84. | tee "$PI_HOLE_CONFIG_DIR/"{GitHubVersions,localversions} > /dev/null
  85. #=================================================
  86. # BUILD THE LISTS WITH GRAVITY
  87. #=================================================
  88. ynh_script_progression --message="Building the lists with Gravity..." --weight=7
  89. ynh_add_config --template="adlists.default" --destination="$PI_HOLE_CONFIG_DIR/adlists.list"
  90. ynh_exec_warn_less "$PI_HOLE_INSTALL_DIR/gravity.sh" --force
  91. #=================================================
  92. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  93. #=================================================
  94. ynh_script_progression --message="Configuring DNS for the local domains..." --weight=7
  95. # Find the IP associated to the network interface
  96. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  97. # List all YunoHost domains
  98. while read -r perdomain; do
  99. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  100. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  101. # And add a resolution on the local IP instead
  102. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  103. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  104. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  105. #=================================================
  106. # DISABLING DNSMASQ
  107. #=================================================
  108. ynh_script_progression --message="Disabling Dnsmasq in system and yunohost..." --weight=1
  109. # Stop dnsmasq to replace it by pihole-FTL
  110. ynh_systemd_action --service_name=dnsmasq --action=stop
  111. # Replace the service dnsmasq by pihole-FTL
  112. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  113. #ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  114. systemctl mask dnsmasq.service
  115. # Reload systemd config
  116. systemctl daemon-reload
  117. # Workaround for strings to not be replaced
  118. a_range="__A_RANGE__"
  119. b_range="__B_RANGE__"
  120. gateway="__GATEWAY__"
  121. ynh_add_config --template="dnsmasq_regenconf_hook" --destination="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  122. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  123. #=================================================
  124. # SYSTEM CONFIGURATION
  125. #=================================================
  126. ynh_script_progression --message="Adding system configurations related to $app..." --weight=1
  127. # Create a dedicated PHP-FPM config
  128. ynh_add_fpm_config
  129. # Create a dedicated NGINX config
  130. ynh_add_nginx_config
  131. # Create sudoers config
  132. _add_sudoers_config
  133. _add_cron_jobs
  134. _add_logrotate_config
  135. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
  136. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  137. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log" --needs_exposed_ports 53 67
  138. #=================================================
  139. # START SYSTEMD SERVICE
  140. #=================================================
  141. ynh_script_progression --message="Starting $app's systemd service..." --weight=2
  142. ynh_systemd_action --service_name="pihole-FTL" --action=restart --log_path="/var/log/pihole-FTL.log"
  143. #=================================================
  144. # END OF SCRIPT
  145. #=================================================
  146. ynh_script_progression --message="Installation of $app completed" --last