2012-01-06 08:56:09 +00:00
|
|
|
require 'timeout'
|
|
|
|
|
|
|
|
require 'log4r'
|
|
|
|
require 'net/ssh'
|
|
|
|
require 'net/scp'
|
|
|
|
|
2012-01-24 03:29:07 +00:00
|
|
|
require 'vagrant/util/ansi_escape_code_remover'
|
2012-01-06 08:56:09 +00:00
|
|
|
require 'vagrant/util/file_mode'
|
|
|
|
require 'vagrant/util/platform'
|
|
|
|
require 'vagrant/util/retryable'
|
|
|
|
|
|
|
|
module Vagrant
|
|
|
|
module Communication
|
|
|
|
# Provides communication with the VM via SSH.
|
2012-01-07 01:24:47 +00:00
|
|
|
class SSH < Base
|
2012-01-24 03:29:07 +00:00
|
|
|
include Util::ANSIEscapeCodeRemover
|
2012-01-06 08:56:09 +00:00
|
|
|
include Util::Retryable
|
|
|
|
|
|
|
|
def initialize(vm)
|
|
|
|
@vm = vm
|
|
|
|
@logger = Log4r::Logger.new("vagrant::communication::ssh")
|
2012-01-07 19:57:46 +00:00
|
|
|
@connection = nil
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ready?
|
|
|
|
@logger.debug("Checking whether SSH is ready...")
|
|
|
|
|
2012-03-29 05:30:01 +00:00
|
|
|
# Attempt to connect. This will raise an exception if it fails.
|
|
|
|
connect
|
2012-01-06 08:56:09 +00:00
|
|
|
|
|
|
|
# If we reached this point then we successfully connected
|
|
|
|
@logger.info("SSH is ready!")
|
|
|
|
true
|
2012-03-29 05:30:01 +00:00
|
|
|
rescue Errors::VagrantError => e
|
|
|
|
# We catch a `VagrantError` which would signal that something went
|
|
|
|
# wrong expectedly in the `connect`, which means we didn't connect.
|
2012-01-06 08:56:09 +00:00
|
|
|
@logger.info("SSH not up: #{e.inspect}")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-01-07 04:03:56 +00:00
|
|
|
def execute(command, opts=nil, &block)
|
|
|
|
opts = {
|
|
|
|
:error_check => true,
|
|
|
|
:error_class => Errors::VagrantError,
|
|
|
|
:error_key => :ssh_bad_exit_status,
|
|
|
|
:command => command,
|
|
|
|
:sudo => false
|
|
|
|
}.merge(opts || {})
|
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
# Connect via SSH and execute the command in the shell.
|
2012-01-07 04:03:56 +00:00
|
|
|
exit_status = connect do |connection|
|
|
|
|
shell_execute(connection, command, opts[:sudo], &block)
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
2012-01-07 04:03:56 +00:00
|
|
|
# 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
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
2012-01-07 04:03:56 +00:00
|
|
|
|
|
|
|
# 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)
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
2012-05-06 02:52:10 +00:00
|
|
|
def download(from, to=nil)
|
|
|
|
@logger.debug("Downloading: #{from} to #{to}")
|
|
|
|
|
|
|
|
scp_connect do |scp|
|
|
|
|
scp.download!(from, to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
def upload(from, to)
|
2012-01-20 01:02:18 +00:00
|
|
|
@logger.debug("Uploading: #{from} to #{to}")
|
|
|
|
|
2012-05-06 02:52:10 +00:00
|
|
|
scp_connect do |scp|
|
2012-01-06 08:56:09 +00:00
|
|
|
scp.upload!(from, to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Opens an SSH connection and yields it to a block.
|
|
|
|
def connect
|
2012-01-07 19:37:08 +00:00
|
|
|
if @connection && !@connection.closed?
|
|
|
|
# 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
|
2012-01-20 01:02:18 +00:00
|
|
|
@logger.debug("Re-using SSH connection.")
|
2012-01-07 19:37:08 +00:00
|
|
|
return yield @connection if block_given?
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-07 02:31:32 +00:00
|
|
|
ssh_info = @vm.ssh.info
|
2012-01-06 08:56:09 +00:00
|
|
|
|
|
|
|
# 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
|
2012-01-07 01:57:50 +00:00
|
|
|
@vm.ssh.check_key_permissions(ssh_info[:private_key_path])
|
2012-01-06 08:56:09 +00:00
|
|
|
|
|
|
|
# Connect to SSH, giving it a few tries
|
2012-03-13 21:27:16 +00:00
|
|
|
connection = nil
|
|
|
|
begin
|
2012-03-29 05:30:01 +00:00
|
|
|
exceptions = [Errno::ECONNREFUSED, Net::SSH::Disconnect, Timeout::Error]
|
2012-03-13 21:27:16 +00:00
|
|
|
connection = retryable(:tries => @vm.config.ssh.max_tries, :on => exceptions) do
|
2012-03-29 05:30:01 +00:00
|
|
|
Timeout.timeout(@vm.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
|
2012-03-13 21:27:16 +00:00
|
|
|
end
|
2012-03-29 05:30:01 +00:00
|
|
|
rescue Timeout::Error
|
|
|
|
# This happens if we continued to timeout when attempting to connect.
|
|
|
|
raise Errors::SSHConnectionTimeout
|
2012-03-13 21:27:16 +00:00
|
|
|
rescue Net::SSH::AuthenticationFailed
|
|
|
|
# This happens if authentication failed. We wrap the error in our
|
|
|
|
# own exception.
|
|
|
|
raise Errors::SSHAuthenticationFailed
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
# This is raised if we failed to connect the max amount of times
|
|
|
|
raise 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 Errors::SSHKeyTypeNotSupported
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
2012-01-07 19:37:08 +00:00
|
|
|
@connection = connection
|
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
# 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?
|
2012-03-13 21:27:16 +00:00
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
|
|
|
|
# Executes the command on an SSH connection within a login shell.
|
|
|
|
def shell_execute(connection, command, sudo=false)
|
2012-01-20 01:02:18 +00:00
|
|
|
@logger.info("Execute: #{command} (sudo=#{sudo.inspect})")
|
2012-01-06 08:56:09 +00:00
|
|
|
exit_status = nil
|
|
|
|
|
|
|
|
# Determine the shell to execute. If we are using `sudo` then we
|
|
|
|
# need to wrap the shell in a `sudo` call.
|
2012-06-01 21:17:31 +00:00
|
|
|
shell = @vm.config.ssh.shell
|
|
|
|
shell = "sudo -H #{shell}" if sudo
|
2012-01-06 08:56:09 +00:00
|
|
|
|
|
|
|
# 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|
|
2012-01-19 23:14:11 +00:00
|
|
|
if block_given?
|
|
|
|
# Filter out the clear screen command
|
2012-01-24 03:29:07 +00:00
|
|
|
data = remove_ansi_escape_codes(data)
|
2012-01-20 01:02:18 +00:00
|
|
|
@logger.debug("stdout: #{data}")
|
2012-01-19 23:14:11 +00:00
|
|
|
yield :stdout, data
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
ch2.on_extended_data do |ch3, type, data|
|
2012-01-19 23:14:11 +00:00
|
|
|
if block_given?
|
|
|
|
# Filter out the clear screen command
|
2012-01-24 03:29:07 +00:00
|
|
|
data = remove_ansi_escape_codes(data)
|
2012-01-20 01:02:18 +00:00
|
|
|
@logger.debug("stderr: #{data}")
|
2012-01-19 23:14:11 +00:00
|
|
|
yield :stderr, data
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
ch2.on_request("exit-status") do |ch3, data|
|
|
|
|
exit_status = data.read_long
|
2012-01-20 01:02:18 +00:00
|
|
|
@logger.debug("Exit status: #{exit_status}")
|
2012-01-06 08:56:09 +00:00
|
|
|
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"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for the channel to complete
|
|
|
|
channel.wait
|
|
|
|
|
|
|
|
# Return the final exit status
|
|
|
|
return exit_status
|
|
|
|
end
|
2012-05-06 02:52:10 +00:00
|
|
|
|
|
|
|
# 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 Errors::SCPUnavailable if e.message =~ /\(127\)/
|
|
|
|
|
|
|
|
# Otherwise, just raise the error up
|
|
|
|
raise
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|