Class: Support::CloneVm

Inherits:
Object
  • Object
show all
Defined in:
lib/support/clone_vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn_opts, options) ⇒ CloneVm

Returns a new instance of CloneVm.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/support/clone_vm.rb', line 11

def initialize(conn_opts, options)
  @options = options
  @name = options[:name]
  @ssl_verify = !conn_opts[:insecure]

  # Connect to vSphere
  @vim ||= RbVmomi::VIM.connect conn_opts

  @username = options[:vm_username]
  password = options[:vm_password]
  @guest_auth = RbVmomi::VIM::NamePasswordAuthentication(interactiveSession: false, username: username, password: password)

  @benchmark_data = {}
end

Instance Attribute Details

#guest_authObject (readonly)

Returns the value of attribute guest_auth.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def guest_auth
  @guest_auth
end

#ipObject (readonly)

Returns the value of attribute ip.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def ip
  @ip
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def options
  @options
end

#ssl_verifyObject (readonly)

Returns the value of attribute ssl_verify.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def ssl_verify
  @ssl_verify
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def username
  @username
end

#vimObject (readonly)

Returns the value of attribute vim.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def vim
  @vim
end

#vmObject (readonly)

Returns the value of attribute vm.



9
10
11
# File 'lib/support/clone_vm.rb', line 9

def vm
  @vm
end

Instance Method Details

#active_discovery?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/support/clone_vm.rb', line 26

def active_discovery?
  options[:active_discovery] == true
end

#active_ip_discovery(prefix_commands = []) ⇒ Object



259
260
261
262
263
264
265
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
295
296
# File 'lib/support/clone_vm.rb', line 259

def active_ip_discovery(prefix_commands = [])
  # Instant clone needs this to have synchronous reply on the new IP
  return unless active_discovery? || instant_clone?

  Kitchen.logger.info "Attempting active IP discovery"
  begin
    tools = Support::GuestOperations.new(vim, vm, guest_auth, ssl_verify)

    commands = []
    commands << rescan_commands if instant_clone?
    # commands << trigger_tools # deactivated for now, as benefit is doubtful
    commands << discovery_commands
    script = commands.flatten.join(command_separator)

    stdout = tools.run_shell_capture_output(script, :auto, 20)

    # Windows returns wrongly encoded UTF-8 for some reason
    stdout = stdout.bytes.map { |b| (32..126).cover?(b.ord) ? b.chr : nil }.join unless stdout.ascii_only?
    @ip = stdout.match(/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/m)&.captures&.first

    Kitchen.logger.debug format("Script output: %s", stdout)
    raise Support::CloneError.new(format("Could not find IP in script output, fallback to standard discovery")) if ip.nil?
    raise Support::CloneError.new(format("Error getting accessible IP address, got %s. Check DHCP server, scope exhaustion or timing issues", ip)) if ip =~ /^169\.254\./
  rescue RbVmomi::Fault => e
    if e.fault.class.wsdl_name == "InvalidGuestLogin"
      message = format('Error authenticating to guest OS as "%s", check configuration of "vm_username"/"vm_password"', username)
    else
      message = e.message
    end

    raise Support::CloneError.new(message)
  rescue ::StandardError => e
    Kitchen.logger.info format("Active discovery failed: %s", e.message)
    return false
  end

  true
end

#benchmark?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/support/clone_vm.rb', line 87

def benchmark?
  options[:benchmark] == true
end

#benchmark_checkpoint(title) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/support/clone_vm.rb', line 107

def benchmark_checkpoint(title)
  timestamp = Time.new
  checkpoints = @benchmark_data[:checkpoints]

  total = timestamp - checkpoints.first.fetch(:value)
  Kitchen.logger.debug format(
    'Benchmark: Step "%s" at %d (%.1f since start)',
    title, timestamp, total.to_f
  )

  @benchmark_data[:checkpoints] << {
    title: title.to_sym,
    value: total,
  }
end

#benchmark_fileObject



91
92
93
# File 'lib/support/clone_vm.rb', line 91

def benchmark_file
  options[:benchmark_file]
end

#benchmark_persistObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/support/clone_vm.rb', line 123

def benchmark_persist
  # Add total time spent as well
  checkpoints = @benchmark_data[:checkpoints]
  checkpoints << {
    title: :total,
    value: Time.new - checkpoints.first.fetch(:value),
  }

  # Include CSV headers
  unless File.exist?(benchmark_file)
    header = "template, clonetype, active_discovery, "
    header += checkpoints.map { |entry| entry[:title] }.join(", ") + "\n"
    File.write(benchmark_file, header)
  end

  active_discovery = options[:active_discovery] || instant_clone?
  data = [@benchmark_data[:template], @benchmark_data[:clonetype], active_discovery.to_s]
  data << checkpoints.map { |entry| format("%.1f", entry[:value]) }

  file = File.new(benchmark_file, "a")
  file.puts(data.join(", ") + "\n")

  Kitchen.logger.debug format("Benchmark: Appended data to file %s", benchmark_file)
end

#benchmark_startObject



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

def benchmark_start
  Kitchen.logger.debug("Starting benchmark data collection.")

  @benchmark_data = {
    template: options[:template],
    clonetype: options[:clone_type],
    checkpoints: [
      { title: "timestamp", value: Time.new.to_f },
    ],
  }
end

#check_add_disk_config(disk_config) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/support/clone_vm.rb', line 298

def check_add_disk_config(disk_config)
  valid_types = %w{thin flat flat_lazy flat_eager}

  unless valid_types.include? disk_config[:type].to_s
    message = format("Unknown disk type in add_disks: %s. Allowed: %s",
      disk_config[:type].to_s,
      valid_types.join(", "))

    raise Support::CloneError.new(message)
  end
end

#cloneObject



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/support/clone_vm.rb', line 400

def clone
  benchmark_start if benchmark?

  # set the datacenter name
  dc = vim.serviceInstance.find_datacenter(options[:datacenter])

  # reference template using full inventory path
  root_folder = vim.serviceInstance.content.rootFolder
  inventory_path = format("/%s/vm/%s", options[:datacenter], options[:template])
  src_vm = root_folder.findByInventoryPath(inventory_path)
  raise Support::CloneError.new(format("Unable to find template: %s", options[:template])) if src_vm.nil?

  if src_vm.config.template && !full_clone?
    Kitchen.logger.warn "Source is a template, thus falling back to full clone. Reference a VM for linked/instant clones."
    options[:clone_type] = :full
  end

  if src_vm.snapshot.nil? && !full_clone?
    Kitchen.logger.warn "Source VM has no snapshot available, thus falling back to full clone. Create a snapshot for linked/instant clones."
    options[:clone_type] = :full
  end

  # Specify where the machine is going to be created
  relocate_spec = RbVmomi::VIM.VirtualMachineRelocateSpec

  # Setting the host is not allowed for instant clone due to VM memory sharing
  relocate_spec.host = options[:targethost].host unless instant_clone?

  # Change to delta disks for linked clones
  relocate_spec.diskMoveType = :moveChildMostDiskBacking if linked_clone?

  # Set the resource pool
  relocate_spec.pool = options[:resource_pool]

  # Change network, if wanted
  unless options[:network_name].nil?
    networks = dc.network.select { |n| n.name == options[:network_name] }
    raise Support::CloneError.new(format("Could not find network named %s", options[:network_name])) if networks.empty?

    Kitchen.logger.warn format("Found %d networks named %s, picking first one", networks.count, options[:network_name]) if networks.count > 1
    network_obj = networks.first
    network_device = network_device(src_vm)

    if network_obj.is_a? RbVmomi::VIM::DistributedVirtualPortgroup
      Kitchen.logger.info format("Assigning network %s...", network_obj.pretty_path)

      vds_obj = network_obj.config.distributedVirtualSwitch
      Kitchen.logger.info format("Using vDS '%s' for network connectivity...", vds_obj.name)

      network_device.backing = RbVmomi::VIM.VirtualEthernetCardDistributedVirtualPortBackingInfo(
        port: RbVmomi::VIM.DistributedVirtualSwitchPortConnection(
          portgroupKey: network_obj.key,
          switchUuid: vds_obj.uuid
        )
      )
    elsif network_obj.is_a? RbVmomi::VIM::Network
      Kitchen.logger.info format("Assigning network %s...", options[:network_name])

      network_device.backing = RbVmomi::VIM.VirtualEthernetCardNetworkBackingInfo(
        deviceName: options[:network_name]
      )
    else
      raise Support::CloneError.new(format("Unknown network type %s for network name %s", network_obj.class.to_s, options[:network_name]))
    end

    relocate_spec.deviceChange = [
      RbVmomi::VIM.VirtualDeviceConfigSpec(
        operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation("edit"),
        device: network_device
      ),
    ]
  end

  # Set the folder to use
  dest_folder = options[:folder].nil? ? dc.vmFolder : options[:folder][:id]

  Kitchen.logger.info format("Cloning '%s' to create the VM...", options[:template])
  if instant_clone?
    vcenter_data = vim.serviceInstance.content.about
    raise Support::CloneError.new("Instant clones only supported with vCenter 6.7 or higher") unless vcenter_data.version.to_f >= 6.7

    Kitchen.logger.debug format("Detected %s", vcenter_data.fullName)

    resources = dc.hostFolder.children
    hosts = resources.select { |resource| resource.class.to_s =~ /ComputeResource$/ }.map(&:host).flatten
    targethost = hosts.select { |host| host.summary.config.name == options[:targethost].name }.first
    raise Support::CloneError.new("No matching ComputeResource found in host folder") if targethost.nil?

    esx_data = targethost.summary.config.product
    raise Support::CloneError.new("Instant clones only supported with ESX 6.7 or higher") unless esx_data.version.to_f >= 6.7

    Kitchen.logger.debug format("Detected %s", esx_data.fullName)

    # Other tools check for VMWare Tools status, but that will be toolsNotRunning on frozen VMs
    raise Support::CloneError.new("Need a running VM for instant clones") unless src_vm.runtime.powerState == "poweredOn"

    # In first iterations, only support the Frozen Source VM workflow. This is more efficient
    #   but needs preparations (freezing the source VM). Running Source VM support is to be
    #   added later
    raise Support::CloneError.new("Need a frozen VM for instant clones, running source VM not supported yet") unless src_vm.runtime.instantCloneFrozen

    # Swapping NICs not needed anymore (blog posts mention this), instant clones get a new
    # MAC at least with 6.7.0 build 9433931

    # Disconnect network device, so wo don't get IP collisions on start
    network_device = network_device(src_vm)
    network_device.connectable = RbVmomi::VIM.VirtualDeviceConnectInfo(
      allowGuestControl: true,
      startConnected: true,
      connected: false,
      migrateConnect: "disconnect"
    )
    relocate_spec.deviceChange = [
      RbVmomi::VIM.VirtualDeviceConfigSpec(
        operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation("edit"),
        device: network_device
      ),
    ]

    clone_spec = RbVmomi::VIM.VirtualMachineInstantCloneSpec(location: relocate_spec,
                                                             name: name)

    benchmark_checkpoint("initialized") if benchmark?
    task = src_vm.InstantClone_Task(spec: clone_spec)
  else
    clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(location: relocate_spec,
                                                      powerOn: options[:poweron] && options[:customize].nil?,
                                                      template: false)

    benchmark_checkpoint("initialized") if benchmark?
    task = src_vm.CloneVM_Task(spec: clone_spec, folder: dest_folder, name: name)
  end
  task.wait_for_completion

  benchmark_checkpoint("cloned") if benchmark?

  # get the IP address of the machine for bootstrapping
  # machine name is based on the path, e.g. that includes the folder
  path = options[:folder].nil? ? name : format("%s/%s", options[:folder][:name], name)
  @vm = dc.find_vm(path)
  raise Support::CloneError.new(format("Unable to find machine: %s", path)) if vm.nil?

  if options[:vm_os].nil?
    os = detect_os
    Kitchen.logger.debug format('OS for VM not configured, got "%s" from VMware', os.to_s.capitalize)
    options[:vm_os] = os
  end

  # Reconnect network device after Instant Clone is ready
  if instant_clone?
    Kitchen.logger.info "Reconnecting network adapter"
    reconnect_network_device(vm)
  end

  reconfigure_guest unless options[:customize].nil?

  # Start only if specified or customizations wanted; no need for instant clones as they start in running state
  if options[:poweron] && !options[:customize].nil? && !instant_clone?
    task = vm.PowerOnVM_Task
    task.wait_for_completion
  end
  benchmark_checkpoint("powered_on") if benchmark?

  Kitchen.logger.info format("Waiting for VMware tools to become available (timeout: %d seconds)...", options[:wait_timeout])
  wait_for_tools(options[:wait_timeout], options[:wait_interval])

  active_ip_discovery || standard_ip_discovery
  benchmark_checkpoint("ip_detected") if benchmark?

  benchmark_persist if benchmark?
  Kitchen.logger.info format("Created machine %s with IP %s", name, ip)
end

#command_separatorObject



197
198
199
200
201
202
203
204
# File 'lib/support/clone_vm.rb', line 197

def command_separator
  case options[:vm_os].downcase.to_sym
  when :linux
    " && "
  when :windows
    " & "
  end
end

#detect_osObject



148
149
150
# File 'lib/support/clone_vm.rb', line 148

def detect_os
  vm.config&.guestId&.match(/^win/) ? :windows : :linux
end

#discovery_commandsObject

Retrieve IP via OS commands



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/support/clone_vm.rb', line 243

def discovery_commands
  if options[:active_discovery_command].nil?
    case options[:vm_os].downcase.to_sym
    when :linux
      "ip address show scope global | grep global | cut -b10- | cut -d/ -f1"
    when :windows
      ["sleep 5", "ipconfig"]
      # "ipconfig /renew"
      # "wmic nicconfig get IPAddress",
      # "netsh interface ip show ipaddress #{options[:vm_win_network]}"
    end
  else  
    return options[:active_discovery_command]
  end
end

#full_clone?Boolean

Returns:

  • (Boolean)


396
397
398
# File 'lib/support/clone_vm.rb', line 396

def full_clone?
  options[:clone_type] == :full
end

#instant_clone?Boolean

Returns:

  • (Boolean)


388
389
390
# File 'lib/support/clone_vm.rb', line 388

def instant_clone?
  options[:clone_type] == :instant
end

#ip_from_toolsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/support/clone_vm.rb', line 30

def ip_from_tools
  return if vm.guest.net.empty?

  # Don't simply use vm.guest.ipAddress to allow specifying a different interface
  nics = vm.guest.net
  if options[:interface]
    nics.select! { |nic| nic.network == options[:interface] }

    raise Support::CloneError.new(format("No interfaces found on VM which are attached to network '%s'", options[:interface])) if nics.empty?
  end

  vm_ip = nil
  nics.each do |net|
    vm_ip = net.ipConfig.ipAddress.detect { |addr| addr.origin != "linklayer" }
    break unless vm_ip.nil?
  end

  vm_ip&.ipAddress
end

#linked_clone?Boolean

Returns:

  • (Boolean)


392
393
394
# File 'lib/support/clone_vm.rb', line 392

def linked_clone?
  options[:clone_type] == :linked
end

#linux?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/support/clone_vm.rb', line 156

def linux?
  options[:vm_os].downcase.to_sym == :linux
end

#network_device(vm) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/support/clone_vm.rb', line 160

def network_device(vm)
  all_network_devices = vm.config.hardware.device.select do |device|
    device.is_a?(RbVmomi::VIM::VirtualEthernetCard)
  end

  # Only support for first NIC so far
  all_network_devices.first
end

#reconfigure_guestObject



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
348
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
# File 'lib/support/clone_vm.rb', line 310

def reconfigure_guest
  Kitchen.logger.info "Waiting for reconfiguration to finish"

  # Pass some contents right through
  # https://pubs.vmware.com/vsphere-6-5/index.jsp?topic=%2Fcom.vmware.wssdk.smssdk.doc%2Fvim.vm.ConfigSpec.html
  config = options[:customize].select { |key, _| %i{annotation memoryMB numCPUs}.include? key }

  add_disks = options[:customize]&.fetch(:add_disks, nil)
  unless add_disks.nil?
    config[:deviceChange] = []

    # Will create a stem like "default-ubuntu-12345678/default-ubuntu-12345678"
    filename_base = vm.disks.first.backing.fileName.gsub(/(-[0-9]+)?.vmdk/, "")

    # Storage Controller and ID mapping
    controller = vm.config.hardware.device.select { |device| device.is_a? RbVmomi::VIM::VirtualSCSIController }.first

    add_disks.each_with_index do |disk_config, idx|
      # Default to Thin Provisioning and 10GB disk size
      disk_config[:type]    ||= :thin
      disk_config[:size_mb] ||= 10240

      check_add_disk_config(disk_config)

      disk_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(
        fileOperation: "create",
        operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation("add"),
        device: RbVmomi::VIM.VirtualDisk(
          backing: RbVmomi::VIM.VirtualDiskFlatVer2BackingInfo(
            thinProvisioned: true,
            diskMode: "persistent",
            fileName: format("%s_disk%03d.vmdk", filename_base, idx + 1),
            datastore: vm.disks.first.backing.datastore
          ),
          deviceInfo: RbVmomi::VIM::Description(
            label: format("Additional disk %d", idx + 1),
            summary: format("%d MB", disk_config[:size_mb])
          )
        )
      )

      # capacityInKB is marked a deprecated in 6.7 but still a required parameter
      disk_spec.device.capacityInBytes = disk_config[:size_mb] * 1024**2
      disk_spec.device.capacityInKB = disk_config[:size_mb] * 1024

      disk_spec.device.controllerKey = controller.key

      highest_id = vm.disks.map(&:unitNumber).sort.last
      next_id = highest_id + idx + 1

      # Avoid the SCSI controller ID
      next_id += 1 if next_id == controller.scsiCtlrUnitNumber

      # Theoretically could add another SCSI controller, but there are limits to what kitchen should support
      if next_id > 14
        raise Support::CloneError.new(format("Ran out of SCSI IDs while trying to assign new disk %d", idx + 1))
      end

      disk_spec.device.unitNumber = next_id

      device_keys = vm.config.hardware.device.map(&:key).sort
      disk_spec.device.key = device_keys.last + (idx + 1) * 1000

      disk_spec.device.backing.eagerlyScrub = true if disk_config[:type].to_s == "flat_eager"
      disk_spec.device.backing.thinProvisioned = false if disk_config[:type].to_s =~ /^flat/

      config[:deviceChange] << disk_spec
    end
  end

  config_spec = RbVmomi::VIM.VirtualMachineConfigSpec(config)

  task = vm.ReconfigVM_Task(spec: config_spec)
  task.wait_for_completion

  benchmark_checkpoint("reconfigured") if benchmark?
end

#reconnect_network_device(vm) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/support/clone_vm.rb', line 169

def reconnect_network_device(vm)
  network_device = network_device(vm)
  network_device.connectable = RbVmomi::VIM.VirtualDeviceConnectInfo(
    allowGuestControl: true,
    startConnected: true,
    connected: true
  )

  config_spec = RbVmomi::VIM.VirtualMachineConfigSpec(
    deviceChange: [
      RbVmomi::VIM.VirtualDeviceConfigSpec(
        operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation("edit"),
        device: network_device
      ),
    ]
  )

  task = vm.ReconfigVM_Task(spec: config_spec)
  task.wait_for_completion

  benchmark_checkpoint("nic_reconfigured") if benchmark?
end

#rescan_commandsObject

Rescan network adapters for MAC/IP changes



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/support/clone_vm.rb', line 207

def rescan_commands
  Kitchen.logger.info "Refreshing network interfaces in OS"

  case options[:vm_os].downcase.to_sym
  when :linux
    # @todo: allow override if no dhclient
    return [
      "/sbin/modprobe -r vmxnet3",
      "/sbin/modprobe vmxnet3",
      "/sbin/dhclient",
    ]
  when :windows
    return [
      "netsh interface set Interface #{options[:vm_win_network]} disable",
      "netsh interface set Interface #{options[:vm_win_network]} enable",
      "ipconfig /renew",
    ]
  end
end

#standard_ip_discoveryObject



192
193
194
195
# File 'lib/support/clone_vm.rb', line 192

def standard_ip_discovery
  Kitchen.logger.info format("Waiting for IP (timeout: %d seconds)...", options[:wait_timeout])
  wait_for_ip(options[:wait_timeout], options[:wait_interval])
end

#trigger_toolsObject

Available from VMware Tools 10.1.0 this pushes the IP instead of the standard 30 second poll This will be used to provide a quick fallback, if active discovery fails.



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/support/clone_vm.rb', line 229

def trigger_tools
  case options[:vm_os].downcase.to_sym
  when :linux
    [
      "/usr/bin/vmware-toolbox-cmd info update network",
    ]
  when :windows
    [
      '"C:\Program Files\VMware\VMware Tools\VMwareToolboxCmd.exe" info update network',
    ]
  end
end

#wait_for_ip(timeout = 60.0, interval = 2.0) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/support/clone_vm.rb', line 68

def wait_for_ip(timeout = 60.0, interval = 2.0)
  start = Time.new

  ip = nil
  loop do
    ip = ip_from_tools
    if ip || (Time.new - start) >= timeout
      Kitchen.logger.debug format("IP retrieved after %.1f seconds", Time.new - start) if ip
      break
    end
    sleep interval
  end

  raise Support::CloneError.new("Timeout waiting for IP address") if ip.nil?
  raise Support::CloneError.new(format("Error getting accessible IP address, got %s. Check DHCP server and scope exhaustion", ip)) if ip =~ /^169\.254\./

  @ip = ip
end

#wait_for_tools(timeout = 30.0, interval = 2.0) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/support/clone_vm.rb', line 50

def wait_for_tools(timeout = 30.0, interval = 2.0)
  start = Time.new

  loop do
    if vm.guest.toolsRunningStatus == "guestToolsRunning"
      benchmark_checkpoint("tools_detected") if benchmark?

      Kitchen.logger.debug format("Tools detected after %.1f seconds", Time.new - start)
      return
    end
    break if (Time.new - start) >= timeout

    sleep interval
  end

  raise Support::CloneError.new("Timeout waiting for VMware Tools")
end

#windows?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/support/clone_vm.rb', line 152

def windows?
  options[:vm_os].downcase.to_sym == :windows
end