【问题标题】:How to call a Capistrano's task within another Capistrano's task?如何在另一个 Capistrano 的任务中调用 Capistrano 的任务?
【发布时间】:2014-01-28 09:29:53
【问题描述】:

我正在尝试创建一个基本任务,用于将 database.yml 文件复制并链接到我的应用程序中,但我不知道为什么它不起作用。

我正在使用 Capistrnao 3.1

这是我的 lib/capistrano/tasks/databases.cap 文件:

namespace :db_access do

  # Idea : https://github.com/capistrano/sshkit/blob/master/EXAMPLES.md
  desc 'Copy production database.yml from local workstation'
  task :copy_production do
    on roles :all do
      execute :mkdir, '-p', "#{shared_path}/config"
      upload! 'config/deploy/production.database.yml', "#{shared_path}/config/database.yml"
    end
  end


  # Idea : http://stackoverflow.com/questions/9684649/capistrano-cant-deploy-my-database-yml
  desc 'Create database.yml symlinks to current release'
  task :create_symlinks do
    on roles :all do
      unless test "[ -f #{shared_path}/config/database.yml ]"
        invoke 'db_access:copy_production'
      end
      execute :ln, "-nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    end
  end
end

这是我得到的输出:

douglas@bilbo:/var/www/odpf$ cap production db_access:create_symlinks
DEBUG [3f670dfd] Running /usr/bin/env [ -f /var/www/odpf/shared/config/database.yml ] on myserver.net
DEBUG [3f670dfd] Command: [ -f /var/www/odpf/shared/config/database.yml ]
DEBUG [3f670dfd] Finished in 0.589 seconds with exit status 1 (failed).
cap aborted!
**undefined method `verbosity' for "/usr/bin/env db_access:copy_production\n":String**
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/formatters/pretty.rb:10:in `write'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/backends/printer.rb:14:in `block in execute'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/backends/printer.rb:13:in `tap'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/backends/printer.rb:13:in `execute'
/var/www/odpf/lib/capistrano/tasks/databases.cap:20:in `block (3 levels) in <top (required)>'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:54:in `run'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => db_access:create_symlinks

我不知道如何调用其他任务。我的灵感来自https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake

如果我尝试不使用 'invoke' 关键字且不使用 ' ' 符号:

namespace :db_access do
  # Idea : https://github.com/capistrano/sshkit/blob/master/EXAMPLES.md
  desc 'Copy production database.yml from local workstation'
  task :copy_production do
    on roles :all do
      execute :mkdir, '-p', "#{shared_path}/config"
      upload! 'config/deploy/production.database.yml', "#{shared_path}/config/database.yml"
    end
  end

  # Idea : http://stackoverflow.com/questions/9684649/capistrano-cant-deploy-my-database-yml
  desc 'Create database.yml symlinks to current release'
  task :create_symlinks do
    on roles :all do
      unless test "[ -f #{shared_path}/config/database.yml ]"
        db_access.copy_production
      end
      execute :ln, "-nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    end
  end
end

结果如下:

douglas@bilbo:/var/www/odpf$ cap production db_access:create_symlinks --trace
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke db_access:create_symlinks (first_time)
** Execute db_access:create_symlinks
cap aborted!
undefined local variable or method `db_access' for #<SSHKit::Backend::Netssh:0x00000002862340>
/var/www/odpf/lib/capistrano/tasks/databases.cap:20:in `block (3 levels) in <top (required)>'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:54:in `run'
/home/douglas/.rvm/gems/ruby-2.1.0@rails3/gems/sshkit-1.3.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => db_access:create_symlinks

这一定是一个简单的语法错误,但因为我找不到解释我不知道该怎么做的指南。

感谢您的帮助

【问题讨论】:

    标签: ruby ruby-on-rails-3.2 capistrano


    【解决方案1】:

    我的命令 调用 'db_access:copy_production' 是正确的。

    问题来自具有bug 的SSH-KIT。 问题是 Capistrano 3.1 使用的是 ssh-kit 版本 1.3。 所以我们必须使用 Capistrano 3.0.1 直到 ssh-kit 得到修复。

    解决方法:

    gem uninstall capistrano
    gem uninstall capistrano-rails
    gem uninstall sshkit
    rm Gemfile.lock
    

    修改 Gemfile :

      gem 'capistrano', '~> 3.0.1'
      gem 'sshkit', '~> 1.0.0'
    

    安装宝石:

    bundle install
    

    【讨论】:

      猜你喜欢
      • 2011-05-19
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 2011-10-06
      相关资源
      最近更新 更多