Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano 3.x capture output line by line

In Capistrano 2.x you could capture the output line by line using

run "ls -la" do |channel, stream, data|
    puts data
end

This does not work in Capistrano 3.x, and the capture and execute commands do not seem to provide the same functionality.

Is there a way to replicate the 2.x behaviour in 3.x?

like image 871
lsjroberts Avatar asked Oct 10 '13 09:10

lsjroberts


2 Answers

output = capture('cat ~/file.cnf')
output.each_line do |line|
    puts line
end

Thats how I read lines using capture. If you want to capture something specific on a line you can use

if line.include? 'user'
like image 166
Bruno Gustav Lanevik Avatar answered Sep 28 '22 17:09

Bruno Gustav Lanevik


I could not figure out how to get streaming output in cap 4 either. For me in Cap 3.4.0 with sshkit 1.11.1, execute was not doing it.

But looking at the sshkit docs, here's one way to hack it, which kind of works:

class StreamOutputInteractionHandler
  def on_data(_command, stream_name, data, channel)
    $stderr.print data
  end
end

# ...

execute :whatever, interaction_handler: StreamOutputInteractionHandler.new

It might do weird things, especially if you are executing on multiple hosts, it will of course interleave the output. You can use capistrano log similar to the way the built in MappingInteractionHandler does, but I wanted to print directly to console so I could get partial output before newlines (a progress bar from a rake task).

Discussion with sshkit maintainer here. https://github.com/capistrano/sshkit/issues/395#issuecomment-288489866

like image 26
jrochkind Avatar answered Sep 28 '22 17:09

jrochkind