Class: Chef::Knife::Cloud::SshBootstrapProtocol
Instance Attribute Summary
#bootstrap, #config, #initial_sleep_delay, #ui
Instance Method Summary
collapse
#send_bootstrap_command
Methods included from Helpers
#after_exec_command, #before_exec_command, #create_service_instance, #execute_command, #msg_pair, #pretty_key, #set_default_config, #validate!, #validate_params!
Constructor Details
Returns a new instance of SshBootstrapProtocol.
29
30
31
32
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 29
def initialize(config)
@bootstrap = Chef::Knife::Bootstrap.new
super
end
|
Instance Method Details
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 145
def configure_ssh_gateway(ssh_gateway)
gw_host, gw_user = ssh_gateway.split("@").reverse
gw_host, gw_port = gw_host.split(":")
gateway_options = { port: gw_port || 22 }
ssh_gateway_config = Net::SSH::Config.for(gw_host)
gw_user ||= ssh_gateway_config[:user]
gateway_keys = ssh_gateway_config[:keys]
if config[:ssh_gateway_identity]
gateway_keys = Array(config[:ssh_gateway_identity])
end
unless gateway_keys.nil?
gateway_options[:keys] = gateway_keys
end
Net::SSH::Gateway.new(gw_host, gw_user, gateway_options)
end
|
#get_ssh_gateway_for(hostname) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 68
def get_ssh_gateway_for(hostname)
if config[:ssh_gateway]
Chef::Log.debug("Using ssh gateway #{config[:ssh_gateway]} from knife config")
config[:ssh_gateway]
else
ssh_proxy = Net::SSH::Config.for(hostname)[:proxy]
if ssh_proxy.respond_to?(:command_line_template)
proxy_pattern = /ssh\s+(\S+)\s+nc/
matchdata = proxy_pattern.match(ssh_proxy.command_line_template)
if matchdata.nil?
Chef::Log.debug("Unable to determine ssh gateway for '#{hostname}' from ssh config template: #{ssh_proxy.command_line_template}")
nil
else
Chef::Log.debug("Using ssh gateway #{matchdata[1]} from ssh config")
matchdata[1]
end
else
Chef::Log.debug("No ssh gateway found, making a direct connection")
nil
end
end
end
|
#init_bootstrap_options ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 34
def init_bootstrap_options
bootstrap.config[:connection_user] = config[:connection_user]
bootstrap.config[:connection_password] = config[:connection_password]
bootstrap.config[:connection_port] = config[:connection_port]
bootstrap.config[:ssh_identity_file] = config[:ssh_identity_file]
bootstrap.config[:ssh_verify_host_key] = config[:ssh_verify_host_key]
bootstrap.config[:use_sudo] = true unless config[:connection_user] == "root"
bootstrap.config[:ssh_gateway] = config[:ssh_gateway]
bootstrap.config[:forward_agent] = config[:forward_agent]
bootstrap.config[:use_sudo_password] = config[:use_sudo_password]
super
end
|
#tcp_test_ssh(hostname, ssh_port) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 99
def tcp_test_ssh(hostname, ssh_port)
tcp_socket = TCPSocket.new(hostname, ssh_port)
readable = IO.select([tcp_socket], nil, nil, 5)
if readable
ssh_banner = tcp_socket.gets
if ssh_banner.nil? || ssh_banner.empty?
false
else
Chef::Log.debug("ssh accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
yield
true
end
else
false
end
rescue Errno::EPERM, Errno::ETIMEDOUT
Chef::Log.debug("ssh timed out: #{hostname}")
false
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
Chef::Log.debug("ssh failed to connect: #{hostname}")
sleep 2
false
rescue Errno::ECONNRESET
Chef::Log.debug("ssh reset its connection: #{hostname}")
sleep 2
false
ensure
tcp_socket && tcp_socket.close
end
|
#tunnel_test_ssh(ssh_gateway, hostname, &block) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 130
def tunnel_test_ssh(ssh_gateway, hostname, &block)
status = false
gateway = configure_ssh_gateway(ssh_gateway)
remote_ssh_port = config[:connection_port] || config[:ssh_port] || 22
gateway.open(hostname, remote_ssh_port) do |local_tunnel_port|
status = tcp_test_ssh("localhost", local_tunnel_port, &block)
end
status
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
sleep 2
false
rescue Errno::EPERM, Errno::ETIMEDOUT
false
end
|
#wait_for_server_ready ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 47
def wait_for_server_ready
print "\n#{ui.color("Waiting for sshd to host (#{config[:bootstrap_ip_address]})", :magenta)}"
ssh_gateway = get_ssh_gateway_for(config[:bootstrap_ip_address])
if ssh_gateway
print(".") until tunnel_test_ssh(ssh_gateway, config[:bootstrap_ip_address]) do
@initial_sleep_delay = !!config[:subnet_id] ? 40 : 10
sleep @initial_sleep_delay
puts("done")
end
else
print(".") until tcp_test_ssh(config[:bootstrap_ip_address], config[:connection_port] || config[:ssh_port] ) do
@initial_sleep_delay = !!config[:subnet_id] ? 40 : 10
sleep @initial_sleep_delay
puts("done")
end
end
end
|