From 5d6ff9f2ed95c3665133f98ac38a1879d6e6961d Mon Sep 17 00:00:00 2001 From: Eric Renfro Date: Sun, 31 Jan 2021 11:55:46 -0500 Subject: [PATCH] Updated DigitalOcean Agent further, and ready for testing --- src/agents.d/update-dns-do | 115 ++++++++++++++++++++++++++++++++----- 1 file changed, 100 insertions(+), 15 deletions(-) diff --git a/src/agents.d/update-dns-do b/src/agents.d/update-dns-do index baf01a1..087755f 100644 --- a/src/agents.d/update-dns-do +++ b/src/agents.d/update-dns-do @@ -17,28 +17,113 @@ if [[ -z "$agent_update_dns_do_token" ]]; then exit 99 fi -if [[ -z "$agent_update_dns_do_domain" ]]; then - logerr "ERROR: Need 'agent_update_dns_do_domain' to be defined to your DigitalOcean Domain Name" - exit 99 -fi +#if [[ -z "$agent_update_dns_do_domain" ]]; then +# logerr "ERROR: Need 'agent_update_dns_do_domain' to be defined to your DigitalOcean Domain Name" +# exit 99 +#fi + +# Get Domain Name + +_update_dns_do_get_domain() { + local domain + local response + response=$(curl \ + --silent + -X GET \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $agent_update_dns_do_token" \ + "https://api.digitalocean.com/v2/domains?per_page=500") + + #for domain in $(sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq ".domains[].name")") + #do + # if [[ $domain == *"$DIP_RECORD" ]]; then + # echo "$domain" + # return 0 + # fi + #done + + while read -r domain + do + if [[ $domain == *"$DIP_RECORD" ]]; then + echo "$domain" + return 0 + fi + done <<< "$($(sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq ".domains[].name")")" + return 1 +} + + +_update_dns_do_get_record_id() { + local domain=$1 + local dtype=$2 + local subdomain=${DIP_RECORD} + local response + + if [[ "${#subdomain}" -gt "${#domain}" ]]; then + subdomain="${subdomain:0:${#subdomain}-${#domain}-1}" + else + subdomain="@" + fi + + response=$(curl \ + --silent + -X GET \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $agent_update_dns_do_token" \ + "https://api.digitalocean.com/v2/domains/$domain/records?per_page=500") + + #sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq ".domain_names[].name" )" + echo "$response" | jq ".domain_records[] | select(.name==\"$subdomain\" and .type==\"$dtype\") | .id" +} + +#unset agent_update_dns_do_domain +#for domain in $(_update_dns_do_get_domains) +#do +# if [[ $DIP_RECORD == *"$domain" ]] +# then +# agent_update_dns_do_domain="$domain" +# fi +#done +# +#if [[ -z "$agent_update_dns_do_domain" ]]; then +# logerr "ERROR: Cannot find domain in DigitalOcean DNS API" +# exit 99 +#fi # Main -if valid_ipv4 "$DIP_CUR_IP"; then - log "Updating DigitalOcean DNS IPv4 Record: ${DIP_RECORD} to ${DIP_CUR_IP}" +if agent_update_dns_do_domain=$(_update_dns_do_get_domain); then + if valid_ipv4 "$DIP_CUR_IP"; then + log "Updating DigitalOcean DNS IPv4 Record: ${DIP_RECORD} to ${DIP_CUR_IP}" - record_id4=$(curl --silent --request GET --header "Content-Type: application/json" --header "Authorization: Bearer $agent_update_dns_do_token" "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records" | jq ".domain_records[] | select(.name==\"${DIP_RECORD}\" and .type==\"A\") | .id") - if [[ -n "$record_id4" ]]; then - curl --silent -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $agent_update_dns_do_token" -d '{"data":"'$DIP_CUR_IP'"}' "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records/$record_id4" > /dev/null; + record_id4="$(_update_dns_do_get_record_id "$agent_update_dns_do_domain" "A")" + #record_id4=$(curl --silent --request GET --header "Content-Type: application/json" --header "Authorization: Bearer $agent_update_dns_do_token" "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records?per_page=500" | jq ".domain_records[] | select(.name==\"${DIP_RECORD}\" and .type==\"A\") | .id") + if [[ -n "$record_id4" ]]; then + curl --silent \ + -X PUT \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $agent_update_dns_do_token" \ + -d '{"data":"'$DIP_CUR_IP'"}' \ + "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records/$record_id4" > /dev/null; + fi fi -fi -if valid_ipv6 "$DIP_CUR_IP"; then - log "Updating DigitalOcean DNS IPv6 Record: ${DIP_RECORD} to ${DIP_CUR_IP}" + if valid_ipv6 "$DIP_CUR_IP"; then + log "Updating DigitalOcean DNS IPv6 Record: ${DIP_RECORD} to ${DIP_CUR_IP}" - record_id6=$(curl --silent --request GET --header "Content-Type: application/json" --header "Authorization: Bearer $agent_update_dns_do_token" "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records" | jq ".domain_records[] | select(.name==\"${DIP_RECORD}\" and .type==\"AAAA\") | .id") - if [[ -n "$record_id6" ]]; then - curl --silent -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $agent_update_dns_do_token" -d '{"data":"'$DIP_CUR_IP'"}' "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records/$record_id6" > /dev/null; + record_id6="$(_update_dns_do_get_record_id "$agent_update_dns_do_domain" "AAAA")" + #record_id6=$(curl --silent --request GET --header "Content-Type: application/json" --header "Authorization: Bearer $agent_update_dns_do_token" "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records?per_page=500" | jq ".domain_records[] | select(.name==\"${DIP_RECORD}\" and .type==\"AAAA\") | .id") + if [[ -n "$record_id6" ]]; then + curl --silent \ + -X PUT \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $agent_update_dns_do_token" \ + -d '{"data":"'$DIP_CUR_IP'"}' \ + "https://api.digitalocean.com/v2/domains/$agent_update_dns_do_domain/records/$record_id6" > /dev/null; + fi fi +else + logerr "ERROR: Cannot find domain in DigitalOcean DNS API" + exit 99 fi