forked from hashicorp/vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunicator.rb
279 lines (236 loc) · 9.42 KB
/
communicator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
require 'timeout'
require 'log4r'
require 'net/ssh'
require 'net/scp'
require 'vagrant/util/ansi_escape_code_remover'
require 'vagrant/util/file_mode'
require 'vagrant/util/platform'
require 'vagrant/util/retryable'
require 'vagrant/util/ssh'
module VagrantPlugins
module CommunicatorSSH
# This class provides communication with the VM via SSH.
class Communicator < Vagrant.plugin("1", :communicator)
include Vagrant::Util::ANSIEscapeCodeRemover
include Vagrant::Util::Retryable
def self.match?(machine)
# All machines are currently expected to have SSH.
true
end
def initialize(machine)
@machine = machine
@logger = Log4r::Logger.new("vagrant::communication::ssh")
@connection = nil
end
def ready?
@logger.debug("Checking whether SSH is ready...")
# Attempt to connect. This will raise an exception if it fails.
connect
# If we reached this point then we successfully connected
@logger.info("SSH is ready!")
true
rescue Vagrant::Errors::VagrantError => e
# We catch a `VagrantError` which would signal that something went
# wrong expectedly in the `connect`, which means we didn't connect.
@logger.info("SSH not up: #{e.inspect}")
return false
end
def execute(command, opts=nil, &block)
opts = {
:error_check => true,
:error_class => Vagrant::Errors::VagrantError,
:error_key => :ssh_bad_exit_status,
:command => command,
:sudo => false
}.merge(opts || {})
# Connect via SSH and execute the command in the shell.
exit_status = connect do |connection|
shell_execute(connection, command, opts[:sudo], &block)
end
# Check for any errors
if opts[:error_check] && exit_status != 0
# The error classes expect the translation key to be _key,
# but that makes for an ugly configuration parameter, so we
# set it here from `error_key`
error_opts = opts.merge(:_key => opts[:error_key])
raise opts[:error_class], error_opts
end
# Return the exit status
exit_status
end
def sudo(command, opts=nil, &block)
# Run `execute` but with the `sudo` option.
opts = { :sudo => true }.merge(opts || {})
execute(command, opts, &block)
end
def download(from, to=nil)
@logger.debug("Downloading: #{from} to #{to}")
scp_connect do |scp|
scp.download!(from, to)
end
end
def test(command, opts=nil)
opts = { :error_check => false }.merge(opts || {})
execute(command, opts) == 0
end
def upload(from, to)
@logger.debug("Uploading: #{from} to #{to}")
scp_connect do |scp|
scp.upload!(from, to)
end
rescue RuntimeError => e
# Net::SCP raises a runtime error for this so the only way we have
# to really catch this exception is to check the message to see if
# it is something we care about. If it isn't, we re-raise.
raise if e.message !~ /Permission denied/
# Otherwise, it is a permission denied, so let's raise a proper
# exception
raise Vagrant::Errors::SCPPermissionDenied, :path => from.to_s
end
protected
# Opens an SSH connection and yields it to a block.
def connect
if @connection && [email protected]?
# There is a chance that the socket is closed despite us checking
# 'closed?' above. To test this we need to send data through the
# socket.
begin
@connection.exec!("")
rescue IOError
@logger.info("Connection has been closed. Not re-using.")
@connection = nil
end
# If the @connection is still around, then it is valid,
# and we use it.
if @connection
@logger.debug("Re-using SSH connection.")
return yield @connection if block_given?
return
end
end
# XXX: We need to raise some exception if SSH is not ready
ssh_info = @machine.ssh_info
# Build the options we'll use to initiate the connection via Net::SSH
opts = {
:port => ssh_info[:port],
:keys => [ssh_info[:private_key_path]],
:keys_only => true,
:user_known_hosts_file => [],
:paranoid => false,
:config => false,
:forward_agent => ssh_info[:forward_agent]
}
# Check that the private key permissions are valid
Vagrant::Util::SSH.check_key_permissions(ssh_info[:private_key_path])
# Connect to SSH, giving it a few tries
connection = nil
begin
# These are the exceptions that we retry because they represent
# errors that are generally fixed from a retry and don't
# necessarily represent immediate failure cases.
exceptions = [
Errno::ECONNREFUSED,
Errno::EHOSTUNREACH,
Net::SSH::Disconnect,
Timeout::Error
]
connection = retryable(:tries => @machine.config.ssh.max_tries, :on => exceptions) do
Timeout.timeout(@machine.config.ssh.timeout) do
@logger.info("Attempting to connect to SSH: #{ssh_info[:host]}:#{ssh_info[:port]}")
Net::SSH.start(ssh_info[:host], ssh_info[:username], opts)
end
end
rescue Timeout::Error
# This happens if we continued to timeout when attempting to connect.
raise Vagrant::Errors::SSHConnectionTimeout
rescue Net::SSH::AuthenticationFailed
# This happens if authentication failed. We wrap the error in our
# own exception.
raise Vagrant::Errors::SSHAuthenticationFailed
rescue Net::SSH::Disconnect
# This happens if the remote server unexpectedly closes the
# connection. This is usually raised when SSH is running on the
# other side but can't properly setup a connection. This is
# usually a server-side issue.
raise Vagrant::Errors::SSHDisconnected
rescue Errno::ECONNREFUSED
# This is raised if we failed to connect the max amount of times
raise Vagrant::Errors::SSHConnectionRefused
rescue NotImplementedError
# This is raised if a private key type that Net-SSH doesn't support
# is used. Show a nicer error.
raise Vagrant::Errors::SSHKeyTypeNotSupported
end
@connection = connection
# This is hacky but actually helps with some issues where
# Net::SSH is simply not robust enough to handle... see
# issue #391, #455, etc.
sleep 4
# Yield the connection that is ready to be used and
# return the value of the block
return yield connection if block_given?
end
# Executes the command on an SSH connection within a login shell.
def shell_execute(connection, command, sudo=false)
@logger.info("Execute: #{command} (sudo=#{sudo.inspect})")
exit_status = nil
# Determine the shell to execute. If we are using `sudo` then we
# need to wrap the shell in a `sudo` call.
shell = @machine.config.ssh.shell
shell = "sudo -H #{shell}" if sudo
# Open the channel so we can execute or command
channel = connection.open_channel do |ch|
ch.exec(shell) do |ch2, _|
# Setup the channel callbacks so we can get data and exit status
ch2.on_data do |ch3, data|
if block_given?
# Filter out the clear screen command
data = remove_ansi_escape_codes(data)
@logger.debug("stdout: #{data}")
yield :stdout, data
end
end
ch2.on_extended_data do |ch3, type, data|
if block_given?
# Filter out the clear screen command
data = remove_ansi_escape_codes(data)
@logger.debug("stderr: #{data}")
yield :stderr, data
end
end
ch2.on_request("exit-status") do |ch3, data|
exit_status = data.read_long
@logger.debug("Exit status: #{exit_status}")
end
# Set the terminal
ch2.send_data "export TERM=vt100\n"
# Output the command
ch2.send_data "#{command}\n"
# Remember to exit or this channel will hang open
ch2.send_data "exit\n"
# Send eof to let server know we're done
ch2.eof!
end
end
# Wait for the channel to complete
channel.wait
# Return the final exit status
return exit_status
end
# Opens an SCP connection and yields it so that you can download
# and upload files.
def scp_connect
# Connect to SCP and yield the SCP object
connect do |connection|
scp = Net::SCP.new(connection)
return yield scp
end
rescue Net::SCP::Error => e
# If we get the exit code of 127, then this means SCP is unavailable.
raise Vagrant::Errors::SCPUnavailable if e.message =~ /\(127\)/
# Otherwise, just raise the error up
raise
end
end
end
end