knife-vsphere 0.9.8 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
@@ -39,7 +39,7 @@ end
|
|
39
39
|
# Lists all known data store cluster in datacenter with sizes
|
40
40
|
class Chef::Knife::VsphereDatastoreclusterList < Chef::Knife::BaseVsphereCommand
|
41
41
|
|
42
|
-
banner "knife vsphere
|
42
|
+
banner "knife vsphere datastorecluster list"
|
43
43
|
|
44
44
|
get_common_options
|
45
45
|
|
@@ -1,484 +1,521 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
-
# Contributor:: Jesse Campbell (<hikeit@gmail.com>)
|
4
|
-
# Contributor:: Bethany Erskine (<bethany@paperlesspost.com>)
|
5
|
-
# Contributor:: Adrian Stanila (https://github.com/sacx)
|
6
|
-
# License:: Apache License, Version 2.0
|
7
|
-
#
|
8
|
-
|
9
|
-
require 'chef/knife'
|
10
|
-
require 'chef/knife/base_vsphere_command'
|
11
|
-
require 'rbvmomi'
|
12
|
-
require 'netaddr'
|
13
|
-
|
14
|
-
# Clone an existing template into a new VM, optionally applying a customization specification.
|
15
|
-
# usage:
|
16
|
-
# knife vsphere vm clone NewNode UbuntuTemplate --cspec StaticSpec \
|
17
|
-
# --cips 192.168.0.99/24,192.168.1.99/24 \
|
18
|
-
# --chostname NODENAME --cdomain NODEDOMAIN
|
19
|
-
class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
|
20
|
-
|
21
|
-
banner "knife vsphere vm clone VMNAME (options)"
|
22
|
-
|
23
|
-
get_common_options
|
24
|
-
|
25
|
-
option :dest_folder,
|
26
|
-
:long => "--dest-folder FOLDER",
|
27
|
-
:description => "The folder into which to put the cloned VM"
|
28
|
-
|
29
|
-
option :datastore,
|
30
|
-
:long => "--datastore STORE",
|
31
|
-
:description => "The datastore into which to put the cloned VM"
|
32
|
-
|
33
|
-
option :resource_pool,
|
34
|
-
:long => "--resource-pool POOL",
|
35
|
-
:description => "The resource pool into which to put the cloned VM"
|
36
|
-
|
37
|
-
option :source_vm,
|
38
|
-
:long => "--template TEMPLATE",
|
39
|
-
:description => "The source VM / Template to clone from",
|
40
|
-
:required => true
|
41
|
-
|
42
|
-
option :
|
43
|
-
:long => "--
|
44
|
-
:description => "
|
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
|
-
option :
|
104
|
-
:long => "--
|
105
|
-
:description => "Indicates whether to
|
106
|
-
:boolean => false
|
107
|
-
|
108
|
-
option :
|
109
|
-
:long => "--
|
110
|
-
:description => "
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
:long => "--
|
115
|
-
:description => "
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
:
|
120
|
-
:
|
121
|
-
|
122
|
-
|
123
|
-
option :
|
124
|
-
:short => "-
|
125
|
-
:long => "--ssh-
|
126
|
-
:description => "The ssh
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
:
|
131
|
-
:
|
132
|
-
|
133
|
-
|
134
|
-
option :
|
135
|
-
:short => "-
|
136
|
-
:long => "--
|
137
|
-
:description => "The
|
138
|
-
|
139
|
-
option :
|
140
|
-
:
|
141
|
-
:
|
142
|
-
:
|
143
|
-
|
144
|
-
option :
|
145
|
-
:long => "--
|
146
|
-
:description => "
|
147
|
-
:
|
148
|
-
|
149
|
-
option :
|
150
|
-
:long => "--bootstrap-
|
151
|
-
:description => "The
|
152
|
-
:proc => Proc.new { |
|
153
|
-
|
154
|
-
option :
|
155
|
-
:
|
156
|
-
:
|
157
|
-
:
|
158
|
-
|
159
|
-
option :
|
160
|
-
:
|
161
|
-
:
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
:long => "--
|
166
|
-
:description => "
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
:long => "--
|
171
|
-
:description => "
|
172
|
-
$default[:
|
173
|
-
|
174
|
-
option :
|
175
|
-
:long => "--
|
176
|
-
:description => "
|
177
|
-
|
178
|
-
|
179
|
-
option :
|
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
|
-
end
|
305
|
-
|
306
|
-
if get_config(:
|
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
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
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
|
-
def
|
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
|
-
|
1
|
+
#
|
2
|
+
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
+
# Contributor:: Jesse Campbell (<hikeit@gmail.com>)
|
4
|
+
# Contributor:: Bethany Erskine (<bethany@paperlesspost.com>)
|
5
|
+
# Contributor:: Adrian Stanila (https://github.com/sacx)
|
6
|
+
# License:: Apache License, Version 2.0
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'chef/knife'
|
10
|
+
require 'chef/knife/base_vsphere_command'
|
11
|
+
require 'rbvmomi'
|
12
|
+
require 'netaddr'
|
13
|
+
|
14
|
+
# Clone an existing template into a new VM, optionally applying a customization specification.
|
15
|
+
# usage:
|
16
|
+
# knife vsphere vm clone NewNode UbuntuTemplate --cspec StaticSpec \
|
17
|
+
# --cips 192.168.0.99/24,192.168.1.99/24 \
|
18
|
+
# --chostname NODENAME --cdomain NODEDOMAIN
|
19
|
+
class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
|
20
|
+
|
21
|
+
banner "knife vsphere vm clone VMNAME (options)"
|
22
|
+
|
23
|
+
get_common_options
|
24
|
+
|
25
|
+
option :dest_folder,
|
26
|
+
:long => "--dest-folder FOLDER",
|
27
|
+
:description => "The folder into which to put the cloned VM"
|
28
|
+
|
29
|
+
option :datastore,
|
30
|
+
:long => "--datastore STORE",
|
31
|
+
:description => "The datastore into which to put the cloned VM"
|
32
|
+
|
33
|
+
option :resource_pool,
|
34
|
+
:long => "--resource-pool POOL",
|
35
|
+
:description => "The resource pool into which to put the cloned VM"
|
36
|
+
|
37
|
+
option :source_vm,
|
38
|
+
:long => "--template TEMPLATE",
|
39
|
+
:description => "The source VM / Template to clone from",
|
40
|
+
:required => true
|
41
|
+
|
42
|
+
option :linked_clone,
|
43
|
+
:long => "--linked-clone",
|
44
|
+
:description => "Indicates whether to use linked clones.",
|
45
|
+
:boolean => false
|
46
|
+
|
47
|
+
option :annotation,
|
48
|
+
:long => "--annotation TEXT",
|
49
|
+
:description => "Add TEXT in Notes field from annotation"
|
50
|
+
|
51
|
+
option :customization_spec,
|
52
|
+
:long => "--cspec CUST_SPEC",
|
53
|
+
:description => "The name of any customization specification to apply"
|
54
|
+
|
55
|
+
option :customization_plugin,
|
56
|
+
:long => "--cplugin CUST_PLUGIN_PATH",
|
57
|
+
:description => "Path to plugin that implements KnifeVspherePlugin.customize_clone_spec and/or KnifeVspherePlugin.reconfig_vm"
|
58
|
+
|
59
|
+
option :customization_plugin_data,
|
60
|
+
:long => "--cplugin-data CUST_PLUGIN_DATA",
|
61
|
+
:description => "String of data to pass to the plugin. Use any format you wish."
|
62
|
+
|
63
|
+
option :customization_vlan,
|
64
|
+
:long => "--cvlan CUST_VLAN",
|
65
|
+
:description => "VLAN name for network adapter to join"
|
66
|
+
|
67
|
+
option :customization_ips,
|
68
|
+
:long => "--cips CUST_IPS",
|
69
|
+
:description => "Comma-delimited list of CIDR IPs for customization"
|
70
|
+
|
71
|
+
option :customization_dns_ips,
|
72
|
+
:long => "--cdnsips CUST_DNS_IPS",
|
73
|
+
:description => "Comma-delimited list of DNS IP addresses"
|
74
|
+
|
75
|
+
option :customization_dns_suffixes,
|
76
|
+
:long => "--cdnssuffix CUST_DNS_SUFFIXES",
|
77
|
+
:description => "Comma-delimited list of DNS search suffixes"
|
78
|
+
|
79
|
+
option :customization_gw,
|
80
|
+
:long => "--cgw CUST_GW",
|
81
|
+
:description => "CIDR IP of gateway for customization"
|
82
|
+
|
83
|
+
option :customization_hostname,
|
84
|
+
:long => "--chostname CUST_HOSTNAME",
|
85
|
+
:description => "Unqualified hostname for customization"
|
86
|
+
|
87
|
+
option :customization_domain,
|
88
|
+
:long => "--cdomain CUST_DOMAIN",
|
89
|
+
:description => "Domain name for customization"
|
90
|
+
|
91
|
+
option :customization_tz,
|
92
|
+
:long => "--ctz CUST_TIMEZONE",
|
93
|
+
:description => "Timezone invalid 'Area/Location' format"
|
94
|
+
|
95
|
+
option :customization_cpucount,
|
96
|
+
:long => "--ccpu CUST_CPU_COUNT",
|
97
|
+
:description => "Number of CPUs"
|
98
|
+
|
99
|
+
option :customization_memory,
|
100
|
+
:long => "--cram CUST_MEMORY_GB",
|
101
|
+
:description => "Gigabytes of RAM"
|
102
|
+
|
103
|
+
option :power,
|
104
|
+
:long => "--start",
|
105
|
+
:description => "Indicates whether to start the VM after a successful clone",
|
106
|
+
:boolean => false
|
107
|
+
|
108
|
+
option :bootstrap,
|
109
|
+
:long => "--bootstrap",
|
110
|
+
:description => "Indicates whether to bootstrap the VM",
|
111
|
+
:boolean => false
|
112
|
+
|
113
|
+
option :fqdn,
|
114
|
+
:long => "--fqdn SERVER_FQDN",
|
115
|
+
:description => "Fully qualified hostname for bootstrapping"
|
116
|
+
|
117
|
+
option :ssh_user,
|
118
|
+
:short => "-x USERNAME",
|
119
|
+
:long => "--ssh-user USERNAME",
|
120
|
+
:description => "The ssh username"
|
121
|
+
$default[:ssh_user] = "root"
|
122
|
+
|
123
|
+
option :ssh_password,
|
124
|
+
:short => "-P PASSWORD",
|
125
|
+
:long => "--ssh-password PASSWORD",
|
126
|
+
:description => "The ssh password"
|
127
|
+
|
128
|
+
option :ssh_port,
|
129
|
+
:short => "-p PORT",
|
130
|
+
:long => "--ssh-port PORT",
|
131
|
+
:description => "The ssh port"
|
132
|
+
$default[:ssh_port] = 22
|
133
|
+
|
134
|
+
option :identity_file,
|
135
|
+
:short => "-i IDENTITY_FILE",
|
136
|
+
:long => "--identity-file IDENTITY_FILE",
|
137
|
+
:description => "The SSH identity file used for authentication"
|
138
|
+
|
139
|
+
option :chef_node_name,
|
140
|
+
:short => "-N NAME",
|
141
|
+
:long => "--node-name NAME",
|
142
|
+
:description => "The Chef node name for your new node"
|
143
|
+
|
144
|
+
option :prerelease,
|
145
|
+
:long => "--prerelease",
|
146
|
+
:description => "Install the pre-release chef gems",
|
147
|
+
:boolean => false
|
148
|
+
|
149
|
+
option :bootstrap_version,
|
150
|
+
:long => "--bootstrap-version VERSION",
|
151
|
+
:description => "The version of Chef to install",
|
152
|
+
:proc => Proc.new { |v| Chef::Config[:knife][:bootstrap_version] = v }
|
153
|
+
|
154
|
+
option :bootstrap_proxy,
|
155
|
+
:long => "--bootstrap-proxy PROXY_URL",
|
156
|
+
:description => "The proxy server for the node being bootstrapped",
|
157
|
+
:proc => Proc.new { |p| Chef::Config[:knife][:bootstrap_proxy] = p }
|
158
|
+
|
159
|
+
option :distro,
|
160
|
+
:short => "-d DISTRO",
|
161
|
+
:long => "--distro DISTRO",
|
162
|
+
:description => "Bootstrap a distro using a template"
|
163
|
+
|
164
|
+
option :template_file,
|
165
|
+
:long => "--template-file TEMPLATE",
|
166
|
+
:description => "Full path to location of template to use"
|
167
|
+
|
168
|
+
option :run_list,
|
169
|
+
:short => "-r RUN_LIST",
|
170
|
+
:long => "--run-list RUN_LIST",
|
171
|
+
:description => "Comma separated list of roles/recipes to apply"
|
172
|
+
$default[:run_list] = ''
|
173
|
+
|
174
|
+
option :secret_file,
|
175
|
+
:long => "--secret-file SECRET_FILE",
|
176
|
+
:description => "A file containing the secret key to use to encrypt data bag item values"
|
177
|
+
$default[:secret_file] = ''
|
178
|
+
|
179
|
+
option :no_host_key_verify,
|
180
|
+
:long => "--no-host-key-verify",
|
181
|
+
:description => "Disable host key verification",
|
182
|
+
:boolean => true
|
183
|
+
|
184
|
+
option :first_boot_attributes,
|
185
|
+
:short => "-j JSON_ATTRIBS",
|
186
|
+
:long => "--json-attributes",
|
187
|
+
:description => "A JSON string to be added to the first run of chef-client",
|
188
|
+
:proc => lambda { |o| JSON.parse(o) },
|
189
|
+
:default => {}
|
190
|
+
|
191
|
+
option :disable_customization,
|
192
|
+
:long => "--disable-customization",
|
193
|
+
:description => "Disable default customization",
|
194
|
+
:boolean => true,
|
195
|
+
:default => false
|
196
|
+
|
197
|
+
option :log_level,
|
198
|
+
:short => "-l LEVEL",
|
199
|
+
:long => "--log_level",
|
200
|
+
:description => "Set the log level (debug, info, warn, error, fatal) for chef-client",
|
201
|
+
:proc => lambda { |l| l.to_sym }
|
202
|
+
|
203
|
+
def run
|
204
|
+
$stdout.sync = true
|
205
|
+
|
206
|
+
vmname = @name_args[0]
|
207
|
+
if vmname.nil?
|
208
|
+
show_usage
|
209
|
+
fatal_exit("You must specify a virtual machine name")
|
210
|
+
end
|
211
|
+
config[:chef_node_name] = vmname unless config[:chef_node_name]
|
212
|
+
config[:vmname] = vmname
|
213
|
+
|
214
|
+
if get_config(:bootstrap) && get_config(:distro) && !@@chef_config_dir
|
215
|
+
fatal_exit("Can't find .chef for bootstrap files. chdir to a location with a .chef directory and try again")
|
216
|
+
end
|
217
|
+
|
218
|
+
vim = get_vim_connection
|
219
|
+
|
220
|
+
dc = get_datacenter
|
221
|
+
|
222
|
+
src_folder = find_folder(get_config(:folder)) || dc.vmFolder
|
223
|
+
|
224
|
+
src_vm = find_in_folder(src_folder, RbVmomi::VIM::VirtualMachine, config[:source_vm]) or
|
225
|
+
abort "VM/Template not found"
|
226
|
+
|
227
|
+
if get_config(:linked_clone)
|
228
|
+
create_delta_disk(src_vm)
|
229
|
+
end
|
230
|
+
|
231
|
+
clone_spec = generate_clone_spec(src_vm.config)
|
232
|
+
|
233
|
+
cust_folder = config[:dest_folder] || get_config(:folder)
|
234
|
+
|
235
|
+
dest_folder = cust_folder.nil? ? src_vm.vmFolder : find_folder(cust_folder)
|
236
|
+
|
237
|
+
task = src_vm.CloneVM_Task(:folder => dest_folder, :name => vmname, :spec => clone_spec)
|
238
|
+
puts "Cloning template #{config[:source_vm]} to new VM #{vmname}"
|
239
|
+
task.wait_for_completion
|
240
|
+
puts "Finished creating virtual machine #{vmname}"
|
241
|
+
|
242
|
+
if customization_plugin && customization_plugin.respond_to?(:reconfig_vm)
|
243
|
+
target_vm = find_in_folder(dest_folder, RbVmomi::VIM::VirtualMachine, vmname) or abort "VM could not be found in #{dest_folder}"
|
244
|
+
customization_plugin.reconfig_vm(target_vm)
|
245
|
+
end
|
246
|
+
|
247
|
+
if get_config(:power) || get_config(:bootstrap)
|
248
|
+
vm = find_in_folder(dest_folder, RbVmomi::VIM::VirtualMachine, vmname) or
|
249
|
+
fatal_exit("VM #{vmname} not found")
|
250
|
+
vm.PowerOnVM_Task.wait_for_completion
|
251
|
+
puts "Powered on virtual machine #{vmname}"
|
252
|
+
end
|
253
|
+
|
254
|
+
if get_config(:bootstrap)
|
255
|
+
sleep 2 until vm.guest.ipAddress
|
256
|
+
config[:fqdn] = vm.guest.ipAddress unless config[:fqdn]
|
257
|
+
print "Waiting for sshd..."
|
258
|
+
print "." until tcp_test_ssh(config[:fqdn])
|
259
|
+
puts "done"
|
260
|
+
|
261
|
+
bootstrap_for_node.run
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
def create_delta_disk(src_vm)
|
266
|
+
disks = src_vm.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
|
267
|
+
disks.select { |disk| disk.backing.parent == nil }.each do |disk|
|
268
|
+
spec = {
|
269
|
+
:deviceChange => [
|
270
|
+
{
|
271
|
+
:operation => :remove,
|
272
|
+
:device => disk
|
273
|
+
},
|
274
|
+
{
|
275
|
+
:operation => :add,
|
276
|
+
:fileOperation => :create,
|
277
|
+
:device => disk.dup.tap { |new_disk|
|
278
|
+
new_disk.backing = new_disk.backing.dup
|
279
|
+
new_disk.backing.fileName = "[#{disk.backing.datastore.name}]"
|
280
|
+
new_disk.backing.parent = disk.backing
|
281
|
+
},
|
282
|
+
}
|
283
|
+
]
|
284
|
+
}
|
285
|
+
src_vm.ReconfigVM_Task(:spec => spec).wait_for_completion
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
# Builds a CloneSpec
|
290
|
+
def generate_clone_spec (src_config)
|
291
|
+
|
292
|
+
rspec = nil
|
293
|
+
if get_config(:resource_pool)
|
294
|
+
rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => find_pool(get_config(:resource_pool)))
|
295
|
+
else
|
296
|
+
dc = get_datacenter
|
297
|
+
hosts = find_all_in_folder(dc.hostFolder, RbVmomi::VIM::ComputeResource)
|
298
|
+
rp = hosts.first.resourcePool
|
299
|
+
rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => rp)
|
300
|
+
end
|
301
|
+
|
302
|
+
if get_config(:linked_clone)
|
303
|
+
rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:diskMoveType => :moveChildMostDiskBacking)
|
304
|
+
end
|
305
|
+
|
306
|
+
if get_config(:datastore)
|
307
|
+
rspec.datastore = find_datastore(get_config(:datastore))
|
308
|
+
end
|
309
|
+
|
310
|
+
clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => rspec,
|
311
|
+
:powerOn => false,
|
312
|
+
:template => false)
|
313
|
+
|
314
|
+
clone_spec.config = RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange => Array.new)
|
315
|
+
|
316
|
+
if get_config(:annotation)
|
317
|
+
clone_spec.config.annotation = get_config(:annotation)
|
318
|
+
end
|
319
|
+
|
320
|
+
if get_config(:customization_cpucount)
|
321
|
+
clone_spec.config.numCPUs = get_config(:customization_cpucount)
|
322
|
+
end
|
323
|
+
|
324
|
+
if get_config(:customization_memory)
|
325
|
+
clone_spec.config.memoryMB = Integer(get_config(:customization_memory)) * 1024
|
326
|
+
end
|
327
|
+
|
328
|
+
if get_config(:customization_vlan)
|
329
|
+
network = find_network(get_config(:customization_vlan))
|
330
|
+
card = src_config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).first or
|
331
|
+
abort "Can't find source network card to customize"
|
332
|
+
begin
|
333
|
+
switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(:switchUuid => network.config.distributedVirtualSwitch.uuid, :portgroupKey => network.key)
|
334
|
+
card.backing.port = switch_port
|
335
|
+
rescue
|
336
|
+
# not connected to a distibuted switch?
|
337
|
+
card.backing.deviceName = network.name
|
338
|
+
end
|
339
|
+
dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(:device => card, :operation => "edit")
|
340
|
+
clone_spec.config.deviceChange.push dev_spec
|
341
|
+
end
|
342
|
+
|
343
|
+
if get_config(:customization_spec)
|
344
|
+
csi = find_customization(get_config(:customization_spec)) or
|
345
|
+
fatal_exit("failed to find customization specification named #{get_config(:customization_spec)}")
|
346
|
+
|
347
|
+
if csi.info.type != "Linux"
|
348
|
+
fatal_exit("Only Linux customization specifications are currently supported")
|
349
|
+
end
|
350
|
+
cust_spec = csi.spec
|
351
|
+
else
|
352
|
+
global_ipset = RbVmomi::VIM.CustomizationGlobalIPSettings
|
353
|
+
cust_spec = RbVmomi::VIM.CustomizationSpec(:globalIPSettings => global_ipset)
|
354
|
+
end
|
355
|
+
|
356
|
+
if get_config(:customization_dns_ips)
|
357
|
+
cust_spec.globalIPSettings.dnsServerList = get_config(:customization_dns_ips).split(',')
|
358
|
+
end
|
359
|
+
|
360
|
+
if get_config(:customization_dns_suffixes)
|
361
|
+
cust_spec.globalIPSettings.dnsSuffixList = get_config(:customization_dns_suffixes).split(',')
|
362
|
+
end
|
363
|
+
|
364
|
+
if config[:customization_ips]
|
365
|
+
if get_config(:customization_gw)
|
366
|
+
cust_spec.nicSettingMap = config[:customization_ips].split(',').map { |i| generate_adapter_map(i, get_config(:customization_gw)) }
|
367
|
+
else
|
368
|
+
cust_spec.nicSettingMap = config[:customization_ips].split(',').map { |i| generate_adapter_map(i) }
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
unless get_config(:disable_customization)
|
373
|
+
use_ident = !config[:customization_hostname].nil? || !get_config(:customization_domain).nil? || cust_spec.identity.nil?
|
374
|
+
|
375
|
+
|
376
|
+
if use_ident
|
377
|
+
# TODO - verify that we're deploying a linux spec, at least warn
|
378
|
+
ident = RbVmomi::VIM.CustomizationLinuxPrep
|
379
|
+
|
380
|
+
ident.hostName = RbVmomi::VIM.CustomizationFixedName
|
381
|
+
if config[:customization_hostname]
|
382
|
+
ident.hostName.name = config[:customization_hostname]
|
383
|
+
else
|
384
|
+
ident.hostName.name = config[:vmname]
|
385
|
+
end
|
386
|
+
|
387
|
+
if get_config(:customization_domain)
|
388
|
+
ident.domain = get_config(:customization_domain)
|
389
|
+
else
|
390
|
+
ident.domain = ''
|
391
|
+
end
|
392
|
+
|
393
|
+
cust_spec.identity = ident
|
394
|
+
end
|
395
|
+
|
396
|
+
if customization_plugin && customization_plugin.respond_to?(:customize_clone_spec)
|
397
|
+
clone_spec = customization_plugin.customize_clone_spec(src_config, clone_spec)
|
398
|
+
end
|
399
|
+
|
400
|
+
clone_spec.customization = cust_spec
|
401
|
+
end
|
402
|
+
clone_spec
|
403
|
+
end
|
404
|
+
|
405
|
+
# Loads the customization plugin if one was specified
|
406
|
+
# @return [KnifeVspherePlugin] the loaded and initialized plugin or nil
|
407
|
+
def customization_plugin
|
408
|
+
if @customization_plugin.nil?
|
409
|
+
if cplugin_path = get_config(:customization_plugin)
|
410
|
+
if File.exists? cplugin_path
|
411
|
+
require cplugin_path
|
412
|
+
else
|
413
|
+
abort "Customization plugin could not be found at #{cplugin_path}"
|
414
|
+
end
|
415
|
+
|
416
|
+
if Object.const_defined? 'KnifeVspherePlugin'
|
417
|
+
@customization_plugin = Object.const_get('KnifeVspherePlugin').new
|
418
|
+
if cplugin_data = get_config(:customization_plugin_data)
|
419
|
+
if @customization_plugin.respond_to?(:data=)
|
420
|
+
@customization_plugin.data = cplugin_data
|
421
|
+
else
|
422
|
+
abort "Customization plugin has no :data= accessor to receive the --cplugin-data argument. Define both or neither."
|
423
|
+
end
|
424
|
+
end
|
425
|
+
else
|
426
|
+
abort "KnifeVspherePlugin class is not defined in #{cplugin_path}"
|
427
|
+
end
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
@customization_plugin
|
432
|
+
end
|
433
|
+
|
434
|
+
# Retrieves a CustomizationSpecItem that matches the supplied name
|
435
|
+
# @param vim [Connection] VI Connection to use
|
436
|
+
# @param name [String] name of customization
|
437
|
+
# @return [RbVmomi::VIM::CustomizationSpecItem]
|
438
|
+
def find_customization(name)
|
439
|
+
csm = config[:vim].serviceContent.customizationSpecManager
|
440
|
+
csm.GetCustomizationSpec(:name => name)
|
441
|
+
end
|
442
|
+
|
443
|
+
# Generates a CustomizationAdapterMapping (currently only single IPv4 address) object
|
444
|
+
# @param ip [String] Any static IP address to use, otherwise DHCP
|
445
|
+
# @param gw [String] If static, the gateway for the interface, otherwise network address + 1 will be used
|
446
|
+
# @return [RbVmomi::VIM::CustomizationIPSettings]
|
447
|
+
def generate_adapter_map (ip=nil, gw=nil, dns1=nil, dns2=nil, domain=nil)
|
448
|
+
|
449
|
+
settings = RbVmomi::VIM.CustomizationIPSettings
|
450
|
+
|
451
|
+
if ip.nil?
|
452
|
+
settings.ip = RbVmomi::VIM::CustomizationDhcpIpGenerator
|
453
|
+
else
|
454
|
+
cidr_ip = NetAddr::CIDR.create(ip)
|
455
|
+
settings.ip = RbVmomi::VIM::CustomizationFixedIp(:ipAddress => cidr_ip.ip)
|
456
|
+
settings.subnetMask = cidr_ip.netmask_ext
|
457
|
+
|
458
|
+
# TODO - want to confirm gw/ip are in same subnet?
|
459
|
+
# Only set gateway on first IP.
|
460
|
+
if config[:customization_ips].split(',').first == ip
|
461
|
+
if gw.nil?
|
462
|
+
settings.gateway = [cidr_ip.network(:Objectify => true).next_ip]
|
463
|
+
else
|
464
|
+
gw_cidr = NetAddr::CIDR.create(gw)
|
465
|
+
settings.gateway = [gw_cidr.ip]
|
466
|
+
end
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
adapter_map = RbVmomi::VIM.CustomizationAdapterMapping
|
471
|
+
adapter_map.adapter = settings
|
472
|
+
adapter_map
|
473
|
+
end
|
474
|
+
|
475
|
+
def bootstrap_for_node()
|
476
|
+
Chef::Knife::Bootstrap.load_deps
|
477
|
+
bootstrap = Chef::Knife::Bootstrap.new
|
478
|
+
bootstrap.name_args = [config[:fqdn]]
|
479
|
+
bootstrap.config[:run_list] = get_config(:run_list).split(/[\s,]+/)
|
480
|
+
bootstrap.config[:secret_file] = get_config(:secret_file)
|
481
|
+
bootstrap.config[:ssh_user] = get_config(:ssh_user)
|
482
|
+
bootstrap.config[:ssh_password] = get_config(:ssh_password)
|
483
|
+
bootstrap.config[:ssh_port] = get_config(:ssh_port)
|
484
|
+
bootstrap.config[:identity_file] = get_config(:identity_file)
|
485
|
+
bootstrap.config[:chef_node_name] = get_config(:chef_node_name)
|
486
|
+
bootstrap.config[:prerelease] = get_config(:prerelease)
|
487
|
+
bootstrap.config[:bootstrap_version] = get_config(:bootstrap_version)
|
488
|
+
bootstrap.config[:distro] = get_config(:distro)
|
489
|
+
bootstrap.config[:use_sudo] = true unless get_config(:ssh_user) == 'root'
|
490
|
+
bootstrap.config[:template_file] = get_config(:template_file)
|
491
|
+
bootstrap.config[:environment] = get_config(:environment)
|
492
|
+
bootstrap.config[:first_boot_attributes] = get_config(:first_boot_attributes)
|
493
|
+
bootstrap.config[:log_level] = get_config(:log_level)
|
494
|
+
# may be needed for vpc_mode
|
495
|
+
bootstrap.config[:no_host_key_verify] = get_config(:no_host_key_verify)
|
496
|
+
bootstrap
|
497
|
+
end
|
498
|
+
|
499
|
+
def tcp_test_ssh(hostname)
|
500
|
+
tcp_socket = TCPSocket.new(hostname, get_config(:ssh_port))
|
501
|
+
readable = IO.select([tcp_socket], nil, nil, 5)
|
502
|
+
if readable
|
503
|
+
Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
|
504
|
+
true
|
505
|
+
else
|
506
|
+
false
|
507
|
+
end
|
508
|
+
rescue Errno::ETIMEDOUT
|
509
|
+
false
|
510
|
+
rescue Errno::EPERM
|
511
|
+
false
|
512
|
+
rescue Errno::ECONNREFUSED
|
513
|
+
sleep 2
|
514
|
+
false
|
515
|
+
rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH
|
516
|
+
sleep 2
|
517
|
+
false
|
518
|
+
ensure
|
519
|
+
tcp_socket && tcp_socket.close
|
520
|
+
end
|
521
|
+
end
|
@@ -1,47 +1,46 @@
|
|
1
|
-
# Author:: Brian Dupras (<bdupras@rallydev.com>)
|
2
|
-
# License:: Apache License, Version 2.0
|
3
|
-
|
4
|
-
require 'chef/knife'
|
5
|
-
require 'chef/knife/base_vsphere_command'
|
6
|
-
require 'rbvmomi'
|
7
|
-
require 'netaddr'
|
8
|
-
|
9
|
-
class Chef::Knife::VsphereVmConfig < Chef::Knife::BaseVsphereCommand
|
10
|
-
banner "knife vsphere vm config VMNAME PROPERTY VALUE. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html\" for allowed ATTRIBUTE values (any property of type xs:string is supported)."
|
11
|
-
|
12
|
-
get_common_options
|
13
|
-
|
14
|
-
def run
|
15
|
-
$stdout.sync = true
|
16
|
-
vmname = @name_args[0]
|
17
|
-
if vmname.nil?
|
18
|
-
show_usage
|
19
|
-
fatal_exit("You must specify a virtual machine name")
|
20
|
-
end
|
21
|
-
|
22
|
-
property_name = @name_args[1]
|
23
|
-
if property_name.nil?
|
24
|
-
show_usage
|
25
|
-
fatal_exit("You must specify a PROPERTY name (e.g. annotation)")
|
26
|
-
end
|
27
|
-
property_name = property_name.to_sym
|
28
|
-
|
29
|
-
property_value = @name_args[2]
|
30
|
-
if property_value.nil?
|
31
|
-
show_usage
|
32
|
-
fatal_exit("You must specify a PROPERTY value")
|
33
|
-
end
|
34
|
-
|
35
|
-
vim = get_vim_connection
|
36
|
-
|
37
|
-
dc = get_datacenter
|
38
|
-
folder = find_folder(get_config(:folder)) || dc.vmFolder
|
39
|
-
|
40
|
-
vm =
|
41
|
-
|
42
|
-
|
43
|
-
properties =
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
1
|
+
# Author:: Brian Dupras (<bdupras@rallydev.com>)
|
2
|
+
# License:: Apache License, Version 2.0
|
3
|
+
|
4
|
+
require 'chef/knife'
|
5
|
+
require 'chef/knife/base_vsphere_command'
|
6
|
+
require 'rbvmomi'
|
7
|
+
require 'netaddr'
|
8
|
+
|
9
|
+
class Chef::Knife::VsphereVmConfig < Chef::Knife::BaseVsphereCommand
|
10
|
+
banner "knife vsphere vm config VMNAME PROPERTY VALUE. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html\" for allowed ATTRIBUTE values (any property of type xs:string is supported)."
|
11
|
+
|
12
|
+
get_common_options
|
13
|
+
|
14
|
+
def run
|
15
|
+
$stdout.sync = true
|
16
|
+
vmname = @name_args[0]
|
17
|
+
if vmname.nil?
|
18
|
+
show_usage
|
19
|
+
fatal_exit("You must specify a virtual machine name")
|
20
|
+
end
|
21
|
+
|
22
|
+
property_name = @name_args[1]
|
23
|
+
if property_name.nil?
|
24
|
+
show_usage
|
25
|
+
fatal_exit("You must specify a PROPERTY name (e.g. annotation)")
|
26
|
+
end
|
27
|
+
property_name = property_name.to_sym
|
28
|
+
|
29
|
+
property_value = @name_args[2]
|
30
|
+
if property_value.nil?
|
31
|
+
show_usage
|
32
|
+
fatal_exit("You must specify a PROPERTY value")
|
33
|
+
end
|
34
|
+
|
35
|
+
vim = get_vim_connection
|
36
|
+
|
37
|
+
dc = get_datacenter
|
38
|
+
folder = find_folder(get_config(:folder)) || dc.vmFolder
|
39
|
+
|
40
|
+
vm = traverse_folders_for_vm(folder, vmname) or abort "VM #{vmname} not found"
|
41
|
+
|
42
|
+
properties = {}
|
43
|
+
properties[property_name] = property_value
|
44
|
+
vm.ReconfigVM_Task(:spec => RbVmomi::VIM.VirtualMachineConfigSpec(properties)).wait_for_completion
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: knife-vsphere
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.9.
|
5
|
+
version: 0.9.9
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ezra Pagel
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-03-27 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|