lib/esx.rb in esx-0.2.4 vs lib/esx.rb in esx-0.3.1
- old
+ new
@@ -4,11 +4,11 @@
require 'net/scp'
require 'net/ssh'
module ESX
- VERSION = '0.2.4'
+ VERSION = '0.3.1'
class Host
attr_reader :address, :user, :password
@@ -42,30 +42,38 @@
@_host.summary.config.name
end
# Host memory size in bytes
#
+ # returns a Fixnum
+ #
def memory_size
- @_host.hardware.memorySize
+ @_host.hardware.memorySize.to_i
end
# Number of CPU cores available in this host
#
+ # returns a String
+ #
def cpu_cores
@_host.hardware.cpuInfo.numCpuCores
end
# Power state of this host
#
+ # poweredOn, poweredOff
+ #
def power_state
@_host.summary.runtime.powerState
end
# Host memory usage in bytes
+ #
+ # returns a Fixnum
#
def memory_usage
- @_host.summary.quickStats.overallMemoryUsage.megabytes.to.bytes.to_i
+ @_host.summary.quickStats.overallMemoryUsage * 1024 * 1024
end
# Return a list of ESX::Datastore objects available in this host
#
@@ -122,44 +130,55 @@
:operation => :add,
:device => RbVmomi::VIM.VirtualLsiLogicController(
:key => 1000,
:busNumber => 0,
:sharedBus => :noSharing)
- },
- {
- :operation => :add,
- :device => RbVmomi::VIM.VirtualE1000(create_net_dev(specification))
}
],
:extraConfig => [
{
:key => 'bios.bootOrder',
:value => 'ethernet0'
}
]
}
-
+
+ #Add multiple nics
+ nics_count = 0
+ if spec[:nics]
+ spec[:nics].each do |nic_spec|
+ vm_cfg[:deviceChange].push(
+ {
+ :operation => :add,
+ :device => RbVmomi::VIM.VirtualE1000(create_net_dev(nics_count, nic_spec))
+
+ }
+ )
+ nics_count += 1
+ end
+ end
# VMDK provided, replace the empty vmdk
vm_cfg[:deviceChange].push(create_disk_spec(:disk_file => spec[:disk_file],
:disk_type => spec[:disk_type],
:disk_size => spec[:disk_size],
:datastore => spec[:datastore]))
VM.wrap(@_datacenter.vmFolder.CreateVM_Task(:config => vm_cfg, :pool => @_datacenter.hostFolder.children.first.resourcePool).wait_for_completion)
end
- def create_net_dev(spec)
+ def create_net_dev(nic_id, spec)
h = {
- :key => 0,
+ :key => nic_id,
:deviceInfo => {
- :label => 'Network Adapter 1',
- :summary => 'VM Network'
+ :label => "Network Adapter #{nic_id}",
+ :summary => spec[:network] || 'VM Network'
},
:backing => RbVmomi::VIM.VirtualEthernetCardNetworkBackingInfo(
- :deviceName => 'VM Network'
+ :deviceName => spec[:network] || 'VM Network'
)
}
+
if spec[:mac_address]
h[:macAddress] = spec[:mac_address]
h[:addressType] = 'manual'
else
h[:addressType] = 'generated'
@@ -296,11 +315,11 @@
# **This method should never be called manually.**
#
def self.wrap(vm)
_vm = VM.new
_vm.name = vm.name
- _vm.memory_size = vm.summary.config.memorySizeMB.megabytes.to.bytes
+ _vm.memory_size = vm.summary.config.memorySizeMB*1024*1024
_vm.cpus = vm.summary.config.numCpu
_vm.ethernet_cards_number = vm.summary.config.numEthernetCards
_vm.virtual_disks_number = vm.summary.config.numVirtualDisks
_vm.vm_object = vm
_vm
@@ -322,25 +341,12 @@
def power_off
vm_object.PowerOffVM_Task.wait_for_completion
end
# Destroy the VirtualMaching removing it from the inventory
- #
- # This operation does not destroy VM disks
- #
+ # and deleting the disk files
def destroy
- disks = vm_object.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
- #disks.select { |x| x.backing.parent == nil }.each do |disk|
- # spec = {
- # :deviceChange => [
- # {
- # :operation => :remove,
- # :device => disk
- # }
- # ]
- # }
- # vm_object.ReconfigVM_Task(:spec => spec).wait_for_completion
- #end
+ #disks = vm_object.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
vm_object.Destroy_Task.wait_for_completion
end
def reset
vm_object.ResetVM_Task.wait_for_completion