bosh_vsphere_cpi 1.2801.0 → 1.2807.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -129,6 +129,10 @@ module VSphereCloud
129
129
  tasks.each { |task| wait_for_task(task) }
130
130
  end
131
131
 
132
+ def create_datastore_folder(folder_path, datacenter)
133
+ @service_content.file_manager.make_directory(folder_path, datacenter, true)
134
+ end
135
+
132
136
  def create_folder(name)
133
137
  @service_content.root_folder.create_folder(name)
134
138
  end
@@ -14,6 +14,7 @@ require 'cloud/vsphere/resources/cluster'
14
14
  require 'cloud/vsphere/resources/datacenter'
15
15
  require 'cloud/vsphere/resources/datastore'
16
16
  require 'cloud/vsphere/resources/folder'
17
+ require 'cloud/vsphere/resources/disk/persistent_disk'
17
18
  require 'cloud/vsphere/resources/resource_pool'
18
19
  require 'cloud/vsphere/resources/scorer'
19
20
  require 'cloud/vsphere/resources/util'
@@ -380,8 +381,8 @@ module VSphereCloud
380
381
  end
381
382
 
382
383
  def get_vm_host_info(vm_ref)
383
- vm = @cloud_searcher.get_properties(vm_ref, Vim::VirtualMachine, 'runtime')
384
- vm_runtime = vm['runtime']
384
+ vm_properties = @cloud_searcher.get_properties(vm_ref, Vim::VirtualMachine, 'runtime')
385
+ vm_runtime = vm_properties['runtime']
385
386
 
386
387
  properties = @cloud_searcher.get_properties(vm_runtime.host, Vim::HostSystem, ['datastore', 'parent'], ensure_all: true)
387
388
 
@@ -398,112 +399,37 @@ module VSphereCloud
398
399
  { 'cluster' => cluster['name'], 'datastores' => datastores_accessible }
399
400
  end
400
401
 
401
- def find_persistent_datastore(datacenter_name, host_info, disk_size)
402
- # Find datastore
403
- datastore = @resources.place_persistent_datastore(datacenter_name, host_info['cluster'], disk_size)
404
-
405
- if datastore.nil?
406
- raise Bosh::Clouds::NoDiskSpace.new(true), "Not enough persistent space on cluster #{host_info['cluster']}, #{disk_size}"
407
- end
408
-
409
- # Sanity check, verify that the vm's host can access this datastore
410
- unless host_info['datastores'].include?(datastore.name)
411
- raise "Datastore not accessible to host, #{datastore.name}, #{host_info['datastores']}"
412
- end
413
- datastore
414
- end
415
-
416
402
  def attach_disk(vm_cid, disk_cid)
417
403
  with_thread_name("attach_disk(#{vm_cid}, #{disk_cid})") do
418
404
  @logger.info("Attaching disk: #{disk_cid} on vm: #{vm_cid}")
419
- disk = Models::Disk.first(uuid: disk_cid)
420
- raise "Disk not found: #{disk_cid}" if disk.nil?
421
405
 
422
406
  vm = get_vm_by_cid(vm_cid)
423
407
 
424
- datacenter = client.find_parent(vm, Vim::Datacenter)
425
408
  datacenter_name = config.datacenter_name
426
409
 
427
410
  vm_properties = @cloud_searcher.get_properties(vm, Vim::VirtualMachine, 'config.hardware.device', ensure_all: true)
428
411
  host_info = get_vm_host_info(vm)
429
412
 
430
- create_disk = false
431
- if disk.path
432
-
433
- disk_in_correct_datacenter =
434
- (disk.datacenter == datacenter_name &&
435
- @resources.validate_persistent_datastore(datacenter_name, disk.datastore) &&
436
- host_info['datastores'].include?(disk.datastore))
437
-
438
- if disk_in_correct_datacenter
439
- @logger.info("Disk already in the right datastore #{datacenter_name} #{disk.datastore}")
440
- persistent_datastore =
441
- @resources.persistent_datastore(datacenter_name, host_info['cluster'], disk.datastore)
442
- @logger.debug("Datastore: #{persistent_datastore}")
443
- else
444
- @logger.info("Disk needs to move from #{datacenter_name} #{disk.datastore}")
445
- # Find the destination datastore
446
- persistent_datastore = find_persistent_datastore(datacenter_name, host_info, disk.size)
447
-
448
- # Need to move disk to right datastore
449
- source_datacenter = client.find_by_inventory_path(disk.datacenter)
450
- source_path = disk.path
451
- datacenter_disk_path = @resources.datacenters[disk.datacenter].disk_path
452
-
453
- destination_path = "[#{persistent_datastore.name}] #{datacenter_disk_path}/#{disk.uuid}"
454
- @logger.info("Moving #{disk.datacenter}/#{source_path} to #{datacenter_name}/#{destination_path}")
455
-
456
- if config.copy_disks
457
- client.copy_disk(source_datacenter, source_path, datacenter, destination_path)
458
- @logger.info('Copied disk successfully')
459
- else
460
- client.move_disk(source_datacenter, source_path, datacenter, destination_path)
461
- @logger.info('Moved disk successfully')
462
- end
463
-
464
- disk.datacenter = datacenter_name
465
- disk.datastore = persistent_datastore.name
466
- disk.path = destination_path
467
- disk.save
468
- end
469
- else
470
- @logger.info('Need to create disk')
471
-
472
- # Find the destination datastore
473
- persistent_datastore = find_persistent_datastore(datacenter_name, host_info, disk.size)
474
-
475
- # Need to create disk
476
- disk.datacenter = datacenter_name
477
- disk.datastore = persistent_datastore.name
478
- datacenter_disk_path = @resources.datacenters[disk.datacenter].disk_path
479
- disk.path = "[#{disk.datastore}] #{datacenter_disk_path}/#{disk.uuid}"
480
- disk.save
481
- create_disk = true
482
- end
483
-
484
413
  devices = vm_properties['config.hardware.device']
485
414
  system_disk = devices.find { |device| device.kind_of?(Vim::Vm::Device::VirtualDisk) }
486
415
 
487
- vmdk_path = "#{disk.path}.vmdk"
488
- attached_disk_config = create_disk_config_spec(persistent_datastore.mob,
489
- vmdk_path,
490
- system_disk.controller_key,
491
- disk.size.to_i,
492
- create: create_disk, independent: true)
493
- config = Vim::Vm::ConfigSpec.new
494
- config.device_change = []
495
- config.device_change << attached_disk_config
496
- fix_device_unit_numbers(devices, config.device_change)
416
+ disk = PersistentDisk.new(disk_cid, @cloud_searcher, @resources, @client, @logger)
417
+ disk_config_spec = disk.create_spec(datacenter_name, host_info, system_disk.controller_key, @config.copy_disks)
418
+
419
+ vm_config = Vim::Vm::ConfigSpec.new
420
+ vm_config.device_change = []
421
+ vm_config.device_change << disk_config_spec
422
+ fix_device_unit_numbers(devices, vm_config.device_change)
497
423
 
498
424
  env = @agent_env.get_current_env(vm, datacenter_name)
499
425
  @logger.info("Reading current agent env: #{env.pretty_inspect}")
500
- env['disks']['persistent'][disk.uuid] = attached_disk_config.device.unit_number.to_s
426
+ env['disks']['persistent'][disk_cid] = disk_config_spec.device.unit_number.to_s
501
427
  @logger.info("Updating agent env to: #{env.pretty_inspect}")
502
428
 
503
429
  location = get_vm_location(vm, datacenter: datacenter_name)
504
430
  @agent_env.set_env(vm, location, env)
505
431
  @logger.info('Attaching disk')
506
- client.reconfig_vm(vm, config)
432
+ client.reconfig_vm(vm, vm_config)
507
433
  @logger.info('Finished attaching disk')
508
434
  end
509
435
  end
@@ -774,28 +700,7 @@ module VSphereCloud
774
700
  end
775
701
 
776
702
  def create_disk_config_spec(datastore, file_name, controller_key, space, options = {})
777
- backing_info = Vim::Vm::Device::VirtualDisk::FlatVer2BackingInfo.new
778
- backing_info.datastore = datastore
779
- if options[:independent]
780
- backing_info.disk_mode = Vim::Vm::Device::VirtualDiskOption::DiskMode::INDEPENDENT_PERSISTENT
781
- else
782
- backing_info.disk_mode = Vim::Vm::Device::VirtualDiskOption::DiskMode::PERSISTENT
783
- end
784
- backing_info.file_name = file_name
785
703
 
786
- virtual_disk = Vim::Vm::Device::VirtualDisk.new
787
- virtual_disk.key = -1
788
- virtual_disk.controller_key = controller_key
789
- virtual_disk.backing = backing_info
790
- virtual_disk.capacity_in_kb = space * 1024
791
-
792
- device_config_spec = Vim::Vm::Device::VirtualDeviceSpec.new
793
- device_config_spec.device = virtual_disk
794
- device_config_spec.operation = Vim::Vm::Device::VirtualDeviceSpec::Operation::ADD
795
- if options[:create]
796
- device_config_spec.file_operation = Vim::Vm::Device::VirtualDeviceSpec::FileOperation::CREATE
797
- end
798
- device_config_spec
799
704
  end
800
705
 
801
706
  def create_nic_config_spec(v_network_name, network, controller_key, dvs_index)
@@ -0,0 +1,35 @@
1
+ module VSphereCloud
2
+ class DiskConfig
3
+ def initialize(datastore, filename, controller_key, size)
4
+ @datastore = datastore
5
+ @filename = filename
6
+ @controller_key = controller_key
7
+ @size = size
8
+ end
9
+
10
+ def spec(options)
11
+ backing_info = VimSdk::Vim::Vm::Device::VirtualDisk::FlatVer2BackingInfo.new
12
+ backing_info.datastore = @datastore
13
+ if options[:independent]
14
+ backing_info.disk_mode = VimSdk::Vim::Vm::Device::VirtualDiskOption::DiskMode::INDEPENDENT_PERSISTENT
15
+ else
16
+ backing_info.disk_mode = VimSdk::Vim::Vm::Device::VirtualDiskOption::DiskMode::PERSISTENT
17
+ end
18
+ backing_info.file_name = @filename
19
+
20
+ virtual_disk = VimSdk::Vim::Vm::Device::VirtualDisk.new
21
+ virtual_disk.key = -1
22
+ virtual_disk.controller_key = @controller_key
23
+ virtual_disk.backing = backing_info
24
+ virtual_disk.capacity_in_kb = @size * 1024
25
+
26
+ device_config_spec = VimSdk::Vim::Vm::Device::VirtualDeviceSpec.new
27
+ device_config_spec.device = virtual_disk
28
+ device_config_spec.operation = VimSdk::Vim::Vm::Device::VirtualDeviceSpec::Operation::ADD
29
+ if options[:create]
30
+ device_config_spec.file_operation = VimSdk::Vim::Vm::Device::VirtualDeviceSpec::FileOperation::CREATE
31
+ end
32
+ device_config_spec
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'cloud/vsphere/resources/disk/disk_config'
2
+
3
+ module VSphereCloud
4
+ class EphemeralDisk
5
+ def initialize(size, folder_name, datastore)
6
+ @folder_name = folder_name
7
+ @datastore = datastore
8
+ @size = size
9
+ end
10
+
11
+ def create_spec(controller_key)
12
+ DiskConfig.new(@datastore.mob, filename, controller_key, @size).spec(create: true)
13
+ end
14
+
15
+ private
16
+
17
+ def filename
18
+ "[#{@datastore.name}] #{@folder_name}/ephemeral_disk.vmdk"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,110 @@
1
+ require 'cloud/vsphere/resources/disk/disk_config'
2
+
3
+ module VSphereCloud
4
+ class PersistentDisk
5
+ def initialize(disk_cid, cloud_searcher, resources, client, logger)
6
+ @model = Models::Disk.first(uuid: disk_cid)
7
+ raise "Disk not found: #{disk_cid}" if @model.nil?
8
+
9
+ @disk_size = @model.size.to_i
10
+
11
+ @cloud_searcher = cloud_searcher
12
+ @resources = resources
13
+ @client = client
14
+ @logger = logger
15
+ end
16
+
17
+ def create_spec(datacenter_name, host_info, controller_key, copy_disks)
18
+ need_to_create_disk = !disk_exists?
19
+
20
+ destination_datacenter = @resources.datacenters[datacenter_name]
21
+
22
+ if disk_exists?
23
+ if disk_in_correct_datacenter?(destination_datacenter.name, host_info)
24
+ @logger.info("Disk already in the right datastore #{destination_datacenter.name} #{@model.datastore}")
25
+
26
+ persistent_datastore = @resources.persistent_datastore(@model.datacenter, host_info['cluster'], @model.datastore)
27
+ else
28
+ @logger.info("Disk needs to move from #{@model.datacenter} to #{destination_datacenter.name}")
29
+
30
+ persistent_datastore = find_datastore(destination_datacenter, host_info)
31
+ move_disk(persistent_datastore, destination_datacenter, copy_disks)
32
+ update(persistent_datastore, destination_datacenter)
33
+ end
34
+ else
35
+ @logger.info('Need to create disk')
36
+
37
+ persistent_datastore = find_datastore(destination_datacenter, host_info)
38
+ create_parent_folder(persistent_datastore.name, destination_datacenter)
39
+ update(persistent_datastore, destination_datacenter)
40
+ end
41
+
42
+ DiskConfig.new(persistent_datastore, filename, controller_key, @disk_size).
43
+ spec(independent: true, create: need_to_create_disk)
44
+ end
45
+
46
+ private
47
+
48
+ def filename
49
+ "#{@model.path}.vmdk"
50
+ end
51
+
52
+ def datastore_disk_path(datastore_name, datacenter)
53
+ "[#{datastore_name}] #{datacenter.disk_path}/#{@model.uuid}"
54
+ end
55
+
56
+ def disk_exists?
57
+ !!@model.path
58
+ end
59
+
60
+ def disk_in_correct_datacenter?(destination_datacenter_name, host_info)
61
+ (@model.datacenter == destination_datacenter_name &&
62
+ @resources.validate_persistent_datastore(destination_datacenter_name, @model.datastore) &&
63
+ host_info['datastores'].include?(@model.datastore))
64
+ end
65
+
66
+ def find_datastore(datacenter, host_info)
67
+ datastore = @resources.place_persistent_datastore(datacenter.name, host_info['cluster'], @disk_size)
68
+
69
+ if datastore.nil?
70
+ raise Bosh::Clouds::NoDiskSpace.new(true), "Not enough persistent space on cluster #{host_info['cluster']}, #{@disk_size}"
71
+ end
72
+
73
+ # Sanity check, verify that the vm's host can access this datastore
74
+ unless host_info['datastores'].include?(datastore.name)
75
+ raise "Datastore not accessible to host, #{datastore.name}, #{host_info['datastores']}"
76
+ end
77
+
78
+ datastore
79
+ end
80
+
81
+ def update(new_datastore, new_datacenter)
82
+ # Need to create disk
83
+ @model.datacenter = new_datacenter.name
84
+ @model.datastore = new_datastore.name
85
+ @model.path = datastore_disk_path(new_datastore.name, new_datacenter)
86
+ @model.save
87
+ end
88
+
89
+ def move_disk(destination_datastore, destination_datacenter, copy_disks)
90
+ source_datacenter = @client.find_by_inventory_path(@model.datacenter)
91
+ destination_path = datastore_disk_path(destination_datastore.name, destination_datacenter)
92
+ @logger.info("Moving #{@model.path} to #{destination_path}")
93
+
94
+ create_parent_folder(destination_datastore.name, destination_datacenter)
95
+
96
+ if copy_disks
97
+ @client.copy_disk(source_datacenter, @model.path, destination_datacenter, destination_path)
98
+ @logger.info('Copied disk successfully')
99
+ else
100
+ @client.move_disk(source_datacenter, @model.path, destination_datacenter, destination_path)
101
+ @logger.info('Moved disk successfully')
102
+ end
103
+ end
104
+
105
+ def create_parent_folder(datastore_name, datacenter)
106
+ destination_folder = File.dirname(datastore_disk_path(datastore_name, datacenter))
107
+ @client.create_datastore_folder(destination_folder, datacenter.mob)
108
+ end
109
+ end
110
+ end
@@ -1,7 +1,7 @@
1
1
  module Bosh
2
2
  module Clouds
3
3
  class VSphere
4
- VERSION = '1.2801.0'
4
+ VERSION = '1.2807.0'
5
5
  end
6
6
  end
7
7
  end
@@ -1,16 +1,17 @@
1
1
  require 'ruby_vim_sdk'
2
2
  require 'cloud/vsphere/drs_rules/drs_rule'
3
+ require 'cloud/vsphere/resources/disk/ephemeral_disk'
3
4
 
4
5
  module VSphereCloud
5
6
  class VmCreator
6
- def initialize(memory, disk, cpu, placer, client, cloud_searcher, logger, cpi, agent_env, file_provider)
7
+ def initialize(memory, disk_size, cpu, placer, client, cloud_searcher, logger, cpi, agent_env, file_provider)
7
8
  @placer = placer
8
9
  @client = client
9
10
  @cloud_searcher = cloud_searcher
10
11
  @logger = logger
11
12
  @cpi = cpi
12
13
  @memory = memory
13
- @disk = disk
14
+ @disk_size = disk_size
14
15
  @cpu = cpu
15
16
  @agent_env = agent_env
16
17
  @file_provider = file_provider
@@ -28,7 +29,7 @@ module VSphereCloud
28
29
 
29
30
  disks = @cpi.disk_spec(disk_cids)
30
31
  # need to include swap and linked clone log
31
- ephemeral = @disk + @memory + stemcell_size
32
+ ephemeral = @disk_size + @memory + stemcell_size
32
33
  cluster, datastore = @placer.place(@memory, ephemeral, disks)
33
34
 
34
35
  name = "vm-#{@cpi.generate_unique_name}"
@@ -48,9 +49,8 @@ module VSphereCloud
48
49
  system_disk = devices.find { |device| device.kind_of?(VimSdk::Vim::Vm::Device::VirtualDisk) }
49
50
  pci_controller = devices.find { |device| device.kind_of?(VimSdk::Vim::Vm::Device::VirtualPCIController) }
50
51
 
51
- file_name = "[#{datastore.name}] #{name}/ephemeral_disk.vmdk"
52
- ephemeral_disk_config =
53
- @cpi.create_disk_config_spec(datastore.mob, file_name, system_disk.controller_key, @disk, create: true)
52
+ ephemeral_disk = VSphereCloud::EphemeralDisk.new(@disk_size, name, datastore)
53
+ ephemeral_disk_config = ephemeral_disk.create_spec(system_disk.controller_key)
54
54
  config.device_change << ephemeral_disk_config
55
55
 
56
56
  dvs_index = {}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bosh_vsphere_cpi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2801.0
4
+ version: 1.2807.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-24 00:00:00.000000000 Z
12
+ date: 2014-12-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bosh_common
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.2801.0
21
+ version: 1.2807.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 1.2801.0
29
+ version: 1.2807.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: bosh_cpi
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 1.2801.0
37
+ version: 1.2807.0
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 1.2801.0
45
+ version: 1.2807.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: membrane
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -173,7 +173,7 @@ dependencies:
173
173
  version: 0.7.1
174
174
  description: ! 'BOSH VSphere CPI
175
175
 
176
- 7d6c56'
176
+ 0b041c'
177
177
  email: support@cloudfoundry.com
178
178
  executables:
179
179
  - vsphere_cpi
@@ -203,6 +203,9 @@ files:
203
203
  - lib/cloud/vsphere/resources/cluster.rb
204
204
  - lib/cloud/vsphere/resources/datacenter.rb
205
205
  - lib/cloud/vsphere/resources/datastore.rb
206
+ - lib/cloud/vsphere/resources/disk/disk_config.rb
207
+ - lib/cloud/vsphere/resources/disk/ephemeral_disk.rb
208
+ - lib/cloud/vsphere/resources/disk/persistent_disk.rb
206
209
  - lib/cloud/vsphere/resources/folder.rb
207
210
  - lib/cloud/vsphere/resources/resource_pool.rb
208
211
  - lib/cloud/vsphere/resources/scorer.rb
@@ -260,7 +263,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
260
263
  version: '0'
261
264
  segments:
262
265
  - 0
263
- hash: -4276721379970132912
266
+ hash: -1305819405151799674
264
267
  requirements: []
265
268
  rubyforge_project:
266
269
  rubygems_version: 1.8.23