Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
.irbrc

rubyshell-*.gem
sshd-config/
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ AllCops:
NewCops: enable
TargetRubyVersion: 2.6
SuggestExtensions: false
Exclude:
- "sshd-config/**/*"

Style/StringLiterals:
Enabled: true
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
ssh-server:
image: lscr.io/linuxserver/openssh-server:latest
environment:
- USER_NAME=testuser
- USER_PASSWORD=testpass
- PASSWORD_ACCESS=true
ports:
- "2222:2222"
1 change: 1 addition & 0 deletions lib/rubyshell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require_relative "rubyshell/debugger"
require_relative "rubyshell/env_proxy"
require_relative "rubyshell/parallel_executor"
require_relative "rubyshell/remote_executor"

module RubyShell
class << self
Expand Down
32 changes: 21 additions & 11 deletions lib/rubyshell/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@ module Debugger
class << self
def run_wrapper(command, debug: nil)
if debug || RubyShell.debug?

time_one = Process.clock_gettime(Process::CLOCK_MONOTONIC)

result = yield

time_two = Process.clock_gettime(Process::CLOCK_MONOTONIC)

RubyShell.log(<<~TEXT
\nExecuted: #{command.to_shell.chomp}
Duration: #{format("%.6f", time_two - time_one)}s
Pid: #{result.metadata[:exit_status].pid}
Exit code: #{result.metadata[:exit_status].to_i}
Stdout: #{result.to_s.inspect}
TEXT
)
log_result(command, result, time_two - time_one)

result
else
yield
end
end

private

def log_result(command, result, duration)
meta = result.metadata
text = +"\nExecuted: #{command.to_shell.chomp}\n"

if meta[:remote]
text << " Host: #{meta[:host]}\n"
text << " Port: #{meta[:port]}\n"
else
text << " Pid: #{meta[:exit_status].pid}\n"
end

text << " Duration: #{format("%.6f", duration)}s\n"
text << " Exit code: #{meta[:exit_status].to_i}\n"
text << " Stdout: #{result.to_s.inspect}\n"

RubyShell.log(text)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rubyshell/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def parallel(options = {}, &block)
end
end

def remote(host, **options, &block)
RubyShell::RemoteExecutor.new(host, **options).evaluate(&block)
end

def method_missing(method_name, *args, **kwargs)
command = RubyShell::Command.new(method_name.to_s.gsub(/!$/, ""), *args, **kwargs)

Expand Down
107 changes: 107 additions & 0 deletions lib/rubyshell/remote_executor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# frozen_string_literal: true

require_relative "ssh"

module RubyShell
class RemoteExecutor
RemoteCommand = Struct.new(:shell_string) do
def to_shell
shell_string
end
end

RemoteStatus = Struct.new(:exit_code) do
def to_i
exit_code
end

def pid
nil
end

def success?
exit_code.zero?
end
end

def initialize(host, port: 22, key: nil, timeout: SSH::DEFAULT_TIMEOUT, debug: nil)
@host = host
@port = port
@key = key
@timeout = timeout
@debug = debug
end

def evaluate(&block)
user, host = parse_host(@host)
@connection = SSH.new(host, user: user, port: @port, key: @key, timeout: @timeout)

instance_exec(&block)
ensure
@connection&.close
end

def sh(command, *args, **kwargs)
method_missing(command, *args, **kwargs)
end

def chain(options = {}, &block)
chainer = RubyShell::ChainContext.new(options).instance_exec(&block)
execute_remote(chainer.to_shell)
end

def cd(path)
execute_remote("cd '#{path.gsub("'", "'\\''")}'")
end

def method_missing(method_name, *args, **kwargs)
command = RubyShell::Command.new(method_name.to_s.gsub(/!$/, ""), *args, **kwargs)

execute_remote(command.to_shell)
end

def respond_to_missing?(_name, _include_private)
false
end

private

def parse_host(host_string)
if host_string.include?("@")
host_string.split("@", 2)
else
[ENV.fetch("USER", nil), host_string]
end
end

def execute_remote(shell_string)
wrapper = RemoteCommand.new(shell_string)

RubyShell::Debugger.run_wrapper(wrapper, debug: @debug) do
result = @connection.execute(shell_string)

unless result.success?
raise RubyShell::CommandError.new(
command: shell_string,
stdout: result.stdout,
stderr: result.stderr,
status: result.exit_code
)
end

status = RemoteStatus.new(result.exit_code)

RubyShell::Results::StringResult.new(
result.stdout,
metadata: {
command: shell_string,
exit_status: status,
remote: true,
host: @host,
port: @port
}
)
end
end
end
end
107 changes: 107 additions & 0 deletions lib/rubyshell/ssh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# frozen_string_literal: true

require "open3"
require "securerandom"
require "timeout"

module RubyShell
class SSH
class CommandTimeout < StandardError; end

Result = Struct.new(:stdout, :stderr, :exit_code) do
alias_method :to_s, :stdout
alias_method :output, :stdout

def success?
exit_code.zero?
end

def lines
stdout.lines
end
end

attr_reader :host, :port, :user

DEFAULT_TIMEOUT = 30

def initialize(host, user:, port: 22, key: nil, timeout: DEFAULT_TIMEOUT)
@host = host
@user = user
@port = port
@timeout = timeout

cmd = [
"ssh",
"-o", "StrictHostKeyChecking=no",
"-p", port.to_s,
"-T"
]

cmd += ["-i", key] if key

cmd << "#{user}@#{host}"

@stdin, @stdout, @stderr, @wait_thread = Open3.popen3(*cmd)
end

def execute(command, timeout: @timeout) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
delim = "__RUBYSHELL_#{SecureRandom.hex(8)}__"

@stdin.puts("#{command}; echo \"#{delim} $?\"; echo #{delim} >&2")
@stdin.flush

stdout_buf = +""
stderr_buf = +""
exit_code = 0
stdout_done = false
stderr_done = false
ios = [@stdout, @stderr]

Timeout.timeout(timeout, CommandTimeout, "command timed out after #{timeout}s: #{command}") do
until stdout_done && stderr_done
break if ios.empty?

readable, = IO.select(ios, nil, nil, 0.1)
next unless readable

readable.each do |io|
chunk = io.read_nonblock(4096, exception: false)

if chunk.nil?
ios.delete(io)
stdout_done = true if io == @stdout
stderr_done = true if io == @stderr
next
end

next if chunk == :wait_readable

if io == @stdout
stdout_buf << chunk
if stdout_buf.include?(delim)
before, delim_line = stdout_buf.split(delim, 2)
stdout_buf = before
exit_code = delim_line.strip.to_i
stdout_done = true
end
else
stderr_buf << chunk
if stderr_buf.include?(delim)
stderr_buf = stderr_buf.split(delim, 2).first
stderr_done = true
end
end
end
end
end

Result.new(stdout_buf.chomp, stderr_buf.chomp, exit_code)
end

def close
[@stdin, @stdout, @stderr].each { |io| io&.close rescue nil } # rubocop:disable Style/RescueModifier
@wait_thread&.join(5)
end
end
end
Loading
Loading