Class: SharedDns

Inherits:
Object
  • Object
show all
Defined in:
lib/virtualmonkey/shared_dns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain = "virtualmonkey_shared_resources") ⇒ SharedDns

Returns a new instance of SharedDns.



5
6
7
8
9
# File 'lib/virtualmonkey/shared_dns.rb', line 5

def initialize(domain = "virtualmonkey_shared_resources")
  @sdb = Fog::AWS::SimpleDB.new(:aws_access_key_id => Fog.credentials[:aws_access_key_id_test], :aws_secret_access_key => Fog.credentials[:aws_secret_access_key_test])
  @domain = domain
  @reservation = nil
end

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



3
4
5
# File 'lib/virtualmonkey/shared_dns.rb', line 3

def owner
  @owner
end

#reservationObject

Returns the value of attribute reservation.



2
3
4
# File 'lib/virtualmonkey/shared_dns.rb', line 2

def reservation
  @reservation
end

Instance Method Details

#release_allObject



53
54
55
56
57
58
# File 'lib/virtualmonkey/shared_dns.rb', line 53

def release_all
  result = @sdb.select("SELECT * from #{@domain}")
  result.body['Items'].keys.each do |item_name|
    @sdb.put_attributes(@domain, item_name, {"owner" => "available"}, :replace => ["owner"])
  end
end

#release_dns(res = @reservation) ⇒ Object



60
61
62
63
64
# File 'lib/virtualmonkey/shared_dns.rb', line 60

def release_dns(res = @reservation)
  raise "FATAL: could not release dns because there was no @reservation" unless res
  @sdb.put_attributes(@domain, res, {"owner" => "available"}, :replace => ["owner"]) 
  @reservation = nil
end

#reserve_dns(owner, timeout = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/virtualmonkey/shared_dns.rb', line 23

def reserve_dns(owner, timeout = 0)
  puts "Checking DNS reservation for #{owner}"
  result = @sdb.select("SELECT * from #{@domain} where owner = '#{owner}'")
  puts "Reusing DNS reservation" unless result.body["Items"].empty?
  if result.body["Items"].empty?
     result = @sdb.select("SELECT * from #{@domain} where owner = 'available'")
     return false if result.body["Items"].empty?
     puts "Aquired new DNS reservation"
     item_name = result.body["Items"].keys.first
     response = @sdb.put_attributes(@domain, item_name, {'owner' => owner}, :expect => {'owner' => "available"}, :replace => ['owner'])
  end
  @owner = owner
  @reservation = result.body["Items"].keys.first
rescue Excon::Errors::ServiceUnavailable
  puts "Resuce: ServiceUnavailable"
  retry_reservation(owner, timeout)
rescue Excon::Errors::Conflict
  puts "Resuce: Conflict"
  retry_reservation(owner, timeout)
end

#retry_reservation(owner, timeout) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/virtualmonkey/shared_dns.rb', line 44

def retry_reservation(owner, timeout)
  STDOUT.flush
  if timeout > 20 
    return false
  end
  sleep(5)
  reserve_dns(owner, timeout + 1)
end

#set_dns_inputs(deployment) ⇒ Object

  • deployment<~Deployment> the deployment to set inputs on



13
14
15
16
17
18
19
20
21
# File 'lib/virtualmonkey/shared_dns.rb', line 13

def set_dns_inputs(deployment)
  sdb_result = @sdb.get_attributes(@domain, @reservation)

  set_these = sdb_result.body['Attributes'].reject {|k,v| k == 'owner'}
  set_these.each do |key,val|
    deployment.set_input(key, val.to_s)
    deployment.servers.each { |s| s.set_input(key, "text:") }
  end
end