2013-02-04 21:49:03 +00:00
|
|
|
require 'logger'
|
2013-02-05 05:06:28 +00:00
|
|
|
require 'pathname'
|
2013-02-04 21:49:03 +00:00
|
|
|
require 'stringio'
|
2014-01-03 19:13:21 +00:00
|
|
|
require 'thread'
|
2012-01-06 08:56:09 +00:00
|
|
|
require 'timeout'
|
|
|
|
|
|
|
|
require 'log4r'
|
|
|
|
require 'net/ssh'
|
2013-04-03 20:54:21 +00:00
|
|
|
require 'net/ssh/proxy/command'
|
2012-01-06 08:56:09 +00:00
|
|
|
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'
|
2014-10-24 16:58:18 +00:00
|
|
|
require 'vagrant/util/keypair'
|
2012-01-06 08:56:09 +00:00
|
|
|
require 'vagrant/util/platform'
|
|
|
|
require 'vagrant/util/retryable'
|
|
|
|
|
2012-08-09 04:48:51 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module CommunicatorSSH
|
|
|
|
# This class provides communication with the VM via SSH.
|
2012-11-07 05:14:10 +00:00
|
|
|
class Communicator < Vagrant.plugin("2", :communicator)
|
2014-08-29 16:41:35 +00:00
|
|
|
PTY_DELIM_START = "bccbb768c119429488cfd109aacea6b5-pty"
|
|
|
|
PTY_DELIM_END = "bccbb768c119429488cfd109aacea6b5-pty"
|
|
|
|
|
2012-08-09 04:56:22 +00:00
|
|
|
include Vagrant::Util::ANSIEscapeCodeRemover
|
|
|
|
include Vagrant::Util::Retryable
|
2012-01-06 08:56:09 +00:00
|
|
|
|
2012-08-09 04:48:51 +00:00
|
|
|
def self.match?(machine)
|
|
|
|
# All machines are currently expected to have SSH.
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(machine)
|
2014-01-03 19:13:21 +00:00
|
|
|
@lock = Mutex.new
|
2012-08-09 04:48:51 +00:00
|
|
|
@machine = machine
|
|
|
|
@logger = Log4r::Logger.new("vagrant::communication::ssh")
|
2012-01-07 19:57:46 +00:00
|
|
|
@connection = nil
|
2014-01-03 19:13:21 +00:00
|
|
|
@inserted_key = false
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
2014-02-14 04:59:26 +00:00
|
|
|
def wait_for_ready(timeout)
|
|
|
|
Timeout.timeout(timeout) do
|
|
|
|
# Wait for ssh_info to be ready
|
|
|
|
ssh_info = nil
|
|
|
|
while true
|
|
|
|
ssh_info = @machine.ssh_info
|
|
|
|
break if ssh_info
|
|
|
|
sleep 0.5
|
|
|
|
end
|
|
|
|
|
|
|
|
# Got it! Let the user know what we're connecting to.
|
|
|
|
@machine.ui.detail("SSH address: #{ssh_info[:host]}:#{ssh_info[:port]}")
|
|
|
|
@machine.ui.detail("SSH username: #{ssh_info[:username]}")
|
|
|
|
ssh_auth_type = "private key"
|
|
|
|
ssh_auth_type = "password" if ssh_info[:password]
|
|
|
|
@machine.ui.detail("SSH auth method: #{ssh_auth_type}")
|
|
|
|
|
2014-04-11 23:17:02 +00:00
|
|
|
last_message = nil
|
|
|
|
last_message_repeat_at = 0
|
2014-02-14 04:59:26 +00:00
|
|
|
while true
|
2014-04-11 21:36:19 +00:00
|
|
|
message = nil
|
2014-02-14 04:59:26 +00:00
|
|
|
begin
|
|
|
|
begin
|
|
|
|
connect(retries: 1)
|
2014-02-26 19:35:41 +00:00
|
|
|
return true if ready?
|
2014-02-14 04:59:26 +00:00
|
|
|
rescue Vagrant::Errors::VagrantError => e
|
|
|
|
@logger.info("SSH not ready: #{e.inspect}")
|
|
|
|
raise
|
|
|
|
end
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHConnectionTimeout
|
2014-03-11 09:27:47 +00:00
|
|
|
message = "Connection timeout."
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHAuthenticationFailed
|
2014-02-14 04:59:26 +00:00
|
|
|
message = "Authentication failure."
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHDisconnected
|
2014-02-14 04:59:26 +00:00
|
|
|
message = "Remote connection disconnect."
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHConnectionRefused
|
2014-02-14 04:59:26 +00:00
|
|
|
message = "Connection refused."
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHConnectionReset
|
2014-02-14 04:59:26 +00:00
|
|
|
message = "Connection reset."
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHHostDown
|
2014-02-14 04:59:26 +00:00
|
|
|
message = "Host appears down."
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHNoRoute
|
2014-02-14 04:59:26 +00:00
|
|
|
message = "Host unreachable."
|
2014-05-02 04:35:02 +00:00
|
|
|
rescue Vagrant::Errors::SSHInvalidShell
|
|
|
|
raise
|
2014-02-24 06:31:37 +00:00
|
|
|
rescue Vagrant::Errors::SSHKeyTypeNotSupported
|
|
|
|
raise
|
2014-10-23 12:11:10 +00:00
|
|
|
rescue Vagrant::Errors::SSHKeyBadOwner
|
|
|
|
raise
|
|
|
|
rescue Vagrant::Errors::SSHKeyBadPermissions
|
2014-10-23 16:06:28 +00:00
|
|
|
raise
|
2014-10-24 16:58:18 +00:00
|
|
|
rescue Vagrant::Errors::SSHInsertKeyUnsupported
|
|
|
|
raise
|
2014-02-14 05:04:41 +00:00
|
|
|
rescue Vagrant::Errors::VagrantError => e
|
|
|
|
# Ignore it, SSH is not ready, some other error.
|
2014-02-14 04:59:26 +00:00
|
|
|
end
|
|
|
|
|
2014-04-11 23:17:02 +00:00
|
|
|
# If we have a message to show, then show it. We don't show
|
|
|
|
# repeated messages unless they've been repeating longer than
|
|
|
|
# 10 seconds.
|
|
|
|
if message
|
|
|
|
message_at = Time.now.to_f
|
|
|
|
show_message = true
|
|
|
|
if last_message == message
|
|
|
|
show_message = (message_at - last_message_repeat_at) > 10.0
|
|
|
|
end
|
|
|
|
|
|
|
|
if show_message
|
|
|
|
@machine.ui.detail("Warning: #{message} Retrying...")
|
|
|
|
last_message = message
|
|
|
|
last_message_repeat_at = message_at
|
|
|
|
end
|
2014-02-14 05:04:41 +00:00
|
|
|
end
|
2014-02-14 04:59:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
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.
|
2014-01-03 19:13:21 +00:00
|
|
|
begin
|
|
|
|
connect
|
|
|
|
@logger.info("SSH is ready!")
|
|
|
|
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
|
|
|
|
|
2014-05-02 04:35:02 +00:00
|
|
|
# Verify the shell is valid
|
|
|
|
if execute("", error_check: false) != 0
|
|
|
|
raise Vagrant::Errors::SSHInvalidShell
|
|
|
|
end
|
|
|
|
|
2014-01-03 19:13:21 +00:00
|
|
|
# If we're already attempting to switch out the SSH key, then
|
|
|
|
# just return that we're ready (for Machine#guest).
|
|
|
|
@lock.synchronize do
|
2014-01-03 19:29:44 +00:00
|
|
|
return true if @inserted_key || !@machine.config.ssh.insert_key
|
2014-01-03 19:13:21 +00:00
|
|
|
@inserted_key = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# If we used a password, then insert the insecure key
|
|
|
|
ssh_info = @machine.ssh_info
|
2014-10-24 16:58:18 +00:00
|
|
|
insert = ssh_info[:password] && ssh_info[:private_key_path].empty?
|
|
|
|
ssh_info[:private_key_path].each do |pk|
|
|
|
|
if insecure_key?(pk)
|
|
|
|
insert = true
|
|
|
|
@machine.ui.detail("\n"+I18n.t("vagrant.inserting_insecure_detected"))
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if insert
|
|
|
|
# If we don't have the power to insert/remove keys, then its an error
|
|
|
|
cap = @machine.guest.capability?(:insert_public_key) &&
|
|
|
|
@machine.guest.capability?(:remove_public_key)
|
|
|
|
raise Vagrant::Errors::SSHInsertKeyUnsupported if !cap
|
|
|
|
|
|
|
|
_pub, priv, openssh = Vagrant::Util::Keypair.create
|
|
|
|
|
|
|
|
@logger.info("Inserting key to avoid password: #{openssh}")
|
|
|
|
@machine.ui.detail("\n"+I18n.t("vagrant.inserting_random_key"))
|
|
|
|
@machine.guest.capability(:insert_public_key, openssh)
|
2014-01-03 19:13:21 +00:00
|
|
|
|
|
|
|
# Write out the private key in the data dir so that the
|
|
|
|
# machine automatically picks it up.
|
|
|
|
@machine.data_dir.join("private_key").open("w+") do |f|
|
2014-10-24 16:58:18 +00:00
|
|
|
f.write(priv)
|
2014-01-03 19:13:21 +00:00
|
|
|
end
|
|
|
|
|
2014-10-24 16:58:18 +00:00
|
|
|
# Remove the old key if it exists
|
|
|
|
@machine.ui.detail(I18n.t("vagrant.inserting_remove_key"))
|
|
|
|
@machine.guest.capability(
|
|
|
|
:remove_public_key,
|
|
|
|
Vagrant.source_root.join("keys", "vagrant.pub").read.chomp)
|
|
|
|
|
|
|
|
# Done, restart.
|
|
|
|
@machine.ui.detail(I18n.t("vagrant.inserted_key"))
|
2014-01-03 19:13:21 +00:00
|
|
|
@connection.close
|
|
|
|
@connection = nil
|
|
|
|
|
|
|
|
return ready?
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
|
|
|
|
# If we reached this point then we successfully connected
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-01-07 04:03:56 +00:00
|
|
|
def execute(command, opts=nil, &block)
|
|
|
|
opts = {
|
2014-05-22 16:35:12 +00:00
|
|
|
error_check: true,
|
|
|
|
error_class: Vagrant::Errors::VagrantError,
|
|
|
|
error_key: :ssh_bad_exit_status,
|
|
|
|
good_exit: 0,
|
|
|
|
command: command,
|
|
|
|
shell: nil,
|
|
|
|
sudo: false,
|
2012-01-07 04:03:56 +00:00
|
|
|
}.merge(opts || {})
|
|
|
|
|
2014-04-12 21:35:27 +00:00
|
|
|
opts[:good_exit] = Array(opts[:good_exit])
|
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
# Connect via SSH and execute the command in the shell.
|
2013-07-18 03:39:42 +00:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
2012-01-07 04:03:56 +00:00
|
|
|
exit_status = connect do |connection|
|
2014-04-16 02:31:13 +00:00
|
|
|
shell_opts = {
|
|
|
|
sudo: opts[:sudo],
|
|
|
|
shell: opts[:shell],
|
|
|
|
}
|
|
|
|
|
|
|
|
shell_execute(connection, command, **shell_opts) do |type, data|
|
2013-07-18 03:39:42 +00:00
|
|
|
if type == :stdout
|
|
|
|
stdout += data
|
|
|
|
elsif type == :stderr
|
|
|
|
stderr += data
|
|
|
|
end
|
|
|
|
|
|
|
|
block.call(type, data) if block
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
2012-01-07 04:03:56 +00:00
|
|
|
# Check for any errors
|
2014-04-12 21:35:27 +00:00
|
|
|
if opts[:error_check] && !opts[:good_exit].include?(exit_status)
|
2012-01-07 04:03:56 +00:00
|
|
|
# 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`
|
2013-07-18 03:39:42 +00:00
|
|
|
error_opts = opts.merge(
|
2014-05-22 16:35:12 +00:00
|
|
|
_key: opts[:error_key],
|
|
|
|
stdout: stdout,
|
|
|
|
stderr: stderr
|
2013-07-18 03:39:42 +00:00
|
|
|
)
|
2012-01-07 04:03:56 +00:00
|
|
|
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.
|
2014-05-22 16:35:12 +00:00
|
|
|
opts = { sudo: true }.merge(opts || {})
|
2012-01-07 04:03:56 +00:00
|
|
|
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-08-13 01:54:52 +00:00
|
|
|
def test(command, opts=nil)
|
2014-05-22 16:35:12 +00:00
|
|
|
opts = { error_check: false }.merge(opts || {})
|
2012-08-13 01:54:52 +00:00
|
|
|
execute(command, opts) == 0
|
|
|
|
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|
|
2013-02-06 06:20:19 +00:00
|
|
|
if File.directory?(from)
|
|
|
|
# Recurisvely upload directories
|
2014-05-22 16:35:12 +00:00
|
|
|
scp.upload!(from, to, recursive: true)
|
2013-02-06 06:20:19 +00:00
|
|
|
else
|
|
|
|
# Open file read only to fix issue [GH-1036]
|
|
|
|
scp.upload!(File.open(from, "r"), to)
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
2012-06-23 04:04:21 +00:00
|
|
|
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
|
2014-02-03 11:09:13 +00:00
|
|
|
raise Vagrant::Errors::SCPPermissionDenied,
|
|
|
|
from: from.to_s,
|
|
|
|
to: to.to_s
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Opens an SSH connection and yields it to a block.
|
2014-02-14 04:59:26 +00:00
|
|
|
def connect(**opts)
|
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.
|
2014-10-21 23:59:34 +00:00
|
|
|
#
|
|
|
|
# We wrap the check itself in a 5 second timeout because there
|
|
|
|
# are some cases where this will just hang.
|
2012-01-07 19:37:08 +00:00
|
|
|
begin
|
2014-10-21 23:59:34 +00:00
|
|
|
Timeout.timeout(5) do
|
|
|
|
@connection.exec!("")
|
|
|
|
end
|
2013-03-28 22:48:36 +00:00
|
|
|
rescue Exception => e
|
|
|
|
@logger.info("Connection errored, not re-using. Will reconnect.")
|
|
|
|
@logger.debug(e.inspect)
|
2013-04-03 15:31:10 +00:00
|
|
|
@connection = nil
|
2012-01-07 19:37:08 +00:00
|
|
|
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
|
|
|
|
|
2013-03-06 22:27:40 +00:00
|
|
|
# Get the SSH info for the machine, raise an exception if the
|
|
|
|
# provider is saying that SSH is not ready.
|
2012-08-09 04:48:51 +00:00
|
|
|
ssh_info = @machine.ssh_info
|
2013-03-06 22:27:40 +00:00
|
|
|
raise Vagrant::Errors::SSHNotReady if ssh_info.nil?
|
2012-01-06 08:56:09 +00:00
|
|
|
|
2014-02-14 04:59:26 +00:00
|
|
|
# Default some options
|
|
|
|
opts[:retries] = 5 if !opts.has_key?(:retries)
|
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
# Build the options we'll use to initiate the connection via Net::SSH
|
2014-02-14 04:59:26 +00:00
|
|
|
common_connect_opts = {
|
2014-05-22 16:35:12 +00:00
|
|
|
auth_methods: ["none", "publickey", "hostbased", "password"],
|
|
|
|
config: false,
|
|
|
|
forward_agent: ssh_info[:forward_agent],
|
|
|
|
keys: ssh_info[:private_key_path],
|
|
|
|
keys_only: true,
|
|
|
|
paranoid: false,
|
|
|
|
password: ssh_info[:password],
|
|
|
|
port: ssh_info[:port],
|
|
|
|
timeout: 15,
|
|
|
|
user_known_hosts_file: [],
|
|
|
|
verbose: :debug,
|
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-08-29 20:39:03 +00:00
|
|
|
# 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 = [
|
2013-04-01 03:27:08 +00:00
|
|
|
Errno::EACCES,
|
2013-02-19 00:43:33 +00:00
|
|
|
Errno::EADDRINUSE,
|
2012-08-29 20:39:03 +00:00
|
|
|
Errno::ECONNREFUSED,
|
2013-02-01 04:04:57 +00:00
|
|
|
Errno::ECONNRESET,
|
2013-07-20 04:27:25 +00:00
|
|
|
Errno::ENETUNREACH,
|
2012-08-29 20:39:03 +00:00
|
|
|
Errno::EHOSTUNREACH,
|
|
|
|
Net::SSH::Disconnect,
|
|
|
|
Timeout::Error
|
|
|
|
]
|
|
|
|
|
2013-09-16 00:37:40 +00:00
|
|
|
timeout = 60
|
2013-02-04 19:44:56 +00:00
|
|
|
|
2014-06-27 23:27:23 +00:00
|
|
|
@logger.info("Attempting SSH connection...")
|
2014-05-22 16:35:12 +00:00
|
|
|
connection = retryable(tries: opts[:retries], on: exceptions) do
|
2013-02-04 19:44:56 +00:00
|
|
|
Timeout.timeout(timeout) do
|
2013-02-04 21:46:59 +00:00
|
|
|
begin
|
|
|
|
# This logger will get the Net-SSH log data for us.
|
|
|
|
ssh_logger_io = StringIO.new
|
|
|
|
ssh_logger = Logger.new(ssh_logger_io)
|
|
|
|
|
|
|
|
# Setup logging for connections
|
2014-02-14 04:59:26 +00:00
|
|
|
connect_opts = common_connect_opts.dup
|
|
|
|
connect_opts[:logger] = ssh_logger
|
2013-09-05 00:23:43 +00:00
|
|
|
|
|
|
|
if ssh_info[:proxy_command]
|
|
|
|
connect_opts[:proxy] = Net::SSH::Proxy::Command.new(ssh_info[:proxy_command])
|
|
|
|
end
|
2013-02-04 21:46:59 +00:00
|
|
|
|
2013-04-19 16:14:45 +00:00
|
|
|
@logger.info("Attempting to connect to SSH...")
|
|
|
|
@logger.info(" - Host: #{ssh_info[:host]}")
|
|
|
|
@logger.info(" - Port: #{ssh_info[:port]}")
|
|
|
|
@logger.info(" - Username: #{ssh_info[:username]}")
|
2014-01-03 17:55:34 +00:00
|
|
|
@logger.info(" - Password? #{!!ssh_info[:password]}")
|
2013-04-19 16:14:45 +00:00
|
|
|
@logger.info(" - Key Path: #{ssh_info[:private_key_path]}")
|
|
|
|
|
2013-02-04 21:46:59 +00:00
|
|
|
Net::SSH.start(ssh_info[:host], ssh_info[:username], connect_opts)
|
|
|
|
ensure
|
|
|
|
# Make sure we output the connection log
|
|
|
|
@logger.debug("== Net-SSH connection debug-level log START ==")
|
|
|
|
@logger.debug(ssh_logger_io.string)
|
|
|
|
@logger.debug("== Net-SSH connection debug-level log END ==")
|
|
|
|
end
|
2012-03-29 05:30:01 +00:00
|
|
|
end
|
2012-03-13 21:27:16 +00:00
|
|
|
end
|
2013-04-01 03:27:08 +00:00
|
|
|
rescue Errno::EACCES
|
|
|
|
# This happens on connect() for unknown reasons yet...
|
|
|
|
raise Vagrant::Errors::SSHConnectEACCES
|
2013-01-17 00:42:00 +00:00
|
|
|
rescue Errno::ETIMEDOUT, Timeout::Error
|
2012-03-29 05:30:01 +00:00
|
|
|
# This happens if we continued to timeout when attempting to connect.
|
2012-08-09 04:48:51 +00:00
|
|
|
raise Vagrant::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.
|
2012-08-09 04:48:51 +00:00
|
|
|
raise Vagrant::Errors::SSHAuthenticationFailed
|
2012-07-04 18:26:09 +00:00
|
|
|
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.
|
2012-08-09 04:48:51 +00:00
|
|
|
raise Vagrant::Errors::SSHDisconnected
|
2012-03-13 21:27:16 +00:00
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
# This is raised if we failed to connect the max amount of times
|
2012-08-09 04:48:51 +00:00
|
|
|
raise Vagrant::Errors::SSHConnectionRefused
|
2013-02-07 02:08:55 +00:00
|
|
|
rescue Errno::ECONNRESET
|
|
|
|
# This is raised if we failed to connect the max number of times
|
|
|
|
# due to an ECONNRESET.
|
|
|
|
raise Vagrant::Errors::SSHConnectionReset
|
2013-01-12 20:47:49 +00:00
|
|
|
rescue Errno::EHOSTDOWN
|
|
|
|
# This is raised if we get an ICMP DestinationUnknown error.
|
|
|
|
raise Vagrant::Errors::SSHHostDown
|
2013-07-09 12:35:07 +00:00
|
|
|
rescue Errno::EHOSTUNREACH
|
|
|
|
# This is raised if we can't work out how to route traffic.
|
|
|
|
raise Vagrant::Errors::SSHNoRoute
|
2014-08-29 18:40:50 +00:00
|
|
|
rescue Net::SSH::Exception => e
|
|
|
|
# This is an internal error in Net::SSH
|
|
|
|
raise Vagrant::Errors::NetSSHException, message: e.message
|
2012-03-13 21:27:16 +00:00
|
|
|
rescue NotImplementedError
|
|
|
|
# This is raised if a private key type that Net-SSH doesn't support
|
|
|
|
# is used. Show a nicer error.
|
2012-08-09 04:48:51 +00:00
|
|
|
raise Vagrant::Errors::SSHKeyTypeNotSupported
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
2014-02-14 04:34:51 +00:00
|
|
|
@connection = connection
|
|
|
|
@connection_ssh_info = ssh_info
|
2012-01-07 19:37:08 +00:00
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
# Yield the connection that is ready to be used and
|
|
|
|
# return the value of the block
|
|
|
|
return yield connection if block_given?
|
2012-08-09 04:48:51 +00:00
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
|
|
|
|
# Executes the command on an SSH connection within a login shell.
|
2014-04-16 02:31:13 +00:00
|
|
|
def shell_execute(connection, command, **opts)
|
|
|
|
opts = {
|
|
|
|
sudo: false,
|
|
|
|
shell: nil
|
|
|
|
}.merge(opts)
|
|
|
|
|
|
|
|
sudo = opts[:sudo]
|
|
|
|
shell = opts[:shell]
|
|
|
|
|
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
|
|
|
|
|
2013-11-22 23:30:12 +00:00
|
|
|
# Determine the shell to execute. Prefer the explicitly passed in shell
|
|
|
|
# over the default configured shell. If we are using `sudo` then we
|
2012-01-06 08:56:09 +00:00
|
|
|
# need to wrap the shell in a `sudo` call.
|
2013-11-22 23:30:12 +00:00
|
|
|
shell_cmd = @machine.config.ssh.shell
|
|
|
|
shell_cmd = shell if shell
|
2013-12-03 20:07:14 +00:00
|
|
|
shell_cmd = "sudo -E -H #{shell_cmd}" if sudo
|
2012-01-06 08:56:09 +00:00
|
|
|
|
2014-08-29 16:41:35 +00:00
|
|
|
# These variables are used to scrub PTY output if we're in a PTY
|
|
|
|
pty = false
|
|
|
|
pty_stdout = ""
|
2014-08-28 16:02:27 +00:00
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
# Open the channel so we can execute or command
|
|
|
|
channel = connection.open_channel do |ch|
|
2013-11-25 00:48:07 +00:00
|
|
|
if @machine.config.ssh.pty
|
|
|
|
ch.request_pty do |ch2, success|
|
2014-08-29 16:41:35 +00:00
|
|
|
pty = success && command != ""
|
|
|
|
|
2013-11-25 00:48:07 +00:00
|
|
|
if success
|
|
|
|
@logger.debug("pty obtained for connection")
|
|
|
|
else
|
|
|
|
@logger.warn("failed to obtain pty, will try to continue anyways")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-22 23:30:12 +00:00
|
|
|
ch.exec(shell_cmd) do |ch2, _|
|
2012-01-06 08:56:09 +00:00
|
|
|
# Setup the channel callbacks so we can get data and exit status
|
|
|
|
ch2.on_data do |ch3, data|
|
2013-02-01 18:56:00 +00:00
|
|
|
# Filter out the clear screen command
|
|
|
|
data = remove_ansi_escape_codes(data)
|
|
|
|
@logger.debug("stdout: #{data}")
|
2014-08-29 16:41:35 +00:00
|
|
|
if pty
|
|
|
|
pty_stdout << data
|
2014-08-28 16:02:27 +00:00
|
|
|
else
|
|
|
|
yield :stdout, data if block_given?
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
ch2.on_extended_data do |ch3, type, data|
|
2013-02-01 18:56:00 +00:00
|
|
|
# Filter out the clear screen command
|
|
|
|
data = remove_ansi_escape_codes(data)
|
|
|
|
@logger.debug("stderr: #{data}")
|
|
|
|
yield :stderr, data if block_given?
|
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}")
|
2013-07-11 21:40:03 +00:00
|
|
|
|
|
|
|
# Close the channel, since after the exit status we're
|
|
|
|
# probably done. This fixes up issues with hanging.
|
|
|
|
channel.close
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set the terminal
|
|
|
|
ch2.send_data "export TERM=vt100\n"
|
|
|
|
|
2013-07-23 20:07:57 +00:00
|
|
|
# Set SSH_AUTH_SOCK if we are in sudo and forwarding agent.
|
|
|
|
# This is to work around often misconfigured boxes where
|
|
|
|
# the SSH_AUTH_SOCK env var is not preserved.
|
2014-02-14 04:34:51 +00:00
|
|
|
if @connection_ssh_info[:forward_agent] && sudo
|
2013-07-23 20:07:57 +00:00
|
|
|
auth_socket = ""
|
|
|
|
execute("echo; printf $SSH_AUTH_SOCK") do |type, data|
|
|
|
|
if type == :stdout
|
|
|
|
auth_socket += data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if auth_socket != ""
|
|
|
|
# Make sure we only read the last line which should be
|
|
|
|
# the $SSH_AUTH_SOCK env var we printed.
|
|
|
|
auth_socket = auth_socket.split("\n").last.chomp
|
|
|
|
end
|
|
|
|
|
|
|
|
if auth_socket == ""
|
|
|
|
@logger.warn("No SSH_AUTH_SOCK found despite forward_agent being set.")
|
|
|
|
else
|
|
|
|
@logger.info("Setting SSH_AUTH_SOCK remotely: #{auth_socket}")
|
|
|
|
ch2.send_data "export SSH_AUTH_SOCK=#{auth_socket}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-29 16:41:35 +00:00
|
|
|
# Output the command. If we're using a pty we have to do
|
|
|
|
# a little dance to make sure we get all the output properly
|
|
|
|
# without the cruft added from pty mode.
|
|
|
|
if pty
|
|
|
|
data = "stty raw -echo\n"
|
|
|
|
data += "export PS1=\n"
|
|
|
|
data += "export PS2=\n"
|
|
|
|
data += "export PROMPT_COMMAND=\n"
|
|
|
|
data += "printf #{PTY_DELIM_START}\n"
|
2014-08-28 16:02:27 +00:00
|
|
|
data += "#{command}\n"
|
2014-08-29 09:48:54 +00:00
|
|
|
data += "exitcode=$?\n"
|
2014-08-29 16:41:35 +00:00
|
|
|
data += "printf #{PTY_DELIM_END}\n"
|
|
|
|
data += "exit $exitcode\n"
|
2014-08-28 16:02:27 +00:00
|
|
|
data = data.force_encoding('ASCII-8BIT')
|
|
|
|
ch2.send_data data
|
|
|
|
else
|
|
|
|
ch2.send_data "#{command}\n".force_encoding('ASCII-8BIT')
|
2014-08-29 09:48:54 +00:00
|
|
|
# Remember to exit or this channel will hang open
|
|
|
|
ch2.send_data "exit\n"
|
2014-08-28 16:02:27 +00:00
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
|
2012-06-08 12:13:13 +00:00
|
|
|
# Send eof to let server know we're done
|
|
|
|
ch2.eof!
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-08 04:51:14 +00:00
|
|
|
begin
|
|
|
|
keep_alive = nil
|
|
|
|
|
|
|
|
if @machine.config.ssh.keep_alive
|
|
|
|
# Begin sending keep-alive packets while we wait for the script
|
|
|
|
# to complete. This avoids connections closing on long-running
|
|
|
|
# scripts.
|
|
|
|
keep_alive = Thread.new do
|
|
|
|
loop do
|
|
|
|
sleep 5
|
|
|
|
@logger.debug("Sending SSH keep-alive...")
|
|
|
|
connection.send_global_request("keep-alive@openssh.com")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for the channel to complete
|
2013-08-29 17:35:56 +00:00
|
|
|
begin
|
|
|
|
channel.wait
|
2014-05-09 22:58:48 +00:00
|
|
|
rescue Errno::ECONNRESET, IOError
|
2014-08-07 18:53:44 +00:00
|
|
|
@logger.info(
|
|
|
|
"SSH connection unexpected closed. Assuming reboot or something.")
|
2013-08-29 17:35:56 +00:00
|
|
|
exit_status = 0
|
2014-08-29 16:41:35 +00:00
|
|
|
pty = false
|
2014-08-08 16:11:25 +00:00
|
|
|
rescue Net::SSH::ChannelOpenFailed
|
|
|
|
raise Vagrant::Errors::SSHChannelOpenFail
|
2014-08-07 18:53:44 +00:00
|
|
|
rescue Net::SSH::Disconnect
|
|
|
|
raise Vagrant::Errors::SSHDisconnected
|
2013-08-29 17:35:56 +00:00
|
|
|
end
|
2013-04-08 04:51:14 +00:00
|
|
|
ensure
|
|
|
|
# Kill the keep-alive thread
|
|
|
|
keep_alive.kill if keep_alive
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
|
2014-08-29 16:41:35 +00:00
|
|
|
# If we're in a PTY, we now finally parse the output
|
|
|
|
if pty
|
|
|
|
@logger.debug("PTY stdout: #{pty_stdout}")
|
|
|
|
if !pty_stdout.include?(PTY_DELIM_START) || !pty_stdout.include?(PTY_DELIM_END)
|
|
|
|
@logger.error("PTY stdout doesn't include delims")
|
2014-08-28 16:02:27 +00:00
|
|
|
raise Vagrant::Errors::SSHInvalidShell.new
|
|
|
|
end
|
2014-08-29 16:41:35 +00:00
|
|
|
|
2014-09-04 01:47:46 +00:00
|
|
|
data = pty_stdout[/.*#{PTY_DELIM_START}(.*?)#{PTY_DELIM_END}/m, 1]
|
2014-08-29 16:41:35 +00:00
|
|
|
@logger.debug("PTY stdout parsed: #{data}")
|
2014-08-28 16:02:27 +00:00
|
|
|
yield :stdout, data if block_given?
|
|
|
|
end
|
|
|
|
|
2012-01-06 08:56:09 +00:00
|
|
|
# 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.
|
2012-08-09 04:48:51 +00:00
|
|
|
raise Vagrant::Errors::SCPUnavailable if e.message =~ /\(127\)/
|
2012-05-06 02:52:10 +00:00
|
|
|
|
|
|
|
# Otherwise, just raise the error up
|
|
|
|
raise
|
|
|
|
end
|
2014-10-24 16:58:18 +00:00
|
|
|
|
|
|
|
# This will test whether path is the Vagrant insecure private key.
|
|
|
|
#
|
|
|
|
# @param [String] path
|
|
|
|
def insecure_key?(path)
|
|
|
|
return false if !path
|
|
|
|
return false if !File.file?(path)
|
|
|
|
source_path = Vagrant.source_root.join("keys", "vagrant")
|
|
|
|
return File.read(path).chomp == source_path.read.chomp
|
|
|
|
end
|
2012-01-06 08:56:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|