Class: Beaker::Vmpooler

Inherits:
Hypervisor show all
Defined in:
lib/beaker/hypervisor/vmpooler.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::ARCHLINUX_PACKAGES, HostPrebuiltSteps::CUMULUS_PACKAGES, HostPrebuiltSteps::DEBIAN_PACKAGES, HostPrebuiltSteps::ETC_HOSTS_PATH, HostPrebuiltSteps::ETC_HOSTS_PATH_SOLARIS, HostPrebuiltSteps::FREEBSD_PACKAGES, HostPrebuiltSteps::IPS_PKG_REPO, HostPrebuiltSteps::NTPSERVER, HostPrebuiltSteps::OPENBSD_PACKAGES, HostPrebuiltSteps::PSWINDOWS_PACKAGES, HostPrebuiltSteps::ROOT_KEYS_SCRIPT, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD_AIX, HostPrebuiltSteps::SLEEPWAIT, HostPrebuiltSteps::SLES10_PACKAGES, HostPrebuiltSteps::SLES_PACKAGES, HostPrebuiltSteps::SOLARIS10_PACKAGES, HostPrebuiltSteps::SOLARIS11_PACKAGES, HostPrebuiltSteps::TRIES, HostPrebuiltSteps::UNIX_PACKAGES, HostPrebuiltSteps::WINDOWS_PACKAGES

Instance Attribute Summary collapse

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, #disable_updates, #enable_root_login, #get_domain_name, #get_ip, #hack_etc_hosts, #install_one_of_packages, #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(vmpooler_hosts, options) ⇒ Vmpooler

Returns a new instance of Vmpooler.



20
21
22
23
24
25
# File 'lib/beaker/hypervisor/vmpooler.rb', line 20

def initialize(vmpooler_hosts, options)
  @options = options
  @logger = options[:logger]
  @hosts = vmpooler_hosts
  @credentials = load_credentials(@options[:dot_fog])
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



18
19
20
# File 'lib/beaker/hypervisor/vmpooler.rb', line 18

def credentials
  @credentials
end

#hostsObject (readonly)

Returns the value of attribute hosts.



18
19
20
# File 'lib/beaker/hypervisor/vmpooler.rb', line 18

def hosts
  @hosts
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/beaker/hypervisor/vmpooler.rb', line 18

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/beaker/hypervisor/vmpooler.rb', line 18

def options
  @options
end

Instance Method Details

#add_disk(hostname, disk_size) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/beaker/hypervisor/vmpooler.rb', line 276

def add_disk(hostname, disk_size)
  @logger.notify "Requesting an additional disk of size #{disk_size}GB for #{hostname}"

  if !disk_size.to_s.match /[0123456789]/ || size <= '0'
    raise NameError.new "Disk size must be an integer greater than zero!"
  end

  begin
    uri = URI.parse(@options[:pooling_api] + '/api/v1/vm/' + hostname + '/disk/' + disk_size.to_s)

    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri)
    request['X-AUTH-TOKEN'] = @credentials[:vmpooler_token]

    response = http.request(request)

    parsed = parse_response(response)

    raise "Response from #{hostname} indicates disk was not added" if !parsed['ok']

  rescue NameError, RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError,
         Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e
    report_and_raise(@logger, e, 'Vmpooler.add_disk')
  end
end

#add_tags(host) ⇒ Hash

Override host tags with presets

Parameters:

Returns:

  • (Hash)

    Tag hash



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/beaker/hypervisor/vmpooler.rb', line 82

def add_tags(host)
  host[:host_tags].merge(
    'beaker_version'    => Beaker::Version::STRING,
    'jenkins_build_url' => @options[:jenkins_build_url],
    'department'        => @options[:department],
    'project'           => @options[:project],
    'created_by'        => @options[:created_by],
    'name'              => host.name,
    'roles'             => host.host_hash[:roles].join(', ')
  )
end

