Cleaning up Capistrano Deployment Output

UPDATE: I have now wrapped this into a gem for easier/cleaner usage.

If you have every used, or are beginning to use Capistrano, for deploying web applications then you can probably attest to the fact that it spits out every bit of information it can.  This is very useful while setting your deployment up and debugging, but once everything is fine tuned it just becomes a mess.  I not only like quality tools (like Capistrano), but I like pretty tools. Capistrano’s default output is far from pretty and here I’m going to outline how you can quiet it down and continue to give you the needed feedback of your deployments progress.

Shhh…

The first step is to quite all that chatter down.  Capistrano has a built in command line option to do this -q , but I don’t really want to type that every time so to have the cap command default to --quiet , add this to the top of your deploy.rb file

logger.level = Logger::IMPORTANT  

This will suppress the default output except for the most important errors (which is good).  It won’t tell you every verbose command it is getting ready to run, that it is running them, and the running of them has completed.

Talking to yourself

At this point your deployment, if successful, will generate no output at all.  You just sit there asking yourself if it is frozen or not responding.  Now we are going to add our own output, keeping our self informed about where we are in the deployment process.  Capistrano has a nice chaining syntax of before and after.  At the most basic level, you can write out to the console before, and write your completed message after.

before "deploy:update_code" do  
    puts "Updating Code?"
end

after "deploy:update_code" do  
    puts "Yes."
end  

So at this point you’ve hidden all the output that was too verbose for an every day environment, but it still looks a bit janky.

$> cap deploy
Updating Code?  
Yes.  
Cleaning Up?  
Yes.  

Making it pretty

Go ahead and grab the colored gem…

gem install colored  

the basic usage of this is puts “string I want green”.green

Also, we will be continuing output on the same line with the usage of print , which does not flush immediately to stdout, so we need to place STDOUT.sync in our deploy.rb file to make sure we get stdout flushed with the usage of print.

an example of the output we will be targeting

(imagine “Done.” is green, mmk?)

$> cap deploy
Updating Code........Done.  
Cleaning Up..........Done.  

So we modify the above example

STDOUT.sync  
before "deploy:update_code" do  
    print "Updating Code........"
end

after "deploy:update_code" do  
    puts "Done.".green
end

before "deploy:cleanup" do  
    print "Cleaning Up.........."
end

after "deploy:update_code" do  
    puts "Done.".green
end  

In my own setup I like to use some UTF8 characters like checkmarks, etc…

Some example capistrano output from before following the steps above…

Example of my own output after following the steps above.

Curious about how I’m syncing my compiled static assets with Rackspace Couldfiles?

Sync static assets to Rackpace CloudFiles with a rake task.

Bonus: Console Spinner

Also, I wanted a spinner to be sitting on the end of the lines as the tasks progressed.  There was a spinner gem available but I thought it would be interesting to write my own.

# spinner stuff
@spinner_running = false
@chars = ['|', '/', '-', '\\']
@spinner = Thread.new do
  loop do
    unless @spinner_running
      Thread.stop
    end
    print @chars[0]
    sleep(0.1)
    print "\b"
    @chars.push @chars.shift
  end
end

def start_spinner  
  @spinner_running = true
  @spinner.wakeup
end

# stops the spinner and backspaces over last displayed character
def stop_spinner  
  @spinner_running = false
  print "\b"
end  

now with these functions available, simply add them like this.

STDOUT.sync  
before "deploy:update_code" do  
    print "Updating Code........"
    start_spinner()
end

after "deploy:update_code" do  
    stop_spinner()
    puts "Done.".green
end

before "deploy:cleanup" do  
    print "Cleaning Up.........."
    start_spinner()
end

after "deploy:cleanup" do  
    stop_spinner()
    puts "Done.".green
end