From ba45fe932f74fc8fc7af686fde96d006e1b59137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Mon, 29 Nov 2021 12:25:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9A=20Add=20script=20to=20configure=20?= =?UTF-8?q?Netlify=20DNS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prepare-vms/netlify-dns.sh | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 prepare-vms/netlify-dns.sh diff --git a/prepare-vms/netlify-dns.sh b/prepare-vms/netlify-dns.sh new file mode 100755 index 00000000..6d3bdde0 --- /dev/null +++ b/prepare-vms/netlify-dns.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +[ "$1" ] || { + echo "" + echo "Add a record in Netlify DNS." + echo "This script is hardcoded to add a record to container.training". + echo "" + echo "Syntax:" + echo "$0 " + echo "" + echo "Example to create a A record for eu.container.training:" + echo "$0 eu 185.145.250.0" + echo "" + 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) + +netlify() { + URI=$1 + shift + http https://api.netlify.com/api/v1/$URI "$@" "Authorization:Bearer $NETLIFY_TOKEN" +} + +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?) + +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'")'