From eb1763d61e58d183d9f0c5a453deb235c86a11ba Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Wed, 23 May 2012 15:28:02 -0700 Subject: [PATCH 1/2] ssh/run optionally takes a block and yields [STDOUT, STDERR] Useful if you need real-timeish output --- lib/fog/compute/models/server.rb | 4 ++-- lib/fog/core/ssh.rb | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/fog/compute/models/server.rb b/lib/fog/compute/models/server.rb index 53038a89d..f5d901e6b 100644 --- a/lib/fog/compute/models/server.rb +++ b/lib/fog/compute/models/server.rb @@ -24,12 +24,12 @@ module Fog Fog::SCP.new(public_ip_address, username, scp_options).download(remote_path, local_path, download_options) end - def ssh(commands, options={}) + def ssh(commands, options={}, &blk) require 'net/ssh' requires :public_ip_address, :username options[:key_data] = [private_key] if private_key - Fog::SSH.new(public_ip_address, username, options).run(commands) + Fog::SSH.new(public_ip_address, username, options).run(commands, &blk) end end diff --git a/lib/fog/core/ssh.rb b/lib/fog/core/ssh.rb index 57d915f17..74112ff71 100644 --- a/lib/fog/core/ssh.rb +++ b/lib/fog/core/ssh.rb @@ -29,7 +29,7 @@ module Fog @options = options end - def run(commands) + def run(commands, &blk) self.class.data[@address] << {:commands => commands, :username => @username, :options => @options} end @@ -52,7 +52,7 @@ module Fog @options = { :paranoid => false }.merge(options) end - def run(commands) + def run(commands, &blk) commands = [*commands] results = [] begin @@ -68,11 +68,13 @@ module Fog channel.on_data do |ch, data| result.stdout << data + yield [data, nil] if blk end channel.on_extended_data do |ch, type, data| next unless type == 1 result.stderr << data + yield [nil, data] if blk end channel.on_request('exit-status') do |ch, data| From c6c288bb047b45ced7cc253e8ee6c4b763745125 Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Wed, 23 May 2012 15:54:35 -0700 Subject: [PATCH 2/2] return '' not nil --- lib/fog/core/ssh.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fog/core/ssh.rb b/lib/fog/core/ssh.rb index 74112ff71..ca38cb04f 100644 --- a/lib/fog/core/ssh.rb +++ b/lib/fog/core/ssh.rb @@ -68,13 +68,13 @@ module Fog channel.on_data do |ch, data| result.stdout << data - yield [data, nil] if blk + yield [data, ''] if blk end channel.on_extended_data do |ch, type, data| next unless type == 1 result.stderr << data - yield [nil, data] if blk + yield ['', data] if blk end channel.on_request('exit-status') do |ch, data|