Class: RemoteMarshal

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_marshal.rb,
lib/remote_marshal/version.rb

Constant Summary collapse

VERSION =
"0.1.4"

Instance Method Summary collapse

Constructor Details

#initialize(host, user, options) ⇒ RemoteMarshal

Returns a new instance of RemoteMarshal.



4
5
6
# File 'lib/remote_marshal.rb', line 4

def initialize(host, user, options)
  @ssh = create_ssh(host, user, options)
end

Instance Method Details

#eval(&block) ⇒ Object



8
9
10
11
12
# File 'lib/remote_marshal.rb', line 8

def eval(&block)
  command = block.to_source(strip_enclosure: true)

  call { |dir| marshal(StringIO.new(command), File.join(dir, token)) }
end

#exec!(command) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/remote_marshal.rb', line 18

def exec!(command)
  status, output, error = nil, [], []

  @ssh.call do |ssh|
    channel = ssh.open_channel do |chan|
      chan.exec command do |ch, success|
        raise command unless success

        ch.on_data                   { |_, data|    output << data }
        ch.on_extended_data          { |_, _, data| error  << data }
        ch.on_request('exit-status') { |_, data|    status =  data.read_long }
      end
    end

    channel.wait
  end

  [status] + [output, error].map(&:join)
end

#file(filename) ⇒ Object



14
15
16
# File 'lib/remote_marshal.rb', line 14

def file(filename)
  call { |dir| marshal(filename, File.join(dir, File.basename(filename))) }
end

#test?(name, options) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/remote_marshal.rb', line 44

def test?(name, options)
  exec!(%([ #{option} #{name} ]))
end