knife-vsphere 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/chef/knife/BaseVsphereCommand.rb +169 -167
- data/lib/chef/knife/vsphere_customization_list.rb +29 -29
- data/lib/chef/knife/vsphere_datastore_list.rb +58 -0
- data/lib/chef/knife/vsphere_template_list.rb +32 -32
- data/lib/chef/knife/vsphere_vm_clone.rb +397 -373
- data/lib/chef/knife/vsphere_vm_delete.rb +39 -39
- data/lib/chef/knife/vsphere_vm_list.rb +32 -28
- data/lib/chef/knife/vsphere_vm_state.rb +85 -85
- data/lib/knife-vsphere/version.rb +4 -4
- metadata +38 -46
@@ -1,32 +1,32 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
|
6
|
-
require 'chef/knife'
|
7
|
-
require 'chef/knife/BaseVsphereCommand'
|
8
|
-
|
9
|
-
# Lists all known VM templates in the configured datacenter
|
10
|
-
class Chef::Knife::VsphereTemplateList < Chef::Knife::BaseVsphereCommand
|
11
|
-
|
12
|
-
banner "knife vsphere template list"
|
13
|
-
|
14
|
-
get_common_options
|
15
|
-
|
16
|
-
def run
|
17
|
-
|
18
|
-
$stdout.sync = true
|
19
|
-
$stderr.sync = true
|
20
|
-
|
21
|
-
vim = get_vim_connection
|
22
|
-
|
23
|
-
baseFolder = find_folder(config[:folder]);
|
24
|
-
|
25
|
-
vms = find_all_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine).
|
26
|
-
select {|v| !v.config.nil? && v.config.template == true }
|
27
|
-
|
28
|
-
vms.each do |vm|
|
29
|
-
puts "#{ui.color("Template Name", :cyan)}: #{vm.name}"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
1
|
+
#
|
2
|
+
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
|
6
|
+
require 'chef/knife'
|
7
|
+
require 'chef/knife/BaseVsphereCommand'
|
8
|
+
|
9
|
+
# Lists all known VM templates in the configured datacenter
|
10
|
+
class Chef::Knife::VsphereTemplateList < Chef::Knife::BaseVsphereCommand
|
11
|
+
|
12
|
+
banner "knife vsphere template list"
|
13
|
+
|
14
|
+
get_common_options
|
15
|
+
|
16
|
+
def run
|
17
|
+
|
18
|
+
$stdout.sync = true
|
19
|
+
$stderr.sync = true
|
20
|
+
|
21
|
+
vim = get_vim_connection
|
22
|
+
|
23
|
+
baseFolder = find_folder(config[:folder]);
|
24
|
+
|
25
|
+
vms = find_all_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine).
|
26
|
+
select {|v| !v.config.nil? && v.config.template == true }
|
27
|
+
|
28
|
+
vms.each do |vm|
|
29
|
+
puts "#{ui.color("Template Name", :cyan)}: #{vm.name}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,373 +1,397 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
-
# Contributor:: Jesse Campbell (<hikeit@gmail.com>)
|
4
|
-
# License:: Apache License, Version 2.0
|
5
|
-
#
|
6
|
-
|
7
|
-
require 'chef/knife'
|
8
|
-
require 'chef/knife/BaseVsphereCommand'
|
9
|
-
require 'rbvmomi'
|
10
|
-
require 'netaddr'
|
11
|
-
|
12
|
-
# Clone an existing template into a new VM, optionally applying a customization specification.
|
13
|
-
#
|
14
|
-
# usage:
|
15
|
-
# knife vsphere vm clone NewNode UbuntuTemplate --cspec StaticSpec \
|
16
|
-
# --cips 192.168.0.99/24,192.168.1.99/24 \
|
17
|
-
# --chostname NODENAME --cdomain NODEDOMAIN
|
18
|
-
class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
|
19
|
-
|
20
|
-
banner "knife vsphere vm clone VMNAME (options)"
|
21
|
-
|
22
|
-
get_common_options
|
23
|
-
|
24
|
-
option :dest_folder,
|
25
|
-
:long => "--dest-folder FOLDER",
|
26
|
-
:description => "The folder into which to put the cloned VM"
|
27
|
-
|
28
|
-
option :datastore,
|
29
|
-
:long => "--datastore STORE",
|
30
|
-
:description => "The datastore into which to put the cloned VM"
|
31
|
-
|
32
|
-
option :resource_pool,
|
33
|
-
:long => "--resource-pool POOL",
|
34
|
-
:description => "The resource pool into which to put the cloned VM"
|
35
|
-
|
36
|
-
option :source_vm,
|
37
|
-
:long => "--template TEMPLATE",
|
38
|
-
:description => "The source VM / Template to clone from",
|
39
|
-
:required => true
|
40
|
-
|
41
|
-
option :customization_spec,
|
42
|
-
:long => "--cspec CUST_SPEC",
|
43
|
-
:description => "The name of any customization specification to apply"
|
44
|
-
|
45
|
-
option :customization_vlan,
|
46
|
-
:long => "--cvlan CUST_VLAN",
|
47
|
-
:description => "VLAN name for network adapter to join"
|
48
|
-
|
49
|
-
option :customization_ips,
|
50
|
-
:long => "--cips CUST_IPS",
|
51
|
-
:description => "Comma-delimited list of CIDR IPs for customization"
|
52
|
-
|
53
|
-
option :
|
54
|
-
:long => "--
|
55
|
-
:description => "
|
56
|
-
|
57
|
-
option :
|
58
|
-
:long => "--
|
59
|
-
:description => "
|
60
|
-
|
61
|
-
option :
|
62
|
-
:long => "--
|
63
|
-
:description => "
|
64
|
-
|
65
|
-
option :
|
66
|
-
:long => "--
|
67
|
-
:description => "
|
68
|
-
|
69
|
-
option :
|
70
|
-
:long => "--
|
71
|
-
:description => "
|
72
|
-
|
73
|
-
option :
|
74
|
-
:long => "--
|
75
|
-
:description => "
|
76
|
-
|
77
|
-
option :
|
78
|
-
:long => "--
|
79
|
-
:description => "
|
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
|
-
:long => "--
|
125
|
-
:description => "The
|
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
|
-
end
|
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
|
-
end
|
1
|
+
#
|
2
|
+
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
+
# Contributor:: Jesse Campbell (<hikeit@gmail.com>)
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'chef/knife'
|
8
|
+
require 'chef/knife/BaseVsphereCommand'
|
9
|
+
require 'rbvmomi'
|
10
|
+
require 'netaddr'
|
11
|
+
|
12
|
+
# Clone an existing template into a new VM, optionally applying a customization specification.
|
13
|
+
#
|
14
|
+
# usage:
|
15
|
+
# knife vsphere vm clone NewNode UbuntuTemplate --cspec StaticSpec \
|
16
|
+
# --cips 192.168.0.99/24,192.168.1.99/24 \
|
17
|
+
# --chostname NODENAME --cdomain NODEDOMAIN
|
18
|
+
class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
|
19
|
+
|
20
|
+
banner "knife vsphere vm clone VMNAME (options)"
|
21
|
+
|
22
|
+
get_common_options
|
23
|
+
|
24
|
+
option :dest_folder,
|
25
|
+
:long => "--dest-folder FOLDER",
|
26
|
+
:description => "The folder into which to put the cloned VM"
|
27
|
+
|
28
|
+
option :datastore,
|
29
|
+
:long => "--datastore STORE",
|
30
|
+
:description => "The datastore into which to put the cloned VM"
|
31
|
+
|
32
|
+
option :resource_pool,
|
33
|
+
:long => "--resource-pool POOL",
|
34
|
+
:description => "The resource pool into which to put the cloned VM"
|
35
|
+
|
36
|
+
option :source_vm,
|
37
|
+
:long => "--template TEMPLATE",
|
38
|
+
:description => "The source VM / Template to clone from",
|
39
|
+
:required => true
|
40
|
+
|
41
|
+
option :customization_spec,
|
42
|
+
:long => "--cspec CUST_SPEC",
|
43
|
+
:description => "The name of any customization specification to apply"
|
44
|
+
|
45
|
+
option :customization_vlan,
|
46
|
+
:long => "--cvlan CUST_VLAN",
|
47
|
+
:description => "VLAN name for network adapter to join"
|
48
|
+
|
49
|
+
option :customization_ips,
|
50
|
+
:long => "--cips CUST_IPS",
|
51
|
+
:description => "Comma-delimited list of CIDR IPs for customization"
|
52
|
+
|
53
|
+
option :customization_dns_ips,
|
54
|
+
:long => "--cdnsips CUST_DNS_IPS",
|
55
|
+
:description => "Comma-delimited list of DNS IP addresses"
|
56
|
+
|
57
|
+
option :customization_dns_suffixes,
|
58
|
+
:long => "--cdnssuffix CUST_DNS_SUFFIXES",
|
59
|
+
:description => "Comma-delimited list of DNS search suffixes"
|
60
|
+
|
61
|
+
option :customization_gw,
|
62
|
+
:long => "--cgw CUST_GW",
|
63
|
+
:description => "CIDR IP of gateway for customization"
|
64
|
+
|
65
|
+
option :customization_hostname,
|
66
|
+
:long => "--chostname CUST_HOSTNAME",
|
67
|
+
:description => "Unqualified hostname for customization"
|
68
|
+
|
69
|
+
option :customization_domain,
|
70
|
+
:long => "--cdomain CUST_DOMAIN",
|
71
|
+
:description => "Domain name for customization"
|
72
|
+
|
73
|
+
option :customization_tz,
|
74
|
+
:long => "--ctz CUST_TIMEZONE",
|
75
|
+
:description => "Timezone invalid 'Area/Location' format"
|
76
|
+
|
77
|
+
option :customization_cpucount,
|
78
|
+
:long => "--ccpu CUST_CPU_COUNT",
|
79
|
+
:description => "Number of CPUs"
|
80
|
+
|
81
|
+
option :customization_memory,
|
82
|
+
:long => "--cram CUST_MEMORY_GB",
|
83
|
+
:description => "Gigabytes of RAM"
|
84
|
+
|
85
|
+
option :power,
|
86
|
+
:long => "--start STARTVM",
|
87
|
+
:description => "Indicates whether to start the VM after a successful clone",
|
88
|
+
:default => false
|
89
|
+
|
90
|
+
option :bootstrap,
|
91
|
+
:long => "--bootstrap FALSE",
|
92
|
+
:description => "Indicates whether to bootstrap the VM",
|
93
|
+
:default => false
|
94
|
+
|
95
|
+
option :fqdn,
|
96
|
+
:long => "--fqdn SERVER_FQDN",
|
97
|
+
:description => "Fully qualified hostname for bootstrapping"
|
98
|
+
|
99
|
+
option :ssh_user,
|
100
|
+
:short => "-x USERNAME",
|
101
|
+
:long => "--ssh-user USERNAME",
|
102
|
+
:description => "The ssh username",
|
103
|
+
:default => "root"
|
104
|
+
|
105
|
+
option :ssh_password,
|
106
|
+
:short => "-P PASSWORD",
|
107
|
+
:long => "--ssh-password PASSWORD",
|
108
|
+
:description => "The ssh password"
|
109
|
+
|
110
|
+
option :ssh_port,
|
111
|
+
:short => "-p PORT",
|
112
|
+
:long => "--ssh-port PORT",
|
113
|
+
:description => "The ssh port",
|
114
|
+
:default => "22",
|
115
|
+
:proc => Proc.new { |key| Chef::Config[:knife][:ssh_port] = key }
|
116
|
+
|
117
|
+
option :identity_file,
|
118
|
+
:short => "-i IDENTITY_FILE",
|
119
|
+
:long => "--identity-file IDENTITY_FILE",
|
120
|
+
:description => "The SSH identity file used for authentication"
|
121
|
+
|
122
|
+
option :chef_node_name,
|
123
|
+
:short => "-N NAME",
|
124
|
+
:long => "--node-name NAME",
|
125
|
+
:description => "The Chef node name for your new node"
|
126
|
+
|
127
|
+
option :prerelease,
|
128
|
+
:long => "--prerelease",
|
129
|
+
:description => "Install the pre-release chef gems"
|
130
|
+
|
131
|
+
option :bootstrap_version,
|
132
|
+
:long => "--bootstrap-version VERSION",
|
133
|
+
:description => "The version of Chef to install",
|
134
|
+
:proc => lambda { |v| Chef::Config[:knife][:bootstrap_version] = v }
|
135
|
+
|
136
|
+
option :bootstrap_proxy,
|
137
|
+
:long => "--bootstrap-proxy PROXY_URL",
|
138
|
+
:description => "The proxy server for the node being bootstrapped",
|
139
|
+
:proc => Proc.new { |p| Chef::Config[:knife][:bootstrap_proxy] = p }
|
140
|
+
|
141
|
+
option :distro,
|
142
|
+
:short => "-d DISTRO",
|
143
|
+
:long => "--distro DISTRO",
|
144
|
+
:description => "Bootstrap a distro using a template",
|
145
|
+
:default => "ubuntu10.04-gems"
|
146
|
+
|
147
|
+
option :template_file,
|
148
|
+
:long => "--template-file TEMPLATE",
|
149
|
+
:description => "Full path to location of template to use",
|
150
|
+
:default => false
|
151
|
+
|
152
|
+
option :run_list,
|
153
|
+
:short => "-r RUN_LIST",
|
154
|
+
:long => "--run-list RUN_LIST",
|
155
|
+
:description => "Comma separated list of roles/recipes to apply",
|
156
|
+
:proc => lambda { |o| o.split(/[\s,]+/) },
|
157
|
+
:default => []
|
158
|
+
|
159
|
+
option :no_host_key_verify,
|
160
|
+
:long => "--no-host-key-verify",
|
161
|
+
:description => "Disable host key verification",
|
162
|
+
:boolean => true,
|
163
|
+
:default => false
|
164
|
+
|
165
|
+
def run
|
166
|
+
$stdout.sync = true
|
167
|
+
|
168
|
+
vmname = @name_args[0]
|
169
|
+
if vmname.nil?
|
170
|
+
show_usage
|
171
|
+
fatal_exit("You must specify a virtual machine name")
|
172
|
+
end
|
173
|
+
config[:fqdn] = vmname unless config[:fqdn]
|
174
|
+
config[:chef_node_name] = vmname unless config[:chef_node_name]
|
175
|
+
config[:vmname] = vmname
|
176
|
+
|
177
|
+
vim = get_vim_connection
|
178
|
+
|
179
|
+
dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
|
180
|
+
dc = vim.serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
|
181
|
+
|
182
|
+
src_folder = find_folder(config[:folder]) || dc.vmFolder
|
183
|
+
|
184
|
+
src_vm = find_in_folder(src_folder, RbVmomi::VIM::VirtualMachine, config[:source_vm]) or
|
185
|
+
abort "VM/Template not found"
|
186
|
+
|
187
|
+
clone_spec = generate_clone_spec(src_vm.config)
|
188
|
+
|
189
|
+
cust_folder = config[:dest_folder] || config[:folder]
|
190
|
+
|
191
|
+
dest_folder = cust_folder.nil? ? src_vm.vmFolder : find_folder(cust_folder)
|
192
|
+
|
193
|
+
task = src_vm.CloneVM_Task(:folder => dest_folder, :name => vmname, :spec => clone_spec)
|
194
|
+
puts "Cloning template #{config[:source_vm]} to new VM #{vmname}"
|
195
|
+
task.wait_for_completion
|
196
|
+
puts "Finished creating virtual machine #{vmname}"
|
197
|
+
|
198
|
+
if config[:power] || config[:bootstrap]
|
199
|
+
vm = find_in_folder(dest_folder, RbVmomi::VIM::VirtualMachine, vmname) or
|
200
|
+
fatal_exit("VM #{vmname} not found")
|
201
|
+
vm.PowerOnVM_Task.wait_for_completion
|
202
|
+
puts "Powered on virtual machine #{vmname}"
|
203
|
+
end
|
204
|
+
|
205
|
+
if config[:bootstrap]
|
206
|
+
print "Waiting for sshd..."
|
207
|
+
print "." until tcp_test_ssh(config[:fqdn])
|
208
|
+
puts "done"
|
209
|
+
|
210
|
+
bootstrap_for_node.run
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
# Builds a CloneSpec
|
215
|
+
def generate_clone_spec (src_config)
|
216
|
+
|
217
|
+
rspec = nil
|
218
|
+
if config[:resource_pool]
|
219
|
+
rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => find_pool(config[:resource_pool]))
|
220
|
+
else
|
221
|
+
dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
|
222
|
+
dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
|
223
|
+
hosts = find_all_in_folder(dc.hostFolder, RbVmomi::VIM::ComputeResource)
|
224
|
+
rp = hosts.first.resourcePool
|
225
|
+
rspec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => rp)
|
226
|
+
end
|
227
|
+
|
228
|
+
if config[:datastore]
|
229
|
+
rspec.datastore = find_datastore(config[:datastore])
|
230
|
+
end
|
231
|
+
|
232
|
+
clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(:location => rspec,
|
233
|
+
:powerOn => false,
|
234
|
+
:template => false)
|
235
|
+
|
236
|
+
clone_spec.config = RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange => Array.new)
|
237
|
+
|
238
|
+
if config[:customization_cpucount]
|
239
|
+
clone_spec.config.numCPUs = config[:customization_cpucount]
|
240
|
+
end
|
241
|
+
|
242
|
+
if config[:customization_memory]
|
243
|
+
clone_spec.config.memoryMB = Integer(config[:customization_memory]) * 1024
|
244
|
+
end
|
245
|
+
|
246
|
+
if config[:customization_vlan]
|
247
|
+
network = find_network(config[:customization_vlan])
|
248
|
+
switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(:switchUuid => network.config.distributedVirtualSwitch.uuid ,:portgroupKey => network.key)
|
249
|
+
card = src_config.hardware.device.find { |d| d.deviceInfo.label == "Network adapter 1" } or
|
250
|
+
abort "Can't find source network card to customize"
|
251
|
+
card.backing.port = switch_port
|
252
|
+
dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(:device => card, :operation => "edit")
|
253
|
+
clone_spec.config.deviceChange.push dev_spec
|
254
|
+
end
|
255
|
+
|
256
|
+
if config[:customization_spec]
|
257
|
+
csi = find_customization(config[:customization_spec]) or
|
258
|
+
fatal_exit("failed to find customization specification named #{config[:customization_spec]}")
|
259
|
+
|
260
|
+
if csi.info.type != "Linux"
|
261
|
+
fatal_exit("Only Linux customization specifications are currently supported")
|
262
|
+
end
|
263
|
+
cust_spec = csi.spec
|
264
|
+
else
|
265
|
+
global_ipset = RbVmomi::VIM.CustomizationGlobalIPSettings
|
266
|
+
cust_spec = RbVmomi::VIM.CustomizationSpec(:globalIPSettings => global_ipset)
|
267
|
+
end
|
268
|
+
|
269
|
+
if config[:customization_dns_ips]
|
270
|
+
cust_spec.globalIPSettings.dnsServerList = config[:customization_dns_ips].split(',')
|
271
|
+
end
|
272
|
+
|
273
|
+
if config[:customization_dns_suffixes]
|
274
|
+
cust_spec.globalIPSettings.dnsSuffixList = config[:customization_dns_suffixes].split(',')
|
275
|
+
end
|
276
|
+
|
277
|
+
if config[:customization_ips]
|
278
|
+
if config[:customization_gw]
|
279
|
+
cust_spec.nicSettingMap = config[:customization_ips].split(',').map { |i| generate_adapter_map(i,config[:customization_gw]) }
|
280
|
+
else
|
281
|
+
cust_spec.nicSettingMap = config[:customization_ips].split(',').map { |i| generate_adapter_map(i) }
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
use_ident = !config[:customization_hostname].nil? || !config[:customization_domain].nil? || cust_spec.identity.nil?
|
286
|
+
|
287
|
+
if use_ident
|
288
|
+
# TODO - verify that we're deploying a linux spec, at least warn
|
289
|
+
ident = RbVmomi::VIM.CustomizationLinuxPrep
|
290
|
+
|
291
|
+
ident.hostName = RbVmomi::VIM.CustomizationFixedName
|
292
|
+
if config[:customization_hostname]
|
293
|
+
ident.hostName.name = config[:customization_hostname]
|
294
|
+
elsif config[:customization_domain]
|
295
|
+
ident.hostName.name = config[:customization_domain]
|
296
|
+
else
|
297
|
+
ident.hostName.name = config[:vmname]
|
298
|
+
end
|
299
|
+
|
300
|
+
if config[:customization_domain]
|
301
|
+
ident.domain = config[:customization_domain]
|
302
|
+
else
|
303
|
+
ident.domain = ''
|
304
|
+
end
|
305
|
+
|
306
|
+
cust_spec.identity = ident
|
307
|
+
end
|
308
|
+
|
309
|
+
clone_spec.customization = cust_spec
|
310
|
+
clone_spec
|
311
|
+
end
|
312
|
+
|
313
|
+
# Retrieves a CustomizationSpecItem that matches the supplied name
|
314
|
+
# @param vim [Connection] VI Connection to use
|
315
|
+
# @param name [String] name of customization
|
316
|
+
# @return [RbVmomi::VIM::CustomizationSpecItem]
|
317
|
+
def find_customization(name)
|
318
|
+
csm = config[:vim].serviceContent.customizationSpecManager
|
319
|
+
csm.GetCustomizationSpec(:name => name)
|
320
|
+
end
|
321
|
+
|
322
|
+
# Generates a CustomizationAdapterMapping (currently only single IPv4 address) object
|
323
|
+
# @param ip [String] Any static IP address to use, otherwise DHCP
|
324
|
+
# @param gw [String] If static, the gateway for the interface, otherwise network address + 1 will be used
|
325
|
+
# @return [RbVmomi::VIM::CustomizationIPSettings]
|
326
|
+
def generate_adapter_map (ip=nil, gw=nil, dns1=nil, dns2=nil, domain=nil)
|
327
|
+
|
328
|
+
settings = RbVmomi::VIM.CustomizationIPSettings
|
329
|
+
|
330
|
+
if ip.nil?
|
331
|
+
settings.ip = RbVmomi::VIM::CustomizationDhcpIpGenerator
|
332
|
+
else
|
333
|
+
cidr_ip = NetAddr::CIDR.create(ip)
|
334
|
+
settings.ip = RbVmomi::VIM::CustomizationFixedIp(:ipAddress => cidr_ip.ip)
|
335
|
+
settings.subnetMask = cidr_ip.netmask_ext
|
336
|
+
|
337
|
+
# TODO - want to confirm gw/ip are in same subnet?
|
338
|
+
# Only set gateway on first IP.
|
339
|
+
if config[:customization_ips].split(',').first == ip
|
340
|
+
if gw.nil?
|
341
|
+
settings.gateway = [cidr_ip.network(:Objectify => true).next_ip]
|
342
|
+
else
|
343
|
+
gw_cidr = NetAddr::CIDR.create(gw)
|
344
|
+
settings.gateway = [gw_cidr.ip]
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
adapter_map = RbVmomi::VIM.CustomizationAdapterMapping
|
350
|
+
adapter_map.adapter = settings
|
351
|
+
adapter_map
|
352
|
+
end
|
353
|
+
|
354
|
+
def bootstrap_for_node()
|
355
|
+
Chef::Knife::Bootstrap.load_deps
|
356
|
+
bootstrap = Chef::Knife::Bootstrap.new
|
357
|
+
bootstrap.name_args = [config[:fqdn]]
|
358
|
+
bootstrap.config[:run_list] = config[:run_list]
|
359
|
+
bootstrap.config[:ssh_user] = config[:ssh_user]
|
360
|
+
bootstrap.config[:ssh_password] = config[:ssh_password]
|
361
|
+
bootstrap.config[:ssh_port] = config[:ssh_port]
|
362
|
+
bootstrap.config[:identity_file] = config[:identity_file]
|
363
|
+
bootstrap.config[:chef_node_name] = config[:chef_node_name]
|
364
|
+
bootstrap.config[:prerelease] = config[:prerelease]
|
365
|
+
bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
|
366
|
+
bootstrap.config[:distro] = locate_config_value(:distro)
|
367
|
+
bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
|
368
|
+
bootstrap.config[:template_file] = locate_config_value(:template_file)
|
369
|
+
bootstrap.config[:environment] = config[:environment]
|
370
|
+
# may be needed for vpc_mode
|
371
|
+
bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
|
372
|
+
bootstrap
|
373
|
+
end
|
374
|
+
|
375
|
+
def tcp_test_ssh(hostname)
|
376
|
+
tcp_socket = TCPSocket.new(hostname, 22)
|
377
|
+
readable = IO.select([tcp_socket], nil, nil, 5)
|
378
|
+
if readable
|
379
|
+
Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
|
380
|
+
true
|
381
|
+
else
|
382
|
+
false
|
383
|
+
end
|
384
|
+
rescue Errno::ETIMEDOUT
|
385
|
+
false
|
386
|
+
rescue Errno::EPERM
|
387
|
+
false
|
388
|
+
rescue Errno::ECONNREFUSED
|
389
|
+
sleep 2
|
390
|
+
false
|
391
|
+
rescue Errno::EHOSTUNREACH
|
392
|
+
sleep 2
|
393
|
+
false
|
394
|
+
ensure
|
395
|
+
tcp_socket && tcp_socket.close
|
396
|
+
end
|
397
|
+
end
|