Added linode-v4 agent

This commit is contained in:
Eric Renfro 2021-08-23 10:36:23 -04:00
parent d537ffa170
commit 564b246940
Signed by: psi-jack
GPG key ID: 14977F3A50D9A5BF

137
src/agents.d/update-dns-linode-v4 Executable file
View file

@ -0,0 +1,137 @@
#!/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_linode_v4_token" ]]; then
logerr "ERROR: Need 'agent_update_dns_linode_v4_token' to be defined to your Linode API Access Token"
exit 99
fi
# Get Domain Name
_update_dns_linode_v4_get_domain() {
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
page=$(echo "$response" | jq ".page")
total_pages=$(echo "$response" | jq ".pages")
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" ]]
then
_sub_domain=$(printf "%s" "$DIP_RECORD" | cut -d . -f-$p)
_domain=$h
_domain_id=$domain_id
return 0
fi
p=$i
(( i++ )) || true
done
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_linode_v4_token" "https://api.linode.com/v4/domains/")"
return 1
}
_update_dns_linode_v4_get_record_id() {
local domain=$1
local dtype=$2
local subdomain=$3
local response
local record_id
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_linode_v4_token" \
"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')"
if [[ -z "$record_id" ]]
then
return 1
else
_record_id=$record_id
return 0
fi
}
if _update_dns_linode_v4_get_domain; then
if valid_ipv4 "$DIP_CUR_IP"; then
log "Updating Linode DNS IPv4 Record: ${DIP_RECORD} to ${DIP_CUR_IP}"
if _update_dns_do_get_record_id "$_domain_id" "A" "$_sub_domain"
then
curl --silent \
-X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_linode_v4_token" \
-d '{"target":"'$DIP_CUR_IP'"}' \
"https://api.linode.com/v4/domains/$_domain_id/records/$_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} to ${DIP_CUR_IP}"
if _update_dns_do_get_record_id "$_domain_id" "AAAA" "$_sub_domain"
then
curl --silent \
-X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $agent_update_dns_linode_v4_token" \
-d '{"target":"'$DIP_CUR_IP'"}' \
"https://api.linode.com/v4/domains/$_domain_id/records/$_record_id" > /dev/null;
else
logerr "WARN: No AAAA record exists for $DIP_RECORD"
fi
fi
else
logerr "ERROR: Cannot find domain in DigitalOcean DNS API"
exit 99
fi