Skip to content

Instantly share code, notes, and snippets.

@jasonm23
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonm23/8cd85f248f5bdb1c7e36 to your computer and use it in GitHub Desktop.
Save jasonm23/8cd85f248f5bdb1c7e36 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'digitalocean'
require 'yaml'
if ARGV.length < 2
puts "Usage:\n"
puts " do-dns-update domain subdomain\n\n"
exit
end
current_ip = File.read("#{ENV['HOME']}/.wan_ip").strip rescue ""
wan_ip = `curl ifconfig.me 2>/dev/null`.strip
# Read the
domain_name = ARGV[0]
sub_domain_name = ARGV[1]
if ENV['DIGITALOCEAN_CLIENTID'] and ENV['DIGITALOCEAN_APIKEY']
Digitalocean.client_id = ENV['DIGITALOCEAN_CLIENTID']
Digitalocean.api_key = ENV['DIGITALOCEAN_APIKEY']
elsif File.exist? "#{ENV['HOME']}/.digitalocean"
yaml = YAML.load_file "#{ENV['HOME']}/.digitalocean"
if yaml["client_id"] and yaml["api_key"]
Digitalocean.client_id = yaml['client_id']
Digitalocean.api_key = yaml['api_key']
end
else
puts "Set DIGITALOCEAN_CLIENTID and DIGITALOCEAN_APIKEY or create ~/.digitalocean,\n with client_id and api_key as YAML property names."
end
if current_ip != wan_ip
all_domains = Digitalocean::Record.all(domain_name)
if all_domains.status == "OK"
sub_domain_id = all_domains.to_h[:records].select{|r| r["name"] == sub_domain_name }.first["id"]
Digitalocean::Record.edit(domain_name, sub_domain_id, {data: wan_ip})
File.open("#{ENV['HOME']}/.wan_ip", "w") do |f|
f.print wan_ip
end
puts wan_ip
elsif all_domains.status == "ERROR" and all_domains.error_message == "Access Denied"
puts "Access denied - set DIGITALOCEAN_CLIENTID and DIGITALOCEAN_APIKEY to the\nDigital Ocean client id and API key"
else
puts "unknown error"
end
else
puts "wan ip is unchanged : #{wan_ip}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment