Skip to content

Instantly share code, notes, and snippets.

@hazanjon
Forked from davidchua/zerigo_to_aws_route53.rb
Created January 20, 2014 23:20
Show Gist options
  • Save hazanjon/8531391 to your computer and use it in GitHub Desktop.
Save hazanjon/8531391 to your computer and use it in GitHub Desktop.
#!/usr/local/rvm/rubies/ruby-1.9.2-p180/bin/ruby
require 'rubygems'
require 'zerigo_dns'
require 'route53'
Zerigo::DNS::Base.user = 'username@email.com'
Zerigo::DNS::Base.api_key = 'yourkeyhere'
# iterate through all domain names
domains = Zerigo::DNS::Zone.find(:all)
domains.each do |z|
puts "#{z.domain} (id: #{z.id})"
hosts = Zerigo::DNS::Host.find(:all, :params=>{ :zone_id => z.id, :per_page=>100, :page=>1 })
host_map = {}
hosts.each do |host|
host.hostname = '' if host.hostname.nil?
host_map[host.hostname] = {} unless host_map[host.hostname].present?
host_map[host.hostname][host.host_type] = [] unless host_map[host.hostname][host.host_type].present?
host_map[host.hostname][host.host_type] << { :fqdn => host.fqdn, :priority => host.priority, :hostname => host.hostname, :data => host.data }
end
puts "Connecting to Route 53"
aws = Route53::Connection.new('access_key_id', 'secret_key')
puts "Creating zone on Route 53"
new_zone = Route53::Zone.new("#{z.domain}.", nil, aws)
response = new_zone.create_zone
exit 1 if response.error?
while response.pending?
sleep 1
end
puts "Zone created on Route 53"
host_map.each do |hostname, records|
puts " Hostname: #{hostname.blank? ? 'domain apex' : hostname}"
records.each do |record_type, values|
puts " Record type: #{record_type}"
case record_type
when "A", "CNAME"
values.each do |record|
new_record = Route53::DNSRecord.new("#{ record[:fqdn] }.", record_type, "600", [ "#{ record[:data] }#{ '.' if record_type != 'A' }" ], new_zone)
resp = new_record.create
end
when "MX"
fqdn = values.first[:fqdn]
new_mx_record = Route53::DNSRecord.new("#{fqdn}.", "MX", "600", values.collect{ |record| "#{ record[:priority] } #{ record[:data] }." }, new_zone)
resp = new_mx_record.create
when "TXT", "SPF"
fqdn = values.first[:fqdn]
new_txt_record = Route53::DNSRecord.new("#{fqdn}.", record_type, "600", values.collect{ |record| "\"#{ record[:data] }\"" }, new_zone)
resp = new_txt_record.create
end
end
end
end
@hazanjon
Copy link
Author

The script uses the zerigo_dns gem, however the current version of the gem's Gemspec is incorrect and installs active record 4 rather than 3.2.0 which is required. Therefore you will want to checkout Joaomsa's fix-gemspec-deps branch and compile the gem from source, this is modified version of zerigo_dns that has the correct Gemspec.

git clone https://github.com/joaomsa/zerigo_dns/
cd zerigo_dns
git checkout -b fix-gemspec-deps origin/fix-gemspec-deps
gem build zerigo_dns.gemspec
gem install ./zerigo_dns-1.5.6.gem

@dpnsan
Copy link

dpnsan commented Apr 3, 2017

I had to use the following Gemfile to get this working:

source "https://rubygems.org"

gem 'zerigo_dns', git: 'https://github.com/Shuttlerock/zerigo_dns.git'
gem 'route53'
gem 'activeresource', '~> 3.2.0'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment