Prevent overly verbose output from SSH communicator

If the type of error changes on retry the messages will effectively
spam the user display with alternating messages. Log each message
sent and only re-display each message once within 10 seconds.
This commit is contained in:
Chris Roberts 2018-10-19 15:18:03 -07:00
parent 5f5d874997
commit 8562daf85e
2 changed files with 47 additions and 6 deletions

View File

@ -75,8 +75,7 @@ module VagrantPlugins
ssh_auth_type = "password" if ssh_info[:password] ssh_auth_type = "password" if ssh_info[:password]
@machine.ui.detail("SSH auth method: #{ssh_auth_type}") @machine.ui.detail("SSH auth method: #{ssh_auth_type}")
last_message = nil previous_messages = {}
last_message_repeat_at = 0
while true while true
message = nil message = nil
begin begin
@ -123,14 +122,13 @@ module VagrantPlugins
if message if message
message_at = Time.now.to_f message_at = Time.now.to_f
show_message = true show_message = true
if last_message == message if previous_messages[message]
show_message = (message_at - last_message_repeat_at) > 10.0 show_message = (message_at - previous_messages[message]) > 10.0
end end
if show_message if show_message
@machine.ui.detail("Warning: #{message} Retrying...") @machine.ui.detail("Warning: #{message} Retrying...")
last_message = message previous_messages[message] = message_at
last_message_repeat_at = message_at
end end
end end
end end

View File

@ -116,6 +116,49 @@ describe VagrantPlugins::CommunicatorSSH::Communicator do
expect(communicator.wait_for_ready(0.6)).to eq(true) expect(communicator.wait_for_ready(0.6)).to eq(true)
end end
end end
context "when printing message to the user" do
before do
allow(machine).to receive(:ssh_info).
and_return(host: '10.1.2.3', port: 22).ordered
allow(communicator).to receive(:connect)
allow(communicator).to receive(:ready?).and_return(true)
end
it "should print message" do
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHConnectionTimeout)
expect(ui).to receive(:detail).with(/timeout/)
communicator.wait_for_ready(0.5)
end
it "should not print the same message twice" do
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHConnectionTimeout)
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHConnectionTimeout)
expect(ui).to receive(:detail).with(/timeout/)
expect(ui).not_to receive(:detail).with(/timeout/)
communicator.wait_for_ready(0.5)
end
it "should print different messages" do
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHConnectionTimeout)
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHDisconnected)
expect(ui).to receive(:detail).with(/timeout/)
expect(ui).to receive(:detail).with(/disconnect/)
communicator.wait_for_ready(0.5)
end
it "should not print different messages twice" do
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHConnectionTimeout)
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHDisconnected)
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHConnectionTimeout)
expect(communicator).to receive(:connect).and_raise(Vagrant::Errors::SSHDisconnected)
expect(ui).to receive(:detail).with(/timeout/)
expect(ui).to receive(:detail).with(/disconnect/)
expect(ui).not_to receive(:detail).with(/timeout/)
expect(ui).not_to receive(:detail).with(/disconnect/)
communicator.wait_for_ready(0.5)
end
end
end end
end end