Search code examples
capistranocapistrano3

How do I structure a custom task and how do I get pretty output?


I'm writing an a custom task in Capistrano 3 (lib/capistrano/tasks/revision) that is fetching the revision of a running application via curl.

Then it compares the running version with the latest deployed and compares them, if they are not the same an error should be thrown.

I got to the point that the revision of the running application is fetched and compared with an empty string so to throw an error.

The problem I'm having is that the output of this task is not pretty, it's just plain white text.

What am I missing? I've been digging in the documentation all day.

Regards!

revision.rake:

namespace :revision do
  desc 'Check revision of all applications to determine if the application is running the latest deployed revison'
  task :check do
    puts 'Checking revision of all supported applications'

    invoke 'revision:httpapi'
  end

  task :httpapi do
    on roles(:httpapi), in: :sequence do |host|
      puts "Checking revision of httpapi on #{host}"
      begin
        response = capture "curl -L 'http://#{fetch(:diagnostics_username)}:#{fetch(:diagnostics_password)}@#{host}/diagnostics/status?mode=extended&output=detailed'"
        object = JSON.parse(response, object_class: OpenStruct)

        unless object.result.revision == "" #For test, just compare to empty string so error is thrown
          raise 'The running revision is not the same as the installed, please restart all applications'
        end
      rescue Exception => e
        raise e.message
      end
    end
  end
end

Output:

gonace@ubuntu ~/Development/tulo-deployment (master) $ cap test revision:check
Enter a branch or tag name to deploy (defaults to develop)
Please enter branch (develop): 
Deploying branch/tag: develop
rvm 1.28.0 (latest) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
rvm 1.28.0 (latest) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
ruby-1.9.3-p545
ruby-1.9.3-p545
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-linux]
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-linux]
Checking revision of all supported applications
Checking revision of httpapi on 10.30.1.1
(Backtrace restricted to imported tasks)
cap aborted!
The running revision is not the same as the installed, please restart all applications
/home/gonace/Development/tulo-deployment/lib/capistrano/tasks/revision.rake:20:in `rescue in block (3 levels) in <top (required)>'
/home/gonace/Development/tulo-deployment/lib/capistrano/tasks/revision.rake:12:in `block (3 levels) in <top (required)>'
/home/gonace/Development/tulo-deployment/lib/capistrano/tasks/revision.rake:10:in `block (2 levels) in <top (required)>'
/home/gonace/Development/tulo-deployment/lib/capistrano/tasks/revision.rake:6:in `block (2 levels) in <top (required)>'
The running revision is not the same as the installed, please restart all applications
/home/gonace/Development/tulo-deployment/lib/capistrano/tasks/revision.rake:17:in `block (3 levels) in <top (required)>'
/home/gonace/Development/tulo-deployment/lib/capistrano/tasks/revision.rake:10:in `block (2 levels) in <top (required)>'
/home/gonace/Development/tulo-deployment/lib/capistrano/tasks/revision.rake:6:in `block (2 levels) in <top (required)>'
Tasks: TOP => revision:httpapi
(See full trace by running task with --trace)

Solution

  • A good example of a similar problem being solved in core is here: https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake#L91

    Along those lines, you'll probably want code something like this:

    task :httpapi do
      on roles(:httpapi), in: :sequence do |host|
        puts "Checking revision of httpapi on #{host}"
        response = capture "curl -L 'http://#{fetch(:diagnostics_username)}:#{fetch(:diagnostics_password)}@#{host}/diagnostics/status?mode=extended&output=detailed'"
        object = JSON.parse(response, object_class: OpenStruct)
    
        unless object.result.revision == "" #For test, just compare to empty string so error is thrown
          error 'The running revision is not the same as the installed, please restart all applications'
          exit 1
        end
      end
    end
    

    Edit:

    In order to output the colorized text, you can use:

    Airbrussh::Colors.green('Your message')
    

    From: https://github.com/mattbrictson/airbrussh/blob/master/lib/airbrussh/colors.rb