Thibault Jouan 825bdec74d Improve `sh' task action error handling:
Handle exit status code in Remote#execute.
2013-08-07 23:39:49 +00:00

37 lines
774 B
Ruby

module Producer
module Core
class Remote
require 'etc'
require 'net/ssh'
attr_accessor :hostname
def initialize(hostname)
@hostname = hostname
end
def session
@session ||= Net::SSH.start(@hostname, Etc.getlogin)
end
def execute(command)
output = ''
session.open_channel do |channel|
channel.exec command do |ch, success|
ch.on_data do |c, data|
output << data
end
ch.on_request('exit-status') do |c, data|
exit_status = data.read_long
raise RemoteCommandExecutionError if exit_status != 0
end
end
end
session.loop
output
end
end
end
end