Created
September 5, 2011 15:28
-
-
Save rubiojr/1195252 to your computer and use it in GitHub Desktop.
Ruby VMWare VSphere API Example 1 (rbvmomi)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rbvmomi' | |
require 'pp' | |
require 'alchemist' | |
hyper = 'thunder03' | |
vim = RbVmomi::VIM.connect :host => hyper, :user => 'root', :password => 'secret', :insecure => true | |
# | |
# get current time | |
# | |
vim.serviceInstance.CurrentTime | |
# | |
# get datacenter | |
# | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.Datacenter.html | |
dc = vim.serviceInstance.find_datacenter | |
# get host object | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.HostSystem.html | |
host = dc.hostFolder.children.first.host.first | |
# | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.HardwareInfo.html | |
# | |
# host.hardware | |
# | |
puts host.hardware.memorySize.bytes.to.megabytes | |
puts host.hardware.cpuInfo.numCpuCores | |
# | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.RuntimeInfo.html | |
# | |
puts host.summary.runtime.powerState | |
# | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.Summary.QuickStats.html | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.Summary.html | |
puts host.summary.quickStats.overallMemoryUsage.megabytes | |
puts host.summary.quickStats.overallCpuUsage | |
puts host.summary.config.name | |
# | |
# Product Info | |
# | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.AboutInfo.html | |
# | |
puts host.summary.config.product.fullName | |
puts host.summary.config.product.apiType | |
puts host.summary.config.product.apiVersion | |
puts host.summary.config.product.osType | |
puts host.summary.config.product.productLineId | |
puts host.summary.config.product.vendor | |
puts host.summary.config.product.version | |
# List all VM | |
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html | |
vm = dc.vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine).find do |x| | |
puts x.name | |
puts x.summary.config.memorySizeMB | |
puts x.summary.config.numCpu | |
puts x.summary.config.numEthernetCards | |
puts x.summary.config.numVirtualDisks | |
end |
This is vbery good, but to get all machines, you n eed recursion like described here: http://stackoverflow.com/questions/19812868/getting-all-virtualmachines-using-api-rbvmomi
To get all VMs:
def list_vms(folder)
children = folder.children.find_all
children.each do |child|
if child.class == RbVmomi::VIM::VirtualMachine
puts child.name
elsif child.class == RbVmomi::VIM::Folder
list_vms(child)
end
end
end
Is there an example to edit .vmx file from vsphere api ?
Thanks!!!! Really Thanks
When I run this, I get undefined method 'host' for line 22
Can you please help on how to create a vm with rbvmomi! Please
host = dc.hostFolder.children.first.host.first gives
NoMethodError: undefined method `first' for #RbVmomi::VIM::Folder:0x0000562d6b2f6388
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're a lifesaver. Thank you.