Skip to content

Commit

Permalink
Instead of fixing the issue in where platform detection is happening …
Browse files Browse the repository at this point in the history
…fixed it in command execution as this is the origin

Signed-off-by: Vasu1105 <vasundhara.jagdale@msystechnologies.com>
  • Loading branch information
Vasu1105 committed Jun 14, 2019
1 parent 4625a8b commit 5fec477
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
12 changes: 3 additions & 9 deletions lib/train/extras/command_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,16 @@ class CommandWrapper
include_options WindowsCommand

def self.load(transport, options)
if transport.class == Train::Transports::SSH::Connection && !transport.connected?
# Need to call run_command here to get the message and show it to user while raising exception
msg = transport.run_command("whoami").stdout
# this will throw the error as
# Train::UserError: Ssh failed: Please login as the user "username" rather than the user "root".
raise Train::UserError.new("Ssh failed: #{msg}")
elsif transport.os.unix?
if transport.platform.unix?
return nil unless LinuxCommand.active?(options)
res = LinuxCommand.new(transport, options)
verification_res = res.verify
if verification_res
msg, reason = verification_res
raise Train::UserError.new("Sudo failed: #{msg}", reason)
raise Train::UserError, "Sudo failed: #{msg}", reason
end
res
elsif transport.os.windows?
elsif transport.platform.windows?
res = WindowsCommand.new(transport, options)
res
end
Expand Down
6 changes: 6 additions & 0 deletions lib/train/platforms/detect/helpers/os_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def unix_file_exist?(path)

def command_output(cmd)
res = @backend.run_command(cmd).stdout
# When you try to execute command using ssh connction as root user and you have provided ssh user identity file
# it gives standard output to login as authorised user other than root. To show this standard ouput as an error
# to user we are matching the string of stdout and raising the error here so that user gets exact information.
if @backend.class.to_s == "Train::Transports::SSH::Connection" && res =~ /Please login as the user/
raise Train::UserError, "SSH failed: #{res}"
end
res.strip! unless res.nil?
res
end
Expand Down
5 changes: 0 additions & 5 deletions lib/train/transports/ssh_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ def login_command
LoginCommand.new("ssh", args)
end

def connected?
return false if session.nil?
session.exec!("whoami").match?(/Please login as the user/) ? false : true
end

# (see Base::Connection#upload)
def upload(locals, remote)
waits = []
Expand Down

0 comments on commit 5fec477

Please sign in to comment.