Add Cloudflare DNS record creation to create-server script

This commit is contained in:
Chandler Swift 2024-08-04 00:00:26 -05:00
parent 75f7dfa9ed
commit 6e16dd7162
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F

View file

@ -82,3 +82,32 @@ while res['instance']['main_ip'] == '0.0.0.0':
time.sleep(5)
print(res['instance']['main_ip'])
# Create DNS record
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {config['cloudflare-token']}",
}
# get zone id
zone_name = args.label.split('.',maxsplit=1)[1]
zones = requests.get("https://api.cloudflare.com/client/v4/zones", headers=headers).json()['result']
zones = [z for z in zones if z['name'] == zone_name]
if len(zones) != 1:
print(zones)
raise Exception(f"Expected 1 zone; got {len(zones)}")
url = f"https://api.cloudflare.com/client/v4/zones/{zones[0]['id']}/dns_records"
payload = {
"content": res['instance']['main_ip'],
"name": args.label,
"proxied": False,
"type": "A",
"id": os.urandom(15).hex(), # not sure why this is required
"ttl": 1, # automatic
}
response = requests.request("POST", url, json=payload, headers=headers)
print("Created DNS record!")
print(response.text)