2010-12-10 14:07:00 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'rubygems'
|
2010-12-14 00:04:23 -05:00
|
|
|
require File.join(File.dirname(File.expand_path(__FILE__)), '..', 'lib', 'fog')
|
|
|
|
require 'fog/core/bin'
|
2010-12-10 14:07:00 -05:00
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
Fog.bin = true
|
2010-12-10 14:07:00 -05:00
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
#mark true for each cloud you wish to enable/run the sample for
|
|
|
|
RUN_AWS_SAMPLE = true
|
|
|
|
RUN_LINODE_SAMPLE = true
|
|
|
|
RUN_SLICEHOST_SAMPLE = false
|
|
|
|
RUN_ZERIGO_SAMPLE = false
|
2010-12-10 14:07:00 -05:00
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
#sample data
|
|
|
|
TEST_DOMAIN='sample53-domain.com'
|
|
|
|
|
|
|
|
|
|
|
|
# example of how to use AWS Route 53 DNS calls
|
|
|
|
def show_aws_dns_usage
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
aws = AWS[:dns].list_hosted_zones
|
|
|
|
|
|
|
|
debugger
|
|
|
|
|
|
|
|
response = AWS[:dns].create_hosted_zone( TEST_DOMAIN)
|
|
|
|
if response.status == 201
|
|
|
|
zone_id = response.body['HostedZone']['Id']
|
|
|
|
aws.delete_hosted_zone( zone_id)
|
|
|
|
(response.status == 200) ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
#get a list of the zones AWS is hosting for this account
|
|
|
|
options= { :max_items => 50 }
|
|
|
|
response= AWS[:dns].list_hosted_zones( options)
|
|
|
|
if response.status == 200
|
|
|
|
domain_count = response.body['HostedZones'].count
|
|
|
|
puts "you have #{domain_count} zones hosted at AWS Route 53"
|
|
|
|
end
|
|
|
|
|
|
|
|
# debugger
|
|
|
|
|
|
|
|
#delete all the zones
|
|
|
|
zones= response.body['HostedZones']
|
|
|
|
zones.each { |zone|
|
|
|
|
|
|
|
|
zone_id = zone['Id']
|
|
|
|
puts "starting process of deleting zone #{zone_id}"
|
|
|
|
|
|
|
|
#get the list of record sets
|
|
|
|
options = { :max_items => 50 }
|
|
|
|
response = AWS[:dns].list_resource_record_sets( zone_id, options)
|
|
|
|
if response.status == 200
|
|
|
|
records= response.body['ResourceRecordSets']
|
|
|
|
count= records.count
|
|
|
|
|
|
|
|
if count > 0
|
|
|
|
puts "#{count} resource records to delete"
|
|
|
|
#need to build a change batch
|
|
|
|
change_batch = []
|
|
|
|
records.each { |record|
|
|
|
|
#can't delete NS or SOA that AWS added so skip
|
|
|
|
if record[:type] == 'NS' or record[:type] == 'SOA'
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
#add to batch of records to delete
|
|
|
|
resource_record_set = { :action => 'DELETE', :name => record['Name'], :type => record['Type'],
|
|
|
|
:ttl => record['TTL'], :resource_records => record['ResourceRecords'] }
|
|
|
|
change_batch << resource_record_set
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if change_batch.count > 0
|
|
|
|
response = AWS[:dns].change_resource_record_sets( zone_id, change_batch)
|
|
|
|
debugger
|
|
|
|
if response.status == 200
|
|
|
|
#zone should now be in a state where it can be deleted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#delete the records
|
|
|
|
|
|
|
|
response = AWS[:dns].delete_hosted_zone( zone_id)
|
|
|
|
if response.status == 200
|
|
|
|
change_id = response.body['ChangeInfo']['Id']
|
|
|
|
puts "request delete zone #{zone_id} is in progress and has change ID #{change_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# debugger
|
|
|
|
|
|
|
|
#add a zone - note required period at end of domain
|
|
|
|
options= { :comment => 'for client ABC' }
|
|
|
|
response= AWS[:dns].create_hosted_zone( 'sample-domain.com.' )
|
|
|
|
if response.status == 201
|
|
|
|
zone_id = response.body['HostedZone']['Id']
|
|
|
|
change_id = response.body['ChangeInfo']['Id']
|
|
|
|
status = response.body['ChangeInfo']['Status']
|
|
|
|
end
|
|
|
|
|
|
|
|
# debugger
|
|
|
|
|
|
|
|
#wait until zone ready
|
|
|
|
while status == 'PENDING'
|
|
|
|
sleep 2
|
|
|
|
response = AWS[:dns].get_change( change_id)
|
|
|
|
if response.status == 200
|
|
|
|
change_id = response.body['Id']
|
|
|
|
status = response.body['Status']
|
|
|
|
end
|
|
|
|
puts "your changes are #{status}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# debugger
|
|
|
|
|
|
|
|
response = AWS[:dns].get_hosted_zone( zone_id)
|
|
|
|
if response.status == 200
|
|
|
|
name_servers = response.body['NameServers']
|
|
|
|
end
|
|
|
|
|
|
|
|
debugger
|
|
|
|
|
|
|
|
#add resource records to zone
|
|
|
|
change_batch = []
|
|
|
|
resource_record_set = { :action => 'CREATE', :name => 'www.sample-domain.com.', :type => 'A',
|
|
|
|
:ttl => 3600, :resource_records => ['1.2.3.4'] }
|
|
|
|
change_batch << resource_record_set
|
|
|
|
=begin
|
|
|
|
resource_record_set = { :action => 'CREATE', :name => 'mail.sample-domain.com.', :type => 'CNAME',
|
|
|
|
:ttl => 3600, :resource_records => ['www.sample-domain.com'] }
|
|
|
|
change_batch << resource_record_set
|
|
|
|
resource_record_set = { :action => 'CREATE', :name => 'sample-domain.com.', :type => 'MX',
|
|
|
|
:ttl => 3600, :resource_records => ['10 mail.sample-domain.com'] }
|
|
|
|
change_batch << resource_record
|
|
|
|
=end
|
|
|
|
options = { :comment => 'migrate records from BIND'}
|
|
|
|
response = AWS[:dns].change_resource_record_sets( zone_id, change_batch, options)
|
|
|
|
if response.status == 200
|
|
|
|
change_id = response.body['Id']
|
|
|
|
status = response.body['Status']
|
|
|
|
end
|
|
|
|
|
|
|
|
debugger
|
|
|
|
|
|
|
|
#wait until zone ready
|
|
|
|
while status == 'PENDING'
|
|
|
|
sleep 2
|
|
|
|
response = AWS[:dns].get_change( change_id)
|
|
|
|
if response.status == 200
|
|
|
|
change_id = response.body['Id']
|
|
|
|
status = response.body['Id']
|
|
|
|
end
|
|
|
|
puts "your changes are #{status}"
|
|
|
|
end
|
|
|
|
|
|
|
|
#get a list of the zones AWS is hosting for this account
|
|
|
|
options= { :max_items => 5 }
|
|
|
|
response= AWS[:dns].list_hosted_zones( )
|
|
|
|
if response.status == 200
|
|
|
|
end
|
|
|
|
|
|
|
|
response = AWS[:dns].get_hosted_zone()
|
|
|
|
if response.status == 200
|
|
|
|
end
|
|
|
|
|
|
|
|
response = AWS[:dns].list_resource_record_sets
|
|
|
|
if response.status == 200
|
|
|
|
end
|
|
|
|
|
|
|
|
#delete the resource records
|
|
|
|
response = AWS[:dns].change_resource_record_sets( zone_id, change_batch, options)
|
|
|
|
if response.status == 200
|
|
|
|
change_id = response.body['ChangeInfo']['Id']
|
|
|
|
end
|
|
|
|
|
|
|
|
#wait?
|
|
|
|
|
|
|
|
#delete the zone
|
|
|
|
response = AWS[:dns].delete_hosted_zone( zone_id)
|
|
|
|
if response.status == 200
|
|
|
|
change_id = response.body['ChangeInfo']['Id']
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
rescue
|
|
|
|
#opps, ran into a problem
|
|
|
|
puts $!.message
|
|
|
|
return false
|
2010-12-10 14:07:00 -05:00
|
|
|
end
|
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# example of how to use Linode DNS calls
|
|
|
|
def show_linode_dns_usage
|
|
|
|
|
2010-12-10 14:07:00 -05:00
|
|
|
begin
|
2010-12-11 13:39:25 -05:00
|
|
|
|
2010-12-11 11:32:41 -05:00
|
|
|
#create a zone for a domain
|
|
|
|
type = 'master'
|
|
|
|
options = { :SOA_email => 'netops@sample-domain.com', :description => "Sample-Domain Inc", :status => 0}
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Linode[:compute].domain_create( TEST_DOMAIN, type, options)
|
2010-12-11 11:32:41 -05:00
|
|
|
if response.status == 200
|
2010-12-11 13:39:25 -05:00
|
|
|
master_zone_id = response.body['DATA']['DomainID']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "created zone for #{TEST_DOMAIN} - ID: #{master_zone_id}"
|
2010-12-11 11:32:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
#create a slave zone
|
|
|
|
domain = 'sample-slave-domain.com'
|
|
|
|
type = 'slave'
|
|
|
|
options = { :master_ips => '1.2.3.4; 1.2.3.5'}
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Linode[:compute].domain_create( domain, type, options)
|
2010-12-11 11:32:41 -05:00
|
|
|
if response.status == 200
|
2010-12-11 13:39:25 -05:00
|
|
|
slave_zone_id = response.body['DATA']['DomainID']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "created slave zone for #{domain} - ID: #{slave_zone_id}"
|
2010-12-11 11:32:41 -05:00
|
|
|
end
|
2010-12-11 13:39:25 -05:00
|
|
|
|
2010-12-11 11:32:41 -05:00
|
|
|
#get a list of zones Linode hosted for account
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Linode[:compute].domain_list()
|
2010-12-11 11:32:41 -05:00
|
|
|
if response.status == 200
|
2010-12-14 00:04:23 -05:00
|
|
|
zones = response.body['DATA']
|
|
|
|
num_zones = zones.count
|
|
|
|
puts "Linode is hosting #{num_zones} DNS zones for this account:"
|
|
|
|
zones.each { |zone|
|
|
|
|
puts " #{zone['DOMAIN']}\n"
|
|
|
|
}
|
2010-12-11 11:32:41 -05:00
|
|
|
end
|
2010-12-11 13:39:25 -05:00
|
|
|
|
|
|
|
#add an A and a MX record
|
2010-12-14 00:04:23 -05:00
|
|
|
domain= 'www.' + TEST_DOMAIN
|
|
|
|
options = { :name => domain, :target => '4.5.6.7', :ttl_sec => 7200 }
|
|
|
|
response = Linode[:compute].domain_resource_create( master_zone_id, 'A', options)
|
2010-12-11 13:39:25 -05:00
|
|
|
if response.status == 200
|
|
|
|
resource_id = response.body['DATA']['ResourceID']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "added an A record for #{domain} - ID: #{resource_id}"
|
2010-12-11 13:39:25 -05:00
|
|
|
end
|
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
domain= 'mail.' + TEST_DOMAIN
|
|
|
|
options = { :target => domain, :priority => 1 }
|
|
|
|
response = Linode[:compute].domain_resource_create( master_zone_id, 'MX', options)
|
2010-12-11 13:39:25 -05:00
|
|
|
if response.status == 200
|
|
|
|
resource_id = response.body['DATA']['ResourceID']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "added a MX record for #{TEST_DOMAIN} - ID: #{resource_id}"
|
2010-12-11 13:39:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
#change MX to have a lower priority
|
|
|
|
options = { :priority => 5 }
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Linode[:compute].domain_resource_update( master_zone_id, resource_id, options)
|
2010-12-11 13:39:25 -05:00
|
|
|
if response.status == 200
|
|
|
|
resource_id = response.body['DATA']['ResourceID']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "updated MX record for #{TEST_DOMAIN} with new priority - ID: #{resource_id}"
|
2010-12-11 13:39:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
#get the list of resource records for the domain
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Linode[:compute].domain_resource_list( master_zone_id)
|
2010-12-11 13:39:25 -05:00
|
|
|
if response.status == 200
|
|
|
|
num_records = response.body['DATA'].count
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "#{TEST_DOMAIN} zone has #{num_records} resource records"
|
2010-12-11 13:39:25 -05:00
|
|
|
end
|
|
|
|
|
2010-12-11 11:32:41 -05:00
|
|
|
#finally cleanup by deleting the zone we created
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Linode[:compute].domain_delete( master_zone_id)
|
2010-12-11 11:32:41 -05:00
|
|
|
if response.status == 200
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "deleted #{TEST_DOMAIN} zone"
|
2010-12-11 11:32:41 -05:00
|
|
|
end
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Linode[:compute].domain_delete( slave_zone_id)
|
2010-12-11 11:32:41 -05:00
|
|
|
if response.status == 200
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "deleted slave zone"
|
2010-12-11 11:32:41 -05:00
|
|
|
end
|
|
|
|
|
2010-12-10 14:07:00 -05:00
|
|
|
rescue
|
|
|
|
#opps, ran into a problem
|
|
|
|
puts $!.message
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
|
2010-12-10 14:07:00 -05:00
|
|
|
# example of how to use Slicehost DNS calls
|
2010-12-14 00:04:23 -05:00
|
|
|
def show_slicehost_dns_usage
|
2010-12-10 14:07:00 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
#create a zone for a domain
|
|
|
|
zone_id = 0
|
|
|
|
options = { :ttl => 1800, :active => 'N' }
|
2010-12-14 00:04:23 -05:00
|
|
|
response= Slicehost[:compute].create_zone( TEST_DOMAIN, options)
|
|
|
|
if response.status == 201
|
2010-12-10 14:07:00 -05:00
|
|
|
zone_id= response.body['id']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "created zone for #{TEST_DOMAIN} - ID: #{zone_id}"
|
2010-12-10 14:07:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
#add an A record for website
|
|
|
|
record_id = 0
|
|
|
|
options = { :ttl => 3600, :active => 'N' }
|
2010-12-14 00:04:23 -05:00
|
|
|
host = "www.#{TEST_DOMAIN}"
|
|
|
|
response= Slicehost[:compute].create_record( 'A', zone_id, host, "1.2.3.4", options)
|
|
|
|
if response.status == 201
|
2010-12-10 14:07:00 -05:00
|
|
|
record_id= response.body['id']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "created 'A' record for #{host} - ID: #{record_id}"
|
2010-12-10 14:07:00 -05:00
|
|
|
end
|
|
|
|
|
2010-12-11 11:32:41 -05:00
|
|
|
#get a list of zones Slicehost hosted for account
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Slicehost[:compute].get_zones()
|
2010-12-11 11:32:41 -05:00
|
|
|
if response.status == 200
|
2010-12-14 00:04:23 -05:00
|
|
|
zones = response.body['zones']
|
|
|
|
num_zones= zones.count
|
2010-12-11 11:32:41 -05:00
|
|
|
puts "Slicehost is hosting #{num_zones} DNS zones for this account"
|
2010-12-14 00:04:23 -05:00
|
|
|
zones.each { |zone|
|
|
|
|
puts " #{zone['origin']}\n"
|
|
|
|
}
|
2010-12-11 11:32:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
#now get details on www record for the zone
|
2010-12-10 14:07:00 -05:00
|
|
|
if record_id > 0
|
2010-12-14 00:04:23 -05:00
|
|
|
response = Slicehost[:compute].get_record( record_id)
|
2010-12-10 14:07:00 -05:00
|
|
|
if response.status == 200
|
|
|
|
record = response.body['records'][0]
|
|
|
|
name = record['name']
|
|
|
|
type = record['record-type']
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "got details on record #{record_id} -is an #{type} record for #{name} domain"
|
2010-12-10 14:07:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#finally cleanup by deleting the zone we created
|
2010-12-14 00:04:23 -05:00
|
|
|
if zone_id > 0
|
|
|
|
response = Slicehost[:compute].delete_zone( zone_id)
|
2010-12-10 14:07:00 -05:00
|
|
|
if response.status == 200
|
2010-12-14 00:04:23 -05:00
|
|
|
puts "#{TEST_DOMAIN} removed from Slicehost DNS"
|
2010-12-10 14:07:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue
|
|
|
|
#opps, ran into a problem
|
|
|
|
puts $!.message
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
######################################
|
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
# make sure Fog credentials file has been setup
|
|
|
|
# (will throw exception if not)
|
|
|
|
Fog::credentials
|
|
|
|
|
|
|
|
# AWS Route 53
|
|
|
|
if RUN_AWS_SAMPLE and Fog::credentials[:aws_access_key_id] and Fog::credentials[:aws_secret_access_key]
|
|
|
|
show_aws_dns_usage
|
|
|
|
end
|
|
|
|
|
|
|
|
# Linode
|
|
|
|
if RUN_LINODE_SAMPLE and Fog::credentials[:linode_api_key]
|
|
|
|
show_linode_dns_usage
|
|
|
|
end
|
|
|
|
|
|
|
|
# Slicehost
|
|
|
|
if RUN_SLICEHOST_SAMPLE and Fog::credentials[:slicehost_password]
|
|
|
|
show_slicehost_dns_usage
|
|
|
|
end
|
2010-12-10 14:07:00 -05:00
|
|
|
|
2010-12-14 00:04:23 -05:00
|
|
|
# if RUN_ZERIGO_SAMPLE and Fog::credentials[:zerigo_user] and Fog::credentials[:zerigo_password]
|
|
|
|
# show_zerigo_dns_usage
|
|
|
|
# end
|