#!/bin/sh set -e source ./.env domain=$(python -c "print('.'.join('${PUBLIC_URL}'.split('.')[-2:]))") baseSubdomain=$(python -c "url='${PUBLIC_URL}'; print('.'.join(url.split('.')[:-2]) if url.count('.') > 1 else '@')") domainList=$(doctl compute domain records list "$domain" --output json) ttl=600 function recordID() { subdomain=$1 domain=$2 echo $domainList | jq -r '.[] | select(.type == "A" and .name == "'"$subdomain"'") | .id' } function ipOfRecord() { recID=$1 echo $domainList | jq -r '.[] | select(.id == '"$recID"') | .data' } function _currentIP() { ipData=$(curl -s https://am.i.mullvad.net/json) if test 'true' = `echo $ipData | jq .mullvad_exit_ip` then echo "error: connected to anonymizing VPN. This won't work." exit 1 else echo $ipData | jq -r .ip fi } currentIP=`_currentIP` function checkRecord() { subdomain="$1" domain="$2" fullDomain="$(test "$subdomain" = @ && printf "" || printf "${subdomain}.")$domain" echo checking $fullDomain recID=`recordID $subdomain $domain` if test -n "$recID" then # record exists! printf "found record $recID " recIP=`ipOfRecord $recID` echo with IP $recIP if test "$recIP" = "$currentIP" then echo $fullDomain already set to correct IP $currentIP else echo $fullDomain IP set to $recIP when it should be $currentIP, updating... doctl compute domain records update $domain --record-id $recID --record-data $currentIP echo ...done fi else echo no record yet for $fullDomain, setting to $currentIP... doctl compute domain records create $domain \ --record-ttl $ttl \ --record-type A \ --record-name $subdomain \ --record-data $currentIP echo ...done fi } checkRecord $baseSubdomain $domain if test $baseSubdomain = @ then checkRecord wopi $domain checkRecord office $domain else checkRecord wopi.$baseSubdomain $domain checkRecord office.$baseSubdomain $domain fi