#check_url(url) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/beaker/hypervisor/vmpooler.rb', line 54

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

#cleanupObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/beaker/hypervisor/vmpooler.rb', line 247

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 vmpooler 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)

    if @credentials[:vmpooler_token]
      request['X-AUTH-TOKEN'] = @credentials[:vmpooler_token]
    end

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

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

#disk_added?(host, disk_size, index) ⇒ Boolean

Returns:

  • (Boolean)


306
307
308
309
310
311
312
# File 'lib/beaker/hypervisor/vmpooler.rb', line 306

def disk_added?(host, disk_size, index)
  if host['disk'].nil?
    false
  else
    host['disk'][index] == "+#{disk_size}gb"
  end
end

#get_host_info(parsed_response, template) ⇒ Hash

Get host info hash from parsed json response

Parameters:

  • parsed_response (Hash)

    hash

  • template (String)

    string

Returns:

  • (Hash)

    Host info hash



98
99
100
# File 'lib/beaker/hypervisor/vmpooler.rb', line 98

def get_host_info(parsed_response, template)
  parsed_response[template]
end

#get_template_url(pooling_api, template) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/beaker/hypervisor/vmpooler.rb', line 63

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

#get_vm(hostname) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/beaker/hypervisor/vmpooler.rb', line 314

def get_vm(hostname)
  begin
    uri = URI.parse(@options[:pooling_api] + '/vm/' + hostname)

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

    response = http.request(request)
  rescue RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError,
         Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e
    @logger.notify "Failed to connect to vmpooler while getting VM information!"
  end
end

#load_credentials(dot_fog = '.fog') ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/beaker/hypervisor/vmpooler.rb', line 27

def load_credentials(dot_fog = '.fog')
  creds = {}

  if fog = read_fog_file(dot_fog)
    if fog[:default] && fog[:default][:vmpooler_token]
      creds[:vmpooler_token] = fog[:default][:vmpooler_token]
    else
      @logger.warn "Credentials file (#{dot_fog}) is missing a :default section with a :vmpooler_token value; proceeding without authentication"
    end
  else
    @logger.warn "Credentials file (#{dot_fog}) is empty; proceeding without authentication"
  end

  creds

rescue TypeError, Psych::SyntaxError => e
  @logger.warn "#{e.class}: Credentials file (#{dot_fog}) has invalid syntax; proceeding without authentication"
  creds
rescue Errno::ENOENT
  @logger.warn "Credentials file (#{dot_fog}) not found; proceeding without authentication"
  creds
end

#parse_response(response) ⇒ Object



302
303
304
# File 'lib/beaker/hypervisor/vmpooler.rb', line 302

def parse_response(response)
  parsed_response = JSON.parse(response.body)
end

#provisionObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/beaker/hypervisor/vmpooler.rb', line 102

