Class: ESX::Host
- Inherits:
-
Object
- Object
- ESX::Host
- Defined in:
- lib/esx.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.connect(host, user, password, insecure = true) ⇒ Object
Connect to a ESX host.
Instance Method Summary collapse
-
#cpu_cores ⇒ Object
Number of CPU cores available in this host.
- #create_net_dev(nic_id, spec) ⇒ Object
-
#create_vm(specification) ⇒ Object
Create a Virtual Machine.
-
#datastores ⇒ Object
Return a list of ESX::Datastore objects available in this host.
-
#host_info ⇒ Object
Return product info as an array of strings containing.
- #import_disk(source, destination, print_progress = true) ⇒ Object
-
#initialize(address, user, password) ⇒ Host
constructor
A new instance of Host.
-
#memory_size ⇒ Object
Host memory size in bytes.
-
#memory_usage ⇒ Object
Host memory usage in bytes .
-
#name ⇒ Object
Returns the name of this host.
-
#power_state ⇒ Object
Power state of this host.
-
#remote_command(cmd) ⇒ Object
Run a command in the ESX host via SSH.
-
#upload_file(source, dest, print_progress = true) ⇒ Object
Upload file.
- #vim=(vim) ⇒ Object
-
#virtual_machines ⇒ Object
Return a list of VM available in the inventory.
Constructor Details
#initialize(address, user, password) ⇒ Host
Returns a new instance of Host.
15 16 17 18 19 |
# File 'lib/esx.rb', line 15 def initialize(address, user, password) @address = address @password = password @user = user end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
13 14 15 |
# File 'lib/esx.rb', line 13 def address @address end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
13 14 15 |
# File 'lib/esx.rb', line 13 def password @password end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
13 14 15 |
# File 'lib/esx.rb', line 13 def user @user end |
Class Method Details
.connect(host, user, password, insecure = true) ⇒ Object
Connect to a ESX host
Requires hostname/ip, username and password
Host connection is insecure by default
26 27 28 29 30 31 |
# File 'lib/esx.rb', line 26 def self.connect(host, user, password, insecure=true) vim = RbVmomi::VIM.connect :host => host, :user => user, :password => password, :insecure => insecure host = Host.new(host, user,password) host.vim = vim host end |
Instance Method Details
#cpu_cores ⇒ Object
Number of CPU cores available in this host
returns a String
57 58 59 |
# File 'lib/esx.rb', line 57 def cpu_cores @_host.hardware.cpuInfo.numCpuCores end |
#create_net_dev(nic_id, spec) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/esx.rb', line 168 def create_net_dev(nic_id, spec) h = { :key => nic_id, :deviceInfo => { :label => "Network Adapter #{nic_id}", :summary => spec[:network] || 'VM Network' }, :backing => RbVmomi::VIM.VirtualEthernetCardNetworkBackingInfo( :deviceName => spec[:network] || 'VM Network' ) } if spec[:mac_address] h[:macAddress] = spec[:mac_address] h[:addressType] = 'manual' else h[:addressType] = 'generated' end h end |
#create_vm(specification) ⇒ Object
Create a Virtual Machine
Requires a Hash with the following keys:
:vm_name => name, (string, required)
:cpus => 1, #(int, optional)
:guest_id => 'otherGuest', #(string, optional)
:disk_size => 4096, #(in MB, optional)
:memory => 128, #(in MB, optional)
:datastore => datastore1 #(string, optional)
:disk_file => path to vmdk inside datastore (optional)
:disk_type => flat, sparse (default flat)
supported guest_id list: pubs.vmware.com/vsphere-50/index.jsp?topic=/com.vmware.wssdk.apiref.doc_50/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html
Default values above.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 |
# File 'lib/esx.rb', line 107 def create_vm(specification) spec = specification spec[:cpus] = (specification[:cpus] || 1).to_i spec[:guest_id] = specification[:guest_id] || 'otherGuest' if specification[:disk_size] spec[:disk_size] = (specification[:disk_size].to_i * 1024) else spec[:disk_size] = 4194304 end spec[:memory] = (specification[:memory] || 128).to_i if specification[:datastore] spec[:datastore] = "[#{specification[:datastore]}]" else spec[:datastore] = '[datastore1]' end vm_cfg = { :name => spec[:vm_name], :guestId => spec[:guest_id], :files => { :vmPathName => spec[:datastore] }, :numCPUs => spec[:cpus], :memoryMB => spec[:memory], :deviceChange => [ { :operation => :add, :device => RbVmomi::VIM.VirtualLsiLogicController( :key => 1000, :busNumber => 0, :sharedBus => :noSharing) } ], :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 |
#datastores ⇒ Object
Return a list of ESX::Datastore objects available in this host
80 81 82 83 84 85 86 |
# File 'lib/esx.rb', line 80 def datastores datastores = [] @_host.datastore.each do |ds| datastores << Datastore.wrap(ds) end datastores end |
#host_info ⇒ Object
Return product info as an array of strings containing
fullName, apiType, apiVersion, osType, productLineId, vendor, version
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/esx.rb', line 193 def host_info [ @_host.summary.config.product.fullName, @_host.summary.config.product.apiType, @_host.summary.config.product.apiVersion, @_host.summary.config.product.osType, @_host.summary.config.product.productLineId, @_host.summary.config.product.vendor, @_host.summary.config.product.version ] end |
#import_disk(source, destination, print_progress = true) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/esx.rb', line 239 def import_disk(source, destination, print_progress = true) tmp_dest = destination + ".tmp" Net::SSH.start(@address, @user, :password => @password) do |ssh| if not (ssh.exec! "ls #{destination} 2>/dev/null").nil? raise Exception.new("Destination file #{destination} already exists") end puts "Uploading file... (#{File.basename(source)})" ssh.scp.upload!(source, tmp_dest) do |ch, name, sent, total| if print_progress print "\rProgress: #{(sent.to_f * 100 / total.to_f).to_i}%" end end if print_progress puts "\nConverting disk..." ssh.exec "vmkfstools -i #{tmp_dest} --diskformat thin #{destination}; rm -f #{tmp_dest}" else ssh.exec "vmkfstools -i #{tmp_dest} --diskformat thin #{destination} >/dev/null 2>&1; rm -f #{tmp_dest}" end end puts end |
#memory_size ⇒ Object
Host memory size in bytes
returns a Fixnum
49 50 51 |
# File 'lib/esx.rb', line 49 def memory_size @_host.hardware.memorySize.to_i end |
#memory_usage ⇒ Object
Host memory usage in bytes
returns a Fixnum
73 74 75 |
# File 'lib/esx.rb', line 73 def memory_usage @_host.summary.quickStats.overallMemoryUsage * 1024 * 1024 end |
#name ⇒ Object
Returns the name of this host
41 42 43 |
# File 'lib/esx.rb', line 41 def name @_host.summary.config.name end |
#power_state ⇒ Object
Power state of this host
poweredOn, poweredOff
65 66 67 |
# File 'lib/esx.rb', line 65 def power_state @_host.summary.runtime.powerState end |
#remote_command(cmd) ⇒ Object
Run a command in the ESX host via SSH
218 219 220 221 222 |
# File 'lib/esx.rb', line 218 def remote_command(cmd) Net::SSH.start(@address, @user, :password => @password) do |ssh| ssh.exec! cmd end end |
#upload_file(source, dest, print_progress = true) ⇒ Object
Upload file
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/esx.rb', line 227 def upload_file(source, dest, print_progress = true) Net::SSH.start(@address, @user, :password => @password) do |ssh| puts "Uploading file... (#{File.basename(source)})" ssh.scp.upload!(source, dest) do |ch, name, sent, total| if print_progress print "\rProgress: #{(sent.to_f * 100 / total.to_f).to_i}% completed" end end end puts if print_progress end |
#vim=(vim) ⇒ Object
33 34 35 36 37 |
# File 'lib/esx.rb', line 33 def vim=(vim) @_vim = vim @_datacenter = @_vim.serviceInstance.find_datacenter @_host = @_datacenter.hostFolder.children.first.host.first end |