Skip to content

Instantly share code, notes, and snippets.

@hisui
Created March 1, 2019 03:34
Show Gist options
  • Save hisui/d6d12541aa547b629113eb1fdc393a3d to your computer and use it in GitHub Desktop.
Save hisui/d6d12541aa547b629113eb1fdc393a3d to your computer and use it in GitHub Desktop.
Certbot hook script for Cloud DNS
#!/usr/bin/env ruby
require 'timeout'
require 'rake'
CLOUD_DNS_ZONE = "..."
def cloud_dns(action, args = [])
sh "gcloud dns record-sets transaction #{action} --zone=#{CLOUD_DNS_ZONE} #{args.join(" ")}"
end
def load_txt
`dig +short TXT _acme-challenge.#{ENV['CERTBOT_DOMAIN']}`.chomp
end
cloud_dns "start"
if (old = load_txt()) != ""
cloud_dns "remove", [
old,
"--type=TXT",
"--name=_acme-challenge.#{ENV['CERTBOT_DOMAIN']}.",
"--ttl=1"
]
end
cloud_dns "add", [
ENV['CERTBOT_VALIDATION'],
"--type=TXT",
"--name=_acme-challenge.#{ENV['CERTBOT_DOMAIN']}.",
"--ttl=1"
]
cloud_dns "execute"
started_at = Time.now.to_i
while load_txt() != '"' + ENV['CERTBOT_VALIDATION'] + '"'
if Time.now.to_i - started_at > 60 * 5
raise Timeout::Error
end
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment