Class: Beaker::VcloudPooled

Inherits:
Hypervisor show all
Defined in:
lib/beaker/hypervisor/vcloud_pooled.rb

Constant Summary collapse

CHARMAP =
[('a'..'z'),('0'..'9')].map{|r| r.to_a}.flatten
SSH_EXCEPTIONS =
[
  SocketError,
  Timeout::Error,
  Errno::ETIMEDOUT,
  Errno::EHOSTDOWN,
  Errno::EHOSTUNREACH,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ENETUNREACH,
]

Instance Method Summary collapse

Methods inherited from Hypervisor

#configure, create

Constructor Details

#initialize(vcloud_hosts, options) ⇒ VcloudPooled

Returns a new instance of VcloudPooled.



19
20
21
22
23
24
25
26
27
# File 'lib/beaker/hypervisor/vcloud_pooled.rb', line 19

def initialize(vcloud_hosts, options)
  @options = options
  @logger = options[:logger]
  @vcloud_hosts = vcloud_hosts

  raise 'You must specify a datastore for vCloud instances!' unless @options['datastore']
  raise 'You must specify a resource pool for vCloud instances!' unless @options['resourcepool']
  raise 'You must specify a folder for vCloud instances!' unless @options['folder']
end

Instance Method Details

#cleanupObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/beaker/hypervisor/vcloud_pooled.rb', line 71

def cleanup
  vm_names = @vcloud_hosts.map {|h| h['vmhostname'] }.compact
  if @vcloud_hosts.length != vm_names.length
    @logger.warn "Some hosts did not have vmhostname set correctly! This likely means VM provisioning was not successful"
  end

  start = Time.now
  vm_names.each do |name|
    @logger.notify "Handing '#{name}' back to pooling API for VM destruction"

    uri = URI.parse(@options['pooling_api']+'/vm/'+name)

    http = Net::HTTP.new( uri.host, uri.port )
    request = Net::HTTP::Delete.new(uri.request_uri)

    begin
      response = http.request(request)
    rescue *SSH_EXCEPTIONS => e
      report_and_raise(@logger, e, 'vCloudPooled.cleanup (http.request)')
    end
  end 

  @logger.notify "Spent %.2f seconds cleaning up" % (Time.now - start)
end

#provisionObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/beaker/hypervisor/vcloud_pooled.rb', line 29

def provision
  start = Time.now
  try = 1
  @vcloud_hosts.each_with_index do |h, i|
    if h['template'] =~ /\//
      templatefolders = h['template'].split('/')
      h['template'] = templatefolders.pop
    end

    @logger.notify "Requesting '#{h['template']}' VM from vCloud host pool"

    begin
      uri = URI.parse(@options['pooling_api']+'/vm/'+h['template'])

      http = Net::HTTP.new( uri.host, uri.port )
      request = Net::HTTP::Post.new(uri.request_uri)

      request.set_form_data({'pool' => @options['resourcepool'], 'folder' => 'foo'})

      attempts = @options[:timeout].to_i / 5
      response = http.request(request)
      parsed_response = JSON.parse(response.body)
      if parsed_response[h['template']] && parsed_response[h['template']]['ok'] && parsed_response[h['template']]['hostname'] 
        h['vmhostname'] = parsed_response[h['template']]['hostname']
      else
        raise "VcloudPooled.provision - no vCloud host free for #{h.name} in pool"
      end
    rescue JSON::ParserError, RuntimeError, *SSH_EXCEPTIONS => e
      if try <= attempts
        sleep 5
        try += 1
        retry
      end
      report_and_raise(@logger, e, 'vCloudPooled.provision')
    end

    @logger.notify "Using available vCloud host '#{h['vmhostname']}' (#{h.name})"
  end

  @logger.notify 'Spent %.2f seconds grabbing VMs' % (Time.now - start)
end