Updated linode to use new domain fact

This commit is contained in:
Eric Renfro 2023-10-31 18:45:47 -04:00
parent 312afc71ee
commit ce87e9a990
Signed by: psi-jack
GPG key ID: 14977F3A50D9A5BF

View file

@ -17,9 +17,17 @@ if [[ -z "$agent_update_dns_linode_v4_token" ]]; then
exit 99 exit 99
fi fi
# Get Domain Name if [[ -z "$DIP_CUR_IP" ]] || [[ -z "$DIP_OLD_IP" ]] || [[ -z "$DIP_RECORD" ]] || [[ -z "$DIP_DOMAIN" ]]; then
logerr "ERROR: Agent expects currentip, existingip, record and domain."
exit 98
else
DIP_RECORD="${DIP_RECORD%%."$DIP_DOMAIN"}"
fi
_update_dns_linode_v4_get_domain() { # Get Domain ID
_update_dns_linode_v4_get_domain_id() {
local domain=$1
local domain_id local domain_id
local response local response
local page local page
@ -37,36 +45,14 @@ _update_dns_linode_v4_get_domain() {
page=$(echo "$response" | jq ".page") page=$(echo "$response" | jq ".page")
total_pages=$(echo "$response" | jq ".pages") total_pages=$(echo "$response" | jq ".pages")
domain_id="$(sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq --arg domain "$DIP_RECORD" '.data[] | select(.domain==$domain).id')")" # FIXME
if [[ -n "$domain_id" ]] domain_id="$(sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq --arg domain "$domain" '.data[] | select(.domain==$domain).id')")"
then
_sub_domain=""
_domain=$DIP_RECORD
_domain_id=$domain_id
return 0
fi
while true
do
h=$(printf "%s" "$DIP_RECORD" | cut -d . -f $i-100)
if [[ -z "$h" ]]
then
return 1
fi
domain_id="$(sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq --arg domain "$h" '.data[] | select(.domain==$domain).id')")"
if [[ -n "$domain_id" ]] if [[ -n "$domain_id" ]]
then then
_sub_domain=$(printf "%s" "$DIP_RECORD" | cut -d . -f-$p) DIP_DOMAIN_ID=$domain_id
_domain=$h
_domain_id=$domain_id
return 0 return 0
fi fi
p=$i
(( i++ )) || true
done
if [[ $page -ge $total_pages ]] if [[ $page -ge $total_pages ]]
then then
@ -76,6 +62,8 @@ _update_dns_linode_v4_get_domain() {
return 1 return 1
} }
# Get Record ID
_update_dns_linode_v4_get_record_id() { _update_dns_linode_v4_get_record_id() {
local domain=$1 local domain=$1
local dtype=$2 local dtype=$2
@ -83,12 +71,6 @@ _update_dns_linode_v4_get_record_id() {
local response local response
local record_id local record_id
#if [[ "${#subdomain}" -gt "${#domain}" ]]; then
# subdomain="${subdomain:0:${#subdomain}-${#domain}-1}"
#else
# subdomain="@"
#fi
response=$(curl \ response=$(curl \
--silent \ --silent \
-X GET \ -X GET \
@ -96,30 +78,29 @@ _update_dns_linode_v4_get_record_id() {
-H "Authorization: Bearer $agent_update_dns_linode_v4_token" \ -H "Authorization: Bearer $agent_update_dns_linode_v4_token" \
"https://api.linode.com/v4/domains/$domain/records") "https://api.linode.com/v4/domains/$domain/records")
#sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq ".domain_names[].name" )"
record_id="$(echo "$response" | jq --arg subdomain "$subdomain" --arg dtype "$dtype" '.data[] | select(.name==$subdomain and .type==$dtype).id')" record_id="$(echo "$response" | jq --arg subdomain "$subdomain" --arg dtype "$dtype" '.data[] | select(.name==$subdomain and .type==$dtype).id')"
if [[ -z "$record_id" ]] if [[ -z "$record_id" ]]
then then
return 1 return 1
else else
_record_id=$record_id DIP_RECORD_ID=$record_id
return 0 return 0
fi fi
} }
if _update_dns_linode_v4_get_domain; then if _update_dns_linode_v4_get_domain_id "$DIP_DOMAIN"; then
if valid_ipv4 "$DIP_CUR_IP"; then if valid_ipv4 "$DIP_CUR_IP"; then
log "Updating Linode DNS IPv4 Record: ${DIP_RECORD} to ${DIP_CUR_IP}" log "Updating Linode DNS IPv4 Record: ${DIP_RECORD} to ${DIP_CUR_IP}"
if _update_dns_linode_v4_get_record_id "$_domain_id" "A" "$_sub_domain" if _update_dns_linode_v4_get_record_id "${DIP_DOMAIN_ID}" "A" "${DIP_RECORD}"
then then
curl --silent \ curl --silent \
-X PUT \ -X PUT \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_linode_v4_token" \ -H "Authorization: Bearer $agent_update_dns_linode_v4_token" \
-d '{"target":"'$DIP_CUR_IP'"}' \ -d '{"target":"'$DIP_CUR_IP'"}' \
"https://api.linode.com/v4/domains/$_domain_id/records/$_record_id" > /dev/null; "https://api.linode.com/v4/domains/${DIP_DOMAIN_ID}/records/${DIP_RECORD_ID}" > /dev/null;
else else
logerr "WARN: No A record exists for $DIP_RECORD" logerr "WARN: No A record exists for $DIP_RECORD"
fi fi
@ -128,18 +109,20 @@ if _update_dns_linode_v4_get_domain; then
if valid_ipv6 "$DIP_CUR_IP"; then if valid_ipv6 "$DIP_CUR_IP"; then
log "Updating Linux DNS IPv6 Record: ${DIP_RECORD} to ${DIP_CUR_IP}" log "Updating Linux DNS IPv6 Record: ${DIP_RECORD} to ${DIP_CUR_IP}"
if _update_dns_linode_v4_get_record_id "$_domain_id" "AAAA" "$_sub_domain" if _update_dns_linode_v4_get_record_id "${DIP_DOMAIN_ID}" "AAAA" "${DIP_RECORD}"
then then
curl --silent \ curl --silent \
-X PUT \ -X PUT \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_linode_v4_token" \ -H "Authorization: Bearer $agent_update_dns_linode_v4_token" \
-d '{"target":"'$DIP_CUR_IP'"}' \ -d '{"target":"'$DIP_CUR_IP'"}' \
"https://api.linode.com/v4/domains/$_domain_id/records/$_record_id" > /dev/null; "https://api.linode.com/v4/domains/${DIP_DOMAIN_ID}/records/${DIP_RECORD_ID}" > /dev/null;
else else
logerr "WARN: No AAAA record exists for $DIP_RECORD" logerr "WARN: No AAAA record exists for $DIP_RECORD"
fi fi
fi fi
unset DIP_DOMAIN_ID
unset DIP_RECORD_ID
else else
logerr "ERROR: Cannot find domain in Linode DNS API" logerr "ERROR: Cannot find domain in Linode DNS API"
exit 99 exit 99