Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
add ssh_gateway support for private hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
eden committed May 22, 2012
1 parent 386e499 commit 9321dc7
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions lib/chef/knife/ec2_server_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,36 @@ class Ec2ServerCreate < Knife
:description => "Auto-assign an Elastic IP (only in VPC mode)",
:default => false

option :ssh_gateway,
:long => "--ssh-gateway GATEWAY",
:description => "Use a gateway to the machine for bootstrap",
:default => false

def tcp_test_ssh(hostname)
tcp_socket = TCPSocket.new(hostname, config[:ssh_port])
port = nil
gateway = nil
if config[:ssh_gateway]
require 'net/ssh/gateway'
userhostport = config[:ssh_gateway].split(':')
userhost = userhostport[0].split('@')

if userhost.length == 1
user = 'ubuntu'
host = userhost[0]
else
user = userhost[0]
host = userhost[1]
end
gateway = Net::SSH::Gateway.new(host, user, {
:port => (userhostport[1] || '22').to_i
})
port = gateway.open(hostname, config[:ssh_port])
end
if gateway
tcp_socket = TCPSocket.new('localhost', port)
else
tcp_socket = TCPSocket.new(hostname, config[:ssh_port])
end
readable = IO.select([tcp_socket], nil, nil, 5)
if readable
Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
Expand Down Expand Up @@ -196,6 +223,9 @@ def tcp_test_ssh(hostname)
false
ensure
tcp_socket && tcp_socket.close
if gateway && port
gateway.close(port)
end
end

def run
Expand Down Expand Up @@ -263,7 +293,7 @@ def run
end

print(".") until tcp_test_ssh(fqdn) {
sleep @initial_sleep_delay ||= (vpc_mode? ? 40 : 10)
sleep @initial_sleep_delay ||= 10
puts("done")
}

Expand Down Expand Up @@ -324,6 +354,9 @@ def bootstrap_for_node(server,fqdn)
bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
bootstrap.config[:template_file] = locate_config_value(:template_file)
bootstrap.config[:environment] = config[:environment]
if config[:ssh_gateway]
bootstrap.config[:ssh_gateway] = config[:ssh_gateway]
end
# may be needed for vpc_mode
bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
bootstrap
Expand Down

0 comments on commit 9321dc7

Please sign in to comment.