Files
python-scripts/generate-dns-record.py

52 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# Purpose of this script: to create a new TXT record
# on the Gandi DNS provider using the information
# supplied from the CERTBOT_VALIDATION and CERTBOT_DOMAIN
# environment variables (this was made to mainly work
# with certbot)
import os
import random
import requests
import time
from DnsRecordDeleter import delete_dns_record
from Logger import load_logger
from GandiAuthenticationHeader import get_authentication_headers
headers = get_authentication_headers()
logger = load_logger()
validation_token = os.environ['CERTBOT_VALIDATION']
domain = os.environ['CERTBOT_DOMAIN']
SUBDOMAIN = '_acme-challenge'
LIVEDNS_API_URL = 'https://api.gandi.net/v5/livedns/domains'
# Create a new TXT record from scratch
record = {
"rrset_name": SUBDOMAIN,
"rrset_type": "TXT",
"rrset_ttl": 1800,
"rrset_values": [validation_token],
}
# Due to there being multiple subdomains being requested, this
# script will be invoked multiple times with different domain
# values. As such, to avoid conflicts, we will delay each run
# of this script a random number of seconds, between 0 and 180
# to make sure there are no conflicts between the separate
# runs of this script
random_nr = random.randrange(0, 180)
time.sleep(random_nr)
response = requests.post(LIVEDNS_API_URL + '/' + domain
+ '/records', headers=headers, json=record)
if not response.ok:
logger.error('Could not create proper DNS record for LETSENCRYPT')
logger.error(response.raise_for_status())
exit(1)
time.sleep(30)