Skip to content

Commit 2b827cd

Browse files
committed
Add ENV variable for ConfigMap for adlists
1 parent c2887ae commit 2b827cd

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ There are other environment variables if you want to customize various things in
148148
| `WEB_UID` | `33` | Number | Overrides image's default www-data user id to match a host user id<br/>**IMPORTANT**: id must not already be in use inside the container! (Make sure it is different to `PIHOLE_UID` if you are using that, also)|
149149
| `WEB_GID` | `33` | Number | Overrides image's default www-data group id to match a host group id<br/>**IMPORTANT**: id must not already be in use inside the container! (Make sure it is different to `PIHOLE_GID` if you are using that, also)|
150150
| `WEBLOGS_STDOUT` | 0 | 0&vert;1 | 0 logs to defined files, 1 redirect access and error logs to stdout |
151+
| `CONFIGMAP_ADLISTS` | unset | `<"true"\|"false">` | Environmental variable to indicate that a ConfigMap was used to supply adlists. |
151152

152153
## Deprecated environment variables:
153154
While these may still work, they are likely to be removed in a future version. Where applicable, alternative variable names are indicated. Please review the table above for usage of the alternative variables

src/s6/debian-root/usr/local/bin/_startup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ fi
88
# shellcheck source=/dev/null
99
. /usr/local/bin/bash_functions.sh
1010

11+
# Experimental feature to allow for declarative adlists in kubernetes
12+
. /usr/local/bin/configmap_adlists.sh
13+
1114
# shellcheck source=/dev/null
1215
SKIP_INSTALL=true . /etc/.pihole/automated\ install/basic-install.sh
1316

@@ -44,6 +47,7 @@ setup_lighttpd_bind
4447
# Misc Setup
4548
# ===========================
4649
installCron
50+
[[ -n "${CONFIGMAP_ADLISTS}" && "${CONFIGMAP_ADLISTS}" == "true" ]] && echo " [i] Using configMap for adlists" && configMap_adlists
4751
setup_blocklists
4852

