diff --git a/vmware-host-pooler b/vmware-host-pooler index 1281d46..4247452 100755 --- a/vmware-host-pooler +++ b/vmware-host-pooler @@ -4,7 +4,7 @@ require 'rbvmomi' require 'redis' require 'yaml' -require File.expand_path( File.dirname( __FILE__ ), './lib/vsphere_helper.rb' ) +require File.expand_path( File.dirname( __FILE__ ), 'lib/vsphere_helper.rb' ) vsphere_helper = VsphereHelper.new # Load the pool configuration @@ -27,8 +27,7 @@ $vim = RbVmomi::VIM.connect( # Connect to Redis $redis = Redis.new - - +# Update loop loop do pools.each do |pool| total = 0 diff --git a/vmware-host-pooler-api b/vmware-host-pooler-api index 2f82803..87a587d 100755 --- a/vmware-host-pooler-api +++ b/vmware-host-pooler-api @@ -49,22 +49,22 @@ post '/vm/:template' do if ( ( ! params[:folder] ) or ( ! params[:pool] )) result[params[:template]]['error'] = 'You must specify a destination \'folder\' and \'pool\'' else - datacenter = $vim.serviceInstance.find_datacenter - base = datacenter.hostFolder + vm = $redis.srandmember('vmware_host_pool-'+params[:template]) + $redis.srem('vmware_host_pool-'+params[:template], vm) - # Move the VM to the specified folder and resource pool - relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec( - :pool => vsphere_helper.find_pool(params[:pool]) - ) + datacenter = $vim.serviceInstance.find_datacenter + base = datacenter.hostFolder - vm = $redis.srandmember('vmware_host_pool-'+params[:template]) - $redis.srem('vmware_host_pool-'+params[:template], vm) + # Move the VM to the specified folder and resource pool + relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec( + :pool => vsphere_helper.find_pool(params[:pool]) + ) - vm = vsphere_helper.find_vms(vm)[vm] - vm.RelocateVM_Task(:spec => relocateSpec).wait_for_completion + vm = vsphere_helper.find_vms(vm)[vm] + vm.RelocateVM_Task(:spec => relocateSpec) - result[params[:template]]['ok'] = 'true' - result[params[:template]]['hostname'] = vm['name'] + result[params[:template]]['ok'] = 'true' + result[params[:template]]['hostname'] = vm['name'] end result.to_json