【问题标题】:Undefined error with db:migrates when deploying with capistranodb: 使用 capistrano 部署时出现未定义错误
【发布时间】:2013-11-03 11:12:00
【问题描述】:

当我尝试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文件?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      文件/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'
      

      【讨论】:

      • 我的存储库中没有database.yml,我已经在gitignore中添加了它,并且有database.example.yml。在shared/config 目录中创建的文件与database.example.yml 类似,我已根据我的 postgres 设置对其进行了相应更改。
      • 但是如果数据库文件没有从共享符号链接到当前,那么就 rails 而言,没有,所以没有数据库配置。我已经用 Railscasts 提供的更多信息更新了我的答案。
      • 我已经尝试过您的解决方案,但仍然出现此错误。这是正确的,但它不起作用。我添加了更多信息。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多