Skip to content

Commit

Permalink
Prefix debug output with ssh hostname (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakolehm authored Dec 20, 2018
1 parent d049269 commit 4184803
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
16 changes: 14 additions & 2 deletions lib/pharos/ssh/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ module SSH
class Client
include MonitorMixin

attr_reader :session
attr_reader :session, :host

# @param host [String]
# @param user [String, NilClass]
# @param opts [Hash]
def initialize(host, user = nil, opts = {})
super()
@host = host
Expand All @@ -37,6 +40,7 @@ def logger
end
end

# @return [Hash,NilClass]
def bastion
@bastion ||= @opts.delete(:bastion)
end
Expand All @@ -55,6 +59,8 @@ def connect
end
end

# @param host [String]
# @param port [Integer]
# @return [Integer] local port number
def gateway(host, port)
Net::SSH::Gateway.new(@host, @user, @opts).open(host, port)
Expand All @@ -78,21 +84,24 @@ def tempfile(prefix: "pharos", content: nil, &block)
end

# @param cmd [String] command to execute
# @param options [Hash]
# @return [Pharos::Command::Result]
def exec(cmd, **options)
require_session!
synchronize { RemoteCommand.new(self, cmd, **options).run }
end

# @param cmd [String] command to execute
# @param options [Hash]
# @raise [Pharos::SSH::RemoteCommand::ExecError]
# @return [String] stdout
def exec!(cmd, **options)
require_session!
synchronize { RemoteCommand.new(self, cmd, **options).run!.stdout }
end

# @param script [String] name of script
# @param name [String] name of script
# @param env [Hash] environment variables hash
# @param path [String] real path to file, defaults to script
# @raise [Pharos::SSH::RemoteCommand::ExecError]
# @return [String] stdout
Expand All @@ -107,11 +116,14 @@ def exec_script!(name, env: {}, path: nil, **options)
end

# @param cmd [String] command to execute
# @param options [Hash]
# @return [Boolean]
def exec?(cmd, **options)
exec(cmd, **options).success?
end

# @param path [String]
# @return [Pharos::SSH::RemoteFile]
def file(path)
Pharos::SSH::RemoteFile.new(self, path)
end
Expand Down
1 change: 1 addition & 0 deletions lib/pharos/ssh/interactive_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module SSH
class InteractiveSession
attr_reader :client

# @param client [Pharos::SSH::Client]
def initialize(client)
@client = client
end
Expand Down
23 changes: 18 additions & 5 deletions lib/pharos/ssh/remote_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.debug?
# @param client [Pharos::SSH::Client] ssh client instance
# @param cmd [String,Array<String>] command to execute
# @param stdin [String,IO] attach string or stream to command STDIN
# @param debug
# @param source [String]
def initialize(client, cmd, stdin: nil, source: nil)
@client = client
@cmd = cmd.is_a?(Array) ? cmd.join(' ') : cmd
Expand All @@ -61,12 +61,15 @@ def initialize(client, cmd, stdin: nil, source: nil)
freeze
end

# @return [Result]
# @raises [ExecError] if result errors
def run!
result = run
raise ExecError.new(@source || cmd, result.exit_status, result.output) if result.error?
result
end

# @return [Result]
def run
debug_cmd(@cmd, source: @source) if debug?

Expand Down Expand Up @@ -122,29 +125,39 @@ def initialize_debug
end
end

# @return [Boolean]
def debug?
@debug
end

# @param cmd [String]
# @param source [String, NilClass]
# @return [Integer]
def debug_cmd(cmd, source: nil)
$stdout.write(INDENT + pastel.cyan("$ #{cmd}" + (source ? " < #{source}" : "")) + "\n")
$stdout.write("#{INDENT} #{pastel.cyan("#{@client.host}:")} #{pastel.cyan("$ #{cmd}" + (source ? " < #{source}" : ''))}\n")
end

# @param data [String]
# @return [String]
def debug_stdout(data)
data.each_line do |line|
$stdout.write(INDENT + pastel.dim(line.to_s))
$stdout.write("#{INDENT} #{pastel.dim("#{@client.host}:")} #{pastel.dim(line.to_s)}")
end
end

# @param data [String]
# @return [String]
def debug_stderr(data)
data.each_line do |line|
# TODO: stderr is not line-buffered, this indents each write
$stdout.write(INDENT + pastel.red(line.to_s))
$stdout.write("#{INDENT} #{pastel.dim("#{@client.host}:")} #{pastel.red(line.to_s)}")
end
end

# @param exit_status [Integer]
# @return [Integer]
def debug_exit(exit_status)
$stdout.write(INDENT + pastel.yellow("! #{exit_status}") + "\n")
$stdout.write("#{INDENT} #{pastel.dim("#{@client.host}:")} #{pastel.yellow("! #{exit_status}")}\n")
end
end
end
Expand Down
21 changes: 20 additions & 1 deletion lib/pharos/ssh/remote_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ def initialize(client, path)
alias to_s path

# Removes the remote file
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def unlink
@client.exec!("sudo rm #{escaped_path}")
end
alias rm unlink

# @return [String]
def basename
File.basename(@path)
end

# @return [String]
def dirname
File.dirname(@path)
end

# @param content [String]
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def write(content)
tmp = temp_file_path.shellescape
@client.exec!(
Expand All @@ -40,18 +47,23 @@ def write(content)
)
end

# @param mode [String, Integer]
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def chmod(mode)
@client.exec!("sudo chmod #{mode} #{escaped_path}")
end

# Returns remote jfile content
# @return [String]
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def read
@client.exec!("sudo cat #{escaped_path}")
end

# True if the file exists. Assumes a bash-like shell.
# @return [Boolean]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def exist?
@client.exec!("sudo env -i bash --norc --noprofile -c -- 'test -e #{escaped_path} && echo true || echo false'").strip == "true"
end
Expand All @@ -64,25 +76,32 @@ def with_existing

# Moves the current file to target path
# @param target [String]
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def move(target)
@client.exec!("sudo mv #{@path} #{target.shellescape}")
end
alias mv move

# Copies the current file to target path
# @param target [String]
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def copy(target)
@client.exec!("sudo cp #{escaped_path} #{target.shellescape}")
end
alias cp copy

# Creates a symlink at the target path that points to the current file
# @param target [String]
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def link(target)
@client.exec!("sudo ln -s #{escaped_path} #{target.shellescape}")
end

# @return [String, nil]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def readlink
target = @client.exec!("readlink #{escaped_path} || echo").strip

Expand Down
3 changes: 3 additions & 0 deletions lib/pharos/ssh/tempfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def initialize(client, prefix: "pharos", content: nil, &block)
run(&block) if block_given?
end

# @param content [String]
# @return [Pharos::SSH::RemoteCommand::Result]
# @raises [Pharos::SSH::RemoteCommand::ExecError]
def write(content)
@client.exec!(
"cat > #{escaped_path}",
Expand Down

0 comments on commit 4184803

Please sign in to comment.