~craftyguy/ddns.sh

a9ffc08e747e8cc76d0bf45db0d86f5277d6ea78 — Clayton Craft 10 months ago 0cacc4a
use "fail" to print/exit 1
1 files changed, 6 insertions(+), 14 deletions(-)

M ddns.sh
M ddns.sh => ddns.sh +6 -14
@@ 19,10 19,7 @@ CONFIG="${1:-}"
PROVIDER=
IP_ADDRESS="${IP_ADDRESS:-}"

if [ -z "$IP_ADDRESS" ]; then
	echo "error: IP_ADDRESS variable is unset"
	exit 1
fi
[ -n "$IP_ADDRESS" ] || fail "error: IP_ADDRESS variable is unset"

[ -z "$CONFIG" ] && usage
# shellcheck source=/dev/null


@@ 59,23 56,19 @@ luadns() {
	local _zone_id
	# shellcheck disable=SC2016
	_zone_id="$(echo "$_zones" | gojq --arg domain "$DOMAIN" ' .[] | select(.name==$domain) | .["id"] ' )"
	[ -z "$_zone_id" ] && \
		( echo "error: unable to get zone ID for $DOMAIN"; exit 1)
	[ -n "$_zone_id" ] || fail "error: unable to get zone ID for $DOMAIN"

	local _zone
	_zone="$(luadns_get zones/"$_zone_id")"
	[ -z "$_zone" ] && \
		( echo "error: unable to get zone info for $DOMAIN"; exit 1)
	[ -n "$_zone" ] || fail "error: unable to get zone info for $DOMAIN"

	local _dns_ip
	_dns_ip="$(echo "$_zone" | gojq '.["records"] | .[] | select(.type=="A") | .["content"] ' | sed 's|"||g' )"
	[ -z "$_dns_ip" ] && \
		( echo "error: unable to determine DNS IP for $DOMAIN"; exit 1)
	[ -n "$_dns_ip" ] || fail "error: unable to determine DNS IP for $DOMAIN"

	local _record_id
	_record_id="$(echo "$_zone" | gojq '.["records"] | .[] | select(.type=="A") | .["id"] ' )"
	[ -z "$_record_id" ] && \
		( echo "error: unable to determine record ID"; exit 1)
	[ -n "$_record_id" ] || fail "error: unable to determine record ID"

	if [ "$_dns_ip" != "$IP_ADDRESS" ]; then
		echo "IP in DNS is *not* expected ($_dns_ip), updating to $IP_ADDRESS"


@@ 91,7 84,6 @@ case "$PROVIDER" in
		luadns
		;;
	*)
		echo "unknown provider: $PROVIDER"
		exit 1
		fail "error: unknown provider: $PROVIDER"
		;;
esac