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_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
53 54 55 |
# File 'lib/esx.rb', line 53 def cpu_cores @_host.hardware.cpuInfo.numCpuCores 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)
Default values above.
96 97 98 99 100 101 102 103 104 105 106 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 |
# File 'lib/esx.rb', line 96 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) }, { :operation => :add, :device => RbVmomi::VIM.VirtualE1000( :key => 0, :deviceInfo => { :label => 'Network Adapter 1', :summary => 'VM Network' }, :backing => RbVmomi::VIM.VirtualEthernetCardNetworkBackingInfo( :deviceName => 'VM Network' ), :addressType => 'generated') } ], :extraConfig => [ { :key => 'bios.bootOrder', :value => 'ethernet0' } ] } 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
72 73 74 75 76 77 78 |
# File 'lib/esx.rb', line 72 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
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/esx.rb', line 157 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
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/esx.rb', line 203 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
47 48 49 |
# File 'lib/esx.rb', line 47 def memory_size @_host.hardware.memorySize end |
#memory_usage ⇒ Object
Host memory usage in bytes
65 66 67 |
# File 'lib/esx.rb', line 65 def memory_usage @_host.summary.quickStats.overallMemoryUsage.megabytes.to.bytes.to_i 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
59 60 61 |
# File 'lib/esx.rb', line 59 def power_state @_host.summary.runtime.powerState end |
#remote_command(cmd) ⇒ Object
Run a command in the ESX host via SSH
182 183 184 185 186 |
# File 'lib/esx.rb', line 182 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
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/esx.rb', line 191 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 |