Skip to content

Instantly share code, notes, and snippets.

@mukunda-
Created July 23, 2022 21:43
Show Gist options
  • Save mukunda-/75696e571838e398bca20b6ed44e16d6 to your computer and use it in GitHub Desktop.
Save mukunda-/75696e571838e398bca20b6ed44e16d6 to your computer and use it in GitHub Desktop.
Dreamhost Certbot DNS hook example
import requests, uuid, json, os, time
DREAMHOST_KEY = '<dreamhost API key>';
#----------------------------------------------------------------------------------------
def dh_request( **kwargs ):
params = kwargs
params["key"] = DREAMHOST_KEY
params["unique_id"] = str(uuid.uuid4())
params["format"] = "json"
return json.loads(
requests.get("https://api.dreamhost.com/", params = params).text )
#----------------------------------------------------------------------------------------
def update_dreamhost_dns( name, dnstype, value ):
print( "Querying records from dreamhost." )
records = dh_request( cmd="dns-list_records" )
for record in records["data"]:
if record["record"] == name and record["type"] == dnstype:
print( record )
if record["value"] == value:
print( f"{name} is already up to date." )
return
print( "Removing existing record." )
result = dh_request(
cmd = "dns-remove_record",
record = name,
type = dnstype,
value = record["value"] )
print( result )
break
print( "Adding record." )
result = dh_request(
cmd = "dns-add_record",
record = name,
type = dnstype,
value = value )
print( result )
certbot_domain = os.environ.get("CERTBOT_DOMAIN")
certbot_validation = os.environ.get("CERTBOT_VALIDATION")
print("Domain:", certbot_domain)
print("Validation string:", certbot_validation)
if certbot_domain == "mukunda.com":
update_dreamhost_dns( "_acme-challenge.mukunda.com", "TXT", certbot_validation )
print("Sleeping for 10 minutes to allow DNS to propagate.")
time.sleep(600)
else:
print("Unhandled domain:", certbot_domain);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment