aboutsummaryrefslogtreecommitdiffstats
path: root/templates/usr_local_bin_do_dyndns.sh.j2
blob: f23d635e918c619ebbdfeb0c8746472a0dc701ab (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
41
42
#!/bin/sh
set -eu

DOMAIN="jan.systems"
HOSTNAME="pursotin"
AUTH_FILE="/usr/local/etc/do_dyndns_auth"
IF4="$(jexec ingress ifconfig epw1b)"

# END OF CONFIG

info() {
        echo "$(date) [INFO]" $@
}

err() {
        >&2 echo "$(date) [ERROR]" $@
}

if [ ! -r "$AUTH_FILE" ]; then
        err "Auth file '$AUTH_FILE' not readable"
        exit 1
fi

IP4=$(echo "$IF4" | awk '/inet / { print $2 }' | head -n1)
DIGITALOCEAN_TOKEN=$(awk '{$1=$1; print}' "$AUTH_FILE")

info "Updating A record for hostname $HOSTNAME in domain $DOMAIN → $IP4"

RECORD_ID=$(curl -s -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
"https://api.digitalocean.com/v2/domains/$DOMAIN/records?per_page=999999" |
jq -r ".domain_records[] | select(.type==\"A\" and .name==\"$HOSTNAME\") | .id")

if [ -n "$RECORD_ID" ]; then
        curl -s -X PUT \
            -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
            -H "Content-Type: application/json" \
            -d "{\"data\":\"$IP4\"}" \
            "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID" > /dev/null
        info "Record $RECORD_ID updated"
else
        err "No A record found for hostname \"$HOSTNAME\" in domain \"$DOMAIN\". Skipping."
fi