Skip to content

Commit

Permalink
Merge pull request #54 from diginc/pihole1.9.2
Browse files Browse the repository at this point in the history
Pihole 1.9.2 dockerized
  • Loading branch information
diginc authored Oct 7, 2016
2 parents e8b3216 + b3df863 commit cf42873
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 182 deletions.
3 changes: 3 additions & 0 deletions alpine.docker
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FROM alpine:edge
MAINTAINER [email protected] <[email protected]>

ENV IMAGE alpine

RUN apk add --update \
dnsmasq \
nginx \
Expand Down Expand Up @@ -57,6 +59,7 @@ RUN grep -q '^user=root' || echo 'user=root' >> /etc/dnsmasq.conf
ENV PHP_ENV_CONFIG '/etc/php5/fpm.d/envs.conf'
ENV PHP_ERROR_LOG '/var/log/nginx/error.log'
COPY ./alpine/start.sh /
COPY ./common_start.sh /

# IPV6 disable flag for networks/devices that do not support it
ENV IPv6 True
Expand Down
56 changes: 23 additions & 33 deletions alpine/gravity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,26 @@ function helpFunc()
exit 1
}

piholeIPfile=/etc/pihole/piholeIP
piholeIPv6file=/etc/pihole/.useIPv6

adListFile=/etc/pihole/adlists.list
adListDefault=/etc/pihole/adlists.default
whitelistScript=/opt/pihole/whitelist.sh
blacklistScript=/opt/pihole/blacklist.sh

