summaryrefslogtreecommitdiff
path: root/update.sh
blob: 410075bf0802c6a59b2e180193ca2fd6c649095e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash
set -e 
if [ $# -ne 2 ]; then
    echo "Usage: ./update.sh DOMAIN API_CONFIG" >&2
    exit 1
fi

DOMAIN=$1
API_CONFIG=$2

source $API_CONFIG
config_ip=$(dig +short $DOMAIN)
actual_ip=$(curl -s https://ipinfo.io/ip)

if [ "$config_ip" != "$actual_ip" ]; then
    echo "Updating IP from $config_id to $actual_ip for $DOMAIN"
    res=$(curl -s -X POST  "https://porkbun.com/api/json/v3/dns/retrieve/$DOMAIN" \
	       -d "{ \"secretapikey\": \"${SECRETAPIKEY}\", \"apikey\": \"${APIKEY}\" }")
    status=$(echo "$res" | jq -r '.status')
    if [ "$status" != "SUCCESS" ]; then
	echo -e "Failed to fetch domain ID:\n\t$res" >&2
	exit 1
    fi
    ID=$(echo "$res" | jq -r ".records[] | select(.type == \"A\") | select(.content == \"${config_ip}\") | .id")
    res=$(curl -s -X POST "https://porkbun.com/api/json/v3/dns/edit/$DOMAIN/$ID" \
	       -d "{ \"secretapikey\": \"${SECRETAPIKEY}\", \"apikey\": \"${APIKEY}\", \"content\": \"${actual_ip}\", \"ttl\": \"600\", \"type\": \"A\", \"name\": \"\" }")
    status=$(echo "$res" | jq -r '.status')
    if [ "$status" == "SUCCESS" ]; then
	echo "Successfully updated $DOMAIN record $ID from $config_ip to $actual_ip"
    else
	echo "Failed to update DNS record for $DOMAIN with $ID from $config_ip to $actual_ip" >&2
	pretty=$(echo "$res" | jq '.')
	echo "$pretty" >&2
	exit 1
    fi
else
    echo "$DOMAIN is currently registered properly: $config_ip $actual_ip"
fi

exit 0