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
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
|
# File 'lib/chef/knife/esx_vm_create.rb', line 293
def run
$stdout.sync = true
if config[:batch]
Chef::Log.info "Running in batch mode. Extra arguments will be ignored."
if not config[:async]
counter = 0
script = KnifeESX::DeployScript.new(config[:batch])
script.each_job do |job|
counter += 1
status, stdout, stderr = job.run
if status == 0
Chef::Log.info 'Ok'
else
Chef::Log.error 'Failed'
stderr.each_line do |l|
ui.error l
end
end
end
else
Chef::Log.info "Asynchronous boostrapping selected"
Chef::Log.info "Now do something productive while I finish my job ;)"
script = KnifeESX::DeployScript.new(config[:batch])
futures = []
script.each_job do |job|
futures << job.future(:run)
end
futures.each do |f|
f.value
end
end
return
end
if not config[:use_template]
unless config[:vm_disk]
ui.error("You have not provided a valid VMDK file. (--vm-disk)")
exit 1
end
unless File.exist?(config[:vm_disk])
ui.error("Invalid VMDK disk file (--vm-disk)")
exit 1
end
end
vm_name = config[:vm_name]
if not vm_name
ui.error("Invalid Virtual Machine name (--vm-name)")
exit 1
end
datastore = config[:datastore]
memory = config[:memory]
cpus = config[:cpus]||1
cpu_cores = config[:cpu_cores]||1
vm_disk = config[:vm_disk]
guest_id =config[:guest_id]
destination_path = "/vmfs/volumes/#{datastore}/#{vm_name}"
connection.remote_command "mkdir #{destination_path}"
ui.info "Creating VM #{vm_name}"
if config[:use_template]
ui.info "Using template #{config[:use_template]}"
if connection.template_exist?(config[:use_template])
puts "#{ui.color("Cloning template...",:magenta)}"
connection.copy_from_template config[:use_template], destination_path + "/#{vm_name}.vmdk"
else
ui.error "Template #{config[:use_template]} not found"
exit 1
end
else
puts "#{ui.color("Importing VM disk... ", :magenta)}"
connection.import_disk vm_disk, destination_path + "/#{vm_name}.vmdk"
end
vm = connection.create_vm :vm_name => vm_name,
:datastore => datastore,
:disk_file => "#{vm_name}/#{vm_name}.vmdk",
:memory => memory,
:cpus => cpus,
:cpu_cores => cpu_cores,
:guest_id => guest_id,
:nics => create_nics(config[:vm_network], config[:mac_address])
vm.power_on
puts "#{ui.color("VM Created", :cyan)}"
puts "#{ui.color("VM Name", :cyan)}: #{vm.name}"
puts "#{ui.color("VM Memory", :cyan)}: #{(vm.memory_size.to_f/1024/1024).round} MB"
return if config[:skip_bootstrap]
print "\n#{ui.color("Waiting server... ", :magenta)}"
timeout = 100
found = connection.virtual_machines.find { |v| v.name == vm.name }
loop do
if not vm.ip_address.nil? and not vm.ip_address.empty?
puts "\n#{ui.color("VM IP Address: #{vm.ip_address}", :cyan)}"
break
end
timeout -= 1
if timeout == 0
ui.error "Timeout trying to reach the VM. Does it have vmware-tools installed?"
exit 1
end
sleep 1
found = connection.virtual_machines.find { |v| v.name == vm.name }
end
print "\n#{ui.color("Waiting for sshd... ", :magenta)}"
print(".") until tcp_test_ssh(vm.ip_address) { sleep @initial_sleep_delay ||= 10; puts(" done") }
bootstrap_for_node(vm).run
puts "\n"
puts "#{ui.color("Name", :cyan)}: #{vm.name}"
puts "#{ui.color("IP Address", :cyan)}: #{vm.ip_address}"
puts "#{ui.color("Environment", :cyan)}: #{config[:environment] || '_default'}"
puts "#{ui.color("Run List", :cyan)}: #{config[:run_list].join(', ')}"
puts "#{ui.color("Done!", :green)}"
end
|