all repos — dotfiles @ 083a6969b204afbfcc06b1d3e46a32d5cc46e4d9

linux dotfiles

scripts: update
Prithu Goswami pg@prithu.dev
Thu, 19 Jan 2023 20:37:17 +0530
commit

083a6969b204afbfcc06b1d3e46a32d5cc46e4d9

parent

3b2c094af138f46b4a7c4dc278371f1534e42bbc

1 files changed, 28 insertions(+), 67 deletions(-)

jump to
M bin/scripts/update_home_dns.shbin/scripts/update_home_dns.sh

@@ -1,79 +1,40 @@

#!/bin/bash # -# This is a script to update a Netlify subdomain A record with the current external IP. -# The example below would update the local.example.com A record to the current external IP with a TTL of 5 minutes. -# -# Usage: -# netlify-ddns.sh <ACCESS_TOKEN> <DOMAIN> <SUBDOMAIN> <TTL> -# -# Example: -# netlify-ddns.sh aCcEsStOKeN example.com local 300 -# -# if [ "$#" -ne 4 ]; then -# echo "Wrong number of parameters passed" -# echo "Usage:" -# echo "$0 <ACCESS_TOKEN> <DOMAIN> <SUBDOMAIN> <TTL>" -# exit -# fi - -# ACCESS_TOKEN="$1" -# DOMAIN="$2" -# SUBDOMAIN="$3" -# TTL="$4" - -ACCESS_TOKEN="$NETLIFY_ACCESS_TOKEN" -DOMAIN="prithu.xyz" -SUBDOMAIN="$SUBDOMAIN_" -TTL="5" +# a quick dirty script to update a specific dns record using netlify api +# could spend some time later improving it and making it robust +# set env variables DNS_HOSTNAME_ and NETLIFY_ACCESS_TOKEN_ -NETLIFY_API="https://api.netlify.com/api/v1" +netlify_api="https://api.netlify.com/api/v1" +token="$D_NETLIFY_ACCESS_TOKEN" +hostname="$D_HOSTNAME" +domain=$(echo -n $hostname | cut -d'.' -f2-3) ping -q -c 1 ifconfig.io >/dev/null || exit -EXTERNAL_IP=`curl -s ifconfig.io` +IP=`curl -s ifconfig.io` -#EXTERNAL_IP=`dig +short myip.opendns.com @resolver1.opendns.com` -echo "Current external IP is $EXTERNAL_IP" +echo "Fetching records" -HOSTNAME="$SUBDOMAIN.$DOMAIN" +dns_zones=`curl -s "$netlify_api/dns_zones?access_token=$token" --header "Content-Type:application/json"` +zone_id=`echo $dns_zones | jq ".[] | select(.name == \"$domain\") | .id" --raw-output` +record=$(curl -s "$netlify_api/dns_zones/$zone_id/dns_records?access_token=$token" --header "Content-Type:application/json" | jq ".[] | select(.hostname == \"$hostname\")" --raw-output) +record_val=`echo $record | jq ".value" --raw-output` +record_id=`echo $record | jq ".id" --raw-output` -DNS_ZONES_RESPONSE=`curl -s "$NETLIFY_API/dns_zones?access_token=$ACCESS_TOKEN" --header "Content-Type:application/json"` -ZONE_ID=`echo $DNS_ZONES_RESPONSE | jq ".[] | select(.name == \"$DOMAIN\") | .id" --raw-output` -DNS_RECORDS_RESPONSE=`curl -s "$NETLIFY_API/dns_zones/$ZONE_ID/dns_records?access_token=$ACCESS_TOKEN" --header "Content-Type:application/json"` -RECORD=`echo $DNS_RECORDS_RESPONSE | jq ".[] | select(.hostname == \"$HOSTNAME\")" --raw-output` -RECORD_VALUE=`echo $RECORD | jq ".value" --raw-output` -echo "Current $HOSTNAME value is $RECORD_VALUE" +if [[ "$record_val" != "$IP" ]]; then -if [[ "$RECORD_VALUE" != "$EXTERNAL_IP" ]]; then + # delete the record first + [[ $record_val -ne "" ]] && curl -X DELETE -s "$netlify_api/dns_zones/$zone_id/dns_records/$record_id?access_token=$token" --header "Content-Type:application/json" - if [[ "$RECORD_VALUE" != "" ]]; then - echo "Deleting current entry for $HOSTNAME" - RECORD_ID=`echo $RECORD | jq ".id" --raw-output` - DELETE_RESPONSE_CODE=`curl -X DELETE -s -w "%{response_code}" "$NETLIFY_API/dns_zones/$ZONE_ID/dns_records/$RECORD_ID?access_token=$ACCESS_TOKEN" --header "Content-Type:application/json"` + body=$(cat <<EOF +{ + "type": "A", + "hostname": "$hostname", + "value": "$IP", + "ttl": 5 +} +EOF + ) - if [[ $DELETE_RESPONSE_CODE != 204 ]]; then - echo "There was a problem deleting the existing $HOSTNAME entry, response code was $DELETE_RESPONSE_CODE" - exit - fi - fi - - echo "Creating new entry for $HOSTNAME with value $EXTERNAL_IP" - CREATE_BODY=`jq -n --arg hostname "$HOSTNAME" --arg externalIp "$EXTERNAL_IP" --arg ttl $TTL ' - { - "type": "A", - "hostname": $hostname, - "value": $externalIp, - "ttl": $ttl|tonumber - }'` - - CREATE_RESPONSE=`curl -s --data "$CREATE_BODY" "$NETLIFY_API/dns_zones/$ZONE_ID/dns_records?access_token=$ACCESS_TOKEN" --header "Content-Type:application/json"` - - NEW_RECORD_TYPE=`echo $CREATE_RESPONSE | jq ".type" --raw-output` - NEW_RECORD_HOSTNAME=`echo $CREATE_RESPONSE | jq ".hostname" --raw-output` - NEW_RECORD_VALUE=`echo $CREATE_RESPONSE | jq ".value" --raw-output` - NEW_RECORD_TTL=`echo $CREATE_RESPONSE | jq ".ttl" --raw-output` - - if [[ $NEW_RECORD_TYPE != "A" ]] || [[ $NEW_RECORD_HOSTNAME != $HOSTNAME ]] || [[ $NEW_RECORD_VALUE != $EXTERNAL_IP ]] || [[ $NEW_RECORD_TTL != $TTL ]]; then - echo "There was a problem creating the new entry, some values did not match" - exit - fi + echo "Setting new record" + response=$(curl -s --data "$body" "$netlify_api/dns_zones/$zone_id/dns_records?access_token=$token" --header "Content-Type:application/json") fi