Skip to content

Instantly share code, notes, and snippets.

@mizzy
Created November 5, 2012 08:32
Show Gist options
  • Select an option

  • Save mizzy/4016041 to your computer and use it in GitHub Desktop.

Select an option

Save mizzy/4016041 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'resolv'
require 'route53'
SQALE_DOMAIN = 'i4pc-mizzy.sqale.jp.'
ORIGINAL_DOMAIN = 'i4pc.net.'
# This code is from
# https://github.com/bgentry/fog/commit/26d366d3047f437154946b2bb15935c2ee34a209
def hosted_zone_for_alias_target(dns_name)
k = elb_hosted_zone_mapping.keys.find do |k|
dns_name =~ /\A.+\.#{k}\.elb\.amazonaws\.com\.?\z/
end
elb_hosted_zone_mapping[k]
end
def elb_hosted_zone_mapping
@elb_hosted_zone_mapping ||= {
"ap-northeast-1" => "Z2YN17T5R711GT",
"ap-southeast-1" => "Z1WI8VXHPB1R38",
"eu-west-1" => "Z3NF1Z3NOM5OY2",
"sa-east-1" => "Z2ES78Y61JGQKS",
"us-east-1" => "Z3DZXE0Q79N41H",
"us-west-1" => "Z1M58G0W56PQJA",
"us-west-2" => "Z33MTJ483KN6FU",
}
end
elb = Resolv::DNS.new.getresources(
SQALE_DOMAIN,
Resolv::DNS::Resource::IN::CNAME
)[0].name.to_s
hosted_zone_id = hosted_zone_for_alias_target(elb)
conn = Route53::Connection.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET_KEY'])
conn.get_zones.each do |zone|
next if zone.name != ORIGINAL_DOMAIN
found = false
zone.get_records.each do |record|
if record.name == ORIGINAL_DOMAIN and record.type == 'A'
found = true
record.update(nil, nil, nil, [ elb ], hosted_zone_id)
end
end
unless found
record = Route53::DNSRecord.new(
ORIGINAL_DOMAIN,
'A',
nil,
[ elb ],
zone,
hosted_zone_id
)
record.create
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment