Module: ChefMetalVsphere::Helpers

Included in:
VsphereDriver
Defined in:
lib/chef_metal_vsphere/vsphere_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_extra_nic(action_handler, vm_template, options, vm) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 181

def add_extra_nic(action_handler, vm_template, options, vm)
  deviceAdditions, changes = network_device_changes(action_handler, vm_template, options)

  if deviceAdditions.count > 0
    current_networks = find_ethernet_cards_for(vm).map{|card| card.backing.deviceName}
    new_devices = deviceAdditions.select { |device| !current_networks.include?(device.device.backing.deviceName)}
    
    if new_devices.count > 0
      action_handler.report_progress "Adding extra NICs"
      task = vm.ReconfigVM_Task(:spec => RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange => new_devices))
      task.wait_for_completion
      new_devices
    end
  end
end

#create_delta_disk(vm_template) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 220

def create_delta_disk(vm_template)
  disks = vm_template.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
  disks.select { |disk| disk.backing.parent == nil }.each do |disk|
    spec = {
        :deviceChange => [
            {
                :operation => :remove,
                :device => disk
            },
            {
                :operation => :add,
                :fileOperation => :create,
                :device => disk.dup.tap { |new_disk|
                  new_disk.backing = new_disk.backing.dup
                  new_disk.backing.fileName = "[#{disk.backing.datastore.name}]"
                  new_disk.backing.parent = disk.backing
                },
            }
        ]
    }
    vm_template.ReconfigVM_Task(:spec => spec).wait_for_completion
    end
end

#customization_options_from(action_handler, vm_template, vm_name, options) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 301

def customization_options_from(action_handler, vm_template, vm_name, options)
  if options.has_key?(:customization_spec)
    if(options[:customization_spec].is_a?(Hash))
        cust_options = options[:customization_spec]
        raise ArgumentError, "domain is required" unless cust_options.key?(:domain)
        cust_ip_settings = nil
        if cust_options.key?(:ipsettings) and cust_options[:ipsettings].key?(:ip)
          raise ArgumentError, "ip and subnetMask is required for static ip" unless cust_options[:ipsettings].key?(:ip) and
                                                                                    cust_options[:ipsettings].key?(:subnetMask)
          cust_ip_settings = RbVmomi::VIM::CustomizationIPSettings.new(cust_options[:ipsettings])
          action_handler.report_progress "customizing #{vm_name} with static IP #{cust_options[:ipsettings][:ip]}"
          cust_ip_settings.ip = RbVmomi::VIM::CustomizationFixedIp(:ipAddress => cust_options[:ipsettings][:ip])
        end
        cust_domain = cust_options[:domain]
        if cust_ip_settings.nil?
          cust_ip_settings= RbVmomi::VIM::CustomizationIPSettings.new(:ip => RbVmomi::VIM::CustomizationDhcpIpGenerator.new())
          cust_ip_settings.dnsServerList = cust_options[:ipsettings][:dnsServerList]
          action_handler.report_progress "customizing #{vm_name} with dynamic IP and DNS: #{cust_options[:ipsettings][:dnsServerList]}"
        end

        cust_ip_settings.dnsDomain = cust_domain
        cust_global_ip_settings = RbVmomi::VIM::CustomizationGlobalIPSettings.new
        cust_global_ip_settings.dnsServerList = cust_ip_settings.dnsServerList
        cust_global_ip_settings.dnsSuffixList = [cust_domain]
        cust_hostname = hostname_from(options[:customization_spec], vm_name)
        cust_hwclockutc = cust_options[:hw_clock_utc]
        cust_timezone = cust_options[:time_zone]

        if vm_template.config.guestId.start_with?('win')
          cust_prep = windows_prep_for(action_handler, options, vm_name)
        else
          cust_prep = RbVmomi::VIM::CustomizationLinuxPrep.new(
            :domain => cust_domain,
            :hostName => cust_hostname,
            :hwClockUTC => cust_hwclockutc,
            :timeZone => cust_timezone)
        end
          cust_adapter_mapping = [RbVmomi::VIM::CustomizationAdapterMapping.new(:adapter => cust_ip_settings)]
          RbVmomi::VIM::CustomizationSpec.new(
            :identity => cust_prep,
            :globalIPSettings => cust_global_ip_settings,
            :nicSettingMap => cust_adapter_mapping)
    else
      find_customization_spec(options[:customization_spec])
    end
  end
end

#dc(dc_name) ⇒ Object



108
109
110
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 108

def dc(dc_name)
  vim.serviceInstance.find_datacenter(dc_name) or raise("vSphere Datacenter not found [#{datacenter}]")
end

#do_vm_clone(action_handler, dc_name, vm_template, vm_name, options) ⇒ Object



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
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 132

def do_vm_clone(action_handler, dc_name, vm_template, vm_name, options)
  deviceAdditions = []

  clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(
    location: relocate_spec_for(dc_name, vm_template, options),
    powerOn: false,
    template: false,
    config: RbVmomi::VIM.VirtualMachineConfigSpec(
      :cpuHotAddEnabled => true,
      :memoryHotAddEnabled => true,
      :cpuHotRemoveEnabled => true,
      :deviceChange => Array.new)
  )

  clone_spec.customization = customization_options_from(action_handler, vm_template, vm_name, options)

  unless options[:annotation].to_s.nil?
    clone_spec.config.annotation = options[:annotation]
  end

  unless options[:num_cpus].to_s.nil?
    clone_spec.config.numCPUs = options[:num_cpus]
  end

  unless options[:memory_mb].to_s.nil?
    clone_spec.config.memoryMB = options[:memory_mb]
  end

  unless options[:network_name].nil?
    deviceAdditions, changes = network_device_changes(action_handler, vm_template, options)
    clone_spec.config.deviceChange = changes
  end

  vm_template.CloneVM_Task(
    name: vm_name,
    folder: find_folder(dc_name, options[:vm_folder]),
    spec: clone_spec
  ).wait_for_completion

  vm = find_vm(dc_name, options[:vm_folder], vm_name)

  if options[:additional_disk_size_gb].to_i > 0
    task = vm.ReconfigVM_Task(:spec => RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange => [virtual_disk_for(vm, options)]))
    task.wait_for_completion
  end

  vm
end

#find_customization_spec(customization_spec) ⇒ Object



446
447
448
449
450
451
452
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 446

def find_customization_spec(customization_spec)
  csm = vim.serviceContent.customizationSpecManager
  csi = csm.GetCustomizationSpec(:name => customization_spec)
  spec = csi.spec
  raise "Customization Spec not found [#{customization_spec}]" if spec.nil?
  spec
end

#find_datastore(dc, datastore_name) ⇒ Object



296
297
298
299
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 296

def find_datastore(dc, datastore_name)
    baseEntity = dc.datastore
    baseEntity.find { |f| f.info.name == datastore_name } or raise "no such datastore #{datastore_name}"    
end

#find_ethernet_cards_for(vm) ⇒ Object



128
129
130
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 128

def find_ethernet_cards_for(vm)
  vm.config.hardware.device.select {|d| d.is_a?(RbVmomi::VIM::VirtualEthernetCard)}
end

#find_folder(dc_name, folder_name) ⇒ Object

folder could be like: /Level1/Level2/folder_name



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 95

def find_folder(dc_name, folder_name)
  baseEntity = dc(dc_name).vmFolder
  if folder_name && folder_name.length > 0
    entityArray = folder_name.split('/')
    entityArray.each do |entityArrItem|
      if entityArrItem != ''
        baseEntity = baseEntity.childEntity.grep(RbVmomi::VIM::Folder).find { |f| f.name == entityArrItem }
      end
    end
  end
  baseEntity
end

#find_host(dc, host_name) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 400

