mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
(POOLER-70) Update _clone_vm for VM Provider
Previously the Pool Manager would use vSphere objects directly. This commit - Modifies the pool_manager to use the VM provider methods instead
This commit is contained in:
parent
cc1910fd76
commit
b21d78fa49
2 changed files with 78 additions and 326 deletions
|
|
@ -195,99 +195,35 @@ module Vmpooler
|
|||
end
|
||||
|
||||
def _clone_vm(pool, provider)
|
||||
template = pool['template']
|
||||
folder = pool['folder']
|
||||
datastore = pool['datastore']
|
||||
target = pool['clone_target']
|
||||
vm = {}
|
||||
|
||||
if template =~ /\//
|
||||
templatefolders = template.split('/')
|
||||
vm['template'] = templatefolders.pop
|
||||
end
|
||||
|
||||
if templatefolders
|
||||
vm[vm['template']] = provider.find_folder(templatefolders.join('/')).find(vm['template'])
|
||||
else
|
||||
fail 'Please provide a full path to the template'
|
||||
end
|
||||
|
||||
if vm['template'].length == 0
|
||||
fail "Unable to find template '#{vm['template']}'!"
|
||||
end
|
||||
pool_name = pool['name']
|
||||
|
||||
# Generate a randomized hostname
|
||||
o = [('a'..'z'), ('0'..'9')].map(&:to_a).flatten
|
||||
vm['hostname'] = $config[:config]['prefix'] + o[rand(25)] + (0...14).map { o[rand(o.length)] }.join
|
||||
new_vmname = $config[:config]['prefix'] + o[rand(25)] + (0...14).map { o[rand(o.length)] }.join
|
||||
|
||||
# Add VM to Redis inventory ('pending' pool)
|
||||
$redis.sadd('vmpooler__pending__' + vm['template'], vm['hostname'])
|
||||
$redis.hset('vmpooler__vm__' + vm['hostname'], 'clone', Time.now)
|
||||
$redis.hset('vmpooler__vm__' + vm['hostname'], 'template', vm['template'])
|
||||
|
||||
# Annotate with creation time, origin template, etc.
|
||||
# Add extraconfig options that can be queried by vmtools
|
||||
configSpec = RbVmomi::VIM.VirtualMachineConfigSpec(
|
||||
annotation: JSON.pretty_generate(
|
||||
name: vm['hostname'],
|
||||
created_by: $config[:vsphere]['username'],
|
||||
base_template: vm['template'],
|
||||
creation_timestamp: Time.now.utc
|
||||
),
|
||||
extraConfig: [
|
||||
{ key: 'guestinfo.hostname',
|
||||
value: vm['hostname']
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
# Choose a clone target
|
||||
if target
|
||||
$clone_target = provider.find_least_used_host(target)
|
||||
elsif $config[:config]['clone_target']
|
||||
$clone_target = provider.find_least_used_host($config[:config]['clone_target'])
|
||||
end
|
||||
|
||||
# Put the VM in the specified folder and resource pool
|
||||
relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec(
|
||||
datastore: provider.find_datastore(datastore),
|
||||
host: $clone_target,
|
||||
diskMoveType: :moveChildMostDiskBacking
|
||||
)
|
||||
|
||||
# Create a clone spec
|
||||
spec = RbVmomi::VIM.VirtualMachineCloneSpec(
|
||||
location: relocateSpec,
|
||||
config: configSpec,
|
||||
powerOn: true,
|
||||
template: false
|
||||
)
|
||||
|
||||
# Clone the VM
|
||||
$logger.log('d', "[ ] [#{vm['template']}] '#{vm['hostname']}' is being cloned from '#{vm['template']}'")
|
||||
$redis.sadd('vmpooler__pending__' + pool_name, new_vmname)
|
||||
$redis.hset('vmpooler__vm__' + new_vmname, 'clone', Time.now)
|
||||
$redis.hset('vmpooler__vm__' + new_vmname, 'template', pool_name)
|
||||
|
||||
begin
|
||||
$logger.log('d', "[ ] [#{pool_name}] Starting to clone '#{new_vmname}'")
|
||||
start = Time.now
|
||||
vm[vm['template']].CloneVM_Task(
|
||||
folder: provider.find_folder(folder),
|
||||
name: vm['hostname'],
|
||||
spec: spec
|
||||
).wait_for_completion
|
||||
provider.create_vm(pool_name, new_vmname)
|
||||
finish = '%.2f' % (Time.now - start)
|
||||
|
||||
$redis.hset('vmpooler__clone__' + Date.today.to_s, vm['template'] + ':' + vm['hostname'], finish)
|
||||
$redis.hset('vmpooler__vm__' + vm['hostname'], 'clone_time', finish)
|
||||
$redis.hset('vmpooler__clone__' + Date.today.to_s, pool_name + ':' + new_vmname, finish)
|
||||
$redis.hset('vmpooler__vm__' + new_vmname, 'clone_time', finish)
|
||||
$logger.log('s', "[+] [#{pool_name}] '#{new_vmname}' cloned in #{finish} seconds")
|
||||
|
||||
$logger.log('s', "[+] [#{vm['template']}] '#{vm['hostname']}' cloned from '#{vm['template']}' in #{finish} seconds")
|
||||
$metrics.timing("clone.#{pool_name}", finish)
|
||||
rescue => err
|
||||
$logger.log('s', "[!] [#{vm['template']}] '#{vm['hostname']}' clone failed with an error: #{err}")
|
||||
$redis.srem('vmpooler__pending__' + vm['template'], vm['hostname'])
|
||||
$logger.log('s', "[!] [#{pool_name}] '#{new_vmname}' clone failed with an error: #{err}")
|
||||
$redis.srem('vmpooler__pending__' + pool_name, new_vmname)
|
||||
raise
|
||||
ensure
|
||||
$redis.decr('vmpooler__tasks__clone')
|
||||
end
|
||||
|
||||
$redis.decr('vmpooler__tasks__clone')
|
||||
|
||||
$metrics.timing("clone.#{vm['template']}", finish)
|
||||
end
|
||||
|
||||
# Destroy a VM
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue