-
-
Save pburleson/248707 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'logger' | |
require 'open-uri' | |
begin | |
require 'active_resource' | |
rescue LoadError => e | |
# active_resource has probably been installed via rubygems | |
# which, of course, does not put it on the search path | |
# silly package manager :-( | |
begin | |
require 'rubygems' | |
require 'active_resource' | |
rescue LoadError | |
puts "you need to install the active_resource gem" | |
puts "see http://bit.ly/3Bnnwo for active resource info" | |
exit 1 | |
end | |
end | |
FLAGS = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #$0 -H <host name> -k <api key>" | |
opts.on("-H", "--host HOST", "Host name") do |host| | |
host = host + "." unless host =~ /\.$/ | |
FLAGS[:host] = host | |
end | |
opts.on("-k", "--api-key KEY", "Slicehost API Key") {|FLAGS[:key]|} | |
opts.on("-v", "--verbose", "Log HTTP requests") do | |
ActiveResource::Base.logger = Logger.new(STDERR) | |
end | |
opts.on("-h", "--help", "Show help") { FLAGS[:help] = true } | |
opts.on_tail "See http://articles.slicehost.com/api for API details" | |
opts.parse! | |
if !FLAGS[:host] or !FLAGS[:key] or FLAGS[:help] | |
puts opts | |
exit 1 | |
end | |
end | |
IP_PAT = Regexp.compile("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)") | |
DNS_VAL = if `host #{FLAGS[:host]}` =~ IP_PAT | |
$1 | |
else | |
puts "unable to locate host #{FLAGS[:host]}" | |
exit 1 | |
end | |
IP = open("http://checkip.dyndns.org/") do |i| | |
unless i.readlines.join(" ") =~ IP_PAT | |
puts "did not find a valid IP address, exiting!" | |
exit 1 | |
end | |
$1 | |
end | |
unless DNS_VAL == IP | |
class Record < ActiveResource::Base | |
self.site = "https://#{FLAGS[:key]}@api.slicehost.com/" | |
end | |
r = Record.find(:first, :params => { | |
:record_type => 'A', | |
:name => FLAGS[:host] | |
}) | |
unless r | |
puts "Failed to find an A record for #{FLAGS[:host]}" | |
exit 1 | |
end | |
r.data = IP | |
if r.save | |
puts "updated IP to #{IP}" | |
else | |
puts "failed to update IP to #{IP}" | |
exit 1 | |
end | |
else | |
puts "nothing to be done" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment