Class: Vmreverter::Vsphere

Inherits:
Object
  • Object
show all
Defined in:
lib/vmreverter/hypervisor/vsphere.rb

Instance Method Summary collapse

Constructor Details

#initialize(vsphere_hosts, options, config) ⇒ Vsphere

Returns a new instance of Vsphere.



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

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

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

  @vsphere_helper = VsphereHelper.new( vsphere_credentials )

  #Transpose Hash for @vsphere_vms = {"test-server01" => "gold-image", "test-server02" => "silver-image"}
  @vsphere_vms = {}
  @vsphere_hosts.each do |host|
    @vsphere_vms[host] = config['HOSTS'][host]['snapshot']
  end

  #Index Hosts Available via rbvmomi
  vms = @vsphere_helper.find_vms(@vsphere_vms.keys)
  
  # Test if host exists and host's snapshot requested exists
  @vsphere_vms.each_pair do |name, snap|
    #Find Host in Index
    report_and_raise(@logger, RuntimeError.new("Couldn't find VM #{name} in vSphere!"), "VSphere::initialize") unless vm = vms[name]
    #snap ~> config['HOSTS'][vm]['snapshot']
    report_and_raise(@logger, RuntimeError.new("Could not find snapshot '#{snap}' for VM #{vm.name}!"), "VSphere::initialize")  unless @vsphere_helper.find_snapshot(vm, snap) 
  end

  return self    
end

Instance Method Details

#close_connectionObject



42
43
44
# File 'lib/vmreverter/hypervisor/vsphere.rb', line 42

def close_connection
  @vsphere_helper.close_connection
end

#invokeObject



38
39
40
# File 'lib/vmreverter/hypervisor/vsphere.rb', line 38

def invoke 
  revert
end