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
#locate_config_value, #msg_pair
Constructor Details
Returns a new instance of SshBootstrapProtocol.
30
31
32
33
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 30
def initialize(config)
@bootstrap = (config[:image_os_type] == 'linux') ? Chef::Knife::Bootstrap.new : Chef::Knife::BootstrapWindowsSsh.new
super
end
|
Instance Method Details
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 150
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 locate_config_value(:ssh_gateway_identity)
gateway_keys = Array(locate_config_value(: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
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
98
99
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 70
def get_ssh_gateway_for(hostname)
if locate_config_value(:ssh_gateway)
Chef::Log.debug("Using ssh gateway #{locate_config_value(:ssh_gateway)} from knife config")
locate_config_value(: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
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 35
def init_bootstrap_options
bootstrap.config[:ssh_user] = @config[:ssh_user]
bootstrap.config[:ssh_password] = @config[:ssh_password]
bootstrap.config[:ssh_port] = locate_config_value(:ssh_port)
bootstrap.config[:identity_file] = @config[:identity_file]
bootstrap.config[:host_key_verify] = @config[:host_key_verify]
bootstrap.config[:use_sudo] = true unless @config[:ssh_user] == 'root'
bootstrap.config[:template_file] = @config[:template_file]
bootstrap.config[:ssh_gateway] = locate_config_value(:ssh_gateway)
bootstrap.config[:forward_agent] = locate_config_value(:forward_agent)
bootstrap.config[:use_sudo_password] = locate_config_value(:use_sudo_password)
super
end
|
#tcp_test_ssh(hostname, ssh_port) ⇒ Object
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
129
130
131
132
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 101
def tcp_test_ssh(hostname, ssh_port)
begin
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? or 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
end
|
#tunnel_test_ssh(ssh_gateway, hostname, &block) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 134
def tunnel_test_ssh(ssh_gateway, hostname, &block)
begin
status = false
gateway = configure_ssh_gateway(ssh_gateway)
gateway.open(hostname, locate_config_value(: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
end
|
#wait_for_server_ready ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/chef/knife/cloud/chefbootstrap/ssh_bootstrap_protocol.rb', line 49
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]) {
@initial_sleep_delay = !!locate_config_value(:subnet_id) ? 40 : 10
sleep @initial_sleep_delay
puts("done")
}
else
print(".") until tcp_test_ssh(@config[:bootstrap_ip_address], locate_config_value(:ssh_port)) {
@initial_sleep_delay = !!locate_config_value(:subnet_id) ? 40 : 10
sleep @initial_sleep_delay
puts("done")
}
end
end
|