Shuffle some files around, change dependencies to make output clearer, change ::: for [i] to further clarify output.

Allow for  PH_VERBOSE to set -x on all scripts that it would be useful for

Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This commit is contained in:
Adam Warner 2022-08-29 13:17:22 +01:00
parent 2b60df6d2b
commit 97f81bae21
No known key found for this signature in database
GPG Key ID: 872950F3ECF2B173
11 changed files with 73 additions and 75 deletions

View File

@ -1,5 +1,3 @@
#!/command/with-contenv bash
s6-echo "Starting crond"
exec -c
fdmove -c 2 1 /usr/sbin/cron -f

View File

@ -1,6 +1,8 @@
#!/command/with-contenv bash
s6-echo "Starting lighttpd"
if [ "${PH_VERBOSE:-0}" -gt 0 ] ; then
set -x ;
fi
if [[ 1 -eq ${WEBLOGS_STDOUT:-0} ]]; then
#lighthttpd cannot use /dev/stdout https://redmine.lighttpd.net/issues/2731

View File

@ -1,6 +1,9 @@
#!/command/with-contenv bash
s6-echo "Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}"
if [ "${PH_VERBOSE:-0}" -gt 0 ] ; then
set -x ;
fi
# Remove possible leftovers from previous pihole-FTL processes
rm -f /dev/shm/FTL-* 2> /dev/null
rm /run/pihole/FTL.sock 2> /dev/null

View File

@ -1,4 +1,9 @@
#!/bin/bash
if [ "${PH_VERBOSE:-0}" -gt 0 ] ; then
set -x ;
fi
gravityDBfile="/etc/pihole/gravity.db"
config_file="/etc/pihole/pihole-FTL.conf"
# make a point to mention which config file we're checking, as breadcrumb to revisit if/when pihole-FTL.conf is succeeded by TOML

View File

@ -11,11 +11,11 @@ fi
# shellcheck source=/dev/null
SKIP_INSTALL=true . "${PIHOLE_INSTALL}"
echo " ::: Starting docker specific checks & setup for docker pihole/pihole"
echo " [i] Starting docker specific checks & setup for docker pihole/pihole"
# TODO:
#if [ ! -f /.piholeFirstBoot ] ; then
# echo " ::: Not first container startup so not running docker's setup, re-create container to run setup again"
# echo " [i] Not first container startup so not running docker's setup, re-create container to run setup again"
#else
# regular_setup_functions
#fi
@ -66,8 +66,12 @@ test_configs
[ -f /.piholeFirstBoot ] && rm /.piholeFirstBoot
echo "::: Docker start setup complete"
echo " [i] Docker start setup complete"
echo ""
pihole -v
echo " Container tag is: ${PIHOLE_DOCKER_TAG}"
echo ""
echo " [i] pihole-FTL ($FTL_CMD) will be started as ${DNSMASQ_USER}"
echo ""

View File

@ -13,7 +13,7 @@ modifyUser()
local currentId=$(id -u ${username})
[[ ${currentId} -eq ${newId} ]] && return
echo "Changing ID for user: ${username} (${currentId} => ${newId})"
echo " [i] Changing ID for user: ${username} (${currentId} => ${newId})"
usermod -o -u ${newId} ${username}
}
@ -25,7 +25,7 @@ modifyGroup()
local currentId=$(id -g ${groupname})
[[ ${currentId} -eq ${newId} ]] && return
echo "Changing ID for group: ${groupname} (${currentId} => ${newId})"
echo " [i] Changing ID for group: ${groupname} (${currentId} => ${newId})"
groupmod -o -g ${newId} ${groupname}
}

View File

