From e67fca695eee3b21f4bfebf081c1f11c8a392c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Mon, 3 Jan 2022 13:18:31 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20=20Add=20'list'=20funct?= =?UTF-8?q?ion=20to=20Netlify=20helper=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prepare-vms/netlify-dns.sh | 56 ++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/prepare-vms/netlify-dns.sh b/prepare-vms/netlify-dns.sh index 6d3bdde0..623f18f3 100755 --- a/prepare-vms/netlify-dns.sh +++ b/prepare-vms/netlify-dns.sh @@ -6,7 +6,8 @@ echo "This script is hardcoded to add a record to container.training". echo "" echo "Syntax:" - echo "$0 " + echo "$0 list" + echo "$0 add " echo "" echo "Example to create a A record for eu.container.training:" echo "$0 eu 185.145.250.0" @@ -14,9 +15,6 @@ exit 1 } -NAME=$1.container.training -ADDR=$2 - NETLIFY_USERID=$(jq .userId < ~/.config/netlify/config.json) NETLIFY_TOKEN=$(jq -r .users[$NETLIFY_USERID].auth.token < ~/.config/netlify/config.json) @@ -29,19 +27,43 @@ netlify() { ZONE_ID=$(netlify dns_zones | jq -r '.[] | select ( .name == "container.training" ) | .id') -# It looks like if we create two identical records, then delete one of them, -# Netlify DNS ends up in a weird state (the name doesn't resolve anymore even -# though it's still visible through the API and the website?) +_list() { + netlify dns_zones/$ZONE_ID/dns_records | + jq -r '.[] | select(.type=="A") | [.hostname, .type, .value] | @tsv' +} -if netlify dns_zones/$ZONE_ID/dns_records | - jq '.[] | select(.hostname=="'$NAME'" and .type=="A" and .value=="'$ADDR'")' | - grep . -then - echo "It looks like that record already exists. Refusing to create it." - exit 1 -fi +_add() { + NAME=$1.container.training + ADDR=$2 -netlify dns_zones/$ZONE_ID/dns_records type=A hostname=$NAME value=$ADDR ttl=300 -netlify dns_zones/$ZONE_ID/dns_records | - jq '.[] | select(.hostname=="'$NAME'")' + # It looks like if we create two identical records, then delete one of them, + # Netlify DNS ends up in a weird state (the name doesn't resolve anymore even + # though it's still visible through the API and the website?) + + if netlify dns_zones/$ZONE_ID/dns_records | + jq '.[] | select(.hostname=="'$NAME'" and .type=="A" and .value=="'$ADDR'")' | + grep . + then + echo "It looks like that record already exists. Refusing to create it." + exit 1 + fi + + netlify dns_zones/$ZONE_ID/dns_records type=A hostname=$NAME value=$ADDR ttl=300 + + netlify dns_zones/$ZONE_ID/dns_records | + jq '.[] | select(.hostname=="'$NAME'")' +} + +case "$1" in + list) + _list + ;; + add) + _add $2 $3 + ;; + *) + echo "Unknown command '$1'." + exit 1 + ;; +esac