• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Nu-hin / remote_ruby / 13702531568

06 Mar 2025 03:43PM UTC coverage: 93.21% (-0.1%) from 93.306%
13702531568

push

github

Nu-hin
Extract cmd variable in SSHAdapter

0 of 2 new or added lines in 1 file covered. (0.0%)

906 of 972 relevant lines covered (93.21%)

90.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

24.66
/lib/remote_ruby/ssh_adapter.rb
1
# frozen_string_literal: true
2

3
require 'net/ssh'
20✔
4
require 'remote_ruby/compat_io_reader'
20✔
5

6
module RemoteRuby
20✔
7
  # An adapter for executing Ruby code on a remote host via SSH
8
  class SSHAdapter < ConnectionAdapter
20✔
9
    UnableToExecuteError = Class.new(StandardError)
20✔
10

11
    attr_reader :host, :config, :working_dir, :user
20✔
12

13
    def initialize(host:, working_dir: nil, use_ssh_config_file: true, **params)
20✔
14
      super
×
15
      @host = host
×
16
      @working_dir = working_dir
×
17
      @config = Net::SSH.configuration_for(@host, use_ssh_config_file)
×
18

19
      @config = @config.merge(params)
×
20
      @user = @config[:user]
×
21
    end
22

23
    def open(code, stdin, stdout, stderr)
20✔
24
      ret = nil
×
25
      Net::SSH.start(host, nil, config) do |ssh|
×
26
        with_temp_file(code, ssh) do |fname|
×
27
          res = run_code(ssh, fname, stdin, stdout, stderr)
×
28
          raise "Process exited with code #{status}" unless res.zero?
×
29

30
          ret = get_result(ssh, fname)
×
31
        end
32
      end
33
      ret
×
34
    end
35

36
    def connection_name
20✔
37
      "#{user}@#{host}:#{working_dir || '~'}> "
×
38
    end
39

40
    private
20✔
41

42
    def handle_stdin(chan, stdin)
20✔
43
      return if stdin.nil?
×
44

45
      if stdin.is_a?(StringIO)
×
46
        chan.send_data(stdin.string)
×
47
        chan.eof!
×
48
        return
×
49
      end
50

51
      stdin = RemoteRuby::CompatIOReader.new(stdin)
×
52

53
      chan.connection.listen_to(stdin.readable) do |io|
×
54
        data = io.read_nonblock(4096)
×
55
        chan.send_data(data)
×
56
      rescue EOFError
57
        chan.connection.stop_listening_to(stdin.readable)
×
58
        chan.eof!
×
59
      end
60

61
      chan.on_close do
×
62
        chan.connection.stop_listening_to(stdin.readable)
×
63
        stdin.join
×
64
      end
65
    end
66

67
    def handle_stdout(chan, stdout)
20✔
68
      return if stdout.nil?
×
69

70
      chan.on_data do |_, data|
×
71
        stdout.write(data)
×
72
      end
73
    end
74

75
    def handle_stderr(chan, stderr)
20✔
76
      return if stderr.nil?
×
77

78
      chan.on_extended_data do |_, _, data|
×
79
        stderr.write(data)
×
80
      end
81
    end
82

83
    def handle_exit_code(chan)
20✔
84
      chan.on_request('exit-status') do |_, data|
×
85
        yield data.read_long
×
86
      end
87
    end
88

89
    def run_remote_process(ssh, cmd, stdin, stdout, stderr)
20✔
90
      res = nil
×
91

92
      ssh.open_channel do |channel|
×
93
        channel.exec(cmd) do |ch, success|
×
94
          raise UnableToExecuteError unless success
×
95

96
          handle_stdin(ch, stdin)
×
97
          handle_stdout(ch, stdout)
×
98
          handle_stderr(ch, stderr)
×
99
          handle_exit_code(ch) do |code|
×
100
            res = code
×
101
          end
102
        end
103
      end.wait
104

105
      res
×
106
    end
107

108
    def run_code(ssh, fname, stdin, stdout, stderr)
20✔
NEW
109
      cmd = "cd '#{working_dir}' && ruby \"#{fname}\""
×
NEW
110
      run_remote_process(ssh, cmd, stdin, stdout, stderr)
×
111
    end
112

113
    def get_result(ssh, fname)
20✔
114
      ssh.exec!("cat \"#{fname}\"")
×
115
    end
116

117
    def with_temp_file(code, ssh)
20✔
118
      out = StringIO.new
×
119
      cmd = 'f=$(mktemp --tmpdir remote_ruby.XXXXXX) && cat > $f && echo $f'
×
120
      run_remote_process(ssh, cmd, StringIO.new(code), out, nil)
×
121
      fname = out.string.strip
×
122

123
      yield fname
×
124
    ensure
125
      ssh.exec!("rm \"#{fname}\"")
×
126
    end
127
  end
128
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc