-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvcair.rb
More file actions
375 lines (303 loc) · 10.2 KB
/
vcair.rb
File metadata and controls
375 lines (303 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
199
200
201
202
203
204
205
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
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
# Encoding: UTF-8
#
# Authors:: Chris McClimans (<c@vulk.co>)
# Authors:: Taylor Carpenter (<t@vulk.co>)
# Authors:: Chef Partner Engineering (<partnereng@chef.io>)
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'fog'
require 'kitchen'
require 'securerandom'
module Kitchen
module Driver
class Vcair < Kitchen::Driver::Base
attr_accessor :vapp_id
default_config :wait_for, 600
default_config :vcair_api_path, '/api'
default_config :catalog_id, nil
default_config :catalog_name, nil
default_config :image_id, nil
default_config :image_name, nil
default_config :vdc_id, nil
default_config :vdc_name, nil
default_config :network_id, nil
default_config :network_name, nil
default_config :cpus, 1
default_config :memory, 1024
default_config :vm_password
required_config :vcair_username
required_config :vcair_password
required_config :vcair_api_host
required_config :vcair_org
def initialize(config)
super
Fog.timeout = config[:wait_for].to_i
end
def name
'vCloudAir'
end
def create(state)
return unless state[:vapp_id].nil?
validate!
create_server(state)
vm.wait_for { ready? }
state[:hostname] = vm.ip_address
info("Server #{state[:hostname]} is powered on. Waiting for it to be ready...")
wait_for_server(state)
end
def destroy(state)
return if state[:vapp_id].nil?
validate!
self.vapp_id = state[:vapp_id]
info("Destroying vApp #{vapp_id}...")
begin
vapp
rescue Fog::Compute::VcloudDirector::Forbidden
warn("Unable to locate vApp <#{state[:vapp_id]}> - assuming it is already destroyed.")
return
end
info('Powering off the vApp...')
vapp.power_off
info('Undeploying the vApp...')
vapp.undeploy
info('Deleting the vApp...')
vapp.destroy
info("vApp <#{state[:vapp_id]}> destroyed.")
end
def vcloud_client
@vcloud_client ||= Fog::Compute.new(fog_server_def)
rescue Excon::Errors::Unauthorized => e
raise "Connection failure, please check your username and password. -- #{e.message}"
end
def org
@org ||= vcloud_client.organizations.get_by_name(config[:vcair_org])
end
def create_server(state)
self.vapp_id = instantiate
state[:vapp_id] = vapp_id
info("vApp ID #{vapp_id} created.")
info('Validating the vApp...')
unless validate_vapp
destroy(state)
return
end
info('Updating the VM customization...')
update_customization
info('Adjusting VM hardware...')
adjust_hardware
info('Attaching it to the network...')
attach_network
info('Tagging the VM...')
tag_vm
info('Powering on the VM...')
power_on
end
def adjust_hardware
vm.cpu = config[:cpus] if config[:cpus]
vm.memory = config[:memory] if config[:memory]
end
def attach_network_payload
{
PrimaryNetworkConnectionIndex: 0,
NetworkConnection: [
{
network: network.name,
needsCustomization: true,
NetworkConnectionIndex: 0,
IsConnected: true,
IpAddressAllocationMode: 'POOL'
}
]
}
end
def attach_network
task = vcloud_client.put_network_connection_system_section_vapp(vm.id, attach_network_payload)
vcloud_client.process_task(task.body)
end
def tag_vm
vm.tags.create('created-by', 'test-kitchen')
end
def power_on
vapp.power_on
end
def wait_for_server(state)
instance.transport.connection(state).wait_until_ready
rescue
error("Server #{vapp.id} (#{vm.name}) not reachable. Destroying server...")
destroy(state)
raise
end
def vcloud_username
[ config[:vcair_username], config[:vcair_org] ].join('@')
end
def fog_server_def
{
provider: 'vclouddirector',
vcloud_director_username: vcloud_username,
vcloud_director_password: config[:vcair_password],
vcloud_director_host: config[:vcair_api_host],
vcloud_director_api_version: config[:vcair_api_version],
vcloud_director_show_progress: false,
path: config[:vcair_api_path]
}
end
def image
image = if config[:image_id]
catalog.catalog_items.get(config[:image_id])
else
catalog.catalog_items.get_by_name(config[:image_name])
end
raise 'Unable to find image - check your image_id or image_name' if image.nil?
image
end
def catalog
catalog = if config[:catalog_id]
org.catalogs.get(config[:catalog_id])
else
org.catalogs.get_by_name(config[:catalog_name])
end
raise 'Unable to find catalog - check your catalog_id or catalog_name' if catalog.nil?
catalog
end
def vdc
vdc = if config[:vdc_id]
org.vdcs.get(config[:vdc_id])
else
org.vdcs.get_by_name(config[:vdc_name])
end
raise 'Unable to find VDC - check your vdc_id or vdc_name' if vdc.nil?
vdc
end
def network
network = if config[:network_id]
org.networks.get(config[:network_id])
else
org.networks.get_by_name(config[:network_name])
end
raise 'Unable to find network - check your network_id or network_name' if network.nil?
network
end
def node_description
config[:node_description] || "Test Kitchen: #{node_name}"
end
def node_name
config[:node_name] || generate_node_name
end
def vmapp_name
config[:vmapp_name] || generate_node_name
end
def generate_node_name
# SecureRandom.hex generates a string 2x the argument.
# We need the name to be 15 chars or less to play nicely
# with windows, so we're generating a 12-char random
# string prefixed with "tk-"
'tk-' + SecureRandom.hex(6)
end
def instantiate_config
{
vdc_id: vdc.id,
network_id: network.id,
description: node_description
}
end
def print_error_and_exit(message)
error(message)
fail message
end
def validate!
%w(vdc catalog image network).each do |param|
validate_parameter_pair!(param)
end
[ :org, :vdc, :catalog, :image, :network].each do |method|
validate_method!(method)
end
validate_customization_script!
validate_computer_name!
end
def validate_parameter_pair!(param)
id_key = param + '_id'
name_key = param + '_name'
print_error_and_exit("No #{param} found. You must specify #{id_key} or #{name_key}.") if
config[id_key.to_sym].nil? && config[name_key.to_sym].nil?
end
def validate_method!(method)
send(method)
rescue => e
raise "Unable to validate #{method} - check your configuration and try again. #{e.class} -- #{e.message}"
end
def validate_computer_name!
# regex proudly modified after stealing from:
# http://stackoverflow.com/questions/2063213/regular-expression-for-validating-dns-label-host-name
print_error_and_exit('Node name is not valid - must be 15 characters or less, and be a valid Windows node name') unless
node_name =~ /^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,15}(?<!-)$/
end
def validate_customization_script!
return unless config[:customization_script]
print_error_and_exit("Customization script #{config[:customization_script]} is not found or not readable.") unless
::File.readable?(config[:customization_script])
end
def instantiate
image.instantiate(vmapp_name, instantiate_config)
end
def vapp
@vapp ||= vdc.vapps.get(vapp_id)
end
def vm
@vm ||= vapp.vms.first
end
def validate_vapp
vms = vapp.vms
if vms.empty?
error('vApp created, but did not contain any VMs')
return false
end
if vms.size > 1
error('vApp created, but contained more than one VM')
return false
end
true
end
def customization
@customization ||= vm.customization
end
def update_customization
set_customization_script if config[:customization_script]
set_customization_password
set_customization_computer_name
save_customization
end
def set_customization_script
customization.script = ::File.read(config[:customization_script])
end
def set_customization_password
customization.reset_password_required = false
if config[:vm_password]
customization.admin_password = config[:vm_password]
customization.admin_password_auto = false
else
customization.admin_password = nil
customization.admin_password_auto = true
end
end
def set_customization_computer_name
customization.computer_name = node_name
end
def save_customization
customization.enabled = true
customization.save
end
end
end
end