Class: Beaker::VcloudPooled

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

Constant Summary collapse

SSH_EXCEPTIONS =
[
  SocketError,
  Timeout::Error,
  Errno::ETIMEDOUT,
  Errno::EHOSTDOWN,
  Errno::EHOSTUNREACH,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ENETUNREACH,
]

Constants inherited from Hypervisor

Hypervisor::CHARMAP

Constants included from HostPrebuiltSteps

HostPrebuiltSteps::APT_CFG, HostPrebuiltSteps::CUMULUS_PACKAGES, HostPrebuiltSteps::DEBIAN_PACKAGES, HostPrebuiltSteps::ETC_HOSTS_PATH, HostPrebuiltSteps::ETC_HOSTS_PATH_SOLARIS, HostPrebuiltSteps::IPS_PKG_REPO, HostPrebuiltSteps::NTPSERVER, HostPrebuiltSteps::ROOT_KEYS_SCRIPT, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD, HostPrebuiltSteps::SLEEPWAIT, HostPrebuiltSteps::SLES_PACKAGES, HostPrebuiltSteps::TRIES, HostPrebuiltSteps::UNIX_PACKAGES, HostPrebuiltSteps::WINDOWS_PACKAGES

Instance Method Summary collapse

Methods inherited from Hypervisor

#configure, create, #generate_host_name, #proxy_package_manager, #validate

Methods included from HostPrebuiltSteps

#add_el_extras, #additive_hash_merge, #apt_get_update, #check_and_install_packages_if_needed, #construct_env, #copy_file_to_remote, #copy_ssh_to_root, #disable_iptables, #disable_se_linux, #echo_on_host, #enable_root_login, #epel_info_for, #get_domain_name, #get_ip, #hack_etc_hosts, #package_proxy, #proxy_config, #set_env, #set_etc_hosts, #sync_root_keys, #timesync, #validate_host

Methods included from DSL::Patterns

#block_on

Constructor Details

#initialize(vcloud_hosts, options) ⇒ VcloudPooled

Returns a new instance of VcloudPooled.



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

def initialize(vcloud_hosts, options)
  @options = options
  @logger = options[:logger]
  @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

#check_url(url) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/beaker/hypervisor/vcloud_pooled.rb', line 28

def check_url url
  begin
    URI.parse(url)
  rescue
    return false
  end
  true
end

#cleanupObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/beaker/hypervisor/vcloud_pooled.rb', line 102

def cleanup
  vm_names = @hosts.map {|h| h['vmhostname'] }.compact
  if @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(get_template_url(@options['pooling_api'], 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

#get_template_url(pooling_api, template) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/beaker/hypervisor/vcloud_pooled.rb', line 37

def get_template_url pooling_api, template
  if not check_url(pooling_api)
    raise ArgumentError, "Invalid pooling_api URL: #{pooling_api}"
  end
  scheme = ''
  if not URI.parse(pooling_api).scheme
    scheme = 'http://'
  end
  #check that you have a valid uri
  template_url = scheme + pooling_api + '/vm/' + template
  if not check_url(template_url)
    raise ArgumentError, "Invalid full template URL: #{template_url}"
  end
  template_url
end

#provisionObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/beaker/hypervisor/vcloud_pooled.rb', line 53

def provision
  start = Time.now
  @hosts.each_with_index do |h, i|
    if not h['template']
      raise ArgumentError, "You must specify a template name for #{h}"
    end
    if h['template'] =~ /\//
      templatefolders = h['template'].split('/')
      h['template'] = templatefolders.pop
    end

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

    last_wait, wait = 0, 1
    waited = 0 #the amount of time we've spent waiting for this host to provision
    begin
      uri = URI.parse(get_template_url(@options['pooling_api'], 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'})

      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']
        hostname = parsed_response[h['template']]['hostname']
        domain = parsed_response['domain']
        h['vmhostname'] = domain ? "#{hostname}.#{domain}" : hostname
      else
        raise "VcloudPooled.provision - no vCloud host free for #{h.name} in pool"
      end
    rescue JSON::ParserError, RuntimeError, *SSH_EXCEPTIONS => e
      if waited <= @options[:timeout].to_i
        @logger.debug("Retrying provision for vCloud host #{h.name} after waiting #{wait} second(s) (failed with #{e.class})")
        sleep wait
        waited += wait
        last_wait, wait = wait, last_wait + wait
        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