简体   繁体   English

用capistrano部署时db:发生未定义的错误

[英]Undefined error with db:migrates when deploying with capistrano

When I try cap deploy:cold , everything is ok, but on this stage: 当我尝试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"

I have error: 我有错误:

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'"

This my deploy.rb file: 这是我的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

And after cap deploy:setup there is database.yml file in /apps/app_name/shared/config directory. cap deploy:setup之后,/ apps / app_name / shared / config目录中有database.yml文件。 I've created user and database with postgres, and add this in this file( database.yml ), it's looks like: 我使用postgres创建了用户和数据库,并将其添加到此文件( database.yml )中,如下所示:

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

UPDATE 更新

I've removed database.example.yml file from git repository and added some instructions in deploy.rb file: 我已经从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

I've checke that it created database.yml file in current directory with right instruction, but I still have error: FATAL: password authentication failed for user "test_app_deployer Why rails ignoring database.yml file? 我已经检查了它是否使用正确的指令在当前目录中创建了database.yml文件,但仍然有错误: FATAL: password authentication failed for user "test_app_deployer为什么铁路忽视了database.yml文件?

The file /apps/app_name/shared/config/database.yml needs to be symlinked into /apps/app_name/current/config/database.yml , there are numerous Capistrano tasks to handle this, or simply check a production version of the database.yml into your own repository. 需要将文件/apps/app_name/shared/config/database.yml链接到/apps/app_name/current/config/database.yml ,有许多Capistrano任务来处理此问题,或者只是检查database.yml的生产版本database.yml到您自己的存储库中。

Railscasts #133 lists a task (amongst others) for doing just this: 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

That :symlink_shared task has to be called sometime, the best place to do it is: :symlink_shared任务必须在某个时间调用,执行此任务的最佳位置是:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM