Capistrano 3 任务参数错误

Capistrano 3 Task Argument Error

我正在使用应该可以工作的 capistrano 3 任务,但出现错误:

> cap staging deploy --trace
** Invoke staging (first_time)
** Invoke use_rvm (first_time)
** Execute use_rvm
** Execute staging
** Invoke load:defaults (first_time)
** Execute load:defaults
cap aborted!
Task Argument Error
/Users/stevenspiel/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task_manager.rb:109:in `resolve_args_with_dependencies'
/Users/stevenspiel/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task_manager.rb:75:in `resolve_args'
/Users/stevenspiel/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task_manager.rb:27:in `define_task'
/Users/stevenspiel/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:365:in `define_task'
/Users/stevenspiel/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.4.2/lib/rake/dsl_definition.rb:66:in `task'
config/deploy.rb:65:in `<top (required)>'
...

任务很简单:

set :backup_to, '/home/deployer/app/db_backups'

desc 'Backup the remote production database'
task :backup_database, roles: :db, only: { primary: true } do
  run "mkdir -p #{backup_to}" # create backup folder unless already exists

  filename = "#{backup_to}/app_#{Time.now.strftime '%Y%m%d%H%M%S'}.sql.gz"

  # Check if we've got database config
  database_config = File.join(deploy_to, '/current/config/database.yml')

  if remote_file_exists?(database_config)
    text = capture("cat #{database_config}")

    # `capture` is returning some bad whitespace that YAML::load can't handle.
    # This strips out whitespace except for innocuous spaces and newlines.
    text.gsub!(/[^\n \S]/, '')

    config = YAML::load(text)[rails_env]

    on_rollback { run "rm #{filename}" } # if migration rolls back, removes backup

    run "pg_dump -U #{config['username']} #{config['database']} -W -h #{config['host']} | gzip --best > #{filename}" do |ch, stream, out|
      ch.send_data "#{config['password']}\n" if out =~ /^Password:/
    end
  else
    logger.debug('[BackupDatabase] No configuration file was found.')
  end
end

before 'deploy:migrate', :backup_database if rails_env == 'staging'

错误来自任务声明:

task :backup_database, roles: :db, only: { primary: true } do

有什么问题吗?

编辑

感谢 basia 的建议,我最终这样做了:

task :backup_database do
  on roles(:app), except: { primary: true } do
    ...
  end
end

您使用的是 Capistrano 2 语法,对于 Capistrano 3,语法不同于 2,看看 https://semaphoreapp.com/blog/2013/11/26/capistrano-3-upgrade-guide.html。您的代码应该看起来更像(未测试):

task :backup_database do
   roles(:db) do
       .....
  on
end 

或降级到 Cap 2 :)