使用capistrano部署时,db:未定义的错误会迁移

时间:2013-10-24 16:09:43

标签: ruby-on-rails ruby postgresql migration capistrano

当我尝试cap deploy:cold时,一切正常,但在此阶段:

 ** transaction: commit
  * 2013-10-25 18:36:38 executing `deploy:migrate'
  * executing "cd /home/test_app_deployer/apps/mini_reader/releases/20131025143548 && bundle exec rake RAILS_ENV=production  db:migrate"

我有错误:

rake aborted!
 ** [out :: 81.218.92.235] FATAL:  password authentication failed for user "test_app_deployer"
 ** [out :: 81.218.92.235] FATAL:  password authentication failed for user "test_app_deployer"
 ** [out :: 81.218.92.235] /home/test_app_deployer/apps/mini_reader/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `initialize'

failed: "rvm_path=$HOME/.rvm $HOME/.rvm/bin/rvm-shell '1.9.3' -c 'cd /home/test_app_deployer/apps/mini_reader/releases/20131025143548 && bundle exec rake RAILS_ENV=production db:migrate'"

这是我的 deploy.rb 文件:

require "bundler/capistrano"
require "rvm/capistrano"

server "ip", :web, :app, :db, primary: true

set :application, "mini_reader"
set :domain, "reader.alma.by"
set :user, "test_app_deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :port, "222"
set :rvm_ruby_string, '1.9.3'
set :rails_env, "production"


set :scm, 'git'
set :repository, "git@github.com:user/app.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command}"
    end
  end

  task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
  end

  after "deploy:setup", "deploy:setup_config"

  task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end

  after "deploy:finalize_update", "deploy:symlink_config"

  desc "Make sure local git is in sync with remote."

  task :check_revision, roles: :web do
    unless `git rev-parse HEAD` == `git rev-parse origin/master`
      puts "WARNING: HEAD is not the same as origin/master"
      puts "Run `git push` to sync changes."
      exit
    end
  end
  before "deploy", "deploy:check_revision"
end
namespace :imagemagick do
  desc "Install the latest release of ImageMagick and the MagickWand Dev Library"
  task :install, roles: :app do
    run "#{sudo} apt-get -y update"
    run "#{sudo} apt-get -y install imagemagick libmagickwand-dev"
  end
  after "deploy:install", "imagemagick:install"
end

cap deploy:setup之后,/ apps / app_name / shared / config目录中有 database.yml 文件。我用postgres创建了用户和数据库,并将其添加到此文件( database.yml )中,它看起来像:

production:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: reader_production
  pool: 5
  host: localhost
  user: user_name
  password: user_pass

更新

我已从git存储库中删除了database.example.yml文件,并在 deploy.rb 文件中添加了一些说明:

before "deploy:setup", :db
after "deploy:update_code", "db:symlink"

namespace :db do
  desc "create database.yml in shared dir"

  task :default do
    db_config = ERB.new <<-EOF
production:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: db_production
  pool: 5
  host: localhost
  user: username
  password: pass
    EOF

    run "mkdir -p #{shared_path}/config"
    put db_config.result, "#{shared_path}/config/database.yml"
  end

我知道它使用正确的指令在当前目录中创建了database.yml文件,但我仍然有错误: FATAL: password authentication failed for user "test_app_deployer 为什么rails忽略了database.yml文件?

2 个答案:

答案 0 :(得分:0)

文件/apps/app_name/shared/config/database.yml需要符号链接到/apps/app_name/current/config/database.yml,有许多Capistrano任务可以处理这个问题,或者只是将database.yml的生产版本检查到您自己的存储库中。

Railscasts #133列出了执行此操作的任务(等等):

desc "Symlink shared configs and folders on each release."
task :symlink_shared do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"
end

必须在某个时间调用:symlink_shared任务,最好的地方是:

after 'deploy:update_code', 'deploy:symlink_shared'

答案 1 :(得分:0)

这是错误的,因为我在database.yml生产设置中使用user而不是username并且它使用OS的默认用户名,在postgres设置中没有密码