Capistrano 3.x output line by line

In Capistrano 2.x you can capture the output of 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 2.x behavior in 3.x?

+6
source share
3 answers
 output = capture('cat ~/file.cnf') output.each_line do |line| puts line end 

This is how I read lines using capture. If you want to capture something specific on the line, you can use

 if line.include? 'user' 
+14
source

I could not figure out how to get streaming output in cap 4. For me, in Cap 3.4.0 with sshkit 1.11.1 execute did not do this.

But looking at sshkit docs, here is one way to crack it, which works:

 class StreamOutputInteractionHandler def on_data(_command, stream_name, data, channel) $stderr.print data end end # ... execute :whatever, interaction_handler: StreamOutputInteractionHandler.new 

This can be strange, especially if you are running on multiple hosts, this, of course, will alternate the output. You can use the capistrano log, similar to the way to create the built-in MappingInteractionHandler, but I wanted to print it directly to the console so that I could get partial output in front of new lines (progress bar from the rake task).

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

+2
source

This is really easier in Capistrano 3.x, you can simply do:

 execute "ls -a" 

And the output will be transmitted automatically, it is great for streaming log files, etc.

-4
source

Source: https://habr.com/ru/post/955651/


All Articles