Move provider_hosts to vsphere provider

This commit updates the providers to move provider_hosts under the vsphere provider, which is the only place it's applicable. Methods where redis is passed through are updated to remove this pass through and use the globally available redis object, where applicable. Remove_vmpooler_migration_vm method is not needed and is removed.
This commit is contained in:
kirby@puppetlabs.com 2017-11-03 15:43:39 -07:00 committed by mattkirby
parent cd979fc24d
commit 0efb79a133
3 changed files with 14 additions and 21 deletions

View file

@ -462,7 +462,7 @@ module Vmpooler
def migrate_vm(vm_name, pool_name, provider) def migrate_vm(vm_name, pool_name, provider)
Thread.new do Thread.new do
begin begin
$redis.srem('vmpooler__migrating__' + pool_name, vm_name) $redis.srem("vmpooler__migrating__#{pool_name}", vm_name)
provider.migrate_vm(pool_name, vm_name) provider.migrate_vm(pool_name, vm_name)
rescue => err rescue => err
$logger.log('s', "[x] [#{pool_name}] '#{vm_name}' migration failed with an error: #{err}") $logger.log('s', "[x] [#{pool_name}] '#{vm_name}' migration failed with an error: #{err}")

View file

@ -19,8 +19,6 @@ module Vmpooler
@logger = logger @logger = logger
@metrics = metrics @metrics = metrics
@provider_name = name @provider_name = name
@provider_hosts = {}
@provider_hosts_lock = Mutex.new
# Ensure that there is not a nil provider configuration # Ensure that there is not a nil provider configuration
@config[:providers] = {} if @config[:providers].nil? @config[:providers] = {} if @config[:providers].nil?

View file

@ -33,6 +33,8 @@ module Vmpooler
new_conn = connect_to_vsphere new_conn = connect_to_vsphere
{ connection: new_conn } { connection: new_conn }
end end
@provider_hosts = {}
@provider_hosts_lock = Mutex.new
end end
# name of the provider class # name of the provider class
@ -879,13 +881,12 @@ module Vmpooler
false false
end end
def migrate_vm(pool_name, vm_name, redis) def migrate_vm(pool_name, vm_name)
redis.srem("vmpooler__migrating__#{pool_name}", vm_name)
@connection_pool.with_metrics do |pool_object| @connection_pool.with_metrics do |pool_object|
connection = ensured_vsphere_connection(pool_object) connection = ensured_vsphere_connection(pool_object)
vm_hash = get_vm_details(vm_name, connection) vm_hash = get_vm_details(vm_name, connection)
migration_limit = @config[:config]['migration_limit'] if @config[:config].key?('migration_limit') migration_limit = @config[:config]['migration_limit'] if @config[:config].key?('migration_limit')
migration_count = redis.scard('vmpooler__migration') migration_count = $redis.scard('vmpooler__migration')
if migration_enabled? @config if migration_enabled? @config
if migration_count >= migration_limit if migration_count >= migration_limit
logger.log('s', "[ ] [#{pool_name}] '#{vm_name}' is running on #{vm_hash['host_name']}. No migration will be evaluated since the migration_limit has been reached") logger.log('s', "[ ] [#{pool_name}] '#{vm_name}' is running on #{vm_hash['host_name']}. No migration will be evaluated since the migration_limit has been reached")
@ -895,41 +896,35 @@ module Vmpooler
if vm_in_target?(pool_name, vm_hash['host_name'], vm_hash['architecture'], @provider_hosts) if vm_in_target?(pool_name, vm_hash['host_name'], vm_hash['architecture'], @provider_hosts)
logger.log('s', "[ ] [#{pool_name}] No migration required for '#{vm_name}' running on #{vm_hash['host_name']}") logger.log('s', "[ ] [#{pool_name}] No migration required for '#{vm_name}' running on #{vm_hash['host_name']}")
else else
migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection, redis) migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection)
end end
end end
end end
end end
def migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection, redis) def migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection)
redis.sadd('vmpooler__migration', vm_name) $redis.sadd('vmpooler__migration', vm_name)
target_host_name = select_next_host(pool_name, @provider_hosts, vm_hash['architecture']) target_host_name = select_next_host(pool_name, @provider_hosts, vm_hash['architecture'])
target_host_object = find_host_by_dnsname(connection, target_host_name) target_host_object = find_host_by_dnsname(connection, target_host_name)
finish = migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, target_host_name, redis) finish = migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, target_host_name)
#logger.log('s', "Provider_hosts is: #{provider.provider_hosts}") #logger.log('s', "Provider_hosts is: #{provider.provider_hosts}")
logger.log('s', "[>] [#{pool_name}] '#{vm_name}' migrated from #{vm_hash['host_name']} to #{target_host_name} in #{finish} seconds") logger.log('s', "[>] [#{pool_name}] '#{vm_name}' migrated from #{vm_hash['host_name']} to #{target_host_name} in #{finish} seconds")
remove_vmpooler_migration_vm(pool_name, vm_name, redis) $redis.srem('vmpooler__migration', vm_name)
end end
def migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, dest_host_name, redis) def migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, dest_host_name)
start = Time.now start = Time.now
migrate_vm_host(vm_hash['object'], target_host_object) migrate_vm_host(vm_hash['object'], target_host_object)
finish = format('%.2f', Time.now - start) finish = format('%.2f', Time.now - start)
metrics.timing("migrate.#{pool_name}", finish) metrics.timing("migrate.#{pool_name}", finish)
metrics.increment("migrate_from.#{vm_hash['host_name']}") metrics.increment("migrate_from.#{vm_hash['host_name']}")
metrics.increment("migrate_to.#{dest_host_name}") metrics.increment("migrate_to.#{dest_host_name}")
checkout_to_migration = format('%.2f', Time.now - Time.parse(redis.hget("vmpooler__vm__#{vm_name}", 'checkout'))) checkout_to_migration = format('%.2f', Time.now - Time.parse($redis.hget("vmpooler__vm__#{vm_name}", 'checkout')))
redis.hset("vmpooler__vm__#{vm_name}", 'migration_time', finish) $redis.hset("vmpooler__vm__#{vm_name}", 'migration_time', finish)
redis.hset("vmpooler__vm__#{vm_name}", 'checkout_to_migration', checkout_to_migration) $redis.hset("vmpooler__vm__#{vm_name}", 'checkout_to_migration', checkout_to_migration)
finish finish
end end
def remove_vmpooler_migration_vm(pool_name, vm_name, redis)
redis.srem('vmpooler__migration', vm_name)
rescue => err
logger.log('s', "[x] [#{pool_name}] '#{vm_name}' removal from vmpooler__migration failed with an error: #{err}")
end
def migrate_vm_host(vm_object, host) def migrate_vm_host(vm_object, host)
relospec = RbVmomi::VIM.VirtualMachineRelocateSpec(host: host) relospec = RbVmomi::VIM.VirtualMachineRelocateSpec(host: host)
vm_object.RelocateVM_Task(spec: relospec).wait_for_completion vm_object.RelocateVM_Task(spec: relospec).wait_for_completion