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

时间:2017-01-04 14:37:17

标签: capistrano capistrano3

我正在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)

1 个答案:

答案 0 :(得分:1)

在核心中解决的类似问题的一个很好的例子是: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