provisioning-catalog/components/resolv/taskserv/install-resolv.sh

116 lines
3.8 KiB
Bash
Raw Normal View History

#!/bin/bash
# Info: Script to install Resolv packages
# Author: JesusPerezLorenzo
# Release: 1.0
# Date: 30-10-2023
USAGE="install-resolv.sh "
[ "$1" == "-h" ] && echo "$USAGE" && exit 1
_config_resolver() {
[ -z "$NAMESERVERS" ] && return
local resolv_cfg_path
local resolv_cfg_file
if [ -d "/etc/resolvconf/resolv.conf.d" ] ; then
resolv_cfg_path=/etc/resolvconf/resolv.conf.d
resolv_cfg_file="head"
else
resolv_cfg_path=/etc
resolv_cfg_file="resolv.conf"
chattr -i "$resolv_cfg_path/$resolv_cfg_file"
fi
[ ! -r "$resolv_cfg_path/_$resolv_cfg_file" ] && sudo mv "$resolv_cfg_path/$resolv_cfg_file" "$resolv_cfg_path/_$resolv_cfg_file"
grep -v "^nameserver" "$resolv_cfg_path/_$resolv_cfg_file" | sudo tee "$resolv_cfg_path/$resolv_cfg_file" &>/dev/null
echo "
#options rotate
options timeout:1
" | sudo tee -a "$resolv_cfg_path/$resolv_cfg_file" &>/dev/null
for ns in $NAMESERVERS
do
echo "nameserver $ns" | sudo tee -a "$resolv_cfg_path/$resolv_cfg_file" &>/dev/null
done
[ -n "$DOMAINS_SEARCH" ] && echo "search $DOMAINS_SEARCH" | sudo tee -a "$resolv_cfg_path/$resolv_cfg_file" &>/dev/null
if [ -d "/etc/resolvconf/resolv.conf.d" ] ; then
sudo timeout -k 10 20 systemctl restart resolvconf
else
chattr +i "$resolv_cfg_path/$resolv_cfg_file"
fi
}
_config_split_dns() {
[ -z "$SPLIT_DNS_SERVER" ] && return
# Prefer systemd-resolved: true per-domain routing via routing domains.
if systemctl cat systemd-resolved &>/dev/null; then
local domains=""
for d in $SPLIT_DNS_DOMAINS; do
domains="$domains ~$d"
done
mkdir -p /etc/systemd/resolved.conf.d
printf '[Resolve]\nDNS=%s\nDomains=%s\n' \
"$SPLIT_DNS_SERVER" "${domains# }" \
> /etc/systemd/resolved.conf.d/10-split-dns.conf
local stub=/run/systemd/resolve/stub-resolv.conf
local cur
cur=$(readlink /etc/resolv.conf 2>/dev/null || echo "")
if [ "$cur" != "$stub" ]; then
chattr -i /etc/resolv.conf 2>/dev/null || true
rm -f /etc/resolv.conf
ln -sf "$stub" /etc/resolv.conf
fi
systemctl enable --now systemd-resolved
systemctl daemon-reload
systemctl restart systemd-resolved
return
fi
# Fallback: prepend SPLIT_DNS_SERVER as primary nameserver.
# coredns-vpn at SPLIT_DNS_SERVER forwards unknown zones to upstream.
# Mirror the same path-selection logic as _config_resolver so resolvconf wins.
if [ -d "/etc/resolvconf/resolv.conf.d" ]; then
# resolvconf is managing /etc/resolv.conf.
# Use 'head': its content is cat-ed verbatim into the output, bypassing the
# 3-nameserver cap enforced on the 'base' file's entries.
local head=/etc/resolvconf/resolv.conf.d/head
if ! grep -q "^nameserver ${SPLIT_DNS_SERVER}$" "$head" 2>/dev/null; then
local tmp
tmp=$(mktemp)
printf 'nameserver %s\n' "$SPLIT_DNS_SERVER" > "$tmp"
grep "^nameserver" "$head" 2>/dev/null | grep -vF "$SPLIT_DNS_SERVER" >> "$tmp" || true
grep -v "^nameserver" "$head" 2>/dev/null >> "$tmp" || true
sudo mv "$tmp" "$head"
fi
resolvconf -u 2>/dev/null || sudo timeout -k 10 20 systemctl restart resolvconf
else
local resolv=/etc/resolv.conf
chattr -i "$resolv" 2>/dev/null || true
if ! grep -q "^nameserver ${SPLIT_DNS_SERVER}$" "$resolv" 2>/dev/null; then
local tmp
tmp=$(mktemp)
printf 'nameserver %s\n' "$SPLIT_DNS_SERVER" > "$tmp"
grep "^nameserver" "$resolv" 2>/dev/null | grep -vF "$SPLIT_DNS_SERVER" >> "$tmp" || true
grep -v "^nameserver" "$resolv" 2>/dev/null >> "$tmp" || true
sudo mv "$tmp" "$resolv"
fi
chattr +i "$resolv" 2>/dev/null || true
fi
}
[ -r "./env-resolv" ] && . ./env-resolv
case "${1:-install}" in
install|update|reinstall|restart)
_config_resolver
_config_split_dns
;;
resolver)
_config_resolver
;;
split-dns)
_config_split_dns
;;
esac
exit 0