vmware-archive / rbvmomi

Ruby interface to the VMware vSphere API.
MIT License
301 stars 174 forks source link

Issues with sample script #94

Open ChrisPr1 opened 8 years ago

ChrisPr1 commented 8 years ago

I'm new to rbvmomi, and trying to get the create_vm-1.9.rb script working.

I'm at home now, and will post the exact error output tomorrow (no access to the dev system from home, a blessing really ;) ), but my issue seems to be with the following code, specifically that I get an exception on the rb = hosts.first.resourcePool with an error message that seems to indicate that no resourcePool objects exist. Indeed, if I simply do a 'puts hosts' I get two 'Folder' object types and two ClusterComputeResource object types as the children of the dc.hostFolder managed object.

Assuming that 'resourcePool' in the API maps to configured 'Resource Pools' visible in vcenter I can confirm that I do NOT have any resource pools configured, only clusters. This is on vSphere 5.5 .

vim = VIM.connect opts
dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
vmFolder = dc.vmFolder
hosts = dc.hostFolder.children
rp = hosts.first.resourcePool

I've been digging through the vSphere SDK reference guide, and have been trying to alter the parsing of the dc.hostFolder tree to return me the clusstercomputeresource's instead of resourcePools, but I'm a bit confused.

jrgarcia commented 8 years ago

Which sample script are you referring to? What is the issue with it?

ChrisPr1 commented 8 years ago

JR, I accidentally hit 'submit' before typing anything, apologies.

I've updated my OP with the relevant information, and will update this thread with the exact error messages tomorrow and some better detail on what exactly I've tried to manipulate the hostFolder children tree to return me ClusterComputerResources. I noticed that the rbvmomi/lib/vim/ directory doesnt have a ClusterComputeResource.rb file. Does the current release of rbvmomi support such a hostFolder child object?

Yours in confusion.

ChrisPr1 commented 8 years ago

I've got this resolved now.

2 things:

1) The script does not take into account a vsphere without a top level hostFolder.resourcePool defined. My local env has two ClusterComputeResources defined as children of the top level hostFolder. I had to modify the script to iterate through the hostFolder tree until i got to the specific ClusterComputeResource I had perms to deploy into and then set resourcePool to 'myClusterComputeResource.resourcePool'

2) I do not have full administrative permissions to vcenter, and the script assumes the ability to write to dc.vmFolder. I'm restricted to deploying to/writing to a specific subfolder of the top level dc.vmFolder object. Again, I had to iterate through the children of dc.vmFolder until I arrived at the Folder object I am allowed to write into and set the script target folder accordingly.

So my 'issues' are mostly specific to my env, but hopefully someone having issues getting started with rbvmomi and the example scripts might read this and understand that IF you do not have full vsphere administrator privs associated with your vcenter login then the example scripts wont work 'out of the box'

jrgarcia commented 8 years ago

Thanks for the feedback! I'll definitely look at updating this script to be a bit easier to use.

ChrisPr1 commented 8 years ago

JR, here's my simple nested interation for a specific ClusterComputeResource.resourcePool and a specific child of vmFolder. Should be easy enough to modify to pass in values for a specific resourcePool and child of vmFolder. Im fairly new to ruby, so I'm finding variable scoping to be a bit of a pain. Maybe split it into two different methods and pass references in and out?

vim = VIM.connect opts
dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
vm = dc.find_vm(vm_source) or abort "VM not found"

dc.hostFolder.children.each do |f|
  if f.name == "My Compute Cluster as shown in vCenter"
    relocateSpec = VIM.VirtualMachineRelocateSpec(:pool => f.resourcePool )
    spec = VIM.VirtualMachineCloneSpec(:location => relocateSpec,
                                       :powerOn => false,
                                       :template => false)

    dc.vmFolder.children.each do |folder|
      if folder.name == "name of Subfolder within my Datacenter I can write to as shown in vcenter"
        vm.CloneVM_Task(:folder => folder, :name => vm_target, :spec => spec).wait_for_completion
      end
    end
  end
end
jrgarcia commented 8 years ago

Thanks for that!

nilsparmar commented 4 years ago

I'm facing issue while creating new vm using create_VM.rb, it gives below error: wait_for_completion': InvalidDeviceSpec: Invalid configuration for device '1'. (RbVmomi::Fault) from ./create_VM.rb:94:in

'

  • Can Anyone Help me?
ChrisPr1 commented 4 years ago

I'm facing issue while creating new vm using create_VM.rb, it gives below error: wait_for_completion': InvalidDeviceSpec: Invalid configuration for device '1'. (RbVmomi::Fault) from ./create_VM.rb:94:in '

* Can Anyone Help me?

Without seeing your code I cant be 100%, but that kind of error is almost certainly related to the way in which you are defining your VM object and its associated devices.

nilsparmar commented 4 years ago

I'm facing issue while creating new vm using create_VM.rb, it gives below error: wait_for_completion': InvalidDeviceSpec: Invalid configuration for device '1'. (RbVmomi::Fault) from ./create_VM.rb:94:in '

* Can Anyone Help me?

Without seeing your code I cant be 100%, but that kind of error is almost certainly related to the way in which you are defining your VM object and its associated devices.

here is my code:

vm_cfg = { :name => vmname_var , :guestId => 'otherGuest', :files => { :vmPathName => '[datastore1]' }, :numCPUs => 1, :memoryMB => 128, :deviceChange => [ { :operation => :add, :device => VIM.VirtualLsiLogicController( :key => 1000, :busNumber => 0, :sharedBus => :noSharing ) }, { :operation => :add, :fileOperation => :create, :device => VIM.VirtualDisk( :key => 0, :backing => VIM.VirtualDiskFlatVer2BackingInfo( :fileName => '[datastore1]', :diskMode => :persistent, :thinProvisioned => true ), :controllerKey => 1000, :unitNumber => 0, :capacityInKB => 400000 ) }, { :operation => :add, :device => VIM.VirtualE1000( :key => 0, :deviceInfo => { :label => 'Network Adapter 1', :summary => 'VM Network' }, :backing => VIM.VirtualEthernetCardNetworkBackingInfo( :deviceName => 'VM Network' ), :addressType => 'generated' ) } ], :extraConfig => [ { :key => 'bios.bootOrder', :value => 'ethernet0' } ] }

ChrisPr1 commented 4 years ago

Please use the "\" tags when posting code so the formatting doesnt get all munged,

Here's a sample that I'm 99% sure still works (havent used it in over 2 years, api may have changed?) from my code at

https://github.com/ChrisPr1/infra_automation/blob/master/vcenter/create_vm.rb


  name: hostname,
  guestId: vmware_clientId,
  files: { vmPathName: datastore },
  numCPUs: cpus,
  memoryMB: memory,
  deviceChange: [
    {
      operation: :add,
      device: VIM.ParaVirtualSCSIController( key: 1000, busNumber: 0, sharedBus: :noSharing,)
    }, {
      operation: :add,
      fileOperation: :create,
      device: VIM.VirtualDisk(
        key: 0,
        backing: VIM.VirtualDiskFlatVer2BackingInfo( fileName: datastore, diskMode: :persistent, thinProvisioned: true,),
        controllerKey: 1000,
        unitNumber: 0,
        capacityInKB: disk0_Size,
      )
    }, {
      operation: :add,
      device: VIM.VirtualVmxnet3(
        key: 0,
        deviceInfo: { label: 'Network Adapter 1', summary: 'Network Adapter 1', },
        backing: network_backing,
        addressType: 'generated'
      )
    }

  ]
}