cluster-deployment/jobs/dns.sh

42 lines
1.3 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Usage:
# configLine [searchPattern] [replaceLine] [filePath]
# Ref https://stackoverflow.com/a/54909102/1460422
config_line() {
2021-12-26 14:59:12 +00:00
local OLD_LINE_PATTERN NEW_LINE NEW FILE
OLD_LINE_PATTERN="$1"; shift
NEW_LINE="$1"; shift
FILE="$1"
NEW="$(echo "${NEW_LINE}" | sed 's/\//\\\//g')";
touch "${FILE}"
sed -i '/'"${OLD_LINE_PATTERN}"'/{s/.*/'"${NEW}"'/;h};${x;/./{x;q100};x}' "${FILE}"
if [[ $? -ne 100 ]] && [[ ${NEW_LINE} != '' ]]
then
echo "${NEW_LINE}" >> "${FILE}"
fi
}
log_msg() {
2021-11-26 22:31:14 +00:00
echo -e "[ ${HOSTNAME} ] [ $(date -u +"%Y-%m-%dT%H:%M:%SZ") ] $*" >&2;
}
# Old approach - we've learned we can override /etc/network/interfaces via /etc/dhcp/dhclient.conf
# config_line "dns-nameservers" "dns-nameservers 172.16.230.100" "/etc/network/interfaces";
completed_file="/etc/dhcp/sbrl-dns-configured";
log_msg "Configuring DNS nameserver";
if [[ ! -f "${completed_file}" ]]; then
log_msg "DNS nameserver not yet configured, appending to /etc/dhcp/dhclient.conf";
echo "interface \"eth0\" {
supersede domain-name-servers 172.16.230.100;
}" | sudo tee -a /etc/dhcp/dhclient.conf;
sudo touch "${completed_file}";
log_msg "Complete, this machine needs a reboot to activate the new config directives";
else
log_msg "DNS nameserver config written already, no changes made";
fi