lib/vagrant-smartos/action/connect_hypervisor.rb in vagrant-smartos-0.0.1alpha vs lib/vagrant-smartos/action/connect_hypervisor.rb in vagrant-smartos-0.0.2pre1

- old
+ new

@@ -4,48 +4,73 @@ module Smartos class Provider + class SshOutput + attr_accessor :exit_code + attr_reader :command + + def initialize(command) + @command = command + @stderr = [] + @stdout = [] + end + + def append_stderr(data) + @stderr << data + end + def append_stdout(data) + @stdout << data + end + + def stdout + @stdout.join("").chomp + end + def stderr + @stderr.join("").chomp + end + end + class SshWrapper def initialize(net_ssh) @ssh = net_ssh end UnexpectedExitCode = Class.new(RuntimeError) CommandExecutionFailed = Class.new(RuntimeError) # Public: Execute and block on a command on the remote SSH server # - # Returns the stdout data, stderr is piped out to screen + # Returns an SshOutput instance # # Raises SshWrapper::UnexpectedExitCode if the exitcode is non-0 # Raises SshWrapper::CommandExecutionFailed if the command failed to execute def exec(command) - stdout_data = [] - channel = @ssh.open_channel do |ch| - ch.exec command do |ch, success| - raise SshWrapper::CommandExecutionFailed unless success + SshOutput.new(command).tap do |output| - # "on_data" is called when the process writes something to stdout - ch.on_data do |c, data| - stdout_data << data - end + channel = @ssh.open_channel do |ch| + ch.exec command do |ch, success| + raise SshWrapper::CommandExecutionFailed unless success - # "on_extended_data" is called when the process writes something to stderr - ch.on_extended_data do |c, type, data| - $stderr.print data - end + # "on_data" is called when the process writes something to stdout + ch.on_data do |c, data| + output.append_stdout(data) + end - channel.on_request("exit-status") do |ch,data| - raise SshWrapper::UnexpectedExitCode unless data.read_long == 0 + # "on_extended_data" is called when the process writes something to stderr + ch.on_extended_data do |c, type, data| + output.append_stderr(data) + end + + channel.on_request("exit-status") do |ch,data| + output.exit_code = data.read_long + end end end - end - channel.wait - - stdout_data.join("") + channel.wait + end end end class ConnectHypervisor