130 lines
4.5 KiB
Bash
Executable file
130 lines
4.5 KiB
Bash
Executable file
#!/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
|
|
|
|
# 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 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="$(_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
|
|
|
|
if valid_ipv6 "$DIP_CUR_IP"; then
|
|
log "Updating DigitalOcean DNS IPv6 Record: ${DIP_RECORD} to ${DIP_CUR_IP}"
|
|
|
|
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
|