Page MenuHomePhabricator
Paste P10716

Script to compare Netbox-generated DNS snippets and the operations/dns templates (T233183)
ActivePublic

Authored by Volans on Mar 18 2020, 11:58 AM.
Tags
None
Referenced Files
F31689387: raw.txt
Mar 18 2020, 11:59 AM
F31689386: raw.txt
Mar 18 2020, 11:58 AM
Subscribers
None
#!/bin/bash
function parse_wmnet() {
local origin=""
local skip=0
echo "Processing zonefile wmnet"
while read -r line; do
if [[ "${line}" =~ ^\; ]]; then
continue
elif [[ "${line}" =~ ^\$ORIGIN\ ]]; then
origin="$(echo "${line}" | cut -d " " -f2)"
if [[ "${origin:0:5}" != "mgmt." ]]; then
echo "Skipping origin ${origin}"
skip=1
else
echo "Found origin ${origin}"
skip=0
fi
continue
fi
if [[ -z "${origin}" || "${skip}" -eq "1" ]]; then
continue
fi
git grep -iq "$(echo "$line" | sed -E 's/ +/ */g')" "${origin::${#origin}-1}" || echo "Missing line: ${line}"
done < "${1}/wmnet"
}
function parse_forward_snippet() {
local file="${1}"
local snippet=$(basename ${file})
local zone="wmnet"
if [[ "${snippet}" == "wikimedia.org" ]]; then
zone="wikimedia.org"
fi
echo "Processing snippet ${snippet} against zonefile ${zone}"
while read -r line; do
git grep -iq "$(echo "$line" | sed -E 's/ +/ */g')" "${zone}" || echo "Missing line: ${line}"
done < "${file}"
}
function parse_reverse_zonefile() {
local file="${1}"
local ext="${2}"
local origin=""
local skip=0
local zone
zone="$(echo "${file##*/}" | rev | cut -d"." -f3- | rev)"
echo "Processing zone ${zone} in zonefile ${file##*/}"
while read -r line; do
if [[ "${line}" =~ ^\; ]]; then
continue
elif [[ "${line}" =~ ^\$ORIGIN\ ]]; then
origin="$(echo "${line}" | cut -d " " -f2 | sed "s/@Z/${zone}/")"
if [[ "${origin}" == "{{" ]]; then
echo "Skipping dynamically generated origin (k8s most likely)"
skip=1
elif [[ ! -f "${origin}${ext}" ]]; then
echo "Skipping origin ${origin} due to missing zonefile ${origin}${ext}"
skip=1
else
echo "Found origin ${origin}"
skip=0
fi
continue
fi
if [[ -z "${origin}" || "${skip}" -eq "1" ]]; then
continue
fi
git grep -iq "$(echo "$line" | sed -E 's/ +/ */g')" "${origin}${ext}" || echo "Missing line: ${line}"
done < "${file}"
}
if [[ -z "${1}" ]]; then
echo "Usage: ${0} PATH_TO_OTHER_REPO_ZONEFILES"
echo "Run from within the repo to check against the other repo"
echo "Example: ./consistency_check.sh ops/dns/templates"
echo "Example: ./consistency_check.sh netbod/dns"
exit 1
fi
if [[ -e "${1}/wmnet" ]]; then
echo "Checking all ops/dns repo records are present here"
for file in "${1}"/*.in-addr.arpa; do
parse_reverse_zonefile "${file}" ".in-addr.arpa"
done
for file in "${1}"/*.ip6.arpa; do
parse_reverse_zonefile "${file}" ".ip6.arpa"
done
parse_wmnet "${1}"
else
echo "Checking all netbox generated records are present here"
for file in "${1}"/*; do
if [[ "${file}" =~ \.in-addr\.arpa$ ]]; then
parse_reverse_zonefile "${file}" ".in-addr.arpa"
elif [[ "${file}" =~ \.ip6\.arpa$ ]]; then
parse_reverse_zonefile "${file}" ".ip6.arpa"
else
parse_forward_snippet "${file}"
fi
done
fi