Added vultr agent

This commit is contained in:
Eric Renfro 2023-11-01 02:02:19 -04:00
parent d8714c02ab
commit 17efb671f1
Signed by: psi-jack
GPG key ID: 14977F3A50D9A5BF
3 changed files with 138 additions and 0 deletions

View file

@ -0,0 +1,132 @@
#!/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_vultr_token" ]]; then
logerr "ERROR: Need 'agent_update_dns_vultr_token' to be defined to your Vultr API Access Token"
exit 99
fi
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
# Get Domain ID
_update_dns_vultr_check_domain() {
local domain=$1
local domain_id
local response
local page
local total_pages
local i=2
local p=1
while read -r response
do
if [[ -n "$(echo "$response" | jq 'select(.errors != null)')" ]]
then
return 1
fi
total_pages=$(echo "$response" | jq ".meta.total")
next_page="$(sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq ".meta.links.next")")"
# FIXME
domain_id="$(sed -e 's/^"//' -e 's/"$//' <<<"$(echo "$response" | jq --arg domain "$domain" '.domains[] | select(.domain==$domain).domain')")"
if [[ -n "$domain_id" ]]
then
return 0
else
return 1
fi
if [[ $page -ge $total_pages ]]
then
return 1
fi
done <<< "$(curl --silent -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_vultr_token" \
"https://api.vultr.com/v2/domains?per_page=500")"
return 1
}
# Get Record ID
_update_dns_vultr_get_record_id() {
local domain=$1
local dtype=$2
local subdomain=$3
local response
local record_id
response=$(curl \
--silent \
-X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_vultr_token" \
"https://api.vultr.com/v2/domains/$domain/records?per_page=500")
record_id="$(echo "$response" | jq --arg subdomain "$subdomain" --arg dtype "$dtype" '.records[] | select(.name==$subdomain and .type==$dtype).id')"
if [[ -z "$record_id" ]]
then
return 1
else
DIP_RECORD_ID=$record_id
return 0
fi
}
if _update_dns_vultr_check_domain "$DIP_DOMAIN"; then
if valid_ipv4 "$DIP_CUR_IP"; then
log "Updating Vultr DNS IPv4 Record: ${DIP_RECORD}.${DIP_DOMAIN} to ${DIP_CUR_IP}"
if _update_dns_vultr_get_record_id "${DIP_DOMAIN_ID}" "A" "${DIP_RECORD}"
then
curl --silent \
-X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_vultr_token" \
-d '{"name":"A", "data": "'$DIP_CUR_IP'", "ttl": 300, "priority": 0}' \
"https://api.vultr.com/v2/domains/${DIP_DOMAIN}/records/${DIP_RECORD_ID}" > /dev/null;
else
logerr "WARN: No A record exists for $DIP_RECORD"
fi
fi
if valid_ipv6 "$DIP_CUR_IP"; then
log "Updating Linux DNS IPv6 Record: ${DIP_RECORD}.${DIP_DOMAIN} to ${DIP_CUR_IP}"
if _update_dns_vultr_get_record_id "${DIP_DOMAIN_ID}" "AAAA" "${DIP_RECORD}"
then
curl --silent \
-X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_vultr_token" \
-d '{"name":"AAAA", "data": "'$DIP_CUR_IP'", "ttl": 300, "priority": 0}' \
"https://api.vultr.com/v2/domains/${DIP_DOMAIN}/records/${DIP_RECORD_ID}" > /dev/null;
else
logerr "WARN: No AAAA record exists for $DIP_RECORD"
fi
fi
unset DIP_RECORD_ID
else
logerr "ERROR: Cannot find domain in Vultr DNS API"
exit 99
fi

View file

@ -0,0 +1,3 @@
# Linode API Access Token
#agent_update_dns_linode_v4_token=

View file

@ -0,0 +1,3 @@
# Vultr API Access Token
#agent_update_dns_vultr_token=