Skip to content

Instantly share code, notes, and snippets.

@miketheman
Created May 24, 2015 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save miketheman/f5103e1eff741a8e3a5b to your computer and use it in GitHub Desktop.
Save miketheman/f5103e1eff741a8e3a5b to your computer and use it in GitHub Desktop.
Tool to migrate DNS records from Rackspace Cloud DNS to AWS Route53
require 'aws-sdk'
require 'fog'
require 'pry'
# The top-level domain name/hosted zone
TOP_DOMAIN = 'example.org'
# A simple Change class, repsenting a single change to end up in Route53
class Change
attr_accessor :name
attr_accessor :type
attr_accessor :value
attr_accessor :ttl
attr_accessor :priority
end
## rackspace methods
def domain_id(domain_list, domain)
domain_list.select { |d| d['name'] == domain }.first['id']
end
def retrieve_rs_records(domain)
rs_dns = Fog::DNS.new({
:provider => 'rackspace',
:rackspace_username => ENV['RACKSPACE_USERNAME'],
:rackspace_api_key => ENV['RACKSPACE_API_KEY'],
})
domains = rs_dns.list_domains.data[:body]['domains']
did = domain_id(domains, domain)
rs_dns.list_domain_details(did).data[:body]['recordsList']['records']
end
## aws methods
def persist_change_batch(change_batch)
# aws-sdk knows how to pick up credentials from environment variables
route53 = Aws::Route53::Client.new(region: 'us-east-1')
r = route53.list_hosted_zones
zone_id = r.hosted_zones.select { |z| z.name == "#{TOP_DOMAIN}." }.first.id
resp = route53.change_resource_record_sets(hosted_zone_id: zone_id, change_batch: { changes: change_batch })
puts resp.inspect
end
## local
def convert_rs_to_changes(records)
records.inject([]) do |accumulator, record|
c = Change.new
c.name = record['name']
c.type = record['type']
c.ttl = record['ttl']
c.value = record['data']
c.priority = record['priority']
accumulator << c
end
end
def convert_mx_to_single_change(mx_changes)
return unless mx_changes.count > 0
c = Change.new
c.name = mx_changes.first.name
c.ttl = mx_changes.first.ttl
c.type = 'MX'
c.value = mx_changes.map do |mx|
"#{mx.priority} #{mx.value}"
end.compact
c
end
def convert_txt_to_single_change(txt_changes)
return unless txt_changes.count > 0
tc = txt_changes[txt_changes.keys.first]
c = Change.new
c.name = tc.first.name
c.ttl = tc.first.ttl
c.type = 'TXT'
# TXT records need to be wrapped in quotes
c.value = tc.map { |t| "\"#{t.value}\"" }
c
end
def create_change_batch(changes)
changes.compact.map do |change|
record_value = determine_record_value(change)
{ action: 'UPSERT', resource_record_set: {
name: change.name,
type: change.type,
ttl: change.ttl,
resource_records: record_value
}
}
end
end
def determine_record_value(change)
if change.value.class == Array
change.value.map { |v| { value: v } }
else
[{ value: change.value }]
end
end
def mx_change?(change)
change.type == 'MX'
end
def ns_change?(change)
change.type == 'NS'
end
def txt_change?(change)
change.type == 'TXT'
end
## main
records = retrieve_rs_records(TOP_DOMAIN)
changes = convert_rs_to_changes(records)
# Route53 doesn't like it when we try to add more NS records for the same level
changes.reject! { |c| ns_change? c }
# MX Records have multiple valuse and need to be submitted as a single record
mx_changes = changes.select { |c| mx_change? c }
mx_change = convert_mx_to_single_change(mx_changes)
# Replace all MX records with the multi-value one
changes.reject! { |c| mx_change? c }
changes.push mx_change
# TXT Records that have the same record need to be consolidated
txt_changes = changes.reject(&:nil?).select { |c| txt_change? c }.group_by(&:name)
txt_changes.select! { |k, v| [k, v] if v.count > 1 }
txt_record = convert_txt_to_single_change(txt_changes)
changes.reject! { |c| txt_change? c }
changes.push txt_record
changes.reject(&:nil?)
puts "We have #{changes.count} records to send to Route53."
change_batch = create_change_batch(changes)
persist_change_batch(change_batch)
puts 'Done!'
@wiicode
Copy link

wiicode commented Sep 14, 2017

SRV records might have issues with this script. Currently debugging but wanted to share with you.

We have 76 records to send to Route53.
/Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-3.4.0/lib/seahorse/client/plugins/raise_response_errors.rb:15:in call': Invalid Resource Record: FATAL problem: SRVRRDATANotFourFields (SRV record doesn't have 4 fields) encountered with '1 443 sipdir.online.lync.com' (Aws::Route53::Errors::InvalidChangeBatch) from /Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-3.4.0/lib/aws-sdk-core/plugins/jsonvalue_converter.rb:20:in call'
from /Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-3.4.0/lib/aws-sdk-core/plugins/idempotency_token.rb:17:in call' from /Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-3.4.0/lib/aws-sdk-core/plugins/param_converter.rb:24:in call'
from /Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-3.4.0/lib/aws-sdk-core/plugins/response_paging.rb:10:in call' from /Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-3.4.0/lib/seahorse/client/plugins/response_target.rb:23:in call'
from /Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-core-3.4.0/lib/seahorse/client/request.rb:70:in send_request' from /Users/X/.rvm/gems/ruby-2.4.0/gems/aws-sdk-route53-1.1.0/lib/aws-sdk-route53/client.rb:960:in change_resource_record_sets'
from clouddns_to_route53.rb.rb:45:in persist_change_batch' from clouddns_to_route53.rb.rb:156:in

'

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