Skip to content

Instantly share code, notes, and snippets.

@larstobi
Created February 20, 2012 13:13
Show Gist options
  • Select an option

  • Save larstobi/1869134 to your computer and use it in GitHub Desktop.

Select an option

Save larstobi/1869134 to your computer and use it in GitHub Desktop.
Update from a local DNS record set to AWS Route53
class Localrecord
attr_accessor :name
attr_accessor :value
attr_accessor :type
attr_accessor :ttl
attr_accessor :weight
def initialize(options = {})
@name = options['name']
@type = options['type']
@ttl = options['ttl']
@weight = options['weight']
if options['value'].class == Array
@value = options['value']
elsif options['value'].class == String
@value = [options['value']].flatten
else
puts "ERROR: variable type not supported: " +
"#{options['value'].class} for #{options['name']}"
end
end
end
#!/usr/bin/env ruby
require 'rubygems'
require 'yaml'
require 'fog'
require 'localrecord'
config = YAML.load_file('myzonefile.yml')
ID = config['access_key_id']
SECRET = config['access_secret_key']
ZONE = config['zone']
TYPE = 'A'
# create a connection to the service
@dns = Fog::DNS.new({
:provider => 'AWS',
:aws_access_key_id => 'my_key',
:aws_secret_access_key => 'my_secret',
})
def zone_by_name name
@dns.zones.all.each do |zone|
if zone.domain == ZONE
return zone
end
end
return nil
end
@zone = zone_by_name(ZONE)
# If new zone, then create it.
if @zone.nil?
puts "Creating zone: #{ZONE}"
@zone = @dns.zones.create(
:domain => ZONE,
:email => "hostmaster@#{ZONE}"
)
end
@localrecords = config['records'].collect do |conf|
record = Localrecord.new(conf)
end
@remoterecords = @zone.records
def remote_lookup(attributes = {})
@remoterecords.each do |record|
if record.name == attributes[:name]
return record
end
end
return nil
end
@localrecords.each do |local|
remote = remote_lookup({:name => local.name})
if remote.nil?
puts "Creating record: " +
"#{local.name} #{local.ttl} IN #{local.type} #{local.value}"
record = @zone.records.create(
:value => local.value,
:name => local.name,
:type => local.type,
:ttl => local.ttl
)
elsif local.value != remote.value
puts "Updating value of record with name #{local.name}: " +
"#{remote.value} -> #{local.value}"
remote.value = local.value
remote.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment