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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cloud/vsphere/cloud.rb', line 14

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

  @logger = config.logger
  @client = config.client
  @cloud_searcher = CloudSearcher.new(@client.service_content, @logger)
  @datacenter = Resources::Datacenter.new({
    client: @client,
    use_sub_folder: @config.datacenter_use_sub_folder,
    vm_folder: @config.datacenter_vm_folder,
    template_folder: @config.datacenter_template_folder,
    name: @config.datacenter_name,
    disk_path: @config.datacenter_disk_path,
    ephemeral_pattern: @config.datacenter_datastore_pattern,
    persistent_pattern: @config.datacenter_persistent_datastore_pattern,
    clusters: @config.datacenter_clusters,
    logger: @config.logger,
    mem_overcommit: @config.mem_overcommit
  })

  @resources = Resources.new(@datacenter, 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.



12
13
14
# File 'lib/cloud/vsphere/cloud.rb', line 12

def client
  @client
end

Instance Method Details

#attach_disk(vm_cid, disk_cid) ⇒ Object



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

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 = vm_provider.find(vm_cid)

    cluster = @datacenter.clusters[vm.cluster]
    disk = disk_provider.find_and_move(disk_cid, cluster, @datacenter.name, vm.accessible_datastores)
    disk_config_spec = disk.attach_spec(vm.system_disk.controller_key)

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

    env = @agent_env.get_current_env(vm.mob, @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.mob, datacenter: @datacenter.name)
    @agent_env.set_env(vm.mob, location, env)
    @logger.info('Attaching disk')
    client.reconfig_vm(vm.mob, vm_config)
    @logger.info('Finished attaching disk')
  end
end

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



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/cloud/vsphere/cloud.rb', line 535

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



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

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

    @logger.info("Configuring: #{vm_cid} to use the following network settings: #{networks.pretty_inspect}")
    config = Vim::Vm::ConfigSpec.new
    config.device_change = []
    vm.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, vm.pci_controller.key, dvs_index)
      config.device_change << nic_config
    end

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

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

    devices = @cloud_searcher.get_property(vm.mob, 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.mob, datacenter: @datacenter.name)
    @agent_env.set_env(vm.mob, location, env)

    @logger.debug('Powering the VM back on')
    vm.power_on
  end
end

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



589
590
591
592
593
594
595
596
597
# File 'lib/cloud/vsphere/cloud.rb', line 589

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_in_mb, cloud_properties, _ = nil) ⇒ Object



369
370
371
372
373
374
375
376
# File 'lib/cloud/vsphere/cloud.rb', line 369

def create_disk(size_in_mb, cloud_properties, _ = nil)
  with_thread_name("create_disk(#{size_in_mb}, _)") do
    @logger.info("Creating disk with size: #{size_in_mb}")
    disk = disk_provider.create(size_in_mb)
    @logger.info("Created disk: #{disk.inspect}")
    disk.cid
  end
end

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



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 553

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
# File 'lib/cloud/vsphere/cloud.rb', line 75

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-#{SecureRandom.uuid}"
      @logger.info("Generated name: #{name}")

      stemcell_size = File.size(image) / (1024 * 1024)
      cluster = @resources.pick_cluster_for_vm(0, stemcell_size, [])
      datastore = @resources.pick_ephemeral_datastore(cluster, 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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cloud/vsphere/cloud.rb', line 156

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,
      disk_provider
    ).create(agent_id, stemcell, networks, disk_locality, environment)
  end
end

#delete_disk(disk_cid) ⇒ Object



378
379
380
381
382
383
384
385
386
# File 'lib/cloud/vsphere/cloud.rb', line 378

def delete_disk(disk_cid)
  with_thread_name("delete_disk(#{disk_cid})") do
    @logger.info("Deleting disk: #{disk_cid}")
    disk = disk_provider.find(disk_cid)
    client.delete_disk(@datacenter.mob, disk.path)

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

#delete_stemcell(stemcell) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cloud/vsphere/cloud.rb', line 131

def delete_stemcell(stemcell)
  with_thread_name("delete_stemcell(#{stemcell})") do
    Bosh::ThreadPool.new(max_threads: 32, logger: @logger).wrap do |pool|
      @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

#delete_vm(vm_cid) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cloud/vsphere/cloud.rb', line 172

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

    vm = vm_provider.find(vm_cid)
    vm.power_off

    persistent_disks = vm.persistent_disks
    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.mob, config) }
      @logger.info("Detached #{persistent_disks.size} persistent disk(s)")
    end

    # Delete env.iso and VM specific files managed by the director
    retry_block { @agent_env.clean_env(vm.mob) } if vm.cdrom

    vm.delete
    @logger.info("Deleted vm: #{vm_cid}")
  end
end

#detach_disk(vm_cid, disk_cid) ⇒ Object



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

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 = disk_provider.find(disk_cid)
    vm = vm_provider.find(vm_cid)
    vm_mob = vm.mob

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

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

    vm.reload
    virtual_disk = vm.disk_by_cid(disk.cid)
    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_mob, 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
      vm.reload
      virtual_disk = vm.disk_by_cid(disk.cid)
      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_providerObject



692
693
694
695
696
697
698
699
700
701
# File 'lib/cloud/vsphere/cloud.rb', line 692

def disk_provider
  DiskProvider.new(
    @client.service_content.virtual_disk_manager,
    @datacenter,
    @resources,
    @config.datacenter_disk_path,
    @client,
    @logger
  )
end

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



480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/cloud/vsphere/cloud.rb', line 480

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



472
473
474
475
476
477
478
# File 'lib/cloud/vsphere/cloud.rb', line 472

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



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

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

#get_primary_datastore(devices) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/cloud/vsphere/cloud.rb', line 519

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_location(vm, options = {}) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/cloud/vsphere/cloud.rb', line 495

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

This method is used by micro bosh deployment cleaner



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

def get_vms
  subfolders = []
  with_thread_name("get_vms") do
    @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
  mobs = subfolders.map { |folder| folder.child_entity }.flatten
  mobs.map do |mob|
    VSphereCloud::Resources::VM.new(mob.name, mob, @client, @logger)
  end
end

#has_disk?(disk_cid) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/cloud/vsphere/cloud.rb', line 68

def has_disk?(disk_cid)
  disk_provider.find(disk_cid)
  true
rescue Bosh::Clouds::DiskNotFound
  false
end

#has_vm?(vm_cid) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/cloud/vsphere/cloud.rb', line 61

def has_vm?(vm_cid)
  vm_provider.find(vm_cid)
  true
rescue Bosh::Clouds::VMNotFound
  false
end

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



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/cloud/vsphere/cloud.rb', line 599

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



615
616
617
# File 'lib/cloud/vsphere/cloud.rb', line 615

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

#pingObject



680
681
682
# File 'lib/cloud/vsphere/cloud.rb', line 680

def ping
  "pong"
end

#reboot_vm(vm_cid) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/cloud/vsphere/cloud.rb', line 200

def reboot_vm(vm_cid)
  with_thread_name("reboot_vm(#{vm_cid})") do
    vm = vm_provider.find(vm_cid)

    @logger.info("Reboot vm = #{vm_cid}")

    unless vm.powered_on?
      @logger.info("VM not in POWERED_ON state. Current state : #{vm.power_state}")
    end

    begin
      vm.reboot
    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
      retry_block { vm.power_off } if vm.powered_on?

      retry_block { vm.power_on }
    end
  end
end

#replicate_stemcell(cluster, datastore, stemcell) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
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
# File 'lib/cloud/vsphere/cloud.rb', line 388

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



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

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 = vm_provider.find(vm_cid)

      .each do |name, value|
        value = '' if value.nil? # value is required
        fields_manager.set_field(vm.mob, 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



56
57
58
59
# File 'lib/cloud/vsphere/cloud.rb', line 56

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



152
153
154
# File 'lib/cloud/vsphere/cloud.rb', line 152

def stemcell_vm(name)
  client.find_by_inventory_path([@datacenter.name, 'vm', @datacenter.template_folder.path_components, name])
end

#upload_ovf(ovf, lease, file_items) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/cloud/vsphere/cloud.rb', line 627

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

#vm_providerObject



684
685
686
687
688
689
690
# File 'lib/cloud/vsphere/cloud.rb', line 684

def vm_provider
  VMProvider.new(
    @datacenter,
    @client,
    @logger
  )
end

#wait_for_nfc_lease(lease) ⇒ Object



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

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