if [[ -f ${piholeIPfile} ]];then
# If the file exists, it means it was exported from the installation script and we should use that value instead of detecting it in this script
piholeIP=$(cat ${piholeIPfile})
#rm $piholeIPfile
else
# Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script
IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
piholeIPCIDR=$(ip -o -f inet addr show dev "$IPv4dev" | awk '{print $4}' | awk 'END {print}')
piholeIP=${piholeIPCIDR%/*}
fi

if [[ -f ${piholeIPv6file} ]];then
# If the file exists, then the user previously chose to use IPv6 in the automated installer
piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }')
fi
#Source the setupVars from install script for the IP
. /etc/pihole/setupVars.conf
#Remove the /* from the end of the IPv4addr.
IPv4addr=${IPv4addr%/*}

# Variables for various stages of downloading and formatting the list
## Nate 3/26/2016 - Commented unused variables
basename=pihole
piholeDir=/etc/${basename}
adList=${piholeDir}/gravity.list
#blacklist=$piholeDir/blacklist.txt
#whitelist=$piholeDir/whitelist.txt
#latentWhitelist=$piholeDir/latentWhitelist.txt
justDomainsExtension=domains
matterandlight=${basename}.0.matterandlight.txt
matterAndLight=${basename}.0.matterandlight.txt
supernova=${basename}.1.supernova.txt
eventHorizon=${basename}.2.eventHorizon.txt
accretionDisc=${basename}.3.accretionDisc.txt
#eyeOfTheNeedle=$basename.4.wormhole.txt

# After setting defaults, check if there's local overrides
if [[ -r ${piholeDir}/pihole.conf ]];then
Expand Down Expand Up @@ -213,10 +195,10 @@ function gravity_Schwarzchild() {
echo "::: "
# Find all active domains and compile them into one file and remove CRs
echo -n "::: Aggregating list of domains..."
truncate -s 0 ${piholeDir}/${matterandlight}
truncate -s 0 ${piholeDir}/${matterAndLight}
for i in "${activeDomains[@]}"
do
cat "$i" | tr -d '\r' >> ${piholeDir}/${matterandlight}
cat "$i" | tr -d '\r' >> ${piholeDir}/${matterAndLight}
done
echo " done!"
}
Expand Down Expand Up @@ -264,17 +246,23 @@ function gravity_unique() {
function gravity_hostFormat() {
# Format domain list as "192.168.x.x domain.com"
echo "::: Formatting domains into a HOSTS file..."
hostname=$(</etc/hostname)
if [[ -f /etc/hostname ]]; then
hostname=$(</etc/hostname)
elif [ -x "$(command -v hostname)" ]; then
hostname=$(hostname -f)
else
echo "::: Error: Unable to determine fully qualified domain name of host"
fi
# If there is a value in the $piholeIPv6, then IPv6 will be used, so the awk command modified to create a line for both protocols
if [[ -n ${piholeIPv6} ]];then
# Add hostname and dummy domain to the top of gravity.list to make ping result return a friendlier looking domain! Also allows for an easy way to access the Pi-hole admin console (pi.hole/admin)
echo -e "$piholeIP $hostname\n$piholeIPv6 $hostname\n$piholeIP pi.hole\n$piholeIPv6 pi.hole" > ${piholeDir}/${accretionDisc}
cat ${piholeDir}/${eventHorizon} | awk -v ipv4addr="$piholeIP" -v ipv6addr="$piholeIPv6" '{sub(/\r$/,""); print ipv4addr" "$0"\n"ipv6addr" "$0}' >> ${piholeDir}/${accretionDisc}
echo -e "$IPv4addr $hostname\n$piholeIPv6 $hostname\n$IPv4addr pi.hole\n$piholeIPv6 pi.hole" > ${piholeDir}/${accretionDisc}
cat ${piholeDir}/${eventHorizon} | awk -v ipv4addr="$IPv4addr" -v ipv6addr="$piholeIPv6" '{sub(/\r$/,""); print ipv4addr" "$0"\n"ipv6addr" "$0}' >> ${piholeDir}/${accretionDisc}
else
# Otherwise, just create gravity.list as normal using IPv4
# Add hostname and dummy domain to the top of gravity.list to make ping result return a friendlier looking domain! Also allows for an easy way to access the Pi-hole admin console (pi.hole/admin)
echo -e "$piholeIP $hostname\n$piholeIP pi.hole" > ${piholeDir}/${accretionDisc}
cat ${piholeDir}/${eventHorizon} | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >> ${piholeDir}/${accretionDisc}
echo -e "$IPv4addr $hostname\n$IPv4addr pi.hole" > ${piholeDir}/${accretionDisc}
cat ${piholeDir}/${eventHorizon} | awk -v ipv4addr="$IPv4addr" '{sub(/\r$/,""); print ipv4addr" "$0}' >> ${piholeDir}/${accretionDisc}
fi

# Copy the file over as /etc/pihole/gravity.list so dnsmasq can use it
Expand All @@ -301,7 +289,10 @@ function gravity_advanced() {
# This helps with that and makes it easier to read
# It also helps with debugging so each stage of the script can be researched more in depth
echo -n "::: Formatting list of domains to remove comments...."
awk '($1 !~ /^#/) { if (NF>1) {print $2} else {print $1}}' ${piholeDir}/${matterandlight} | sed -nr -e 's/\.{2,}/./g' -e '/\./p' > ${piholeDir}/${supernova}
#awk '($1 !~ /^#/) { if (NF>1) {print $2} else {print $1}}' ${piholeDir}/${matterAndLight} | sed -nr -e 's/\.{2,}/./g' -e '/\./p' > ${piholeDir}/${supernova}
#Above line does not correctly grab domains where comment is on the same line (e.g 'addomain.com #comment')
#Add additional awk command to read all lines up to a '#', and then continue as we were
cat ${piholeDir}/${matterAndLight} | awk -F'#' '{print $1}' | awk '($1 !~ /^#/) { if (NF>1) {print $2} else {print $1}}' | sed -nr -e 's/\.{2,}/./g' -e '/\./p' > ${piholeDir}/${supernova}
echo " done!"

numberOf=$(wc -l < ${piholeDir}/${supernova})
Expand Down Expand Up @@ -349,14 +340,13 @@ do
esac
done

#Overwrite adlists.default from /etc/.pihole in case any changes have been made. Changes should be saved in /etc/adlists.list

if [[ ${forceGrav} == true ]]; then
echo -n "::: Deleting exising list cache..."
${SUDO} rm /etc/pihole/list.*
echo " done!"
fi

#Overwrite adlists.default from /etc/.pihole in case any changes have been made. Changes should be saved in /etc/adlists.list
${SUDO} cp /etc/.pihole/adlists.default /etc/pihole/adlists.default
gravity_collapse
gravity_spinup
Expand Down
67 changes: 20 additions & 47 deletions alpine/start.sh
Original file line number Diff line number Diff line change
@@ -1,59 +1,32 @@
#!/bin/sh
if [ -z "$ServerIP" ] ; then
echo "ERROR: To function correctly you must pass an environment variables of 'ServerIP' into the docker container with the IP of your docker host from which you are passing web (80) and dns (53) ports from"
exit 1
fi;

# /tmp/piholeIP is the current override of auto-lookup in gravity.sh
echo "$ServerIP" > /etc/pihole/piholeIP;
echo "ipv4addr=$ServerIP" > /etc/pihole/setupVars.conf;
echo "piholeIPv6=$ServerIPv6" >> /etc/pihole/setupVars.conf;

if [ ! -f /var/run/dockerpihole-firstboot ] ; then
echo "[www]" > $PHP_ENV_CONFIG;
echo "env[PATH] = ${PATH}" >> $PHP_ENV_CONFIG;
echo "env[PHP_ERROR_LOG] = ${PHP_ERROR_LOG}" >> $PHP_ENV_CONFIG;
echo "env[ServerIP] = ${ServerIP}" >> $PHP_ENV_CONFIG;

if [ -z "$VIRTUAL_HOST" ] ; then
VIRTUAL_HOST="$ServerIP"
fi;
echo "env[VIRTUAL_HOST] = ${VIRTUAL_HOST}" >> $PHP_ENV_CONFIG;

touch /var/run/dockerpihole-firstboot
else
echo "Skipped first boot configuration, looks like you're restarting this container"
fi;

echo "Added ENV to php:"
cat $PHP_ENV_CONFIG

dnsType='default'
DNS1=${DNS1:-'8.8.8.8'}
DNS2=${DNS2:-'8.8.4.4'}
if [ "$DNS1" != '8.8.8.8' ] || [ "$DNS2" != '8.8.4.4' ] ; then
dnsType='custom'
fi;

echo "Using $dnsType DNS servers: $DNS1 & $DNS2"
sed -i "s/@DNS1@/$DNS1/" /etc/dnsmasq.d/01-pihole.conf && \
sed -i "s/@DNS2@/$DNS2/" /etc/dnsmasq.d/01-pihole.conf && \

. /common_start.sh
# Dockerfile variables
export IMAGE
export ServerIP
export ServerIPv6
export DNS1
export DNS2
export PYTEST
export PHP_ENV_CONFIG
export PHP_ERROR_LOG

validate_env
setup_saved_variables
setup_php_env
setup_dnsmasq

# alpine unique currently
ip_versions="IPv4 and IPv6"
if [ "$IPv6" != "True" ] ; then
ip_versions="IPv4"
sed -i '/listen \[::\]:80;/ d' /etc/nginx/nginx.conf
fi;
echo "Using $ip_versions"

dnsmasq --test -7 /etc/dnsmasq.d || exit 1
php-fpm -t || exit 1
nginx -t || exit 1
echo " :: All config checks passed, starting ..."
test_configs
test_framework_stubbing

if [ -n "$PYTEST" ] ; then sed -i 's/^gravity_spinup/#donotcurl/g' `which gravity.sh`; fi;
gravity.sh
dnsmasq -7 /etc/dnsmasq.d
gravity.sh # dnsmasq start included
php-fpm
nginx

Expand Down
100 changes: 100 additions & 0 deletions common_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
validate_env() {
if [ -z "$ServerIP" ] ; then
echo "ERROR: To function correctly you must pass an environment variables of 'ServerIP' into the docker container with the IP of your docker host from which you are passing web (80) and dns (53) ports from"
exit 1
fi;
}

setup_saved_variables() {
# /tmp/piholeIP is the current override of auto-lookup in gravity.sh
echo "$ServerIP" > /etc/pihole/piholeIP;
echo "IPv4addr=$ServerIP" > /etc/pihole/setupVars.conf;
echo "piholeIPv6=$ServerIPv6" >> /etc/pihole/setupVars.conf;
}

setup_dnsmasq() {
local dnsType='default'
local DNS1="${1:-8.8.8.8}"
local DNS2="${2:-8.8.4.4}"
if [ "$DNS1" != '8.8.8.8' ] || [ "$DNS2" != '8.8.4.4' ] ; then
dnsType='custom'
fi;

echo "Using $dnsType DNS servers: $DNS1 & $DNS2"
sed -i "s/@DNS1@/$DNS1/" /etc/dnsmasq.d/01-pihole.conf && \
sed -i "s/@DNS2@/$DNS2/" /etc/dnsmasq.d/01-pihole.conf
}

setup_php_env() {
if [ ! -f /var/run/dockerpihole-firstboot ] ; then
case $IMAGE in
"debian") setup_php_env_debian ;;
"alpine") setup_php_env_alpine ;;
esac

touch /var/run/dockerpihole-firstboot
else
echo "Looks like you're restarting this container, skipping php env setup"
fi;
}

setup_php_env_debian() {
sed -i "/bin-environment/ a\\\t\t\t\"ServerIP\" => \"${ServerIP}\"," $PHP_ENV_CONFIG
sed -i "/bin-environment/ a\\\t\t\t\"PHP_ERROR_LOG\" => \"${PHP_ERROR_LOG}\"," $PHP_ENV_CONFIG

if [ -z "$VIRTUAL_HOST" ] ; then
VIRTUAL_HOST="$ServerIP"
fi;
sed -i "/bin-environment/ a\\\t\t\t\"VIRTUAL_HOST\" => \"${VIRTUAL_HOST}\"," $PHP_ENV_CONFIG

echo "Added ENV to php:"
grep -E '(VIRTUAL_HOST|ServerIP)' $PHP_ENV_CONFIG
}

setup_php_env_alpine() {
echo "[www]" > $PHP_ENV_CONFIG;
echo "env[PATH] = ${PATH}" >> $PHP_ENV_CONFIG;
echo "env[PHP_ERROR_LOG] = ${PHP_ERROR_LOG}" >> $PHP_ENV_CONFIG;
echo "env[ServerIP] = ${ServerIP}" >> $PHP_ENV_CONFIG;

if [ -z "$VIRTUAL_HOST" ] ; then
VIRTUAL_HOST="$ServerIP"
fi;
echo "env[VIRTUAL_HOST] = ${VIRTUAL_HOST}" >> $PHP_ENV_CONFIG;

echo "Added ENV to php:"
cat $PHP_ENV_CONFIG
}

test_configs() {
case $IMAGE in
"debian") test_configs_debian ;;
"alpine") test_configs_alpine ;;
esac
}

test_configs_debian() {
set -e
echo -n '::: Testing DNSmasq config: '
dnsmasq --test -7 /etc/dnsmasq.d
echo -n '::: Testing lighttpd config: '
lighttpd -t -f /etc/lighttpd/lighttpd.conf
set +e
echo "::: All config checks passed, starting ..."
}

test_configs_alpine() {
set -e
echo -n '::: Testing DNSmasq config: '
dnsmasq --test -7 /etc/dnsmasq.d
echo -n '::: Testing PHP-FPM config: '
php-fpm -t
echo -n '::: Testing NGINX config: '
nginx -t
set +e
echo "::: All config checks passed, starting ..."
}

test_framework_stubbing() {
if [ -n "$PYTEST" ] ; then sed -i 's/^gravity_spinup/#donotcurl/g' `which gravity.sh`; fi;
}
7 changes: 5 additions & 2 deletions debian-armhf.docker
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ FROM jsurf/rpi-raspbian
MAINTAINER [email protected] <[email protected]>

RUN [ "cross-build-start" ]
ENV IMAGE debian

# Requirements
RUN apt-get -q update && \
apt-get install -y \
dnsmasq \
lighttpd \
php5-common php5-cgi php5 \
bc curl unzip wget sudo && \
bc curl unzip wget sudo dnsutils && \
rm -rf /var/cache/apt/archives

# Original upstream pihole code being used
Expand Down Expand Up @@ -54,11 +55,13 @@ RUN grep -q '^user=root' || echo 'user=root' >> /etc/dnsmasq.conf
# php config start passes special ENVs into
ENV PHP_ENV_CONFIG '/etc/lighttpd/conf-enabled/15-fastcgi-php.conf'
ENV PHP_ERROR_LOG '/var/log/lighttpd/error.log'
COPY ./debian-armhf/start.sh /
COPY ./debian/start.sh /
COPY ./common_start.sh /

EXPOSE 53 53/udp
EXPOSE 80

# Tini doesn't work in ARM
ENTRYPOINT [ "bash", "-c" ]
CMD [ "/start.sh" ]

Expand Down
46 changes: 0 additions & 46 deletions debian-armhf/start.sh

This file was deleted.

Loading

0 comments on commit cf42873

Please sign in to comment.