lib/esx.rb in esx-0.4.2 vs lib/esx.rb in esx-0.4.3
- old
+ new
@@ -2,14 +2,15 @@
require 'rbvmomi'
require 'alchemist'
require 'net/scp'
require 'net/ssh'
require 'erb'
+require 'tempfile'
module ESX
- VERSION = '0.4.2'
+ VERSION = '0.4.3'
if !defined? Log or Log.nil?
Log = Logger.new($stdout)
Log.formatter = proc do |severity, datetime, progname, msg|
"[ESX] #{severity}: #{msg}\n"
@@ -39,11 +40,11 @@
#
# Requires hostname/ip, username and password
#
# Host connection is insecure by default
def self.connect(host, user, password, insecure = true, opts = {})
- vim = RbVmomi::VIM.connect :host => host, :user => user, :password => password, :insecure => insecure
+ vim = RbVmomi::VIM.connect :host => host, :user => user, :password => password, :insecure => insecure, :rev => opts[:rev]||'4.0'
host = Host.new(host, user,password, opts)
host.vim = vim
host
end
@@ -182,18 +183,20 @@
unless @free_license
VM.wrap(@_datacenter.vmFolder.CreateVM_Task(:config => vm_cfg, :pool => @_datacenter.hostFolder.children.first.resourcePool).wait_for_completion,self)
else
gem_root = Gem::Specification.find_by_name("esx").gem_dir
template_path = File.join(gem_root, 'templates', 'vmx_template.erb')
+ spec[:guest_id] = convert_guest_id_for_vmdk(spec[:guest_id])
erb = ERB.new File.read(template_path)
vmx = erb.result binding
tmp_vmx = Tempfile.new 'vmx'
tmp_vmx.write vmx
tmp_vmx.close
ds = spec[:datastore]||'datastore1'
ds = ds.gsub('[','').gsub(']','')
vmx_path = "/vmfs/volumes/#{ds}/#{spec[:vm_name]}/#{spec[:vm_name]}.vmx"
+ remote_command "mkdir -p #{File.dirname(vmx_path)}"
upload_file tmp_vmx.path, vmx_path
remote_command "vim-cmd solo/registervm #{vmx_path}"
VM.wrap(@_datacenter.find_vm(spec[:vm_name]),self)
end
end
@@ -233,20 +236,33 @@
@_host.summary.config.product.vendor,
@_host.summary.config.product.version
]
end
- # Return a list of VM available in the inventory
+ # Return a list of VM available in the inventory * recursive search of folders as well
#
def virtual_machines
vms = []
- vm = @_datacenter.vmFolder.childEntity.each do |x|
- vms << VM.wrap(x,self)
- end
+ vm = @_datacenter.vmFolder
+ vms = recursive_virtual_machine(vm,vms)
+
vms
end
+ def recursive_virtual_machine(parentObject,array)
+ vm = parentObject.childEntity.each do |x|
+ if x.to_s.match("Folder")
+ array = recursive_virtual_machine(x,array)
+ else
+ array << VM.wrap(x,self)
+ end
+ end
+
+ array
+ end
+ private :recursive_virtual_machine
+
#
# Run a command in the ESX host via SSH
#
def remote_command(cmd)
output = ""
@@ -435,9 +451,18 @@
:capacityInKB => disk_size)
}
end
spec[:fileOperation] = :create if disk_file.nil?
spec
+ end
+
+ def convert_guest_id_for_vmdk(guest_id)
+ exceptions = {
+ "winLonghornGuest" => "longhorn",
+ "winLonghorn64Guest" => "longhorn-64",
+ }
+ return exceptions[guest_id] if exceptions[guest_id]
+ guest_id.downcase.gsub(/guest/, '').gsub(/_?64/, '-64')
end
end
class VM