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) ⇒ Vsphere

Returns a new instance of Vsphere.



6
7
8
9
10
# File 'lib/beaker/hypervisor/vsphere.rb', line 6

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

Instance Method Details

#cleanupObject



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

def cleanup
  @logger.notify "Destroying vsphere boxes"
  vsphere_credentials = VsphereHelper.load_config(@options[:dot_fog])

  @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

#provisionObject



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

def provision
  vsphere_credentials = VsphereHelper.load_config(@options[:dot_fog])

  @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