mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 18:08:42 -05:00
Use a thread pool for communicating with vcenter
This commit is contained in:
parent
71d0cf2772
commit
22f0daa92b
1 changed files with 25 additions and 21 deletions
|
|
@ -5,6 +5,8 @@ require 'redis'
|
||||||
require 'time'
|
require 'time'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
|
||||||
|
require 'thread/pool'
|
||||||
|
|
||||||
$:.unshift(File.dirname(__FILE__))
|
$:.unshift(File.dirname(__FILE__))
|
||||||
require 'lib/logger'
|
require 'lib/logger'
|
||||||
require 'lib/require_relative'
|
require 'lib/require_relative'
|
||||||
|
|
@ -37,11 +39,11 @@ $vsphere = {}
|
||||||
# Our thread-tracker object
|
# Our thread-tracker object
|
||||||
$threads = {}
|
$threads = {}
|
||||||
|
|
||||||
|
$vcenter_pool = Thread.pool config[:config]['num_vmware_connections'] || 4
|
||||||
|
|
||||||
# Check the state of a VM
|
# Check the state of a VM
|
||||||
def check_pending_vm vm, pool, timeout
|
def check_pending_vm vm, pool, timeout
|
||||||
Thread.new {
|
$vcenter_pool.process {
|
||||||
host = $vsphere[pool].find_vms(vm)[vm]
|
host = $vsphere[pool].find_vms(vm)[vm]
|
||||||
|
|
||||||
if (host)
|
if (host)
|
||||||
|
|
@ -75,7 +77,7 @@ def check_pending_vm vm, pool, timeout
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_ready_vm vm, pool, ttl
|
def check_ready_vm vm, pool, ttl
|
||||||
Thread.new {
|
$vcenter_pool.process {
|
||||||
host = $vsphere[pool].find_vms(vm)[vm]
|
host = $vsphere[pool].find_vms(vm)[vm]
|
||||||
|
|
||||||
if (host)
|
if (host)
|
||||||
|
|
@ -99,7 +101,7 @@ def check_ready_vm vm, pool, ttl
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_running_vm vm, pool, ttl
|
def check_running_vm vm, pool, ttl
|
||||||
Thread.new {
|
$vcenter_pool.process {
|
||||||
host = $vsphere[pool].find_vms(vm)[vm]
|
host = $vsphere[pool].find_vms(vm)[vm]
|
||||||
|
|
||||||
if (host)
|
if (host)
|
||||||
|
|
@ -127,7 +129,6 @@ end
|
||||||
|
|
||||||
# Clone a VM
|
# Clone a VM
|
||||||
def clone_vm template, pool, folder, datastore
|
def clone_vm template, pool, folder, datastore
|
||||||
Thread.new {
|
|
||||||
vm = {}
|
vm = {}
|
||||||
|
|
||||||
if template =~ /\//
|
if template =~ /\//
|
||||||
|
|
@ -135,6 +136,14 @@ def clone_vm template, pool, folder, datastore
|
||||||
vm['template'] = templatefolders.pop
|
vm['template'] = templatefolders.pop
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Generate a randomized hostname
|
||||||
|
o = [('a'..'z'),('0'..'9')].map{|r| r.to_a}.flatten
|
||||||
|
vm['hostname'] = o[rand(25)]+(0...14).map{o[rand(o.length)]}.join
|
||||||
|
|
||||||
|
# Add VM to Redis inventory ('pending' pool)
|
||||||
|
$redis.sadd('vmware_host_pool__pending__'+vm['template'], vm['hostname'])
|
||||||
|
|
||||||
|
$vcenter_pool.process {
|
||||||
if templatefolders
|
if templatefolders
|
||||||
vm[vm['template']] = $vsphere[vm['template']].find_folder(templatefolders.join('/')).find(vm['template'])
|
vm[vm['template']] = $vsphere[vm['template']].find_folder(templatefolders.join('/')).find(vm['template'])
|
||||||
else
|
else
|
||||||
|
|
@ -145,13 +154,6 @@ def clone_vm template, pool, folder, datastore
|
||||||
raise "Unable to find template '#{vm['template']}'!"
|
raise "Unable to find template '#{vm['template']}'!"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate a randomized hostname
|
|
||||||
o = [('a'..'z'),('0'..'9')].map{|r| r.to_a}.flatten
|
|
||||||
vm['hostname'] = o[rand(25)]+(0...14).map{o[rand(o.length)]}.join
|
|
||||||
|
|
||||||
# Add VM to Redis inventory ('pending' pool)
|
|
||||||
$redis.sadd('vmware_host_pool__pending__'+vm['template'], vm['hostname'])
|
|
||||||
|
|
||||||
# Annotate with creation time, origin template, etc.
|
# Annotate with creation time, origin template, etc.
|
||||||
configSpec = RbVmomi::VIM.VirtualMachineConfigSpec(
|
configSpec = RbVmomi::VIM.VirtualMachineConfigSpec(
|
||||||
:annotation =>
|
:annotation =>
|
||||||
|
|
@ -196,10 +198,12 @@ end
|
||||||
|
|
||||||
# Destroy a VM
|
# Destroy a VM
|
||||||
def destroy_vm vm, pool
|
def destroy_vm vm, pool
|
||||||
Thread.new {
|
# Removing from redis before we kick off the asynchronous task
|
||||||
|
# prevents a nasty race condition that can lead to multiple delete
|
||||||
|
# tasks starting for the same VM.
|
||||||
$redis.srem('vmware_host_pool__completed__'+pool, vm)
|
$redis.srem('vmware_host_pool__completed__'+pool, vm)
|
||||||
$redis.hdel('vmware_host_pool__active__'+pool, vm)
|
$redis.hdel('vmware_host_pool__active__'+pool, vm)
|
||||||
|
$vcenter_pool.process {
|
||||||
host = $vsphere[pool].find_vms(vm)[vm]
|
host = $vsphere[pool].find_vms(vm)[vm]
|
||||||
|
|
||||||
if (host)
|
if (host)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue