refactor(ingress.tjo.cloud/dyndns): function names
Some checks failed
/ lint (push) Failing after 42s

This commit is contained in:
Tine 2025-01-06 18:56:33 +01:00
parent 8ca76b5d92
commit 2a369c4d1b
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw

View file

@ -14,38 +14,38 @@ services=(
"ifconfig.io" "ifconfig.io"
) )
debug() { log::debug() {
echo "DEBUG: $1" echo "level=DEBUG $1"
} }
info() { log::info() {
echo "INFO: $1" echo "level=INFO $1"
} }
warn() { log::warn() {
echo "WARN: $1" echo "level=WARN $1"
} }
error() { log:error() {
warn "$1" echo "level=ERROR $1"
exit 1 exit 1
} }
test -z "${DNSIMPLE_TOKEN}" && error "DNSIMPLE_TOKEN not set!" test -z "${DNSIMPLE_TOKEN}" && log:error "DNSIMPLE_TOKEN not set!"
test -z "${DNSIMPLE_ACCOUNT_ID}" && error "DNSIMPLE_ACCOUNT_ID not set!" test -z "${DNSIMPLE_ACCOUNT_ID}" && log:error "DNSIMPLE_ACCOUNT_ID not set!"
test -z "${DOMAIN}" && error "DOMAIN not set!" test -z "${DOMAIN}" && log:error "DOMAIN not set!"
test -z "${CLOUD_REGION}" && error "CLOUD_REGION not set!" test -z "${CLOUD_REGION}" && log:error "CLOUD_REGION not set!"
base_zone_url="$api_host/$DNSIMPLE_ACCOUNT_ID/zones/$DOMAIN/records" base_zone_url="$api_host/$DNSIMPLE_ACCOUNT_ID/zones/$DOMAIN/records"
dnsimple_list_record() { dnsimple::record::list() {
curl -s -X GET \ curl -s -X GET \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: Bearer $DNSIMPLE_TOKEN" \ -H "Authorization: Bearer $DNSIMPLE_TOKEN" \
"$base_zone_url" "$base_zone_url"
} }
dnsimple_create_record() { dnsimple::record::create() {
local data="$1" local data="$1"
curl -s -X POST \ curl -s -X POST \
@ -55,7 +55,7 @@ dnsimple_create_record() {
"$base_zone_url" "$base_zone_url"
} }
dnsimple_update_record() { dnsimple::record::update() {
local record="$1" local record="$1"
local data="$2" local data="$2"
@ -66,11 +66,11 @@ dnsimple_update_record() {
"$base_zone_url/$record" &>/dev/null "$base_zone_url/$record" &>/dev/null
} }
dnsimeple_delete_record() { dnsimple::record::delete() {
local record="$1" local record="$1"
if [[ "$destructive" == "false" ]]; then if [[ "$destructive" == "false" ]]; then
warn "record=$record Record deletion is disabled. Set DESTRUCTIVE=true to enable." log::warn "record=$record Record deletion is disabled. Set DESTRUCTIVE=true to enable."
return return
fi fi
@ -81,7 +81,7 @@ dnsimeple_delete_record() {
} }
configure_single() { configure::single() {
# disable glob expansion # disable glob expansion
set -f set -f
@ -94,14 +94,14 @@ configure_single() {
record_data=$(echo "$domain_records" | jq -r ".data[] | select(.type == \"$type\" and .name == \"$domain\") | .content") record_data=$(echo "$domain_records" | jq -r ".data[] | select(.type == \"$type\" and .name == \"$domain\") | .content")
if [ "$(echo "$record_id" | wc -l)" -ge 2 ]; then if [ "$(echo "$record_id" | wc -l)" -ge 2 ]; then
warn "domain=$domain type=$type Domain name has duplicate DNS records, removing duplicates." log::warn "domain=$domain type=$type Domain name has duplicate DNS records, removing duplicates."
record_id_to_delete=$(echo "$record_id" | tail -n +2) record_id_to_delete=$(echo "$record_id" | tail -n +2)
record_id=$(echo "$record_id" | head -1) record_id=$(echo "$record_id" | head -1)
record_data=$(echo "$record_data" | head -1) record_data=$(echo "$record_data" | head -1)
while IFS= read -r line; do while IFS= read -r line; do
warn "domain=$domain type=$type record=$line Deleting record" log::warn "domain=$domain type=$type record=$line Deleting record"
dnsimeple_delete_record "$line" dnsimple::record::delete "$line"
done <<<"$record_id_to_delete" done <<<"$record_id_to_delete"
fi fi
@ -111,17 +111,17 @@ configure_single() {
data="{\"type\": \"$type\", \"name\": \"$domain\", \"content\": \"$ip\", \"ttl\": $record_ttl}" data="{\"type\": \"$type\", \"name\": \"$domain\", \"content\": \"$ip\", \"ttl\": $record_ttl}"
if [[ -z $record_id ]]; then if [[ -z $record_id ]]; then
info "domain=$domain type=$type No record found. Creating record." log::info "domain=$domain type=$type No record found. Creating record."
dnsimple_create_record "$data" dnsimple::record::create "$data"
elif [[ "$ip" != "$record_data" ]]; then elif [[ "$ip" != "$record_data" ]]; then
info "domain=$domain type=$type Existing DNS record address ($record_data) doesn't match current IP ($ip)" log::info "domain=$domain type=$type Existing DNS record address ($record_data) doesn't match current IP ($ip)"
dnsimple_update_record "$record_id" "$data" dnsimple::record::update "$record_id" "$data"
else else
info "domain=$domain type=$type Existing DNS record address ($record_data) did not need updating" log::info "domain=$domain type=$type Existing DNS record address ($record_data) did not need updating"
fi fi
} }
configure_many() { configure::many() {
# disable glob expansion # disable glob expansion
set -f set -f
@ -139,10 +139,10 @@ configure_many() {
data="{\"type\": \"$type\", \"name\": \"$domain\", \"content\": \"$ip\", \"ttl\": $record_ttl}" data="{\"type\": \"$type\", \"name\": \"$domain\", \"content\": \"$ip\", \"ttl\": $record_ttl}"
if [[ -z $record_id ]]; then if [[ -z $record_id ]]; then
info "domain=$domain type=$type No record found. Creating record." log::info "domain=$domain type=$type No record found. Creating record."
dnsimple_create_record "$data" dnsimple::record::create "$data"
else else
info "domain=$domain type=$type Existing DNS record address ($record_data) did not need updating." log::info "domain=$domain type=$type Existing DNS record address ($record_data) did not need updating."
fi fi
} }
@ -152,20 +152,20 @@ healthcheck() {
code=$(curl -s -o /dev/null -I -w '%{http_code}' "http://$ip:1337/healthz" || echo "") code=$(curl -s -o /dev/null -I -w '%{http_code}' "http://$ip:1337/healthz" || echo "")
if [[ "$code" != "200" ]]; then if [[ "$code" != "200" ]]; then
warn "ip=$ip code=$code Healthcheck failed." log::warn "ip=$ip code=$code Healthcheck failed."
return 1 return 1
fi fi
info "ip=$ip code=$code Healthcheck passed." log::info "ip=$ip code=$code Healthcheck passed."
return 0 return 0
} }
while (true); do while (true); do
domain_records=$(dnsimple_list_record) domain_records=$(dnsimple::record::list)
debug "domain_records=$domain_records" log::debug "domain_records=$domain_records"
for service in "${services[@]}"; do for service in "${services[@]}"; do
info "service=$service Discovering public IP address..." log::info "service=$service Discovering public IP address..."
ipv4="$(curl -4 -s -f --connect-timeout 2 "$service" || echo "")" ipv4="$(curl -4 -s -f --connect-timeout 2 "$service" || echo "")"
ipv6="$(curl -6 -s -f --connect-timeout 2 "$service" || echo "")" ipv6="$(curl -6 -s -f --connect-timeout 2 "$service" || echo "")"
@ -173,56 +173,56 @@ while (true); do
if [[ -n "$ipv4$ipv6" ]]; then if [[ -n "$ipv4$ipv6" ]]; then
break break
else else
warn "service=$service Failed to retrieve IP address." log::warn "service=$service Failed to retrieve IP address."
fi fi
done done
if [[ -z $ipv4 ]]; then if [[ -z $ipv4 ]]; then
warn "IPv4 address wasn't found." log::warn "IPv4 address wasn't found."
else else
info "Found IPv4 address $ipv4" log::info "Found IPv4 address $ipv4"
if healthcheck "$ipv4"; then if healthcheck "$ipv4"; then
configure_single "$domain_records" "$CLOUD_REGION" "$ipv4" "A" configure::single "$domain_records" "$CLOUD_REGION" "$ipv4" "A"
configure_many "$domain_records" "any" "$ipv4" "A" configure::many "$domain_records" "any" "$ipv4" "A"
fi fi
fi fi
if [[ -z $ipv6 ]]; then if [[ -z $ipv6 ]]; then
warn "IPv6 address wasn't found." log::warn "IPv6 address wasn't found."
else else
info "Found IPv6 address $ipv6" log::info "Found IPv6 address $ipv6"
if healthcheck "[$ipv6]"; then if healthcheck "[$ipv6]"; then
configure_single "$domain_records" "$CLOUD_REGION" "$ipv6" "AAAA" configure::single "$domain_records" "$CLOUD_REGION" "$ipv6" "AAAA"
configure_many "$domain_records" "any" "$ipv6" "AAAA" configure::many "$domain_records" "any" "$ipv6" "AAAA"
fi fi
fi fi
info "type=A Checking for stale records..." log::info "type=A Checking for stale records..."
for domain in $(echo "$domain_records" | jq -r ".data[] | select(.type == "A" and .name != "$CLOUD_REGION") | .name"); do for domain in $(echo "$domain_records" | jq -r ".data[] | select(.type == "A" and .name != "$CLOUD_REGION") | .name"); do
info "type=A domain=$domain Checking..." log::info "type=A domain=$domain Checking..."
record_id=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .id") record_id=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .id")
record_ip=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .content") record_ip=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .content")
if ! healthcheck "$record_ip"; then if ! healthcheck "$record_ip"; then
warn "type=A domain=$domain Unhealthy..." log::warn "type=A domain=$domain Unhealthy..."
dnsimeple_delete_record "$record_id" dnsimple::record::delete "$record_id"
else else
info "type=A domain=$domain Healthy..." log::info "type=A domain=$domain Healthy..."
fi fi
done done
info "type=AAAA Checking for stale records..." log::info "type=AAAA Checking for stale records..."
for domain in $(echo "$domain_records" | jq -r ".data[] | select(.type == "AAAA" and .name != "$CLOUD_REGION") | .name"); do for domain in $(echo "$domain_records" | jq -r ".data[] | select(.type == "AAAA" and .name != "$CLOUD_REGION") | .name"); do
info "type=AAAA domain=$domain Checking..." log::info "type=AAAA domain=$domain Checking..."
record_id=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .id") record_id=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .id")
record_ip=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .content") record_ip=$(echo "$domain_records" | jq -r ".data[] | select(.name == \"$domain\") | .content")
if ! healthcheck "$record_ip"; then if ! healthcheck "$record_ip"; then
warn "type=AAAA domain=$domain Unhealthy." log::warn "type=AAAA domain=$domain Unhealthy."
dnsimeple_delete_record "[$record_id]" dnsimple::record::delete "[$record_id]"
else else
info "type=AAAA domain=$domain Healthy." log::info "type=AAAA domain=$domain Healthy."
fi fi
done done
info "Sleeping for $sleep_interval seconds..." log::info "Sleeping for $sleep_interval seconds..."
sleep "$sleep_interval" sleep "$sleep_interval"
done done