如何构建自定义任务以及如何获得漂亮的输出?

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

我正在 Capistrano 3 (lib/capistrano/tasks/revision) 中编写自定义任务,它通过 curl 获取 运行 应用程序的修订版。

然后它将 运行 版本与最新部署的版本进行比较,如果它们不相同,则应抛出错误。

我说到要获取 运行 应用程序的修订版并将其与空字符串进行比较以引发错误。

我遇到的问题是这个任务的输出不漂亮,它只是纯白色文本。

我错过了什么?我整天都在研究文档。

此致!

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

输出:

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 <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
rvm 1.28.0 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [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)

核心解决类似问题的一个很好的例子在这里:https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake#L91

按照这些思路,您可能需要这样的代码:

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

编辑:

为了输出彩色文本,您可以使用:

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

发件人:https://github.com/mattbrictson/airbrussh/blob/master/lib/airbrussh/colors.rb