-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Naïve SSHAdapter implementation using Net/SSH
- Loading branch information
Showing
5 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'net/ssh' | ||
|
||
module RemoteRuby | ||
class SSHAdapter < ConnectionAdapter | ||
attr_reader :host, :user, :config, :working_dir | ||
|
||
def initialize(host:, user: nil, working_dir: nil) | ||
super | ||
@host = host | ||
@working_dir = working_dir | ||
@config = Net::SSH.configuration_for(@host, true) | ||
@user = user || @config[:user] | ||
end | ||
|
||
def open(code) | ||
Net::SSH.start(host, user, config) do |ssh| | ||
# ssh.exec!('f=$(mktemp) && cat > $f && echo $f') | ||
|
||
fname = '' | ||
code_channel = ssh.open_channel do |channel| | ||
channel.exec('f=$(mktemp remote_ruby.rb.XXXXXX) && cat > $f && echo $f') do |ch, success| | ||
raise 'Could not execute command' unless success | ||
|
||
ch.on_data do |_, data| | ||
fname << data | ||
end | ||
|
||
ch.send_data(code) | ||
ch.eof! | ||
end | ||
end | ||
|
||
code_channel.wait | ||
|
||
stdout_r, stdout_w = IO.pipe | ||
stderr_r, stderr_w = IO.pipe | ||
stdin_r, stdin_w = IO.pipe | ||
|
||
execution_channel = ssh.open_channel do |channel| | ||
channel.exec("cd #{working_dir} && ruby #{fname}") do |ch, success| | ||
raise 'Could not execute command' unless success | ||
|
||
ssh.listen_to(stdin_r) do |io| | ||
data = io.read_nonblock(4096) | ||
ch.send_data(data) | ||
rescue EOFError | ||
ch.eof! | ||
end | ||
|
||
ch.on_data do |_, data| | ||
stdout_w << data | ||
end | ||
|
||
ch.on_extended_data do |_, _, data| | ||
stderr_w << data | ||
end | ||
|
||
ch.on_close do |_| | ||
stdout_w.close | ||
stderr_w.close | ||
end | ||
end | ||
end | ||
|
||
Thread.new { yield stdin_w, stdout_r, stderr_r } | ||
|
||
execution_channel.wait | ||
|
||
ssh.exec!("rm #{fname}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters