From a6c8c76d310435403ce5862223f5bab699f9c451 Mon Sep 17 00:00:00 2001 From: "kirby@puppetlabs.com" Date: Wed, 16 Nov 2016 10:37:08 -0800 Subject: [PATCH] Use open socket method for opening socket This commit updates pool manager to use a method for opening a socket instead of opening it directly from check_pending_vm. Support is added for specifying the domain of the VM to connect to, which lays the groundwork for doing away with the assumption of having DNS search domains set for vmpooler to move VMs to the ready state. Additionally, this commit adds a block to ensure open_socket closes open connections. Without this change sockets are opened to each VM before moving to the ready state, and never explicitly closed. Also, use open socket for check_ready_vm --- lib/vmpooler/pool_manager.rb | 37 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/vmpooler/pool_manager.rb b/lib/vmpooler/pool_manager.rb index 054ddd5..245a9b4 100644 --- a/lib/vmpooler/pool_manager.rb +++ b/lib/vmpooler/pool_manager.rb @@ -26,29 +26,30 @@ module Vmpooler end end - def open_socket(host, domain=nil, timeout=5, port=22) + def open_socket(host, domain=nil, timeout=5, port=22, &block) Timeout.timeout(timeout) do - target_host = vm - target_host = "#{vm}.#{domain}" if domain - TCPSocket.new target_host, port + target_host = host + target_host = "#{host}.#{domain}" if domain + sock = TCPSocket.new target_host, port + begin + yield sock if block_given? + ensure + sock.close + end end end def _check_pending_vm(vm, pool, timeout, vsphere) host = vsphere.find_vm(vm) - if host - begin - Timeout.timeout(5) do - TCPSocket.new vm, 22 - end - move_pending_vm_to_ready(vm, pool, host) - rescue - fail_pending_vm(vm, pool, timeout) - end - else - fail_pending_vm(vm, pool, timeout) + if ! host + fail_pending_vm(vm, pool, timeout, false) + return end + open_socket vm + move_pending_vm_to_ready(vm, pool, host) + rescue + fail_pending_vm(vm, pool, timeout) end def fail_pending_vm(vm, pool, timeout, exists=true) @@ -137,12 +138,12 @@ module Vmpooler end begin - Timeout.timeout(5) do - TCPSocket.new vm, 22 - end + open_socket vm rescue if $redis.smove('vmpooler__ready__' + pool, 'vmpooler__completed__' + pool, vm) $logger.log('d', "[!] [#{pool}] '#{vm}' is unreachable, removed from 'ready' queue") + else + $logger.log('d', "[!] [#{pool}] '#{vm}' is unreachable, and failed to remove from 'ready' queue") end end end