bosh_vsphere_cpi 1.2865.0 → 1.2881.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module Bosh
2
2
  module Clouds
3
3
  class VSphere
4
- VERSION = '1.2865.0'
4
+ VERSION = '1.2881.0'
5
5
  end
6
6
  end
7
7
  end
@@ -1,25 +1,27 @@
1
1
  require 'ruby_vim_sdk'
2
2
  require 'cloud/vsphere/drs_rules/drs_rule'
3
3
  require 'cloud/vsphere/resources/disk/ephemeral_disk'
4
+ require 'cloud/vsphere/resources/vm'
4
5
 
5
6
  module VSphereCloud
6
7
  class VmCreator
7
- def initialize(memory, disk_size, cpu, placer, client, cloud_searcher, logger, cpi, agent_env, file_provider)
8
+ def initialize(memory, disk_size_in_mb, cpu, placer, client, cloud_searcher, logger, cpi, agent_env, file_provider, disk_provider)
8
9
  @placer = placer
9
10
  @client = client
10
11
  @cloud_searcher = cloud_searcher
11
12
  @logger = logger
12
13
  @cpi = cpi
13
14
  @memory = memory
14
- @disk_size = disk_size
15
+ @disk_size_in_mb = disk_size_in_mb
15
16
  @cpu = cpu
16
17
  @agent_env = agent_env
17
18
  @file_provider = file_provider
19
+ @disk_provider = disk_provider
18
20
 
19
21
  @logger.debug("VM creator initialized with memory: #{@memory}, disk: #{@disk}, cpu: #{@cpu}, placer: #{@placer}")
20
22
  end
21
23
 
22
- def create(agent_id, stemcell_cid, networks, disk_cids, environment)
24
+ def create(agent_id, stemcell_cid, networks, persistent_disk_cids, environment)
23
25
  stemcell_vm = @cpi.stemcell_vm(stemcell_cid)
24
26
  raise "Could not find stemcell: #{stemcell_cid}" if stemcell_vm.nil?
25
27
 
@@ -27,86 +29,83 @@ module VSphereCloud
27
29
  @cloud_searcher.get_property(stemcell_vm, VimSdk::Vim::VirtualMachine, 'summary.storage.committed', ensure_all: true)
28
30
  stemcell_size /= 1024 * 1024
29
31
 
30
- disks = @cpi.disk_spec(disk_cids)
31
- # need to include swap and linked clone log
32
- ephemeral = @disk_size + @memory + stemcell_size
33
- cluster, datastore = @placer.place(@memory, ephemeral, disks)
34
-
35
- name = "vm-#{@cpi.generate_unique_name}"
36
- @logger.info("Creating vm: #{name} on #{cluster.mob} stored in #{datastore.mob}")
32
+ persistent_disk_cids ||= []
33
+ persistent_disks = persistent_disk_cids.map { |cid| @disk_provider.find(cid) }
37
34
 
38
- replicated_stemcell_vm = @cpi.replicate_stemcell(cluster, datastore, stemcell_cid)
39
- replicated_stemcell_properties = @cloud_searcher.get_properties(replicated_stemcell_vm, VimSdk::Vim::VirtualMachine,
40
- ['config.hardware.device', 'snapshot'],
41
- ensure_all: true)
42
-
43
- devices = replicated_stemcell_properties['config.hardware.device']
35
+ # need to include swap and linked clone log
36
+ ephemeral_disk_size_in_mb = @disk_size_in_mb + @memory + stemcell_size
37
+ cluster = @placer.pick_cluster_for_vm(@memory, ephemeral_disk_size_in_mb, persistent_disks)
38
+ datastore = @placer.pick_ephemeral_datastore(cluster, ephemeral_disk_size_in_mb)
39
+
40
+ vm_cid = "vm-#{SecureRandom.uuid}"
41
+ @logger.info("Creating vm: #{vm_cid} on #{cluster.mob} stored in #{datastore.mob}")
42
+
43
+ replicated_stemcell_vm_mob = @cpi.replicate_stemcell(cluster, datastore, stemcell_cid)
44
+ replicated_stemcell_properties = @cloud_searcher.get_properties(replicated_stemcell_vm_mob, VimSdk::Vim::VirtualMachine,
45
+ ['snapshot'],
46
+ ensure_all: true)
47
+ replicated_stemcell_vm = Resources::VM.new(stemcell_cid, replicated_stemcell_vm_mob, @client, @logger)
44
48
  snapshot = replicated_stemcell_properties['snapshot']
45
49
 
46
50
  config = VimSdk::Vim::Vm::ConfigSpec.new(memory_mb: @memory, num_cpus: @cpu)
47
51
  config.device_change = []
48
52
 
49
- system_disk = devices.find { |device| device.kind_of?(VimSdk::Vim::Vm::Device::VirtualDisk) }
50
- pci_controller = devices.find { |device| device.kind_of?(VimSdk::Vim::Vm::Device::VirtualPCIController) }
51
-
52
- ephemeral_disk = VSphereCloud::EphemeralDisk.new(@disk_size, name, datastore)
53
- ephemeral_disk_config = ephemeral_disk.create_spec(system_disk.controller_key)
53
+ ephemeral_disk = VSphereCloud::EphemeralDisk.new(@disk_size_in_mb, vm_cid, datastore)
54
+ ephemeral_disk_config = ephemeral_disk.create_spec(replicated_stemcell_vm.system_disk.controller_key)
54
55
  config.device_change << ephemeral_disk_config
55
56
 
56
57
  dvs_index = {}
57
58
  networks.each_value do |network|
58
59
  v_network_name = network['cloud_properties']['name']
59
60
  network_mob = @client.find_by_inventory_path([cluster.datacenter.name, 'network', v_network_name])
60
- nic_config = @cpi.create_nic_config_spec(v_network_name, network_mob, pci_controller.key, dvs_index)
61
+ nic_config = @cpi.create_nic_config_spec(v_network_name, network_mob, replicated_stemcell_vm.pci_controller.key, dvs_index)
61
62
  config.device_change << nic_config
62
63
  end
63
64
 
64
- nics = devices.select { |device| device.kind_of?(VimSdk::Vim::Vm::Device::VirtualEthernetCard) }
65
- nics.each do |nic|
65
+ replicated_stemcell_vm.nics.each do |nic|
66
66
  nic_config = @cpi.create_delete_device_spec(nic)
67
67
  config.device_change << nic_config
68
68
  end
69
69
 
70
- @cpi.fix_device_unit_numbers(devices, config.device_change)
70
+ replicated_stemcell_vm.fix_device_unit_numbers(config.device_change)
71
71
 
72
- @logger.info("Cloning vm: #{replicated_stemcell_vm} to #{name}")
72
+ @logger.info("Cloning vm: #{replicated_stemcell_vm} to #{vm_cid}")
73
73
 
74
- task = @cpi.clone_vm(replicated_stemcell_vm,
75
- name,
76
- cluster.datacenter.vm_folder.mob,
77
- cluster.resource_pool.mob,
78
- datastore: datastore.mob, linked: true, snapshot: snapshot.current_snapshot, config: config)
79
- vm = @client.wait_for_task(task)
74
+ task = @cpi.clone_vm(replicated_stemcell_vm.mob,
75
+ vm_cid,
76
+ cluster.datacenter.vm_folder.mob,
77
+ cluster.resource_pool.mob,
78
+ datastore: datastore.mob, linked: true, snapshot: snapshot.current_snapshot, config: config)
79
+ created_vm_mob = @client.wait_for_task(task)
80
+ created_vm = Resources::VM.new(vm_cid, created_vm_mob, @client, @logger)
80
81
 
81
82
  begin
82
- vm_properties = @cloud_searcher.get_properties(vm, VimSdk::Vim::VirtualMachine, ['config.hardware.device'], ensure_all: true)
83
- devices = vm_properties['config.hardware.device']
84
-
85
- network_env = @cpi.generate_network_env(devices, networks, dvs_index)
86
- disk_env = @cpi.generate_disk_env(system_disk, ephemeral_disk_config.device)
87
- env = @cpi.generate_agent_env(name, vm, agent_id, network_env, disk_env)
83
+ network_env = @cpi.generate_network_env(created_vm.devices, networks, dvs_index)
84
+ disk_env = @cpi.generate_disk_env(created_vm.system_disk, ephemeral_disk_config.device)
85
+ env = @cpi.generate_agent_env(created_vm.cid, created_vm.mob, agent_id, network_env, disk_env)
88
86
  env['env'] = environment
89
87
  @logger.info("Setting VM env: #{env.pretty_inspect}")
90
88
 
91
89
  location = @cpi.get_vm_location(
92
- vm,
90
+ created_vm.mob,
93
91
  datacenter: cluster.datacenter.name,
94
92
  datastore: datastore.name,
95
- vm: name
93
+ vm: created_vm.cid
96
94
  )
97
95
 
98
- @agent_env.set_env(vm, location, env)
96
+ @agent_env.set_env(created_vm.mob, location, env)
99
97
 
100
- @logger.info("Powering on VM: #{vm} (#{name})")
101
- @client.power_on_vm(cluster.datacenter.mob, vm)
98
+ @logger.info("Powering on VM: #{created_vm} (#{created_vm.cid})")
99
+ created_vm.power_on
102
100
 
103
- create_drs_rules(vm, cluster)
101
+ create_drs_rules(created_vm.mob, cluster)
104
102
  rescue => e
