Browse Source

Merge pull request #17120 from giannoug/ddns-scripts-digitalocean

ddns-scripts: Rework DigitalOcean update script
lilik-openwrt-22.03
Florian Eckert 3 years ago
committed by GitHub
parent
commit
fdb471ab9e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 50 deletions
  1. +41
    -50
      net/ddns-scripts/files/usr/lib/ddns/update_digitalocean_com_v2.sh

+ 41
- 50
net/ddns-scripts/files/usr/lib/ddns/update_digitalocean_com_v2.sh View File

@ -1,51 +1,42 @@
# Script for sending user defined updates using DO API
# Script for sending user defined updates using the DigitalOcean API
# 2015 Artem Yakimenko <code at temik dot me>
#
# activated inside /etc/config/ddns by setting
#
# option update_script '/usr/lib/ddns/update_do.sh'
#
# the script is parsed (not executed) inside send_update() function
# of /usr/lib/ddns/dynamic_dns_functions.sh
# so you can use all available functions and global variables inside this script
# already defined in dynamic_dns_updater.sh and dynamic_dns_functions.sh
#
# It make sence to define the update url ONLY inside this script
# because it's anyway unique to the update script
# otherwise it should work with the default scripts
#
# Options are passed from /etc/config/ddns:
# Username - the record name DO Zone
# Password - API Token
# Domain - the domain managed by DO
# Parm_opt - The Record ID in the DO API structure
local __URL="https://api.digitalocean.com/v2/domains/[DOMAIN]/records/[RECORD_ID]"
local __HEADER="Authorization: Bearer [PASSWORD]"
local __HEADER_CONTENT="Content-Type: application/json"
local __BODY='{"name":"[NAME]","data": "[IP]"}'
# inside url we need username and password
[ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing 'domain'"
[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'Zone name in Username'"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
[ -z "$param_opt" ] && write_log 14 "Service section not configured correctly! Missing 'Zone ID in Optional Parameter'"
# do replaces in URL, header and body:
__URL=$(echo $__URL | sed -e "s#\[RECORD_ID\]#$param_opt#g" \
-e "s#\[DOMAIN\]#$domain#g")
__HEADER=$(echo $__HEADER| sed -e "s#\[PASSWORD\]#$password#g")
__HEADER_CONTENT=$(echo $__HEADER_CONTENT)
__BODY=$(echo $__BODY | sed -e "s#\[NAME\]#$username#g" -e "s#\[IP\]#$__IP#g")
#Send PUT request
curl -X PUT -H "$__HEADER_CONTENT" -H "$__HEADER" -d "$__BODY" "$__URL"
write_log 7 "DDNS Provider answered:\n$(cat $DATFILE)"
# analyse provider answers
# If IP is contained in the returned datastructure - API call was sucessful
grep -E "$__IP" $DATFILE >/dev/null 2>&1
return $? # "0" if IP has been changed or no change is needed
# 2021 George Giannou <giannoug at gmail dot com>
# Options passed from /etc/config/ddns:
# Domain - the domain name managed by DigitalOcean (e.g. example.com)
# Username - the hostname of the domain (e.g. myrouter)
# Password - DigitalOcean personal access token (API key)
# Optional Parameter - the API domain record ID of the hostname (e.g. 21694203)
# Use the following command to find your API domain record ID (replace TOKEN and DOMAIN with your own):
# curl -X GET -H 'Content-Type: application/json' \
# -H "Authorization: Bearer TOKEN" \
# "https://api.digitalocean.com/v2/domains/DOMAIN/records"
. /usr/share/libubox/jshn.sh
[ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing domain name as 'Domain'"
[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing hostname as 'Username'"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing personal access token as 'Password'"
[ -z "$param_opt" ] && write_log 14 "Service section not configured correctly! Missing API domain record ID as 'Optional Parameter'"
# Construct JSON payload
json_init
json_add_object
json_add_string name "$username"
json_add_string data "$__IP"
json_close_object
__STATUS=$(curl -Ss -X PUT "https://api.digitalocean.com/v2/domains/${domain}/records/${param_opt}" \
-H "Authorization: Bearer ${password}" \
-H "Content-Type: application/json" \
-d "$(json_dump)" \
-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE)
if [ $? -ne 0 ]; then
write_log 14 "Curl failed: $(cat $ERRFILE)"
return 1
elif [ -z $__STATUS ] || [ $__STATUS != 200 ]; then
write_log 14 "Curl failed: $__STATUS \ndigitalocean.com answered: $(cat $DATFILE)"
return 1
fi

Loading…
Cancel
Save