@ -34,7 +34,7 @@ fix_capabilities() {
# Testing on Docker 20.10.14 with no caps set shows the following caps available to the container:
# Current: cap_chown,cap_dac_override,cap_fowner,cap_fsetid,cap_kill,cap_setgid,cap_setuid,cap_setpcap,cap_net_bind_service,cap_net_raw,cap_sys_chroot,cap_mknod,cap_audit_write,cap_setfcap=ep
# FTL can also use CAP_NET_ADMIN and CAP_SYS_NICE. If we try to set them when they haven't been explicitly enabled, FTL will not start. Test for them first:
echo " [i] Setting capabilites on pihole-FTL where possible"
/sbin/capsh --has-p=cap_chown 2>/dev/null && CAP_STR+=',CAP_CHOWN'
/sbin/capsh --has-p=cap_net_bind_service 2>/dev/null && CAP_STR+=',CAP_NET_BIND_SERVICE'
/sbin/capsh --has-p=cap_net_raw 2>/dev/null && CAP_STR+=',CAP_NET_RAW'
@ -43,6 +43,8 @@ fix_capabilities() {
if [[ ${CAP_STR} ]]; then
# We have the (some of) the above caps available to us - apply them to pihole-FTL
echo " [i] Applying the following caps to pihole-FTL:"
echo " ${CAP_STR:1}"
setcap ${CAP_STR:1}+ep "$(which pihole-FTL)" || ret=$?
if [[ $DHCP_READY == false ]] && [[ $DHCP_ACTIVE == true ]]; then
@ -54,13 +56,13 @@ fix_capabilities() {
fi
if [[ $ret -ne 0 && "${DNSMASQ_USER:-pihole}" != "root" ]]; then
echo "ERROR: Unable to set capabilities for pihole-FTL. Cannot run as non-root."
echo " If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"
echo " [!] ERROR: Unable to set capabilities for pihole-FTL. Cannot run as non-root."
echo " If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"
exit 1
fi
else
echo "WARNING: Unable to set capabilities for pihole-FTL."
echo " Please ensure that the container has the required capabilities."
echo " [!] WARNING: Unable to set capabilities for pihole-FTL."
echo " Please ensure that the container has the required capabilities."
exit 1
fi
}
@ -68,7 +70,7 @@ fix_capabilities() {
# shellcheck disable=SC2034
ensure_basic_configuration() {
echo " [i] Ensuring basic configuration by re-running select functions from basic-install.sh"
# Set Debian webserver variables for installConfigs
LIGHTTPD_USER="www-data"
LIGHTTPD_GROUP="www-data"
@ -78,7 +80,7 @@ ensure_basic_configuration() {
if [ ! -f "${setupVars}" ]; then
install -m 644 /dev/null "${setupVars}"
echo "Creating empty ${setupVars} file."
echo " [i] Creating empty ${setupVars} file."
# The following setting needs to exist else the web interface version won't show in pihole -v
change_setting "INSTALL_WEB_INTERFACE" "true"
fi
@ -129,13 +131,13 @@ validate_env() {
# Optional IPv6 is a valid address
if [[ -n "$FTLCONF_LOCAL_IPV6" ]] ; then
if [[ "$FTLCONF_LOCAL_IPV6" == 'kernel' ]] ; then
echo "ERROR: You passed in IPv6 with a value of 'kernel', this maybe because you do not have IPv6 enabled on your network"
echo " [!] ERROR: You passed in IPv6 with a value of 'kernel', this maybe because you do not have IPv6 enabled on your network"
unset FTLCONF_LOCAL_IPV6
exit 1
fi
if [[ "$(nc -6 -w1 -z "$FTLCONF_LOCAL_IPV6" 53 2>&1)" != "" ]] && ! ip route get "$FTLCONF_LOCAL_IPV6" > /dev/null ; then
echo "ERROR: FTLCONF_LOCAL_IPV6 Environment variable ($FTLCONF_LOCAL_IPV6) doesn't appear to be a valid IPv6 address"
echo " TIP: If your server is not IPv6 enabled just remove '-e FTLCONF_LOCAL_IPV6' from your docker container"
echo " [!] ERROR: FTLCONF_LOCAL_IPV6 Environment variable ($FTLCONF_LOCAL_IPV6) doesn't appear to be a valid IPv6 address"
echo " TIP: If your server is not IPv6 enabled just remove '-e FTLCONF_LOCAL_IPV6' from your docker container"
exit 1
fi
fi;
@ -161,12 +163,12 @@ setup_FTL_Interface(){
if [ "$interface" != 'eth0' ] ; then
interfaceType='custom'
fi;
echo "FTL binding to $interfaceType interface: $interface"
echo " [i] FTL binding to $interfaceType interface: $interface"
change_setting "PIHOLE_INTERFACE" "${interface}"
}
setup_FTL_CacheSize() {
local warning="WARNING: CUSTOM_CACHE_SIZE not used"
local warning=" [i] WARNING: CUSTOM_CACHE_SIZE not used"
local dnsmasq_pihole_01_location="/etc/dnsmasq.d/01-pihole.conf"
# Quietly exit early for empty or default
if [[ -z "${CUSTOM_CACHE_SIZE}" || "${CUSTOM_CACHE_SIZE}" == '10000' ]] ; then return ; fi
@ -186,7 +188,7 @@ setup_FTL_CacheSize() {
echo "$warning - $custom_cache_size is not a positive integer or zero"
return
fi
echo "Custom CUSTOM_CACHE_SIZE set to $custom_cache_size"
echo " [i] Custom CUSTOM_CACHE_SIZE set to $custom_cache_size"
change_setting "CACHE_SIZE" "$custom_cache_size"
sed -i "s/^cache-size=\s*[0-9]*/cache-size=$custom_cache_size/" ${dnsmasq_pihole_01_location}
@ -198,14 +200,14 @@ apply_FTL_Configs_From_Env(){
# setting defined here: https://docs.pi-hole.net/ftldns/configfile/
declare -px | grep FTLCONF_ | sed -E 's/declare -x FTLCONF_([^=]+)=\"(.+)\"/\1 \2/' | while read -r name value
do
echo "Applying pihole-FTL.conf setting $name=$value"
echo " [i] Applying pihole-FTL.conf setting $name=$value"
changeFTLsetting "$name" "$value"
done
}
setup_FTL_dhcp() {
if [ -z "${DHCP_START}" ] || [ -z "${DHCP_END}" ] || [ -z "${DHCP_ROUTER}" ]; then
echo "ERROR: Won't enable DHCP server because mandatory Environment variables are missing: DHCP_START, DHCP_END and/or DHCP_ROUTER"
echo " [!] ERROR: Won't enable DHCP server because mandatory Environment variables are missing: DHCP_START, DHCP_END and/or DHCP_ROUTER"
change_setting "DHCP_ACTIVE" "false"
else
change_setting "DHCP_ACTIVE" "${DHCP_ACTIVE}"
@ -221,14 +223,14 @@ setup_FTL_dhcp() {
setup_FTL_query_logging(){
if [ "${QUERY_LOGGING_OVERRIDE}" == "false" ]; then
echo "::: Disabling Query Logging"
echo " [i] Disabling Query Logging"
change_setting "QUERY_LOGGING" "$QUERY_LOGGING_OVERRIDE"
removeKey "${dnsmasqconfig}" log-queries
else
# If it is anything other than false, set it to true
change_setting "QUERY_LOGGING" "true"
# Set pihole logging on for good measure
echo "::: Enabling Query Logging"
echo " [i] Enabling Query Logging"
addKey "${dnsmasqconfig}" log-queries
fi
@ -255,13 +257,13 @@ setup_FTL_upstream_DNS(){
# For backward compatibility, if DNS1 and/or DNS2 are set, but PIHOLE_DNS_ is not, convert them to
# a semi-colon delimited string and store in PIHOLE_DNS_
# They are not used anywhere if PIHOLE_DNS_ is set already
[ -n "${DNS1}" ] && echo "Converting DNS1 to PIHOLE_DNS_" && PIHOLE_DNS_="$DNS1"
[[ -n "${DNS2}" && "${DNS2}" != "no" ]] && echo "Converting DNS2 to PIHOLE_DNS_" && PIHOLE_DNS_="$PIHOLE_DNS_;$DNS2"
[ -n "${DNS1}" ] && echo " [i] Converting DNS1 to PIHOLE_DNS_" && PIHOLE_DNS_="$DNS1"
[[ -n "${DNS2}" && "${DNS2}" != "no" ]] && echo " [i] Converting DNS2 to PIHOLE_DNS_" && PIHOLE_DNS_="$PIHOLE_DNS_;$DNS2"
fi
# Parse the PIHOLE_DNS variable, if it exists, and apply upstream servers to Pi-hole config
if [ -n "${PIHOLE_DNS_}" ]; then
echo "Setting DNS servers based on PIHOLE_DNS_ variable"
echo " [i] Setting DNS servers based on PIHOLE_DNS_ variable"
# Remove any PIHOLE_DNS_ entries from setupVars.conf, if they exist
sed -i '/PIHOLE_DNS_/d' /etc/pihole/setupVars.conf
# Split into an array (delimited by ;)
@ -295,12 +297,12 @@ setup_FTL_upstream_DNS(){
fi
fi
# If the above tests fail then this is an invalid DNS server
echo "Invalid entry detected in PIHOLE_DNS_: ${i}"
echo " [!] Invalid entry detected in PIHOLE_DNS_: ${i}"
fi
done
if [ $valid_entries -eq 0 ]; then
echo "No Valid entries detected in PIHOLE_DNS_. Aborting"
echo " [!] No Valid entries detected in PIHOLE_DNS_. Aborting"
exit 1
fi
else
@ -310,11 +312,11 @@ setup_FTL_upstream_DNS(){
setupVarsDNS="$(grep 'PIHOLE_DNS_' /etc/pihole/setupVars.conf || true)"
if [ -z "${setupVarsDNS}" ]; then
echo "Configuring default DNS servers: 8.8.8.8, 8.8.4.4"
echo " [i] Configuring default DNS servers: 8.8.8.8, 8.8.4.4"
change_setting "PIHOLE_DNS_1" "8.8.8.8"
change_setting "PIHOLE_DNS_2" "8.8.4.4"
else
echo "Existing DNS servers detected in setupVars.conf. Leaving them alone"
echo " [i] Existing DNS servers detected in setupVars.conf. Leaving them alone"
fi
fi
}
@ -353,12 +355,12 @@ setup_web_php_env() {
fi
done
echo "Added ENV to php:"
echo " [i] Added ENV to php:"
grep -E '(VIRTUAL_HOST|CORS_HOSTS|PHP_ERROR_LOG|PIHOLE_DOCKER_TAG|TZ)' "$PHP_ENV_CONFIG"
}
setup_web_port() {
local warning="WARNING: Custom WEB_PORT not used"
local warning=" [!] WARNING: Custom WEB_PORT not used"
# Quietly exit early for empty or default
if [[ -z "${WEB_PORT}" || "${WEB_PORT}" == '80' ]] ; then return ; fi
@ -372,8 +374,8 @@ setup_web_port() {
echo "$warning - $web_port is not within valid port range of 1-65535"
return
fi
echo "Custom WEB_PORT set to $web_port"
echo "INFO: Without proper router DNAT forwarding to $FTLCONF_LOCAL_IPV4:$web_port, you may not get any blocked websites on ads"
echo " [i] Custom WEB_PORT set to $web_port"
echo " [i] Without proper router DNAT forwarding to $FTLCONF_LOCAL_IPV4:$web_port, you may not get any blocked websites on ads"
# Update lighttpd's port
sed -i '/server.port\s*=\s*80\s*$/ s/80/'"${WEB_PORT}"'/g' /etc/lighttpd/lighttpd.conf
@ -386,11 +388,11 @@ setup_web_theme(){
if [ -n "${WEBTHEME}" ]; then
case "${WEBTHEME}" in
"default-dark" | "default-darker" | "default-light" | "default-auto" | "lcars")
echo "Setting Web Theme based on WEBTHEME variable, using value ${WEBTHEME}"
echo " [i] Setting Web Theme based on WEBTHEME variable, using value ${WEBTHEME}"
change_setting "WEBTHEME" "${WEBTHEME}"
;;
*)
echo "Invalid theme name supplied: ${WEBTHEME}, falling back to default-light."
echo " [!] Invalid theme name supplied: ${WEBTHEME}, falling back to default-light."
change_setting "WEBTHEME" "default-light"
;;
esac
@ -413,10 +415,10 @@ setup_web_password() {
setup_var_exists "WEBPASSWORD" && return
# Generate new random password
WEBPASSWORD=$(tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c 8)
echo "Assigning random password: $WEBPASSWORD"
echo " [i] Assigning random password: $WEBPASSWORD"
else
# ENV WEBPASSWORD_OVERRIDE is set and will be used
echo "::: Assigning password defined by Environment Variable"
echo " [i] Assigning password defined by Environment Variable"
# WEBPASSWORD="$WEBPASSWORD"
fi
@ -442,15 +444,15 @@ setup_ipv4_ipv6() {
ip_versions="IPv4"
sed -i '/use-ipv6.pl/ d' /etc/lighttpd/lighttpd.conf
fi;
echo "Using $ip_versions"
echo " [i] Using $ip_versions"
}
test_configs() {
set -e
echo -n '::: Testing lighttpd config: '
echo -n ' [i] Testing lighttpd config: '
lighttpd -t -f /etc/lighttpd/lighttpd.conf || exit 1
set +e
echo "::: All config checks passed, cleared for startup ..."
echo " [i] All config checks passed, cleared for startup ..."
}
setup_blocklists() {
@ -459,22 +461,21 @@ setup_blocklists() {
exit_string="(exiting ${FUNCNAME[0]} early)"
if [ -n "${skip_setup_blocklists}" ]; then
echo "::: skip_setup_blocklists requested ($exit_string)"
echo " [i] skip_setup_blocklists requested $exit_string"
return
fi
# 2. The adlist file exists already (restarted container or volume mounted list)
if [ -f "${adlistFile}" ]; then
echo "::: Preexisting ad list ${adlistFile} detected ($exit_string)"
cat "${adlistFile}"
echo " [i] Preexisting ad list ${adlistFile} detected $exit_string"
return
fi
echo "::: ${FUNCNAME[0]} now setting default blocklists up: "
echo "::: TIP: Use a docker volume for ${adlistFile} if you want to customize for first boot"
echo " [i] ${FUNCNAME[0]} now setting default blocklists up: "
echo " [i] TIP: Use a docker volume for ${adlistFile} if you want to customize for first boot"
installDefaultBlocklists
echo "::: Blocklists (${adlistFile}) now set to:"
echo " [i] Blocklists (${adlistFile}) now set to:"
cat "${adlistFile}"
}
@ -484,7 +485,7 @@ setup_var_exists() {
local REQUIRED_VALUE="[^\n]+"
fi
if grep -Pq "^${KEY}=${REQUIRED_VALUE}" "$setupVars"; then
echo "::: Pre existing ${KEY} found"
echo " [i] Pre existing ${KEY} found"
true
else
false

View File

@ -1,15 +0,0 @@
#!/bin/bash
# A shim to make busybox timeout take in debian style args
# v1 only need support for this style: `timeout 1 getent hosts github.com`
# Busybox args:
# Usage: timeout [-t SECS] [-s SIG] PROG ARGS
# Debian args:
# Usage: timeout [OPTION] DURATION COMMAND [ARG]...
# or: timeout [OPTION]
TIMEOUT=/usr/bin/timeout
SECS="${1}"
ARGS="${@:2}"
$TIMEOUT -t $SECS $ARGS

View File

@ -22,7 +22,7 @@ def run_and_stream_command_output():
print(line, end='')
build_result.wait()
if build_result.returncode != 0:
print(f' ::: Error running: {command}')
print(f' [i] Error running: {command}')
print(build_result.stderr)
return run_and_stream_command_output_inner

View File

@ -26,7 +26,7 @@ def test_ipv6_not_true_removes_ipv6(docker, slow, test_args, expected_ipv6, expe
IPV6_LINE = 'use-ipv6.pl'
WEB_CONFIG = '/etc/lighttpd/lighttpd.conf'
function = docker.run('. /bash_functions.sh ; setup_ipv4_ipv6')
function = docker.run('. /usr/local/bin/bash_functions.sh ; setup_ipv4_ipv6')
assert "Using {}".format(expected_stdout) in function.stdout
if expected_stdout == 'IPv4':
assert 'IPv6' not in function.stdout
@ -43,9 +43,9 @@ def test_overrides_default_web_port(docker, slow, test_args):
CONFIG_LINE = r'server.port\s*=\s*999'
WEB_CONFIG = '/etc/lighttpd/lighttpd.conf'
function = docker.run('. /bash_functions.sh ; eval `grep setup_web_port /usr/local/bin/_startup.sh`')
assert "Custom WEB_PORT set to 999" in function.stdout
assert "INFO: Without proper router DNAT forwarding to 127.0.0.1:999, you may not get any blocked websites on ads" in function.stdout
function = docker.run('. /usr/local/bin/bash_functions.sh ; eval `grep setup_web_port /usr/local/bin/_startup.sh`')
assert " [i] Custom WEB_PORT set to 999" in function.stdout
assert " [i] Without proper router DNAT forwarding to 127.0.0.1:999, you may not get any blocked websites on ads" in function.stdout
slow(lambda: re.search(CONFIG_LINE, docker.run(_cat(WEB_CONFIG)).stdout) != None)
@ -55,7 +55,7 @@ def test_overrides_default_web_port(docker, slow, test_args):
('-e WEB_PORT="99999"', 'WARNING: Custom WEB_PORT not used - 99999 is not within valid port range of 1-65535'),
])
def test_bad_input_to_web_port(docker, test_args, expected_error):
function = docker.run('. /bash_functions.sh ; eval `grep setup_web_port /usr/local/bin/_startup.sh`')
function = docker.run('. /usr/local/bin/bash_functions.sh ; eval `grep setup_web_port /usr/local/bin/_startup.sh`')
assert expected_error in function.stdout
@ -64,7 +64,7 @@ def test_overrides_default_custom_cache_size(docker, slow, test_args, cache_size
''' Changes the cache_size setting to increase or decrease the cache size for dnsmasq'''
CONFIG_LINE = r'cache-size\s*=\s*{}'.format(cache_size)
function = docker.run('echo ${CUSTOM_CACHE_SIZE};. ./bash_functions.sh; echo ${CUSTOM_CACHE_SIZE}; eval `grep setup_FTL_CacheSize /usr/local/bin/_startup.sh`')
function = docker.run('echo ${CUSTOM_CACHE_SIZE};. ./usr/local/bin/bash_functions.sh; echo ${CUSTOM_CACHE_SIZE}; eval `grep setup_FTL_CacheSize /usr/local/bin/_startup.sh`')
assert "Custom CUSTOM_CACHE_SIZE set to {}".format(cache_size) in function.stdout
slow(lambda: re.search(CONFIG_LINE, docker.run(_cat(DNSMASQ_CONFIG_LOC)).stdout) != None)
@ -113,7 +113,7 @@ expected_debian_lines = [
def test_debian_setup_php_env(docker, expected_lines, repeat_function):
''' confirm all expected output is there and nothing else '''
for _ in range(repeat_function):
docker.run('. /bash_functions.sh ; eval `grep setup_php_env /usr/local/bin/_startup.sh`').stdout
docker.run('. /usr/local/bin/bash_functions.sh ; eval `grep setup_php_env /usr/local/bin/_startup.sh`').stdout
for expected_line in expected_lines:
search_config_cmd = "grep -c '{}' /etc/lighttpd/conf-enabled/15-fastcgi-php.conf".format(expected_line)
search_config_count = docker.run(search_config_cmd)
@ -152,7 +152,7 @@ def test_env_always_updates_password(docker, args_env, test_args):
'''When a user sets the WEBPASSWORD environment variable, ensure it always sets the password'''
function = docker.run(CMD_SETUP_WEB_PASSWORD)
assert '::: Assigning password defined by Environment Variable' in function.stdout
assert ' [i] Assigning password defined by Environment Variable' in function.stdout
@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')])