Class: VSphereCloud::Cloud

Inherits:
Bosh::Cloud
  • Object
show all
Includes:
RetryBlock, VimSdk
Defined in:
lib/cloud/vsphere/cloud.rb

Defined Under Namespace

Classes: TimeoutException

Constant Summary

Constants included from VimSdk

VimSdk::BASE_VERSION, VimSdk::DYNAMIC_TYPES, VimSdk::SOAP_BODY_END, VimSdk::SOAP_BODY_START, VimSdk::SOAP_BODY_TAG, VimSdk::SOAP_END, VimSdk::SOAP_ENVELOPE_END, VimSdk::SOAP_ENVELOPE_START, VimSdk::SOAP_ENVELOPE_TAG, VimSdk::SOAP_FAULT_TAG, VimSdk::SOAP_HEADER_END, VimSdk::SOAP_HEADER_START, VimSdk::SOAP_HEADER_TAG, VimSdk::SOAP_NAMESPACE_MAP, VimSdk::SOAP_START, VimSdk::VERSION1, VimSdk::XMLNS_SOAPENC, VimSdk::XMLNS_SOAPENV, VimSdk::XMLNS_VMODL_BASE, VimSdk::XMLNS_XSD, VimSdk::XMLNS_XSI, VimSdk::XML_ENCODING, VimSdk::XML_HEADER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RetryBlock

#retry_block, #retry_with_timeout

Constructor Details

#initialize(options) ⇒ Cloud

Returns a new instance of Cloud.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cloud/vsphere/cloud.rb', line 37

def initialize(options)
  @config = Config.build(options)

  @logger = config.logger
  @client = config.client
  @cloud_searcher = CloudSearcher.new(@client.service_content, @logger)

  @resources = Resources.new(config)
  @file_provider = FileProvider.new(config.rest_client, config.vcenter_host)
  @agent_env = AgentEnv.new(client, @file_provider, @cloud_searcher)

  # Global lock
  @lock = Mutex.new

  # Resource locks
  @locks = {}
  @locks_mutex = Mutex.new

  # We get disconnected if the connection is inactive for a long period.
  Thread.new do
    while true do
      sleep(60)
      @client.service_instance.current_time
    end
  end

  setup_at_exit
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



35
36
37
# File 'lib/cloud/vsphere/cloud.rb', line 35

def client
  @client
end

Instance Method Details

#attach_disk(vm_cid, disk_cid) ⇒ Object



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
# File 'lib/cloud/vsphere/cloud.rb', line 402

def attach_disk(vm_cid, disk_cid)
  with_thread_name("attach_disk(#{vm_cid}, #{disk_cid})") do
    @logger.info("Attaching disk: #{disk_cid} on vm: #{vm_cid}")

    vm = get_vm_by_cid(vm_cid)

    datacenter_name = config.datacenter_name

    vm_properties = @cloud_searcher.get_properties(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
    host_info = get_vm_host_info(vm)

    devices = vm_properties['config.hardware.device']
    system_disk = devices.find { |device| device.kind_of?(Vim::Vm::Device::VirtualDisk) }

    disk = PersistentDisk.new(disk_cid, @cloud_searcher, @resources, @client, @logger)
    disk_config_spec = disk.create_spec(datacenter_name, host_info, system_disk.controller_key, @config.copy_disks)

    vm_config = Vim::Vm::ConfigSpec.new
    vm_config.device_change = []
    vm_config.device_change << disk_config_spec
    fix_device_unit_numbers(devices, vm_config.device_change)

    env = @agent_env.get_current_env(vm, datacenter_name)
    @logger.info("Reading current agent env: #{env.pretty_inspect}")
    env['disks']['persistent'][disk_cid] = disk_config_spec.device.unit_number.to_s
    @logger.info("Updating agent env to: #{env.pretty_inspect}")

    location = get_vm_location(vm, datacenter: datacenter_name)
    @agent_env.set_env(vm, location, env)
    @logger.info('Attaching disk')
    client.reconfig_vm(vm, vm_config)
    @logger.info('Finished attaching disk')
  end
end

#clone_vm(vm, name, folder, resource_pool, options = {}) ⇒ Object



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'lib/cloud/vsphere/cloud.rb', line 680

def clone_vm(vm, name, folder, resource_pool, options={})
  relocation_spec = Vim::Vm::RelocateSpec.new
  relocation_spec.datastore = options[:datastore] if options[:datastore]
  if options[:linked]
    relocation_spec.disk_move_type = Vim::Vm::RelocateSpec::DiskMoveOptions::CREATE_NEW_CHILD_DISK_BACKING
  end
  relocation_spec.pool = resource_pool

  clone_spec = Vim::Vm::CloneSpec.new
  clone_spec.config = options[:config] if options[:config]
  clone_spec.location = relocation_spec
  clone_spec.power_on = options[:power_on] ? true : false
  clone_spec.snapshot = options[:snapshot] if options[:snapshot]
  clone_spec.template = false

  vm.clone(folder, name, clone_spec)
end

#configure_networks(vm_cid, networks) ⇒ Object



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
# File 'lib/cloud/vsphere/cloud.rb', line 323

def configure_networks(vm_cid, networks)
  with_thread_name("configure_networks(#{vm_cid}, ...)") do
    vm = get_vm_by_cid(vm_cid)

    @logger.debug('Waiting for the VM to shutdown')
    begin
      begin
        vm.shutdown_guest
      rescue => e
        @logger.debug("Ignoring possible race condition when a VM has powered off by the time we ask it to shutdown: #{e.inspect}")
      end

      wait_until_off(vm, 60)
    rescue TimeoutException
      @logger.debug('The guest did not shutdown in time, requesting it to power off')
      client.power_off_vm(vm)
    end

    @logger.info("Configuring: #{vm_cid} to use the following network settings: #{networks.pretty_inspect}")
    vm = get_vm_by_cid(vm_cid)
    devices = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
    datacenter = client.find_parent(vm, Vim::Datacenter)
    datacenter_name = config.datacenter_name
    pci_controller = devices.find { |device| device.kind_of?(Vim::Vm::Device::VirtualPCIController) }

    config = Vim::Vm::ConfigSpec.new
    config.device_change = []
    nics = devices.select { |device| device.kind_of?(Vim::Vm::Device::VirtualEthernetCard) }
    nics.each do |nic|
      nic_config = create_delete_device_spec(nic)
      config.device_change << nic_config
    end

    dvs_index = {}
    networks.each_value do |network|
      v_network_name = network['cloud_properties']['name']
      network_mob = client.find_by_inventory_path([datacenter_name, 'network', v_network_name])
      nic_config = create_nic_config_spec(v_network_name, network_mob, pci_controller.key, dvs_index)
      config.device_change << nic_config
    end

    fix_device_unit_numbers(devices, config.device_change)
    @logger.debug('Reconfiguring the networks')
    @client.reconfig_vm(vm, config)

    env = @agent_env.get_current_env(vm, datacenter_name)
    @logger.debug("Reading current agent env: #{env.pretty_inspect}")

    devices = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
    env['networks'] = generate_network_env(devices, networks, dvs_index)

    @logger.debug("Updating agent env to: #{env.pretty_inspect}")
    location = get_vm_location(vm, datacenter: datacenter_name)
    @agent_env.set_env(vm, location, env)

    @logger.debug('Powering the VM back on')
    client.power_on_vm(datacenter, vm)
  end
end

#create_delete_device_spec(device, options = {}) ⇒ Object



742
743
744
745
746
747
748
749
750
# File 'lib/cloud/vsphere/cloud.rb', line 742

def create_delete_device_spec(device, options = {})
  device_config_spec = Vim::Vm::Device::VirtualDeviceSpec.new
  device_config_spec.device = device
  device_config_spec.operation = Vim::Vm::Device::VirtualDeviceSpec::Operation::REMOVE
  if options[:destroy]
    device_config_spec.file_operation = Vim::Vm::Device::VirtualDeviceSpec::FileOperation::DESTROY
  end
  device_config_spec
end

#create_disk(size, cloud_properties, _ = nil) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
# File 'lib/cloud/vsphere/cloud.rb', line 489

def create_disk(size, cloud_properties, _ = nil)
  with_thread_name("create_disk(#{size}, _)") do
    @logger.info("Creating disk with size: #{size}")
    disk = Models::Disk.new
    disk.uuid = "disk-#{generate_unique_name}"
    disk.size = size
    disk.save
    @logger.info("Created disk: #{disk.inspect}")
    disk.uuid
  end
end

#create_disk_config_spec(datastore, file_name, controller_key, space, options = {}) ⇒ Object



702
703
704
# File 'lib/cloud/vsphere/cloud.rb', line 702

def create_disk_config_spec(datastore, file_name, controller_key, space, options = {})

end

#create_nic_config_spec(v_network_name, network, controller_key, dvs_index) ⇒ Object



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/cloud/vsphere/cloud.rb', line 706

def create_nic_config_spec(v_network_name, network, controller_key, dvs_index)
  raise "Can't find network: #{v_network_name}" if network.nil?
  if network.class == Vim::Dvs::DistributedVirtualPortgroup
    portgroup_properties = @cloud_searcher.get_properties(network,
                                                 Vim::Dvs::DistributedVirtualPortgroup,
                                                 ['config.key', 'config.distributedVirtualSwitch'],
                                                 ensure_all: true)

    switch = portgroup_properties['config.distributedVirtualSwitch']
    switch_uuid = @cloud_searcher.get_property(switch, Vim::DistributedVirtualSwitch, 'uuid', ensure_all: true)

    port = Vim::Dvs::PortConnection.new
    port.switch_uuid = switch_uuid
    port.portgroup_key = portgroup_properties['config.key']

    backing_info = Vim::Vm::Device::VirtualEthernetCard::DistributedVirtualPortBackingInfo.new
    backing_info.port = port

    dvs_index[port.portgroup_key] = v_network_name
  else
    backing_info = Vim::Vm::Device::VirtualEthernetCard::NetworkBackingInfo.new
    backing_info.device_name = network.name
    backing_info.network = network
  end

  nic = Vim::Vm::Device::VirtualVmxnet3.new
  nic.key = -1
  nic.controller_key = controller_key
  nic.backing = backing_info

  device_config_spec = Vim::Vm::Device::VirtualDeviceSpec.new
  device_config_spec.device = nic
  device_config_spec.operation = Vim::Vm::Device::VirtualDeviceSpec::Operation::ADD
  device_config_spec
end

#create_stemcell(image, _) ⇒ Object



93
94
95
96
97
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
144
145
146
# File 'lib/cloud/vsphere/cloud.rb', line 93

def create_stemcell(image, _)
  with_thread_name("create_stemcell(#{image}, _)") do
    result = nil
    Dir.mktmpdir do |temp_dir|
      @logger.info("Extracting stemcell to: #{temp_dir}")
      output = `tar -C #{temp_dir} -xzf #{image} 2>&1`
      raise "Corrupt image, tar exit status: #{$?.exitstatus} output: #{output}" if $?.exitstatus != 0

      ovf_file = Dir.entries(temp_dir).find { |entry| File.extname(entry) == '.ovf' }
      raise 'Missing OVF' if ovf_file.nil?
      ovf_file = File.join(temp_dir, ovf_file)

      name = "sc-#{generate_unique_name}"
      @logger.info("Generated name: #{name}")

      stemcell_size = File.size(image) / (1024 * 1024)
      cluster, datastore = @resources.place(0, stemcell_size, [])
      @logger.info("Deploying to: #{cluster.mob} / #{datastore.mob}")

      import_spec_result = import_ovf(name, ovf_file, cluster.resource_pool.mob, datastore.mob)

      lease_obtainer = LeaseObtainer.new(@cloud_searcher, @logger)
      nfc_lease = lease_obtainer.obtain(
        cluster.resource_pool,
        import_spec_result.import_spec,
        cluster.datacenter.template_folder,
      )

      @logger.info('Uploading')
      vm = upload_ovf(ovf_file, nfc_lease, import_spec_result.file_item)
      result = name

      @logger.info('Removing NICs')
      devices = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
      config = Vim::Vm::ConfigSpec.new
      config.device_change = []

      nics = devices.select { |device| device.kind_of?(Vim::Vm::Device::VirtualEthernetCard) }
      nics.each do |nic|
        nic_config = create_delete_device_spec(nic)
        config.device_change << nic_config
      end
      client.reconfig_vm(vm, config)

      @logger.info('Taking initial snapshot')

      # Despite the naming, this has nothing to do with the Cloud notion of a disk snapshot
      # (which comes from AWS). This is a vm snapshot.
      task = vm.create_snapshot('initial', nil, false, false)
      client.wait_for_task(task)
    end
    result
  end
end

#create_vm(agent_id, stemcell, cloud_properties, networks, disk_locality = nil, environment = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cloud/vsphere/cloud.rb', line 191

def create_vm(agent_id, stemcell, cloud_properties, networks, disk_locality = nil, environment = nil)
  with_thread_name("create_vm(#{agent_id}, ...)") do
    VmCreatorBuilder.new.build(
      choose_placer(cloud_properties),
      cloud_properties,
      @client,
      @cloud_searcher,
      @logger,
      self,
      @agent_env,
      @file_provider
    ).create(agent_id, stemcell, networks, disk_locality, environment)
  end
end

#delete_disk(disk_cid) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/cloud/vsphere/cloud.rb', line 501

def delete_disk(disk_cid)
  with_thread_name("delete_disk(#{disk_cid})") do
    @logger.info("Deleting disk: #{disk_cid}")
    disk = Models::Disk.first(uuid: disk_cid)
    if disk
      unless has_disk?(disk_cid)
        raise Bosh::Clouds::DiskNotFound.new(true), "disk #{disk_cid} not found"
      end

      datacenter = client.find_by_inventory_path(disk.datacenter)
      if datacenter.nil?
        raise Bosh::Clouds::DiskNotFound.new(true), "datacenter for disk #{disk_cid} not found"
      end

      client.delete_disk(datacenter, disk.path) if disk.path

      disk.destroy
      @logger.info('Finished deleting disk')
    else
      raise "Could not find disk: #{disk_cid}"
    end
  end
end

#delete_stemcell(stemcell) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cloud/vsphere/cloud.rb', line 148

def delete_stemcell(stemcell)
  with_thread_name("delete_stemcell(#{stemcell})") do
    Bosh::ThreadPool.new(max_threads: 32, logger: @logger).wrap do |pool|
      @resources.datacenters.each_value do |datacenter|
        @logger.info("Looking for stemcell replicas in: #{datacenter.name}")
        templates = @cloud_searcher.get_property(datacenter.template_folder.mob, Vim::Folder, 'childEntity', ensure_all: true)
        template_properties = @cloud_searcher.get_properties(templates, Vim::VirtualMachine, ['name'])
        template_properties.each_value do |properties|
          template_name = properties['name'].gsub('%2f', '/')
          if template_name.split('/').first.strip == stemcell
            @logger.info("Found: #{template_name}")
            pool.process do
              @logger.info("Deleting: #{template_name}")
              client.delete_vm(properties[:obj])
              @logger.info("Deleted: #{template_name}")
            end
          end
        end
      end
    end
  end
end

#delete_vm(vm_cid) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cloud/vsphere/cloud.rb', line 206

def delete_vm(vm_cid)
  with_thread_name("delete_vm(#{vm_cid})") do
    @logger.info("Deleting vm: #{vm_cid}")

    vm = get_vm_by_cid(vm_cid)
    datacenter = client.find_parent(vm, Vim::Datacenter)
    properties =
      @cloud_searcher.get_properties(
        vm,
        Vim::VirtualMachine,
        ['runtime.powerState', 'runtime.question', 'config.hardware.device', 'name'],
        ensure: ['config.hardware.device']
      )

    retry_block do
      question = properties['runtime.question']
      if question
        choices = question.choice
        @logger.info("VM is blocked on a question: #{question.text}, " +
                       "providing default answer: #{choices.choice_info[choices.default_index].label}")
        client.answer_vm(vm, question.id, choices.choice_info[choices.default_index].key)
        power_state = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'runtime.powerState')
      else
        power_state = properties['runtime.powerState']
      end

      if power_state != Vim::VirtualMachine::PowerState::POWERED_OFF
        @logger.info("Powering off vm: #{vm_cid}")
        client.power_off_vm(vm)
      end
    end

    # Detach any persistent disks in case they were not detached from the instance
    devices = properties['config.hardware.device']
    persistent_disks = devices.select { |device| device.kind_of?(Vim::Vm::Device::VirtualDisk) &&
      device.backing.disk_mode == Vim::Vm::Device::VirtualDiskOption::DiskMode::INDEPENDENT_PERSISTENT }

    unless persistent_disks.empty?
      @logger.info("Found #{persistent_disks.size} persistent disk(s)")
      config = Vim::Vm::ConfigSpec.new
      config.device_change = []
      persistent_disks.each do |virtual_disk|
        @logger.info("Detaching: #{virtual_disk.backing.file_name}")
        config.device_change << create_delete_device_spec(virtual_disk)
      end
      retry_block { client.reconfig_vm(vm, config) }
      @logger.info("Detached #{persistent_disks.size} persistent disk(s)")
    end

    # Delete env.iso and VM specific files managed by the director
    retry_block do
      cdrom = devices.find { |device| device.kind_of?(Vim::Vm::Device::VirtualCdrom) }
      @agent_env.clean_env(vm) if cdrom
    end

    retry_block { client.delete_vm(vm) }
    @logger.info("Deleted vm: #{vm_cid}")
  end
end

#detach_disk(vm_cid, disk_cid) ⇒ Object



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
# File 'lib/cloud/vsphere/cloud.rb', line 437

def detach_disk(vm_cid, disk_cid)
  with_thread_name("detach_disk(#{vm_cid}, #{disk_cid})") do
    @logger.info("Detaching disk: #{disk_cid} from vm: #{vm_cid}")
    disk = Models::Disk.first(uuid: disk_cid)
    raise "Disk not found: #{disk_cid}" if disk.nil?

    vm = get_vm_by_cid(vm_cid)

    location = get_vm_location(vm)
    env = @agent_env.get_current_env(vm, location[:datacenter])
    @logger.info("Reading current agent env: #{env.pretty_inspect}")
    if env['disks']['persistent'][disk.uuid]
      env['disks']['persistent'].delete(disk.uuid)
      @logger.info("Updating agent env to: #{env.pretty_inspect}")

      @agent_env.set_env(vm, location, env)
    end

    devices = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
    virtual_disk =
      devices.find do |device|
        device.kind_of?(Vim::Vm::Device::VirtualDisk) && device.backing.file_name.end_with?("/#{disk_cid}.vmdk")
      end
    raise Bosh::Clouds::DiskNotAttached.new(true), "Disk (#{disk_cid}) is not attached to VM (#{vm_cid})" if virtual_disk.nil?

    config = Vim::Vm::ConfigSpec.new
    config.device_change = []
    config.device_change << create_delete_device_spec(virtual_disk)

    @logger.info('Detaching disk')
    client.reconfig_vm(vm, config)

    # detach-disk is async and task completion does not necessarily mean
    # that changes have been applied to VC side. Query VC until we confirm
    # that the change has been applied. This is a known issue for vsphere 4.
    # Fixed in vsphere 5.
    5.times do
      devices = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
      virtual_disk =
        devices.find do |device|
          device.kind_of?(Vim::Vm::Device::VirtualDisk) &&
            device.backing.file_name.end_with?("/#{disk_cid}.vmdk")
        end
      break if virtual_disk.nil?
      sleep(1.0)
    end
    raise "Failed to detach disk: #{disk_cid} from vm: #{vm_cid}" unless virtual_disk.nil?

    @logger.info('Finished detaching disk')
  end
end

#disk_spec(persistent_disks) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cloud/vsphere/cloud.rb', line 171

def disk_spec(persistent_disks)
  disks = []
  if persistent_disks
    persistent_disks.each do |disk_cid|
      disk = Models::Disk.first(uuid: disk_cid)
      disks << {
        size: disk.size,
        dc_name: disk.datacenter,
        ds_name: disk.datastore
      }
    end
  end
  disks
end

#fix_device_unit_numbers(devices, device_changes) ⇒ Object



752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/cloud/vsphere/cloud.rb', line 752

def fix_device_unit_numbers(devices, device_changes)
  controllers_available_unit_numbers = Hash.new { |h,k| h[k] = (0..15).to_a }
  devices.each do |device|
    if device.controller_key
      available_unit_numbers = controllers_available_unit_numbers[device.controller_key]
      available_unit_numbers.delete(device.unit_number)
    end
  end

  device_changes.each do |device_change|
    device = device_change.device
    if device.controller_key && device.unit_number.nil?
      available_unit_numbers = controllers_available_unit_numbers[device.controller_key]
      raise "No available unit numbers for device: #{device.inspect}" if available_unit_numbers.empty?
      device.unit_number = available_unit_numbers.shift
    end
  end
end

#generate_agent_env(name, vm, agent_id, networking_env, disk_env) ⇒ Object



625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/cloud/vsphere/cloud.rb', line 625

def generate_agent_env(name, vm, agent_id, networking_env, disk_env)
  vm_env = {
    'name' => name,
    'id' => vm.__mo_id__
  }

  env = {}
  env['vm'] = vm_env
  env['agent_id'] = agent_id
  env['networks'] = networking_env
  env['disks'] = disk_env
  env.merge!(config.agent)
  env
end

#generate_disk_env(system_disk, ephemeral_disk) ⇒ Object



617
618
619
620
621
622
623
# File 'lib/cloud/vsphere/cloud.rb', line 617

def generate_disk_env(system_disk, ephemeral_disk)
  {
    'system' => system_disk.unit_number.to_s,
    'ephemeral' => ephemeral_disk.unit_number.to_s,
    'persistent' => {}
  }
end

#generate_network_env(devices, networks, dvs_index) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/cloud/vsphere/cloud.rb', line 589

def generate_network_env(devices, networks, dvs_index)
  nics = {}

  devices.each do |device|
    if device.kind_of?(Vim::Vm::Device::VirtualEthernetCard)
      backing = device.backing
      if backing.kind_of?(Vim::Vm::Device::VirtualEthernetCard::DistributedVirtualPortBackingInfo)
        v_network_name = dvs_index[backing.port.portgroup_key]
      else
        v_network_name = PathFinder.new.path(backing.network)
      end
      allocated_networks = nics[v_network_name] || []
      allocated_networks << device
      nics[v_network_name] = allocated_networks
    end
  end

  network_env = {}
  networks.each do |network_name, network|
    network_entry = network.dup
    v_network_name = network['cloud_properties']['name']
    nic = nics[v_network_name].pop
    network_entry['mac'] = nic.mac_address
    network_env[network_name] = network_entry
  end
  network_env
end

#generate_unique_nameObject



698
699
700
# File 'lib/cloud/vsphere/cloud.rb', line 698

def generate_unique_name
  SecureRandom.uuid
end

#get_primary_datastore(devices) ⇒ Object



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/cloud/vsphere/cloud.rb', line 664

def get_primary_datastore(devices)
  ephemeral_disks = devices.select { |device| device.kind_of?(Vim::Vm::Device::VirtualDisk) &&
    device.backing.disk_mode != Vim::Vm::Device::VirtualDiskOption::DiskMode::INDEPENDENT_PERSISTENT }

  datastore = nil
  ephemeral_disks.each do |disk|
    if datastore
      raise 'Ephemeral disks should all be on the same datastore.' unless datastore.eql?(disk.backing.datastore)
    else
      datastore = disk.backing.datastore
    end
  end

  datastore
end

#get_vm_by_cid(vm_cid) ⇒ Object

Raises:

  • (Bosh::Clouds::VMNotFound)


525
526
527
528
529
530
531
# File 'lib/cloud/vsphere/cloud.rb', line 525

def get_vm_by_cid(vm_cid)
  @resources.datacenters.each_value do |datacenter|
    vm = client.find_by_inventory_path([datacenter.name, 'vm', datacenter.vm_folder.path_components, vm_cid])
    return vm unless vm.nil?
  end
  raise Bosh::Clouds::VMNotFound, "VM `#{vm_cid}' not found"
end

#get_vm_host_info(vm_ref) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/cloud/vsphere/cloud.rb', line 383

def get_vm_host_info(vm_ref)
  vm_properties = @cloud_searcher.get_properties(vm_ref, Vim::VirtualMachine, 'runtime')
  vm_runtime = vm_properties['runtime']

  properties = @cloud_searcher.get_properties(vm_runtime.host, Vim::HostSystem, ['datastore', 'parent'], ensure_all: true)

  # Get the cluster that the vm's host belongs to.
  cluster = @cloud_searcher.get_properties(properties['parent'], Vim::ClusterComputeResource, 'name')

  # Get the datastores that are accessible to the vm's host.
  datastores_accessible = []
  properties['datastore'].each do |store|
    ds = @cloud_searcher.get_properties(store, Vim::Datastore, 'info', ensure_all: true)
    datastores_accessible << ds['info'].name
  end

  { 'cluster' => cluster['name'], 'datastores' => datastores_accessible }
end

#get_vm_location(vm, options = {}) ⇒ Object



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/cloud/vsphere/cloud.rb', line 640

def get_vm_location(vm, options = {})
  datacenter_name = options[:datacenter]
  datastore_name = options[:datastore]
  vm_name = options[:vm]

  unless datacenter_name
    datacenter_name = config.datacenter_name
  end

  if vm_name.nil? || datastore_name.nil?
    vm_properties =
      @cloud_searcher.get_properties(vm, Vim::VirtualMachine, ['config.hardware.device', 'name'], ensure_all: true)
    vm_name = vm_properties['name']

    unless datastore_name
      devices = vm_properties['config.hardware.device']
      datastore = get_primary_datastore(devices)
      datastore_name = @cloud_searcher.get_property(datastore, Vim::Datastore, 'name')
    end
  end

  { datacenter: datacenter_name, datastore: datastore_name, vm: vm_name }
end

#get_vmsObject



847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'lib/cloud/vsphere/cloud.rb', line 847

def get_vms
  subfolders = []
  with_thread_name("get_vms") do
    @resources.datacenters.each_value do |datacenter|
      @logger.info("Looking for VMs in: #{datacenter.name} - #{datacenter.master_vm_folder.path}")
      subfolders += datacenter.master_vm_folder.mob.child_entity
      @logger.info("Looking for Stemcells in: #{datacenter.name} - #{datacenter.master_template_folder.path}")
      subfolders += datacenter.master_template_folder.mob.child_entity
    end
  end

  subfolders.map { |folder| folder.child_entity }.flatten
end

#has_disk?(disk_id) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cloud/vsphere/cloud.rb', line 78

def has_disk?(disk_id)
  disk = VSphereCloud::Models::Disk.find(uuid: disk_id)

  return false unless disk
  # Disk path is only being set when disk is created in vSphere.
  # If the path is not set it means that disk was only created in
  # CPI database and attach disk was not called or failed.
  # We consider that disk is missing only if CPI desired state
  # is to be present but it actually missing in infrastructure.
  return true unless disk.path
  return false unless disk.datacenter

  @client.has_disk?(disk.path, disk.datacenter)
end

#has_vm?(vm_cid) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/cloud/vsphere/cloud.rb', line 71

def has_vm?(vm_cid)
  get_vm_by_cid(vm_cid)
  true
rescue Bosh::Clouds::VMNotFound
  false
end

#import_ovf(name, ovf, resource_pool, datastore) ⇒ Object



771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/cloud/vsphere/cloud.rb', line 771

def import_ovf(name, ovf, resource_pool, datastore)
  import_spec_params = Vim::OvfManager::CreateImportSpecParams.new
  import_spec_params.entity_name = name
  import_spec_params.locale = 'US'
  import_spec_params.deployment_option = ''

  ovf_file = File.open(ovf)
  ovf_descriptor = ovf_file.read
  ovf_file.close

  @client.service_content.ovf_manager.create_import_spec(ovf_descriptor,
                                                         resource_pool,
                                                         datastore,
                                                         import_spec_params)
end

#obtain_nfc_lease(resource_pool, import_spec, folder) ⇒ Object



787
788
789
# File 'lib/cloud/vsphere/cloud.rb', line 787

def obtain_nfc_lease(resource_pool, import_spec, folder)
  resource_pool.import_vapp(import_spec, folder, nil)
end

#pingObject



861
862
863
# File 'lib/cloud/vsphere/cloud.rb', line 861

def ping
  "pong"
end

#reboot_vm(vm_cid) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/cloud/vsphere/cloud.rb', line 266

def reboot_vm(vm_cid)
  with_thread_name("reboot_vm(#{vm_cid})") do
    vm = get_vm_by_cid(vm_cid)
    datacenter = client.find_parent(vm, Vim::Datacenter)
    power_state = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'runtime.powerState')

    @logger.info("Reboot vm = #{vm_cid}")
    if power_state != Vim::VirtualMachine::PowerState::POWERED_ON
      @logger.info("VM not in POWERED_ON state. Current state : #{power_state}")
    end
    begin
      vm.reboot_guest
    rescue => e
      @logger.error("Soft reboot failed #{e} -#{e.backtrace.join("\n")}")
      @logger.info('Try hard reboot')
      # if we fail to perform a soft-reboot we force a hard-reboot
      if power_state == Vim::VirtualMachine::PowerState::POWERED_ON
        retry_block { client.power_off_vm(vm) }
      end
      retry_block { client.power_on_vm(datacenter, vm) }
    end
  end
end

#replicate_stemcell(cluster, datastore, stemcell) ⇒ Object



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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/cloud/vsphere/cloud.rb', line 533

def replicate_stemcell(cluster, datastore, stemcell)
  stemcell_vm = client.find_by_inventory_path([cluster.datacenter.name, 'vm',
                                               cluster.datacenter.template_folder.path_components, stemcell])
  raise "Could not find stemcell: #{stemcell}" if stemcell_vm.nil?
  stemcell_datastore = @cloud_searcher.get_property(stemcell_vm, Vim::VirtualMachine, 'datastore', ensure_all: true)

  if stemcell_datastore != datastore.mob
    @logger.info("Stemcell lives on a different datastore, looking for a local copy of: #{stemcell}.")
    local_stemcell_name = "#{stemcell} %2f #{datastore.mob.__mo_id__}"
    local_stemcell_path =
      [cluster.datacenter.name, 'vm', cluster.datacenter.template_folder.path_components, local_stemcell_name]
    replicated_stemcell_vm = client.find_by_inventory_path(local_stemcell_path)

    if replicated_stemcell_vm.nil?
      @logger.info("Cluster doesn't have stemcell #{stemcell}, replicating")
      lock = nil
      @locks_mutex.synchronize do
        lock = @locks[local_stemcell_name]
        if lock.nil?
          lock = @locks[local_stemcell_name] = Mutex.new
        end
      end

      lock.synchronize do
        replicated_stemcell_vm = client.find_by_inventory_path(local_stemcell_path)
        if replicated_stemcell_vm.nil?
          @logger.info("Replicating #{stemcell} (#{stemcell_vm}) to #{local_stemcell_name}")
          task = clone_vm(stemcell_vm,
                          local_stemcell_name,
                          cluster.datacenter.template_folder.mob,
                          cluster.resource_pool.mob,
                          datastore: datastore.mob)
          replicated_stemcell_vm = client.wait_for_task(task)
          @logger.info("Replicated #{stemcell} (#{stemcell_vm}) to #{local_stemcell_name} (#{replicated_stemcell_vm})")
          @logger.info("Creating initial snapshot for linked clones on #{replicated_stemcell_vm}")
          # Despite the naming, this has nothing to do with the Cloud notion of a disk snapshot
          # (which comes from AWS). This is a vm snapshot.
          task = replicated_stemcell_vm.create_snapshot('initial', nil, false, false)
          client.wait_for_task(task)
          @logger.info("Created initial snapshot for linked clones on #{replicated_stemcell_vm}")
        end
      end
    else
      @logger.info("Found local stemcell replica: #{replicated_stemcell_vm}")
    end
    result = replicated_stemcell_vm
  else
    @logger.info("Stemcell was already local: #{stemcell_vm}")
    result = stemcell_vm
  end

  @logger.info("Using stemcell VM: #{result}")

  result
end

#set_vm_metadata(vm_cid, metadata) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/cloud/vsphere/cloud.rb', line 290

def (vm_cid, )
  with_thread_name("set_vm_metadata(#{vm_cid}, ...)") do
    begin
      fields_manager = client.service_content.custom_fields_manager
      custom_fields = fields_manager.field
      name_to_key_id = {}

      .each_key do |name|
        field = custom_fields.find { |field| field.name == name.to_s &&
          field.managed_object_type == Vim::VirtualMachine }
        unless field
          field = fields_manager.add_field_definition(name.to_s, Vim::VirtualMachine, nil, nil)
        end
        name_to_key_id[name] = field.key
      end

      vm = get_vm_by_cid(vm_cid)

      .each do |name, value|
        value = '' if value.nil? # value is required
        fields_manager.set_field(vm, name_to_key_id[name], value)
      end
    rescue SoapError => e
      if e.fault.kind_of?(Vim::Fault::NoPermission)
        @logger.warn("Can't set custom fields due to lack of " +
                       "permission: #{e.message}")
      else
        raise e
      end
    end
  end
end

#setup_at_exitObject



66
67
68
69
# File 'lib/cloud/vsphere/cloud.rb', line 66

def setup_at_exit
  # HACK: finalizer not getting called, so we'll rely on at_exit
  at_exit { @client.logout }
end

#stemcell_vm(name) ⇒ Object



186
187
188
189
# File 'lib/cloud/vsphere/cloud.rb', line 186

def stemcell_vm(name)
  dc = @resources.datacenters.values.first
  client.find_by_inventory_path([dc.name, 'vm', dc.template_folder.path_components, name])
end

#upload_ovf(ovf, lease, file_items) ⇒ Object



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/cloud/vsphere/cloud.rb', line 799

def upload_ovf(ovf, lease, file_items)
  info = @cloud_searcher.get_property(lease, Vim::HttpNfcLease, 'info', ensure_all: true)
  lease_updater = LeaseUpdater.new(client, lease)

  info.device_url.each do |device_url|
    device_key = device_url.import_key
    file_items.each do |file_item|
      if device_key == file_item.device_id
        http_client = HTTPClient.new
        http_client.send_timeout = 14400 # 4 hours
        http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE

        disk_file_path = File.join(File.dirname(ovf), file_item.path)
        disk_file = File.open(disk_file_path)
        disk_file_size = File.size(disk_file_path)

        progress_thread = Thread.new do
          loop do
            lease_updater.progress = disk_file.pos * 100 / disk_file_size
            sleep(2)
          end
        end

        @logger.info("Uploading disk to: #{device_url.url}")

        http_client.post(device_url.url,
                         disk_file,
                         { 'Content-Type' => 'application/x-vnd.vmware-streamVmdk', 'Content-Length' => disk_file_size })

        progress_thread.kill
        disk_file.close
      end
    end
  end
  lease_updater.finish
  info.entity
end

#wait_for_nfc_lease(lease) ⇒ Object



791
792
793
794
795
796
797
# File 'lib/cloud/vsphere/cloud.rb', line 791

def wait_for_nfc_lease(lease)
  loop do
    state = @cloud_searcher.get_property(lease, Vim::HttpNfcLease, 'state')
    return state unless state == Vim::HttpNfcLease::State::INITIALIZING
    sleep(1.0)
  end
end

#wait_until_off(vm, timeout) ⇒ Object



837
838
839
840
841
842
843
844
845
# File 'lib/cloud/vsphere/cloud.rb', line 837

def wait_until_off(vm, timeout)
  started = Time.now
  loop do
    power_state = @cloud_searcher.get_property(vm, Vim::VirtualMachine, 'runtime.powerState')
    break if power_state == Vim::VirtualMachine::PowerState::POWERED_OFF
    raise TimeoutException if Time.now - started > timeout
    sleep(1.0)
  end
end