Class: Beaker::Vsphere

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

Instance Method Summary collapse

Methods inherited from Hypervisor

#configure, create

Constructor Details

#initialize(vsphere_hosts, options, config) ⇒ Vsphere



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/beaker/hypervisor/vsphere.rb', line 4

def initialize(vsphere_hosts, options, config)
  @options = options
  @@config = config['CONFIG'].dup
  @logger = options[:logger]
  @vsphere_hosts = vsphere_hosts
  require 'yaml' unless defined?(YAML)
  vsphere_credentials = VsphereHelper.load_config

  @logger.notify "Connecting to vSphere at #{vsphere_credentials[:server]}" +
    " with credentials for #{vsphere_credentials[:user]}"

  vsphere_helper = VsphereHelper.new( vsphere_credentials )

  vsphere_vms = {}
  @vsphere_hosts.each do |h|
    name = h["vmname"] || h.name
    vsphere_vms[name] = h["snapshot"]
  end
  vms = vsphere_helper.find_vms(vsphere_vms.keys)
  vsphere_vms.each_pair do |name, snap|
    unless vm = vms[name]
      raise "Couldn't find VM #{name} in vSphere!"
    end

    snapshot = vsphere_helper.find_snapshot(vm, snap) or
      raise "Could not find snapshot '#{snap}' for VM #{vm.name}!"

    @logger.notify "Reverting #{vm.name} to snapshot '#{snap}'"
    start = Time.now
    # This will block for each snapshot...
    # The code to issue them all and then wait until they are all done sucks
    snapshot.RevertToSnapshot_Task.wait_for_completion

    time = Time.now - start
    @logger.notify "Spent %.2f seconds reverting" % time

    unless vm.runtime.powerState == "poweredOn"
      @logger.notify "Booting #{vm.name}"
      start = Time.now
      vm.PowerOnVM_Task.wait_for_completion
      @logger.notify "Spent %.2f seconds booting #{vm.name}" % (Time.now - start)
    end
  end

  vsphere_helper.close
end

Instance Method Details

#cleanupObject



51
52
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
# File 'lib/beaker/hypervisor/vsphere.rb', line 51

def cleanup
  @logger.notify "Destroying vsphere boxes"
  vsphere_credentials = VsphereHelper.load_config

  @logger.notify "Connecting to vSphere at #{vsphere_credentials[:server]}" +
    " with credentials for #{vsphere_credentials[:user]}"

  vsphere_helper = VsphereHelper.new( vsphere_credentials )

  vm_names = @vsphere_hosts.map {|h| h['vmname'] || h.name }
  vms = vsphere_helper.find_vms vm_names
  vm_names.each do |name|
    unless vm = vms[name]
      raise "Couldn't find VM #{name} in vSphere!"
    end

    if vm.runtime.powerState == "poweredOn"
      @logger.notify "Shutting down #{vm.name}"
      start = Time.now
      vm.PowerOffVM_Task.wait_for_completion
      @logger.notify(
        "Spent %.2f seconds halting #{vm.name}" % (Time.now - start) )
    end
  end

  vsphere_helper.close
end