4953
# FTL setup
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#!/usr/bin/env bash
2+
export LC_ALL=C
3+
4+
basename="pihole"
5+
PIHOLE_COMMAND="/usr/local/bin/${basename}"
6+
7+
piholeDir="/etc/${basename}"
8+
9+
adListFile="${piholeDir}/adlists.list"
10+
11+
domainsExtension="domains"
12+
13+
# Set up tmp dir variable in case it's not configured
14+
: "${GRAVITY_TMPDIR:=/tmp}"
15+
16+
if [ ! -d "${GRAVITY_TMPDIR}" ] || [ ! -w "${GRAVITY_TMPDIR}" ]; then
17+
echo -e " ${COL_LIGHT_RED}Gravity temporary directory does not exist or is not a writeable directory, falling back to /tmp. ${COL_NC}"
18+
GRAVITY_TMPDIR="/tmp"
19+
fi
20+
21+
gravityDBfile_default="${piholeDir}/gravity.db"
22+
GRAVITYDB="${gravityDBfile_default}"
23+
24+
# Set this only after sourcing pihole-FTL.conf as the gravity database path may
25+
# have changed
26+
gravityDBfile="${GRAVITYDB}"
27+
gravityTEMPfile="${GRAVITYDB}_temp"
28+
gravityDIR="$(dirname -- "${gravityDBfile}")"
29+
gravityOLDfile="${gravityDIR}/gravity_old.db"
30+
31+
32+
configMap_adlists() {
33+
echo " [i] Deleting existing adlists from gravity"
34+
35+
# Experimental feature to clean out domains from gravity to allow a kubernetes ConfigMap to manage them
36+
pihole-FTL sqlite3 -ni "${gravityDBfile}" "DELETE FROM gravity;"
37+
pihole-FTL sqlite3 -ni "${gravityDBfile}" "DELETE FROM adlist;"
38+
pihole-FTL sqlite3 -ni "${gravityDBfile}" "DELETE FROM adlist_by_group;"
39+
40+
echo " [i] Finished clearing out adlists from gravity"
41+
42+
# Migrate list files to new database
43+
if [ -e "${adListFile}" ]; then
44+
# Store adlist domains in database
45+
echo -e " ${INFO} Migrating content of ${adListFile} into new database"
46+
database_table_from_file "adlist" "${adListFile}"
47+
fi
48+
49+
}
50+
51+
# Import domains from file and store them in the specified database table
52+
database_table_from_file() {
53+
# Define locals
54+
local table src backup_path backup_file tmpFile list_type
55+
table="${1}"
56+
src="${2}"
57+
backup_path="${piholeDir}/migration_backup"
58+
backup_file="${backup_path}/$(basename "${2}")"
59+
# Create a temporary file. We don't use '--suffix' here because not all
60+
# implementations of mktemp support it, e.g. on Alpine
61+
tmpFile="$(mktemp -p "${GRAVITY_TMPDIR}")"
62+
mv "${tmpFile}" "${tmpFile%.*}.gravity"
63+
64+
local timestamp
65+
timestamp="$(date --utc +'%s')"
66+
67+
local rowid
68+
declare -i rowid
69+
rowid=1
70+
71+
# Special handling for domains to be imported into the common domainlist table
72+
if [[ "${table}" == "whitelist" ]]; then
73+
list_type="0"
74+
table="domainlist"
75+
elif [[ "${table}" == "blacklist" ]]; then
76+
list_type="1"
77+
table="domainlist"
78+
elif [[ "${table}" == "regex" ]]; then
79+
list_type="3"
80+
table="domainlist"
81+
fi
82+
83+
# Get MAX(id) from domainlist when INSERTing into this table
84+
if [[ "${table}" == "domainlist" ]]; then
85+
rowid="$(pihole-FTL sqlite3 -ni "${gravityDBfile}" "SELECT MAX(id) FROM domainlist;")"
86+
if [[ -z "$rowid" ]]; then
87+
rowid=0
88+
fi
89+
rowid+=1
90+
fi
91+
92+
# Loop over all domains in ${src} file
93+
# Read file line by line
94+
grep -v '^ *#' < "${src}" | while IFS= read -r domain
95+
do
96+
# Only add non-empty lines
97+
if [[ -n "${domain}" ]]; then
98+
if [[ "${table}" == "domain_audit" ]]; then
99+
# domain_audit table format (no enable or modified fields)
100+
echo "${rowid},\"${domain}\",${timestamp}" >> "${tmpFile}"
101+
elif [[ "${table}" == "adlist" ]]; then
102+
# Adlist table format
103+
echo "${rowid},\"${domain}\",1,${timestamp},${timestamp},\"Migrated from ${src}\",,0,0,0" >> "${tmpFile}"
104+
else
105+
# White-, black-, and regexlist table format
106+
echo "${rowid},${list_type},\"${domain}\",1,${timestamp},${timestamp},\"Migrated from ${src}\"" >> "${tmpFile}"
107+
fi
108+
rowid+=1
109+
fi
110+
done
111+
112+
# Store domains in database table specified by ${table}
113+
# Use printf as .mode and .import need to be on separate lines
114+
# see https://unix.stackexchange.com/a/445615/83260
115+
output=$( { printf ".timeout 30000\\n.mode csv\\n.import \"%s\" %s\\n" "${tmpFile}" "${table}" | pihole-FTL sqlite3 -ni "${gravityDBfile}"; } 2>&1 )
116+
status="$?"
117+
118+
if [[ "${status}" -ne 0 ]]; then
119+
echo -e "\\n ${CROSS} Unable to fill table ${table}${list_type} in database ${gravityDBfile}\\n ${output}"
120+
gravity_Cleanup "error"
121+
fi
122+
123+
# Move source file to backup directory, create directory if not existing
124+
mkdir -p "${backup_path}"
125+
mv "${src}" "${backup_file}" 2> /dev/null || \
126+
echo -e " ${CROSS} Unable to backup ${src} to ${backup_path}"
127+
128+
# Delete tmpFile
129+
rm "${tmpFile}" > /dev/null 2>&1 || \
130+
echo -e " ${CROSS} Unable to remove ${tmpFile}"
131+
}
132+
133+
# Clean up after Gravity upon exit or cancellation
134+
gravity_Cleanup() {
135+
local error="${1:-}"
136+
137+
str="Cleaning up stray matter"
138+
echo -ne " ${INFO} ${str}..."
139+
140+
# Delete tmp content generated by Gravity
141+
rm ${piholeDir}/pihole.*.txt 2> /dev/null
142+
rm ${piholeDir}/*.tmp 2> /dev/null
143+
# listCurlBuffer location
144+
rm "${GRAVITY_TMPDIR}"/*.phgpb 2> /dev/null
145+
# invalid_domains location
146+
rm "${GRAVITY_TMPDIR}"/*.ph-non-domains 2> /dev/null
147+
148+
# Ensure this function only runs when gravity_SetDownloadOptions() has completed
149+
if [[ "${gravity_Blackbody:-}" == true ]]; then
150+
# Remove any unused .domains files
151+
for file in "${piholeDir}"/*."${domainsExtension}"; do
152+
# If list is not in active array, then remove it
153+
if [[ ! "${activeDomains[*]}" == *"${file}"* ]]; then
154+
rm -f "${file}" 2> /dev/null || \
155+
echo -e " ${CROSS} Failed to remove ${file##*/}"
156+
fi
157+
done
158+
fi
159+
160+
echo -e "${OVER} ${TICK} ${str}"
161+
162+
# Only restart DNS service if offline
163+
if ! pgrep pihole-FTL &> /dev/null; then
164+
"${PIHOLE_COMMAND}" restartdns
165+
dnsWasOffline=true
166+
fi
167+
168+
# Print Pi-hole status if an error occurred
169+
if [[ -n "${error}" ]]; then
170+
"${PIHOLE_COMMAND}" status
171+
exit 1
172+
fi
173+
}

0 commit comments

Comments
 (0)