Class: VsphereHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/hypervisor/vsphere_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vInfo = {}) ⇒ VsphereHelper

Returns a new instance of VsphereHelper.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 10

def initialize vInfo = {}
  @logger = vInfo[:logger] || Beaker::Logger.new
  begin
    require 'rbvmomi'
  rescue LoadError
    raise "Unable to load RbVmomi, please ensure its installed"
  end
  @connection = RbVmomi::VIM.connect :host     => vInfo[:server],
                                     :user     => vInfo[:user],
                                     :password => vInfo[:pass],
                                     :insecure => true
end

Class Method Details

.load_configObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 23

def self.load_config
  # support Fog/Cloud Provisioner layout
  # (ie, someplace besides my made up conf)
  vsphere_credentials = nil
  if File.exists? '/etc/plharness/vsphere'
    vsphere_credentials = load_legacy_credentials

  elsif File.exists?( File.join(ENV['HOME'], '.fog') )
    vsphere_credentials = load_fog_credentials
  end

  return vsphere_credentials
end

.load_fog_credentialsObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 37

def self.load_fog_credentials
  vInfo = YAML.load_file( File.join(ENV['HOME'], '.fog') )

  vsphere_credentials = {}
  vsphere_credentials[:server] = vInfo[:default][:vsphere_server]
  vsphere_credentials[:user]   = vInfo[:default][:vsphere_username]
  vsphere_credentials[:pass]   = vInfo[:default][:vsphere_password]

  return vsphere_credentials
end

.load_legacy_credentialsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 48

def self.load_legacy_credentials
  vInfo = YAML.load_file '/etc/plharness/vsphere'

  puts(
    "Use of /etc/plharness/vsphere as a config file is deprecated.\n" +
    "Please use ~/.fog instead\n" +
    "See http://docs.puppetlabs.com/pe/2.0/" +
    "cloudprovisioner_configuring.html for format"
  )

  vsphere_credentials = {}
  vsphere_credentials[:server] = vInfo['location']
  vsphere_credentials[:user]   = vInfo['user']
  vsphere_credentials[:pass]   = vInfo['pass']

  return vsphere_credentials
end

Instance Method Details

#closeObject



196
197
198
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 196

def close
  @connection.close
end

#find_customization(name) ⇒ Object



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

def find_customization name
  csm = @connection.serviceContent.customizationSpecManager

  begin
    customizationSpec = csm.GetCustomizationSpec({:name => name}).spec
  rescue
    customizationSpec = nil
  end

  return customizationSpec
end

#find_datastore(datastorename) ⇒ Object



145
146
147
148
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 145

def find_datastore datastorename
  datacenter = @connection.serviceInstance.find_datacenter
  datacenter.find_datastore(datastorename)
end

#find_folder(foldername) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 150

def find_folder foldername
  datacenter = @connection.serviceInstance.find_datacenter
  base = datacenter.vmFolder
  folders = foldername.split('/')
  folders.each do |folder|
    case base
      when RbVmomi::VIM::Folder
        base = base.childEntity.find { |f| f.name == folder }
      else
        abort "Unexpected object type encountered (#{base.class}) while finding folder"
    end
  end

  base
end

#find_pool(poolname) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 166

def find_pool poolname
  datacenter = @connection.serviceInstance.find_datacenter
  base = datacenter.hostFolder
  pools = poolname.split('/')
  pools.each do |pool|
    case base
      when RbVmomi::VIM::Folder
        base = base.childEntity.find { |f| f.name == pool }
      when RbVmomi::VIM::ClusterComputeResource
        base = base.resourcePool.resourcePool.find { |f| f.name == pool }
      when RbVmomi::VIM::ResourcePool
        base = base.resourcePool.find { |f| f.name == pool }
      else
        abort "Unexpected object type encountered (#{base.class}) while finding resource pool"
    end
  end

  base = base.resourcePool unless base.is_a?(RbVmomi::VIM::ResourcePool) and base.respond_to?(:resourcePool)
  base
end

#find_snapshot(vm, snapname) ⇒ Object



66
67
68
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 66

def find_snapshot vm, snapname
  search_child_snaps vm.snapshot.rootSnapshotList, snapname
end

#find_vms(names, connection = @connection) ⇒ Object

an easier wrapper around the horrid PropertyCollector interface, necessary for searching VMs in all Datacenters that may be nested within folders of arbitrary depth returns a hash array of <name> => <VirtualMachine ManagedObjects>



98
99
100
101
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
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 98

def find_vms names, connection = @connection
  names = names.is_a?(Array) ? names : [ names ]
  containerView = get_base_vm_container_from connection
  propertyCollector = connection.propertyCollector

  objectSet = [{
    :obj => containerView,
    :skip => true,
    :selectSet => [ RbVmomi::VIM::TraversalSpec.new({
        :name => 'gettingTheVMs',
        :path => 'view',
        :skip => false,
        :type => 'ContainerView'
    }) ]
  }]

  propSet = [{
    :pathSet => [ 'name' ],
    :type => 'VirtualMachine'
  }]

  results = propertyCollector.RetrievePropertiesEx({
    :specSet => [{
      :objectSet => objectSet,
      :propSet   => propSet
    }],
    :options => { :maxObjects => nil }
  })

  vms = {}
  results.objects.each do |result|
    name = result.propSet.first.val
    next unless names.include? name
    vms[name] = result.obj
  end

  while results.token do
    results = propertyCollector.ContinueRetrievePropertiesEx({:token => results.token})
    results.objects.each do |result|
      name = result.propSet.first.val
      next unless names.include? name
      vms[name] = result.obj
    end
  end
  vms
end

#get_base_vm_container_from(connection) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 187

def get_base_vm_container_from connection
  viewManager = connection.serviceContent.viewManager
  viewManager.CreateContainerView({
    :container => connection.serviceContent.rootFolder,
    :recursive => true,
    :type      => [ 'VirtualMachine' ]
  })
end

#search_child_snaps(tree, snapname) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/beaker/hypervisor/vsphere_helper.rb', line 70

def search_child_snaps tree, snapname
  snapshot = nil
  tree.each do |child|
    if child.name == snapname
      snapshot ||= child.snapshot
    else
      snapshot ||= search_child_snaps child.childSnapshotList, snapname
    end
  end
  snapshot
end