Created
July 23, 2022 21:43
-
-
Save mukunda-/75696e571838e398bca20b6ed44e16d6 to your computer and use it in GitHub Desktop.
Dreamhost Certbot DNS hook example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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