From 4aa7d9c5605fa7ee917fa088128ebf36989b5b8c Mon Sep 17 00:00:00 2001 From: Eric Renfro Date: Sat, 30 Jan 2021 19:29:25 -0500 Subject: [PATCH] Added update-dns-do for DigitalOcean --- src/agents.d/update-dns-do | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/agents.d/update-dns-do diff --git a/src/agents.d/update-dns-do b/src/agents.d/update-dns-do new file mode 100644 index 0000000..baf01a1 --- /dev/null +++ b/src/agents.d/update-dns-do @@ -0,0 +1,44 @@ +#!/bin/bash + +# Internal Initialization + +source "${DIP_FUNCTIONS}" + +if [[ -r "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_NAME}.conf" ]]; then + source "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_NAME}.conf" +fi + +if [[ -r "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_EXEC}.conf" ]]; then + source "${DIP_BASE_DIR}/conf.d/${DIP_AGENT_EXEC}.conf" +fi + +if [[ -z "$agent_update_dns_do_token" ]]; then + logerr "ERROR: Need 'agent_update_dns_do_token' to be defined to your DigitalOcean API Access Token" + 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 + + +# Main + +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; + fi +fi + +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; + fi +fi