Stop reloading configuration file from vspherehelper and instead source credentials from the configuration object that itself loads the configuration file when the application starts. Without this change the configuration file is reloaded every time vspherehelper is called. Additionally, this change makes it more straightforward to test vspherehelper connections.

A method is added to make more clear what's happening when checking if a socket can be opened to a pending VM on port 22. Additionally, the connection appends domain from the configuration, when present, to the VM name so DNS search is not required.
This commit is contained in:
kirby@puppetlabs.com 2016-11-15 13:49:12 -08:00
parent 58a548bc90
commit a244f9b92a
2 changed files with 30 additions and 23 deletions

View file

@ -26,14 +26,20 @@ module Vmpooler
end
end
def open_socket(host, domain=nil, timeout=5, port=22)
Timeout.timeout(timeout) do
target_host = vm
target_host = "#{vm}.#{domain}" if domain
TCPSocket.new target_host, port
end
end
def _check_pending_vm(vm, pool, timeout)
host = $vsphere[pool].find_vm(vm)
if host
begin
Timeout.timeout(5) do
TCPSocket.new vm, 22
end
open_socket vm, $config[:config]['domain'], timeout
move_pending_vm_to_ready(vm, pool, host)
rescue
fail_pending_vm(vm, pool, timeout)
@ -395,7 +401,7 @@ module Vmpooler
def check_disk_queue
$logger.log('d', "[*] [disk_manager] starting worker thread")
$vsphere['disk_manager'] ||= Vmpooler::VsphereHelper.new
$vsphere['disk_manager'] ||= Vmpooler::VsphereHelper.new $config[:vsphere]
$threads['disk_manager'] = Thread.new do
loop do
@ -421,7 +427,7 @@ module Vmpooler
def check_snapshot_queue
$logger.log('d', "[*] [snapshot_manager] starting worker thread")
$vsphere['snapshot_manager'] ||= Vmpooler::VsphereHelper.new
$vsphere['snapshot_manager'] ||= Vmpooler::VsphereHelper.new $config[:vsphere]
$threads['snapshot_manager'] = Thread.new do
loop do
@ -459,7 +465,7 @@ module Vmpooler
$vsphere[pool].find_vm(vm) || $vsphere[pool].find_vm_heavy(vm)[vm]
end
def migration_enabled?(migration_limit)
def migration_limit(migration_limit)
# Returns migration_limit setting when enabled
return false if migration_limit == 0 or not migration_limit
migration_limit if migration_limit >= 1
@ -476,7 +482,7 @@ module Vmpooler
vm_object = find_vsphere_pool_vm(pool, vm)
parent_host = vm_object.summary.runtime.host
parent_host_name = parent_host.name
migration_limit = migration_enabled? $config[:config]['migration_limit']
migration_limit = migration_limit $config[:config]['migration_limit']
if not migration_limit
$logger.log('s', "[ ] [#{pool}] '#{vm}' is running on #{parent_host_name}")
@ -507,7 +513,7 @@ module Vmpooler
def check_pool(pool)
$logger.log('d', "[*] [#{pool['name']}] starting worker thread")
$vsphere[pool['name']] ||= Vmpooler::VsphereHelper.new
$vsphere[pool['name']] ||= Vmpooler::VsphereHelper.new $config[:vsphere]
$threads[pool['name']] = Thread.new do
loop do

View file

@ -6,14 +6,23 @@ module Vmpooler
DISK_TYPE = 'thin'
DISK_MODE = 'persistent'
def initialize(_vInfo = {})
config_file = File.expand_path('vmpooler.yaml')
vsphere = YAML.load_file(config_file)[:vsphere]
def initialize(credentials)
$credentials = credentials
end
@connection = RbVmomi::VIM.connect host: vsphere['server'],
user: vsphere['username'],
password: vsphere['password'],
insecure: true
def ensure_connected(connection, credentials)
begin
connection.serviceInstance.CurrentTime
rescue
connect_to_vsphere $credentials
end
end
def connect_to_vsphere(credentials)
@connection = RbVmomi::VIM.connect host: credentials['server'],
user: credentials['username'],
password: credentials['password'],
insecure: credentials['insecure'] || true
end
def add_disk(vm, size, datastore)
@ -211,14 +220,6 @@ module Vmpooler
(memory_usage.to_f / memory_size.to_f) * 100
end
def ensure_connected(connection)
begin
connection.serviceInstance.CurrentTime
rescue
initialize
end
end
def find_least_used_host(cluster)
ensure_connected @connection