Skip to content

Instantly share code, notes, and snippets.

@rolandoam
Created May 17, 2013 18:25
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 rolandoam/5600978 to your computer and use it in GitHub Desktop.
Save rolandoam/5600978 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'pp'
# you need these two gems, so `gem install` them before
require 'linode'
require 'aws-sdk'
API_KEY="YOUR_LINODE_KEY"
# create a simple domain and add a record to it
# make sure you have the env variables AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY set before running this
config = AWS.config
client = config.route_53_client
linode = Linode.new(:api_key => API_KEY)
# iterate over all domains
domains = linode.domain.list
domains.each { |d|
# only migrate if they're still attached to linode (this is error prone, but works for me)
if `dig ns #{d.domain}`.match("linode")
# we need to migrate this
resp = linode.domain.resource.list({:domainid => d.domainid})
batch = []
mx_resources = []
txt_resources = []
resp.each { |res|
# collapse mx and txt resources
if res.type.upcase == "MX"
mx_resources.push "#{mx_resources.size + 1} #{res.target}"
elsif res.type.upcase == "TXT"
txt_resources.push res.target
else
batch.push({:target => [{:value => res.target}], :name => res.name, :type => res.type.upcase})
end
}
# I didn't want to migrate somedomain.com
if d.domain != "somedomain.com"
batch_req = {:changes => []}
batch.each do |v|
res_name = v[:name].empty? ? d.domain : "#{v[:name]}.#{d.domain}"
req = {:name => res_name, :ttl => 3600, :type => v[:type], :resource_records => v[:target]}
batch_req[:changes].push({:action => "CREATE", :resource_record_set => req})
end
if mx_resources.size > 0
req = {:name => d.domain, :ttl => 3600, :type => "MX", :resource_records => mx_resources.map {|r| {:value => r}}}
batch_req[:changes].push({:action => "CREATE", :resource_record_set => req})
end
if txt_resources.size > 0
req = {:name => d.domain, :ttl => 3600, :type => "TXT", :resource_records => txt_resources.map {|r| {:value => "\"#{r}\""}}}
batch_req[:changes].push({:action => "CREATE", :resource_record_set => req})
end
# create zone in route53
aws_resp = client.create_hosted_zone({:name => d.domain, :caller_reference => "domain_migration_linode.#{Time.now.to_f}"})
if aws_resp && aws_resp[:hosted_zone][:id]
aws_res_resp = client.change_resource_record_sets({:hosted_zone_id => aws_resp[:hosted_zone][:id], :change_batch => batch_req})
dns = aws_resp[:delegation_set][:name_servers].join(",")
if aws_res_resp
puts "#{d.domain},#{dns}"
end
end
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment