mirror of
https://github.com/YunoHost-Apps/pihole_ynh.git
synced 2024-09-03 20:05:58 +02:00
375 lines
15 KiB
Bash
375 lines
15 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# MANAGE SCRIPT FAILURE
|
|
#=================================================
|
|
|
|
ynh_clean_setup () {
|
|
true
|
|
}
|
|
# Exit if an error occurs during the execution of the script
|
|
ynh_abort_if_errors
|
|
|
|
#=================================================
|
|
# RETRIEVE ARGUMENTS FROM THE MANIFEST
|
|
#=================================================
|
|
|
|
domain=$YNH_APP_ARG_DOMAIN
|
|
path_url=$YNH_APP_ARG_PATH
|
|
admin=$YNH_APP_ARG_ADMIN
|
|
query_logging=$YNH_APP_ARG_QUERY_LOGGING
|
|
enable_dhcp=$YNH_APP_ARG_ENABLE_DHCP
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
#=================================================
|
|
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
|
|
#=================================================
|
|
ynh_script_progression --message="Validating installation parameters..." --weight=2
|
|
|
|
final_path=/var/www/$app
|
|
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
|
|
|
|
# Register (book) web path
|
|
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
|
|
|
|
#=================================================
|
|
# STORE SETTINGS FROM MANIFEST
|
|
#=================================================
|
|
ynh_script_progression --message="Storing installation settings..." --weight=3
|
|
|
|
ynh_app_setting_set --app=$app --key=domain --value=$domain
|
|
ynh_app_setting_set --app=$app --key=path --value=$path_url
|
|
ynh_app_setting_set --app=$app --key=admin --value=$admin
|
|
ynh_app_setting_set --app=$app --key=query_logging --value=$query_logging
|
|
ynh_app_setting_set --app=$app --key=enable_dhcp --value=$enable_dhcp
|
|
|
|
ynh_app_setting_set --app=$app --key=overwrite_setupvars --value=1
|
|
ynh_app_setting_set --app=$app --key=overwrite_ftl --value=1
|
|
ynh_app_setting_set --app=$app --key=overwrite_nginx --value=1
|
|
ynh_app_setting_set --app=$app --key=overwrite_phpfpm --value=1
|
|
ynh_app_setting_set --app=$app --key=admin_mail_html --value=1
|
|
|
|
#=================================================
|
|
# STANDARD MODIFICATIONS
|
|
#=================================================
|
|
# FIND AND OPEN A PORT
|
|
#=================================================
|
|
ynh_script_progression --message="Finding an available port..." --weight=12
|
|
|
|
# Find an available port
|
|
port=$(ynh_find_port --port=4711)
|
|
if [ $port -gt 4720 ]
|
|
then
|
|
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."
|
|
fi
|
|
ynh_app_setting_set --app=$app --key=port --value=$port
|
|
|
|
# Disable the port 53 for upnp
|
|
ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
|
|
ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
|
|
|
|
# Open the UDP port 67 for dhcp
|
|
ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
|
|
|
|
#=================================================
|
|
# INSTALL DEPENDENCIES
|
|
#=================================================
|
|
ynh_script_progression --message="Installing dependencies..." --weight=12
|
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
|
|
|
#=================================================
|
|
# CREATE DEDICATED USER
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring system user..." --weight=2
|
|
|
|
# Create a system user
|
|
ynh_system_user_create --username=$app --home_dir="$final_path"
|
|
|
|
#=================================================
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
#=================================================
|
|
ynh_script_progression --message="Setting up source files..." --weight=4
|
|
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
|
# Download, check integrity, uncompress and patch the source from app.src
|
|
ynh_setup_source --dest_dir="$PI_HOLE_LOCAL_REPO" --source_id="pi-hole_Core"
|
|
ynh_setup_source --dest_dir="$final_path" --source_id=pi-hole_AdminLTE
|
|
FTL_temp_path=$(mktemp -d)
|
|
ynh_setup_source --dest_dir="$FTL_temp_path" --source_id="pi-hole_FTL"
|
|
|
|
chmod 750 "$final_path"
|
|
chmod -R o-rwx "$final_path"
|
|
chown -R $app:www-data "$final_path"
|
|
|
|
#=================================================
|
|
# PHP-FPM CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring PHP-FPM..." --weight=2
|
|
|
|
# Create a dedicated PHP-FPM config
|
|
ynh_add_fpm_config --usage=low --footprint=low --dedicated_service
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring NGINX web server..." --weight=2
|
|
|
|
# Create a dedicated NGINX config
|
|
ynh_add_nginx_config
|
|
|
|
#=================================================
|
|
# SPECIFIC SETUP
|
|
#=================================================
|
|
# INSTALLATION OF PIHOLE-FTL
|
|
#=================================================
|
|
ynh_script_progression --message="Installing PiHole-FTL..." --weight=30
|
|
|
|
# Instead of downloading a binary file, we're going to compile it
|
|
(
|
|
cd "$FTL_temp_path"
|
|
ynh_exec_warn_less cmake .
|
|
ynh_exec_warn_less make
|
|
ynh_exec_warn_less make install
|
|
)
|
|
|
|
ynh_secure_remove --file="$FTL_temp_path"
|
|
|
|
#=================================================
|
|
# INSTALL THE SCRIPTS
|
|
#=================================================
|
|
ynh_script_progression --message="Installing the scripts..." --weight=1
|
|
|
|
pushd "${PI_HOLE_LOCAL_REPO}"
|
|
install -o "${app}" -Dm755 -d "${PI_HOLE_INSTALL_DIR}"
|
|
install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" gravity.sh
|
|
install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" ./advanced/Scripts/*.sh
|
|
install -o "${app}" -Dm755 -t "${PI_HOLE_INSTALL_DIR}" ./advanced/Scripts/COL_TABLE
|
|
install -o "${app}" -Dm755 -t "${PI_HOLE_BIN_DIR}" pihole
|
|
install -Dm644 ./advanced/bash-completion/pihole /etc/bash_completion.d/pihole
|
|
popd
|
|
|
|
#=================================================
|
|
# INSTALL THE CONFIGS
|
|
#=================================================
|
|
ynh_script_progression --message="Installing the configs..." --weight=1
|
|
|
|
install -d -m 0755 ${PI_HOLE_CONFIG_DIR}
|
|
|
|
cp "../conf/dns-servers.conf" "$PI_HOLE_CONFIG_DIR/dns-servers.conf"
|
|
chmod 644 "${PI_HOLE_CONFIG_DIR}/dns-servers.conf"
|
|
|
|
ynh_add_config --template="../conf/pihole-FTL.conf" --destination="$PI_HOLE_CONFIG_DIR/pihole-FTL.conf"
|
|
|
|
install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
|
|
|
|
#=================================================
|
|
# INSTALL SUDOER FILE
|
|
#=================================================
|
|
ynh_script_progression --message="Installing sudoer file..." --weight=1
|
|
|
|
install -m 0640 ${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole.sudo /etc/sudoers.d/pihole
|
|
echo "$app ALL=NOPASSWD: ${PI_HOLE_BIN_DIR}/pihole" >> /etc/sudoers.d/pihole
|
|
|
|
#=================================================
|
|
# INSTALL A CRON JOB
|
|
#=================================================
|
|
ynh_script_progression --message="Installing a cron job..." --weight=1
|
|
|
|
install -D -m 644 -T -o root -g root ${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole.cron /etc/cron.d/pihole
|
|
|
|
# Randomize gravity update time
|
|
ynh_replace_string --match_string="59 1 " --replace_string="$((1 + RANDOM % 58)) $((3 + RANDOM % 2)) " --target_file="/etc/cron.d/pihole"
|
|
|
|
# Randomize update checker time
|
|
ynh_replace_string --match_string="59 17" --replace_string="$((1 + RANDOM % 58)) $((12 + RANDOM % 8))" --target_file="/etc/cron.d/pihole"
|
|
|
|
# Remove git usage for version. Which fails because we use here a release instead of master.
|
|
ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
|
|
|
|
#=================================================
|
|
# INSTALL LOGROTATE SCRIPT FOR PI-HOLE
|
|
#=================================================
|
|
ynh_script_progression --message="Installing logrotate script for PI-HOLE..." --weight=1
|
|
|
|
install -D -m 644 -T "${PI_HOLE_LOCAL_REPO}"/advanced/Templates/logrotate "$PI_HOLE_CONFIG_DIR/logrotate"
|
|
|
|
sed -i "/# su #/d;" "$PI_HOLE_CONFIG_DIR/logrotate"
|
|
|
|
#=================================================
|
|
# DISABLING DNSMASQ
|
|
#=================================================
|
|
ynh_script_progression --message="Disabling DNSMASQ..." --weight=1
|
|
|
|
# Last version available
|
|
# Stopped dnsmasq to replace it by pihole-FTL
|
|
ynh_systemd_action --service_name=dnsmasq --action=stop
|
|
|
|
# Disable the real dnsmasq service
|
|
#ynh_exec_warn_less systemctl disable dnsmasq --quiet
|
|
|
|
#=================================================
|
|
# FINAL EXPORTS
|
|
#=================================================
|
|
ynh_script_progression --message="Final exports..." --weight=1
|
|
|
|
setupVars="$PI_HOLE_CONFIG_DIR/setupVars.conf"
|
|
|
|
# Get the default network interface
|
|
main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
|
|
echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
|
|
echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
|
|
echo "IPV6_ADDRESS=::1" >> $setupVars
|
|
echo "PIHOLE_DNS_1=" >> $setupVars
|
|
echo "PIHOLE_DNS_2=" >> $setupVars
|
|
if [ $query_logging -eq 1 ]; then
|
|
query_logging=true
|
|
else
|
|
query_logging=false
|
|
fi
|
|
echo "QUERY_LOGGING=$query_logging" >> $setupVars
|
|
echo "INSTALL_WEB=true" >> $setupVars
|
|
|
|
# Calculate and store the config file checksum into the app settings
|
|
ynh_store_file_checksum --file="$setupVars"
|
|
|
|
#=================================================
|
|
# ENABLING FTL
|
|
#=================================================
|
|
ynh_script_progression --message="Enable FTL..." --weight=1
|
|
|
|
ynh_exec_warn_less systemctl enable pihole-FTL --quiet
|
|
|
|
# Replace the service dnsmasq by pihole-FTL
|
|
# That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
|
|
#ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
|
|
systemctl mask dnsmasq.service
|
|
|
|
# Reload systemd config
|
|
systemctl daemon-reload
|
|
|
|
#=================================================
|
|
# CREATE LOG FILES
|
|
#=================================================
|
|
ynh_script_progression --message="Creating log files..." --weight=1
|
|
|
|
touch /var/log/{pihole,pihole-FTL}.log
|
|
chmod 644 /var/log/{pihole,pihole-FTL}.log
|
|
dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
|
|
chown $dnsmasq_user:root /var/log/{pihole,pihole-FTL}.log
|
|
|
|
#=================================================
|
|
# BUILD THE LISTS WITH GRAVITY
|
|
#=================================================
|
|
ynh_script_progression --message="Building the lists with Gravity..." --weight=7
|
|
|
|
cp "../conf/adlists.default" "$PI_HOLE_CONFIG_DIR/adlists.list"
|
|
ynh_exec_warn_less $PI_HOLE_INSTALL_DIR/gravity.sh --force
|
|
|
|
#=================================================
|
|
# CONFIGURE DNS FOR THE LOCAL DOMAINS
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring DNS for the local domains..." --weight=7
|
|
|
|
# Find the IP associated to the network interface
|
|
localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
|
|
|
|
# List all YunoHost domains
|
|
while read perdomain
|
|
do
|
|
# Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
|
|
ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
|
|
|
|
# And add a resolution on the local IP instead
|
|
grep -q "^$localipv4.*$perdomain" /etc/hosts || \
|
|
echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
|
|
done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
|
|
|
|
#=================================================
|
|
# SET VERSIONS FOR THE FOOTER OF THE WEB INTERFACE
|
|
#=================================================
|
|
ynh_script_progression --message="Setting versions for the footer of the web interface..." --weight=1
|
|
|
|
echo "master master master" > $PI_HOLE_CONFIG_DIR/localbranches
|
|
echo "$(ynh_app_upstream_version) $pihole_adminlte_version $pihole_flt_version" | tee $PI_HOLE_CONFIG_DIR/{GitHubVersions,localversions} > /dev/null
|
|
|
|
#=================================================
|
|
# SET UP CONF_REGEN HOOK
|
|
#=================================================
|
|
ynh_script_progression --message="Setting up conf_regen hook..." --weight=1
|
|
|
|
cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
|
|
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
|
|
|
|
systemctl daemon-reload
|
|
ynh_exec_warn_less yunohost tools regen-conf dnsmasq
|
|
|
|
#=================================================
|
|
# GENERIC FINALISATION
|
|
#=================================================
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
#=================================================
|
|
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
|
|
|
|
yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log" --needs_exposed_ports 53 67
|
|
|
|
#=================================================
|
|
# START SYSTEMD SERVICE
|
|
#=================================================
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=2
|
|
|
|
ynh_systemd_action --service_name=pihole-FTL --action=restart --log_path="/var/log/pihole-FTL.log"
|
|
|
|
#=================================================
|
|
# SETUP SSOWAT
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring permissions..." --weight=2
|
|
|
|
ynh_permission_update --permission="main" --add="$admin" --remove="all_users"
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
ynh_script_progression --message="Reloading NGINX web server..." --weight=3
|
|
|
|
ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
#=================================================
|
|
# SEND A README FOR THE ADMIN
|
|
#=================================================
|
|
|
|
# Get main domain and buid the url of the admin panel of the app.
|
|
admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
|
|
|
|
if [ $enable_dhcp -eq 1 ]
|
|
then
|
|
dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
|
|
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__
|
|
|
|
"
|
|
else
|
|
dhcp_alert=""
|
|
fi
|
|
|
|
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__.
|
|
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__.
|
|
|
|
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
|
|
|
|
ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=install
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|