Class: SSHLine

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/util/ssh.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user = nil) ⇒ SSHLine

Returns a new instance of SSHLine.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbbt/util/ssh.rb', line 5

def initialize(host, user = nil)
  @host = host
  @user = user

  @ssh = Net::SSH.start(@host, @user)

  @ch = @ssh.open_channel do |ch|
    ch.exec 'bash -l'
  end

  @ch.on_data do |_,data|
    if m = data.match(/DONECMD: (\d+)\n/)
      @exit_status = m[1].to_i
      @output << data.sub(m[0],'')
      serve_output 
    else
      @output << data
    end
  end

  @ch.on_extended_data do |_,c,err|
    STDERR.write err 
  end
end

Class Method Details

.command(server, command, argv = [], options = nil) ⇒ Object



115
116
117
# File 'lib/rbbt/util/ssh.rb', line 115

def self.command(server, command, argv = [], options = nil)
  run(server, [command] + argv, options)
end

.open(host, user = nil) ⇒ Object



93
94
95
# File 'lib/rbbt/util/ssh.rb', line 93

def self.open(host, user = nil)
  @connections[[host, user]] ||= SSHLine.new host, user
end

.rbbt(server, script) ⇒ Object



107
108
109
# File 'lib/rbbt/util/ssh.rb', line 107

def self.rbbt(server, script)
  open(server).rbbt(script)
end

.ruby(server, script) ⇒ Object



103
104
105
# File 'lib/rbbt/util/ssh.rb', line 103

def self.ruby(server, script)
  open(server).ruby(script)
end

.run(server, cmd, options = nil) ⇒ Object



97
98
99
100
101
# File 'lib/rbbt/util/ssh.rb', line 97

def self.run(server, cmd, options = nil)
  cmd = cmd * " " if Array === cmd
  cmd += " " + CMD.process_cmd_options(options) if options
  open(server).run(cmd)
end

.workflow(server, workflow, script) ⇒ Object



111
112
113
# File 'lib/rbbt/util/ssh.rb', line 111

def self.workflow(server, workflow, script)
  open(server).workflow(workflow, script)
end

Instance Method Details

#rbbt(script) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbbt/util/ssh.rb', line 65

def rbbt(script)
  rbbt_script =<<-EOF
require 'rbbt-util'
require 'rbbt/workflow'

res = begin
    old_stdout = STDOUT.dup; STDOUT.reopen(STDERR)
#{script}
    ensure
      STDOUT.reopen(old_stdout)
    end

puts Marshal.dump(res)
  EOF

  m = ruby(rbbt_script)
  Marshal.load m
end

#ruby(script) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbbt/util/ssh.rb', line 50

def ruby(script)
  @output = ""
  @complete_output = false
  cmd = "ruby -e \"#{script.gsub('"','\\"')}\"\n"
  Log.debug "Running ruby on #{@host}:\n#{ script }"
  @ch.send_data(cmd)
  @ch.send_data("echo DONECMD: $?\n")
  @ssh.loop{ !@complete_output }
  if @exit_status.to_i == 0
    return @output
  else
    raise SSHProcessFailed.new @host, "Ruby script:\n#{script}"
  end
end

#run(command) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rbbt/util/ssh.rb', line 40

def run(command)
  send_cmd(command)
  @ssh.loop{ ! @complete_output}
  if @exit_status.to_i == 0
    return @output
  else
    raise SSHProcessFailed.new @host, command
  end
end

#send_cmd(command) ⇒ Object



30
31
32
33
34
# File 'lib/rbbt/util/ssh.rb', line 30

def send_cmd(command)
  @output = ""
  @complete_output = false
  @ch.send_data(command+"\necho DONECMD: $?\n")
end

#serve_outputObject



36
37
38
# File 'lib/rbbt/util/ssh.rb', line 36

def serve_output
  @complete_output = true
end

#workflow(workflow, script) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/rbbt/util/ssh.rb', line 84

def workflow(workflow, script)
  preamble =<<-EOF
wf = Workflow.require_workflow('#{workflow}')
  EOF

  rbbt(preamble + "\n" + script)
end