def find_host(dc, host_name)
  baseEntity = dc.hostFolder
  entityArray = host_name.split('/')
  entityArray.each do |entityArrItem|
    if entityArrItem != ''
      if baseEntity.is_a? RbVmomi::VIM::Folder
        baseEntity = baseEntity.childEntity.find { |f| f.name == entityArrItem } or nil
      elsif baseEntity.is_a? RbVmomi::VIM::ClusterComputeResource or baseEntity.is_a? RbVmomi::VIM::ComputeResource
        baseEntity = baseEntity.host.find { |f| f.name == entityArrItem } or nil
      elsif baseEntity.is_a? RbVmomi::VIM::HostSystem
        baseEntity = baseEntity.host.find { |f| f.name == entityArrItem } or nil
      else
        baseEntity = nil
      end
    end
  end

  raise "vSphere Host not found [#{host_name}]" if baseEntity.nil?

  baseEntity = baseEntity.host if not baseEntity.is_a?(RbVmomi::VIM::HostSystem) and baseEntity.respond_to?(:host)
  baseEntity
end

#find_pool(dc, pool_name) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 423

def find_pool(dc, pool_name)
  baseEntity = dc.hostFolder
  entityArray = pool_name.split('/')
  entityArray.each do |entityArrItem|
    if entityArrItem != ''
      if baseEntity.is_a? RbVmomi::VIM::Folder
        baseEntity = baseEntity.childEntity.find { |f| f.name == entityArrItem } or nil
      elsif baseEntity.is_a? RbVmomi::VIM::ClusterComputeResource or baseEntity.is_a? RbVmomi::VIM::ComputeResource
        baseEntity = baseEntity.resourcePool.resourcePool.find { |f| f.name == entityArrItem } or nil
      elsif baseEntity.is_a? RbVmomi::VIM::ResourcePool
        baseEntity = baseEntity.resourcePool.find { |f| f.name == entityArrItem } or nil
      else
        baseEntity = nil
      end
    end
  end

  raise "vSphere ResourcePool not found [#{pool_name}]" if baseEntity.nil?

  baseEntity = baseEntity.resourcePool if not baseEntity.is_a?(RbVmomi::VIM::ResourcePool) and baseEntity.respond_to?(:resourcePool)
  baseEntity
end

#find_vm(dc_name, vm_folder, vm_name) ⇒ Object



29
30
31
32
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 29

def find_vm(dc_name, vm_folder, vm_name)
  folder = find_folder(dc_name, vm_folder) or raise("vSphere Folder not found [#{vm_folder}] for vm #{vm_name}")
  vm     = folder.find(vm_name, RbVmomi::VIM::VirtualMachine)
end

#find_vm_by_id(uuid, connection = vim) ⇒ Object



34
35
36
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 34

def find_vm_by_id(uuid, connection = vim)
  vm = connection.searchIndex.FindByUuid({:uuid => uuid, :vmSearch => true, :instanceUuid => true})
end

#hostname_from(options, vm_name) ⇒ Object



392
393
394
395
396
397
398
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 392

def hostname_from(options, vm_name)
  if options.key?(:hostname)
    RbVmomi::VIM::CustomizationFixedName.new(:name => options[:hostname])
  else
    RbVmomi::VIM::CustomizationFixedName.new(:name => vm_name)
  end
end

#network_adapter_for(operation, network_name, network_label, device_key) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 112

def network_adapter_for(operation, network_name, network_label, device_key)
    nic_backing_info = RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo(:deviceName => network_name)
    connectable = RbVmomi::VIM::VirtualDeviceConnectInfo(
      :allowGuestControl => true,
      :connected => true,
      :startConnected => true)
    device = RbVmomi::VIM::VirtualVmxnet3(
      :backing => nic_backing_info,
      :deviceInfo => RbVmomi::VIM::Description(:label => network_label, :summary => network_name),
      :key => device_key,
      :connectable => connectable)
    RbVmomi::VIM::VirtualDeviceConfigSpec(
      :operation => operation,
      :device => device)
end

#network_device_changes(action_handler, vm_template, options) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 266