def provision
  request_payload = {}
  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

    request_payload[h['template']] = (request_payload[h['template']].to_i + 1).to_s
  end

  last_wait, wait = 0, 1
  waited = 0 #the amount of time we've spent waiting for this host to provision
  begin
    uri = URI.parse(@options['pooling_api'] + '/vm/')

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

    if @credentials[:vmpooler_token]
      request['X-AUTH-TOKEN'] = @credentials[:vmpooler_token]
      @logger.notify "Requesting VM set from vmpooler (with authentication token)"
    else
      @logger.notify "Requesting VM set from vmpooler"
    end

    request_payload_json = request_payload.to_json
    @logger.trace( "Request payload json: #{request_payload_json}" )
    request.body = request_payload_json

    response = http.request(request)
    parsed_response = JSON.parse(response.body)
    @logger.trace( "Response parsed json: #{parsed_response}" )

    if parsed_response['ok']
      domain = parsed_response['domain']
      request_payload = {}

      @hosts.each_with_index do |h, i|
        # If the requested host template is not available on vmpooler
        host_template = h['template']
        if get_host_info(parsed_response, host_template).nil?
          request_payload[host_template] ||= 0
          request_payload[host_template] += 1
          next
        end
        if parsed_response[h['template']]['hostname'].is_a?(Array)
          hostname = parsed_response[host_template]['hostname'].shift
        else
          hostname = parsed_response[host_template]['hostname']
        end

        h['vmhostname'] = domain ? "#{hostname}.#{domain}" : hostname

        @logger.notify "Using available host '#{h['vmhostname']}' (#{h.name})"
      end
      unless request_payload.empty?
        raise "Vmpooler.provision - requested VM templates #{request_payload.keys} not available"
      end
    else
      if response.code == '401'
        raise "Vmpooler.provision - response from pooler not ok. Vmpooler token not authorized to make request.\n#{parsed_response}"
      else
        raise "Vmpooler.provision - response from pooler not ok. Requested host set #{request_payload.keys} not available in pooler.\n#{parsed_response}"
      end
    end
  rescue JSON::ParserError, RuntimeError, *SSH_EXCEPTIONS => e
    @logger.debug "Failed vmpooler provision: #{e.class} : #{e.message}"
    if waited <= @options[:timeout].to_i
      @logger.debug("Retrying provision for vmpooler host after waiting #{wait} second(s)")
      sleep wait
      waited += wait
      last_wait, wait = wait, last_wait + wait
    retry
    end
    report_and_raise(@logger, e, 'Vmpooler.provision')
  end

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

  start = Time.now
  @logger.notify 'Tagging vmpooler VMs'

  @hosts.each_with_index do |h, i|
    begin
      uri = URI.parse(@options[:pooling_api] + '/vm/' + h['vmhostname'].split('.')[0])

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

      # merge pre-defined tags with host tags
      request.body = { 'tags' => add_tags(h) }.to_json

      response = http.request(request)
    rescue RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError,
           Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e
      @logger.notify "Failed to connect to vmpooler for tagging!"
    end

    begin
      parsed_response = JSON.parse(response.body)

      unless parsed_response['ok']
        @logger.notify "Failed to tag host '#{h['vmhostname']}'!"
      end
    rescue JSON::ParserError => e
        @logger.notify "Failed to tag host '#{h['vmhostname']}'! (failed with #{e.class})"
    end
  end

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

  # add additional disks to vm
  @logger.debug 'Looking for disks to add...'

  @hosts.each do |h|
    hostname = h['vmhostname'].split(".")[0]

    if h['disks']
      @logger.debug "Found disks for #{hostname}!"
      disks = h['disks']

      disks.each_with_index do |disk_size, index|
        start = Time.now

        add_disk(hostname, disk_size)

        done = wait_for_disk(hostname, disk_size, index)
        if done
          @logger.notify "Spent %.2f seconds adding disk #{index}. " % (Time.now - start)
        else
          raise "Could not verify disk was added after %.2f seconds" % (Time.now - start)
        end
      end
    else
      @logger.debug "No disks to add for #{hostname}"
    end
  end
end

#read_fog_file(dot_fog = '.fog') ⇒ Object



50
51
52
# File 'lib/beaker/hypervisor/vmpooler.rb', line 50

def read_fog_file(dot_fog = '.fog')
  YAML.load_file(dot_fog)
end

#wait_for_disk(hostname, disk_size, index) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/beaker/hypervisor/vmpooler.rb', line 328

def wait_for_disk(hostname, disk_size, index)
  response = get_vm(hostname)
  parsed = parse_response(response)

  @logger.notify "Waiting for disk"

  attempts = 0

  while (!disk_added?(parsed[hostname], disk_size, index) && attempts < 20)
    sleep 10
    begin
      response = get_vm(hostname)
      parsed = parse_response(response)
    rescue RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError,
           Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e
      report_and_raise(@logger, e, "Vmpooler.wait_for_disk")
    end
    print "."
    attempts += 1
  end

  puts " "

  disk_added?(parsed[hostname], disk_size, index)
end