Skip to content

Commit

Permalink
Use open socket method for opening socket
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mattkirby committed Nov 22, 2016
1 parent 109f197 commit a6c8c76
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions lib/vmpooler/pool_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,30 @@ def check_pending_vm(vm, pool, timeout, vsphere)
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)
Expand Down Expand Up @@ -137,12 +138,12 @@ def check_ready_vm(vm, pool, ttl, vsphere)
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
Expand Down

0 comments on commit a6c8c76

Please sign in to comment.