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?
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'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With