def network_device_changes(action_handler, vm_template, options)
  additions = []
  changes = []
  networks=options[:network_name]
  if networks.kind_of?(String)
    networks=[networks]
  end

  cards = find_ethernet_cards_for(vm_template)

  key = 4000
  networks.each_index do | i |
    label = "Ethernet #{i+1}"
    if card = cards.shift
      key = card.key
      operation = RbVmomi::VIM::VirtualDeviceConfigSpecOperation('edit')
      action_handler.report_progress "changing template nic for #{networks[i]}"
      changes.push(
        network_adapter_for(operation, networks[i], label, key))
    else
      key = key + 1
      operation = RbVmomi::VIM::VirtualDeviceConfigSpecOperation('add')
      action_handler.report_progress "will be adding nic for #{networks[i]}"
      additions.push(
        network_adapter_for(operation, networks[i], label, key))
    end
  end
  [additions, changes]
end

#port_ready?(vm, port) ⇒ Boolean



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 69

def port_ready?(vm, port)
  vm_ip = vm.guest.ipAddress
  return false if vm_ip.nil?

  begin
    tcp_socket = TCPSocket.new(vm_ip, port)
    readable = IO.select([tcp_socket], nil, nil, 5)
    if readable
      true
    else
      false
    end
  rescue Errno::ETIMEDOUT
    false
  rescue Errno::EPERM
    false
  rescue Errno::ECONNREFUSED
    false
  rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH
    false
  ensure
    tcp_socket && tcp_socket.close
  end
end

#relocate_spec_for(dc_name, vm_template, options) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 197

def relocate_spec_for(dc_name, vm_template, options)
  datacenter = dc(dc_name)
  if options.has_key?(:host)
    host = find_host(datacenter, options[:host])
    rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(host: host) 
  else
    pool = options[:resource_pool] ? find_pool(datacenter, options[:resource_pool]) : vm_template.resourcePool
    rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(pool: pool)
    raise 'either :host or :resource_pool must be specified when cloning from a VM Template' if pool.nil?
  end

  if options.has_key?(:use_linked_clone)
    create_delta_disk(vm_template)
    rspec.diskMoveType = :moveChildMostDiskBacking
  end

  unless options[:datastore].to_s.empty?
    rspec.datastore = find_datastore(datacenter, options[:datastore])
  end

  rspec
end

#start_vm(vm, wait_on_port = 22) ⇒ Object



53
54
55
56
57
58
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 53

def start_vm(vm, wait_on_port = 22)
  state = vm.runtime.powerState
  unless state == 'poweredOn'
    vm.PowerOnVM_Task.wait_for_completion
  end
end

#stop_vm(vm) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 60

def stop_vm(vm)
  begin
    vm.ShutdownGuest
    sleep 2 until vm.runtime.powerState == 'poweredOff'
  rescue
    vm.PowerOffVM_Task.wait_for_completion
  end
end

#upload_file_to_vm(vm, username, password, local, remote) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 454

def upload_file_to_vm(vm, username, password, local, remote)
  auth = RbVmomi::VIM::NamePasswordAuthentication({:username => username, :password => password, :interactiveSession => false})
  size = File.size(local)
  endpoint = $guest_op_managers[vim.pretty_inspect].fileManager.InitiateFileTransferToGuest(
    :vm => vm, 
    :auth => auth, 
    :guestFilePath => remote,
    :overwrite => true,
    :fileAttributes => RbVmomi::VIM::GuestWindowsFileAttributes.new,
    :fileSize => size)

    uri = URI.parse(endpoint)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}")
    req.body_stream = File.open(local)
    req["Content-Type"] = "application/octet-stream"
    req["Content-Length"] = size
    res = http.request(req) 
    unless res.kind_of?(Net::HTTPSuccess)
      raise "Error: #{res.inspect} :: #{res.body} :: sending #{local} to #{remote} at #{vm.name} via #{endpoint} with a size of #{size}"
    end
end

#vim(options = connect_options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 10

def vim(options = connect_options)
  if @current_connection.nil? or @current_connection.serviceContent.sessionManager.currentSession.nil?
    puts "establishing connection to #{options[:host]}"
    @current_connection = RbVmomi::VIM.connect options
    str_conn = @current_connection.pretty_inspect # a string in the format of VIM(host ip)
    
    # we are caching guest operation managers in a global variable...terrible i know
    # this object is available from the serviceContent object on API version 5 forward
    # Its a singleton and if another connection is made for the same host and user
    # that object is not available on any subsequent connection
    # I could find no documentation that discusses this
    if !$guest_op_managers.has_key?(str_conn)
      $guest_op_managers[str_conn] = @current_connection.serviceContent.guestOperationsManager
    end
  end

  @current_connection
end

#virtual_disk_for(vm, options) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 244

def virtual_disk_for(vm, options)
  if options[:datastore].to_s.empty? 
    raise ":datastore must be specified when adding a disk to a cloned vm"
  end
  idx = vm.disks.count
  RbVmomi::VIM::VirtualDeviceConfigSpec(
        :operation     => :add,
        :fileOperation => :create,
        :device        => RbVmomi::VIM.VirtualDisk(
          :key           => idx,
          :backing       => RbVmomi::VIM.VirtualDiskFlatVer2BackingInfo(
            :fileName        => "[#{options[:datastore]}]",
            :diskMode        => 'persistent',
            :thinProvisioned => true
          ),
          :capacityInKB  => options[:additional_disk_size_gb] * 1024 * 1024,
          :controllerKey => 1000,
          :unitNumber    => idx
        )
  )
end

#vm_started?(vm, wait_on_port = 22) ⇒ Boolean



38
39
40
41
42
43
44
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 38

def vm_started?(vm, wait_on_port = 22)
  return false if vm.nil?
  state = vm.runtime.powerState
  return false unless state == 'poweredOn'
  return false unless port_ready?(vm, wait_on_port)
  return true
end

#vm_stopped?(vm) ⇒ Boolean



46
47
48
49
50
51
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 46

def vm_stopped?(vm)
  return true if vm.nil?
  state = vm.runtime.powerState
  return false unless state == 'poweredOff'
  return false
end

#windows_prep_for(action_handler, options, vm_name) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/chef_metal_vsphere/vsphere_helpers.rb', line 349

def windows_prep_for(action_handler, options, vm_name)
  cust_options = options[:customization_spec]
  cust_runonce = RbVmomi::VIM::CustomizationGuiRunOnce.new(
    :commandList => [
      'winrm set winrm/config/client/auth @{Basic="true"}',
      'winrm set winrm/config/service/auth @{Basic="true"}',
      'winrm set winrm/config/service @{AllowUnencrypted="true"}',
      'shutdown -l'])

   = RbVmomi::VIM::CustomizationPassword(
    :plainText => true,
    :value => options[:ssh][:password])
  if cust_options.has_key?(:domain) and cust_options[:domain] != 'local'
    cust_domain_password = RbVmomi::VIM::CustomizationPassword(
      :plainText => true,
      :value => ENV['domainAdminPassword'] || cust_options[:domainAdminPassword])
    cust_id = RbVmomi::VIM::CustomizationIdentification.new(
      :joinDomain => cust_options[:domain],
      :domainAdmin => cust_options[:domainAdmin],
      :domainAdminPassword => cust_domain_password)
    #puts "my env passwd is: #{ENV['domainAdminPassword']}"
    action_handler.report_progress "joining domain #{cust_options[:domain]} with user: #{cust_options[:domainAdmin]}"
  else
    cust_id = RbVmomi::VIM::CustomizationIdentification.new(
      :joinWorkgroup => 'WORKGROUP')
  end
  cust_gui_unattended = RbVmomi::VIM::CustomizationGuiUnattended.new(
    :autoLogon => true,
    :autoLogonCount => 1,
    :password => ,
    :timeZone => cust_options[:win_time_zone])
  cust_userdata = RbVmomi::VIM::CustomizationUserData.new(
    :computerName => hostname_from(cust_options, vm_name),
    :fullName => cust_options[:org_name],
    :orgName => cust_options[:org_name],
    :productId => cust_options[:product_id])
  RbVmomi::VIM::CustomizationSysprep.new(
    :guiRunOnce => cust_runonce,
    :identification => cust_id,
    :guiUnattended => cust_gui_unattended,
    :userData => cust_userdata)
end