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
|
# File 'bin/esx', line 299
def execute
begin
host = ESX::Host.connect address, user, password, true, {:free_license=>free_license?}
rescue Exception => e
$stderr.puts "Can't connect to the host #{address}."
if debug?
$stderr.puts e.message
end
exit 1
end
if disk_size and disk_file
$stderr.puts "Both --disk-file and --disk-size specified. --disk-size will be ignored."
end
downloaded_file = nil
if disk_file.nil?
vm = host.create_vm :vm_name => name,
:datastore => datastore, :disk_type => :flat, :memory => memory,
:disk_size => disk_size,
:guest_id => guest_id, :nics => [{:mac_address => mac_address, :network => vm_network}]
else
df = disk_file.dup
if df.strip.chomp =~ /^http/
begin
downloaded_file = disk_file.dup
tmpfile = "#{tmpdir}/#{Time.now.to_i}.esx"
puts "Downloading file... (#{tmpfile})"
download! downloaded_file, tmpfile
puts
df = tmpfile
rescue Exception => e
FileUtils.rm_f(tmpfile)
$stderr.puts "Error downloading file from #{downloaded_file}."
$stderr.puts e.message if debug?
exit 1
end
end
raise Exception.new("Invalid disk file") if not File.exist?(df)
if not name
$stderr.puts "Invalid VM name."
$stderr.puts "Use --name option to specify the VM name"
exit 1
end
host.remote_command "mkdir /vmfs/volumes/#{datastore}/#{name}"
begin
host.import_disk df, "/vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk"
rescue Exception => e
$stderr.puts "Error uploading file to /vmfs/volumes/#{datastore}/#{name}/#{name}.vmdk"
$stderr.puts e.message if debug?
exit 1
end
if not downloaded_file.nil?
puts "Deleting tmp file #{df}" if debug?
FileUtils.rm_f(df)
end
vm = host.create_vm :vm_name => name,
:disk_file => "#{name}/#{name}.vmdk",
:datastore => datastore, :disk_type => :flat, :memory => memory,
:guest_id => guest_id, :nics => [{mac_address => mac_address, :network => vm_network}]
end
if poweron?
vm.power_on
end
end
|