105
103
  @logger.info("#{e} - #{e.backtrace.join("\n")}")
106
- @cpi.delete_vm(name)
104
+ created_vm.delete if created_vm
107
105
  raise e
108
106
  end
109
- name
107
+
108
+ vm_cid
110
109
  end
111
110
 
112
111
  def create_drs_rules(vm, cluster)
@@ -2,7 +2,7 @@ require 'cloud/vsphere/vm_creator'
2
2
 
3
3
  module VSphereCloud
4
4
  class VmCreatorBuilder
5
- def build(resources, cloud_properties, client, cloud_searcher, logger, cpi, agent_env, file_provider)
5
+ def build(resources, cloud_properties, client, cloud_searcher, logger, cpi, agent_env, file_provider, disk_provider)
6
6
  VmCreator.new(
7
7
  cloud_properties.fetch('ram'),
8
8
  cloud_properties.fetch('disk'),
@@ -14,6 +14,7 @@ module VSphereCloud
14
14
  cpi,
15
15
  agent_env,
16
16
  file_provider,
17
+ disk_provider
17
18
  )
18
19
  end
19
20
  end
@@ -0,0 +1,16 @@
1
+ module VSphereCloud
2
+ class VMProvider
3
+ def initialize(datacenter, client, logger)
4
+ @datacenter = datacenter
5
+ @client = client
6
+ @logger = logger
7
+ end
8
+
9
+ def find(vm_cid)
10
+ vm_mob = @client.find_by_inventory_path(@datacenter.vm_path(vm_cid))
11
+ raise Bosh::Clouds::VMNotFound, "VM `#{vm_cid}' not found" if vm_mob.nil?
12
+
13
+ Resources::VM.new(vm_cid, vm_mob, @client, @logger)
14
+ end
15
+ end
16
+ end
data/lib/cloud/vsphere.rb CHANGED
@@ -18,7 +18,7 @@ module Bosh
18
18
  :create_disk, :has_disk?, :delete_disk,
19
19
  :attach_disk, :detach_disk,
20
20
  :snapshot_disk, :delete_snapshot,
21
- :current_vm_id, :get_disks, :ping
21
+ :current_vm_id, :get_disks, :ping, :disk_provider
22
22
 
23
23
  def initialize(options)
24
24
  @delegate = VSphereCloud::Cloud.new(options)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bosh_vsphere_cpi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2865.0
4
+ version: 1.2881.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - VMware
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-06 00:00:00.000000000 Z
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bosh_common
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.2865.0
19
+ version: 1.2881.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.2865.0
26
+ version: 1.2881.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bosh_cpi
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.2865.0
33
+ version: 1.2881.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.2865.0
40
+ version: 1.2881.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: membrane
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -53,106 +53,162 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.1.0
55
55
  - !ruby/object:Gem::Dependency
56
- name: sequel
56
+ name: builder
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 3.43.0
61
+ version: 3.1.4
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 3.43.0
68
+ version: 3.1.4
69
69
  - !ruby/object:Gem::Dependency
70
- name: pg
70
+ name: nokogiri
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.15.1
75
+ version: 1.5.10
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.15.1
82
+ version: 1.5.10
83
83
  - !ruby/object:Gem::Dependency
84
- name: mysql2
84
+ name: httpclient
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 0.3.11
89
+ version: 2.4.0
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 0.3.11
96
+ version: 2.4.0
97
97
  - !ruby/object:Gem::Dependency
98
- name: builder
98
+ name: mono_logger
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 3.1.4
103
+ version: 1.1.0
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: 3.1.4
110
+ version: 1.1.0
111
111
  - !ruby/object:Gem::Dependency
112
- name: nokogiri
112
+ name: timecop
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 1.5.10
118
- type: :runtime
117
+ version: 0.7.1
118
+ type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 1.5.10
124
+ version: 0.7.1
125
125
  - !ruby/object:Gem::Dependency
126
- name: httpclient
126
+ name: rake
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '='
129
+ - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 2.4.0
132
- type: :runtime
131
+ version: '10.0'
132
+ type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '='
136
+ - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: 2.4.0
138
+ version: '10.0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: timecop
140
+ name: rspec
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.7.1
145
+ version: '3.0'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.7.1
152
+ version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec-its
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rspec-instafail
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: sqlite3
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: fakefs
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
153
209
  description: |-
154
210
  BOSH VSphere CPI
155
- 14ebaf
211
+ 756fc2
156
212
  email: support@cloudfoundry.com
157
213
  executables:
158
214
  - vsphere_cpi
@@ -162,8 +218,6 @@ extra_rdoc_files: []
162
218
  files:
163
219
  - bin/vsphere_cpi
