communicators/ssh: Filter command stdout output

This commit is contained in:
Chris Roberts 2016-08-03 11:29:06 -07:00
parent 3c9e3c6f36
commit 155c78ea93
1 changed files with 24 additions and 4 deletions

View File

@ -19,8 +19,12 @@ module VagrantPlugins
module CommunicatorSSH
# This class provides communication with the VM via SSH.
class Communicator < Vagrant.plugin("2", :communicator)
# Marker for start of PTY enabled command output
PTY_DELIM_START = "bccbb768c119429488cfd109aacea6b5-pty"
# Marker for end of PTY enabled command output
PTY_DELIM_END = "bccbb768c119429488cfd109aacea6b5-pty"
# Marker for start of regular command output
CMD_GARBAGE_MARKER = "41e57d38-b4f7-4e46-9c38-13873d338b86-vagrant-ssh"
include Vagrant::Util::ANSIEscapeCodeRemover
include Vagrant::Util::Retryable
@ -490,16 +494,32 @@ module VagrantPlugins
end
end
marker_found = false
data_buffer = ''
ch.exec(shell_cmd(opts)) do |ch2, _|
# Setup the channel callbacks so we can get data and exit status
ch2.on_data do |ch3, data|
# Filter out the clear screen command
data = remove_ansi_escape_codes(data)
@logger.debug("stdout: #{data}")
if pty
pty_stdout << data
else
yield :stdout, data if block_given?
if !marker_found
data_buffer << data
marker_index = data_buffer.index(CMD_GARBAGE_MARKER)
if marker_index
marker_found = true
data_buffer.slice!(0, marker_index + CMD_GARBAGE_MARKER.size)
data.replace data_buffer
data_buffer = nil
end
end
if block_given? && marker_found
yield :stdout, data
end
end
end
@ -516,7 +536,7 @@ module VagrantPlugins
# Close the channel, since after the exit status we're
# probably done. This fixes up issues with hanging.
channel.close
ch.close
end
# Set the terminal
@ -563,7 +583,7 @@ module VagrantPlugins
data = data.force_encoding('ASCII-8BIT')
ch2.send_data data
else
ch2.send_data "#{command}\n".force_encoding('ASCII-8BIT')
ch2.send_data "printf '#{CMD_GARBAGE_MARKER}'\n#{command}\n".force_encoding('ASCII-8BIT')
# Remember to exit or this channel will hang open
ch2.send_data "exit\n"
end