Class: Tengine::Job::Runtime::SshJob::ShellClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tengine/job/runtime/ssh_job.rb

Instance Method Summary collapse

Constructor Details

#initialize(channel, script, callback) ⇒ ShellClient

Returns a new instance of ShellClient.



70
71
72
73
# File 'lib/tengine/job/runtime/ssh_job.rb', line 70

def initialize(channel, script, callback)
  @channel, @script, @callback = channel, script, callback
  @status = :preparing # :preparing, :waiting, :exiting
end

Instance Method Details

#dispatch(line) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tengine/job/runtime/ssh_job.rb', line 93

def dispatch(line)
  # puts "line: #{line.inspect}"
  case @status
  when :preparing then execute
  when :waiting then
    if line.strip == one_time_token
      returns
    else
      @result << line
    end
  when :exiting then
    # do nothing...
  else
    raise Error, "Unknown shell channel status: #{@status.inspect}"
  end
end

#executeObject



120
121
122
123
124
125
126
127
# File 'lib/tengine/job/runtime/ssh_job.rb', line 120

def execute
  actual = @script.force_encoding("binary")
  Tengine.logger.info("now exec on ssh: " << @script)
  # puts("now exec on ssh: " << @script)
  @result = ""
  @status = :waiting
  @channel.send_data(actual + "; echo \"#{one_time_token}\"\n")
end

#one_time_tokenObject



135
136
137
# File 'lib/tengine/job/runtime/ssh_job.rb', line 135

def one_time_token
  "one_time_token"
end

#prepareObject



114
115
116
117
118
# File 'lib/tengine/job/runtime/ssh_job.rb', line 114

def prepare
  cmd = "export PS1=;"
  Tengine.logger.info("now exec on ssh: \"#{cmd}\"")
  @channel.send_data("#{cmd}\n")
end

#returnsObject



129
130
131
132
133
# File 'lib/tengine/job/runtime/ssh_job.rb', line 129

def returns
  @callback.call(@channel, @result) if @callback
  @status = :exiting
  @channel.send_data("exit\n")
end

#setupObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tengine/job/runtime/ssh_job.rb', line 75

def setup
  @data = ""
  @result = nil

  @channel.on_data do |ch, data|
    # puts "on_data: #{data.inspect}"
    @data << data
    Tengine.logger.info("got STDOUT data: #{data.inspect}")
  end

  @channel.on_process do |ch|
    while @data =~ %r!^.*?\n!
      @data = $'
      dispatch($&)
    end
  end
end

#startObject



110
111
112
# File 'lib/tengine/job/runtime/ssh_job.rb', line 110

def start
  prepare # execute, returnsはdispatchから呼ばれます
end