164
220
  - bin/vsphere_cpi_console
165
- - db/migrations/20120123235022_initial.rb
166
- - db/migrations/20121204174707_add_uuid_to_disks.rb
167
221
  - lib/cloud/vsphere.rb
168
222
  - lib/cloud/vsphere/agent_env.rb
169
223
  - lib/cloud/vsphere/client.rb
@@ -171,6 +225,7 @@ files:
171
225
  - lib/cloud/vsphere/cloud_searcher.rb
172
226
  - lib/cloud/vsphere/cluster_config.rb
173
227
  - lib/cloud/vsphere/config.rb
228
+ - lib/cloud/vsphere/disk_provider.rb
174
229
  - lib/cloud/vsphere/drs_rules/drs_lock.rb
175
230
  - lib/cloud/vsphere/drs_rules/drs_rule.rb
176
231
  - lib/cloud/vsphere/drs_rules/vm_attribute_manager.rb
@@ -178,24 +233,25 @@ files:
178
233
  - lib/cloud/vsphere/fixed_cluster_placer.rb
179
234
  - lib/cloud/vsphere/lease_obtainer.rb
180
235
  - lib/cloud/vsphere/lease_updater.rb
181
- - lib/cloud/vsphere/models/disk.rb
182
236
  - lib/cloud/vsphere/path_finder.rb
183
237
  - lib/cloud/vsphere/resources.rb
184
238
  - lib/cloud/vsphere/resources/cluster.rb
185
239
  - lib/cloud/vsphere/resources/datacenter.rb
186
240
  - lib/cloud/vsphere/resources/datastore.rb
241
+ - lib/cloud/vsphere/resources/disk.rb
187
242
  - lib/cloud/vsphere/resources/disk/disk_config.rb
188
243
  - lib/cloud/vsphere/resources/disk/ephemeral_disk.rb
189
- - lib/cloud/vsphere/resources/disk/persistent_disk.rb
190
244
  - lib/cloud/vsphere/resources/folder.rb
191
245
  - lib/cloud/vsphere/resources/resource_pool.rb
192
246
  - lib/cloud/vsphere/resources/scorer.rb
193
247
  - lib/cloud/vsphere/resources/util.rb
248
+ - lib/cloud/vsphere/resources/vm.rb
194
249
  - lib/cloud/vsphere/retry_block.rb
195
250
  - lib/cloud/vsphere/soap_stub.rb
196
251
  - lib/cloud/vsphere/version.rb
197
252
  - lib/cloud/vsphere/vm_creator.rb
198
253
  - lib/cloud/vsphere/vm_creator_builder.rb
254
+ - lib/cloud/vsphere/vm_provider.rb
199
255
  - lib/ruby_vim_sdk.rb
200
256
  - lib/ruby_vim_sdk/base_type.rb
201
257
  - lib/ruby_vim_sdk/const.rb
@@ -1,24 +0,0 @@
1
- # Copyright (c) 2009-2012 VMware, Inc.
2
-
3
- Sequel.migration do
4
- # there is a bug in sequel where create_table? on postgres causes the transaction to abort with:
5
- # PG::Error: ERROR: current transaction is aborted, commands ignored until end of transaction block
6
- # to work around this until it is fixed, transactions are disabled, which is fine as it is an atomic operation
7
- no_transaction
8
-
9
- up do
10
- # This used to be included in the director migrations, so we should not fail (hence create_table?) if this table
11
- # already exists to allow old installations to keep working with the new migrations.
12
- create_table? :vsphere_disk do
13
- primary_key :id
14
- String :path, :null => true
15
- String :datacenter, :null => true
16
- String :datastore, :null => true
17
- Integer :size, :null => false
18
- end
19
- end
20
-
21
- down do
22
- drop_table :vsphere_disk
23
- end
24
- end
@@ -1,14 +0,0 @@
1
- # Copyright (c) 2009-2012 VMware, Inc.
2
-
3
- Sequel.migration do
4
- up do
5
- add_column :vsphere_disk, :uuid, String
6
- self[:vsphere_disk].update(:uuid => :id)
7
- add_index :vsphere_disk, :uuid, :unique => true
8
- end
9
-
10
- down do
11
- drop_index :vsphere_disk, :uuid, :unique => true
12
- drop_column :vsphere_disk, :uuid
13
- end
14
- end
@@ -1,11 +0,0 @@
1
- # provides validates_*
2
- Sequel::Model.plugin :validation_helpers
3
-
4
- module VSphereCloud::Models
5
- class Disk < Sequel::Model(Bosh::Clouds::Config.db[:vsphere_disk])
6
- def validate
7
- validates_presence :size
8
- validates_integer :size
9
- end
10
- end
11
- end
@@ -1,110 +0,0 @@
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