8

我似乎无法让 Capistrano 与 AmazonRDS 完美配合。我到处寻找有关正确设置的任何信息,但没有找到任何信息。现在,当我时cap deploy,该过程超时。

这是我的 deploy.rb:

set :deploy_to, "/opt/bitnami/apps/annarbortshirtcompany.com/cms/" 
set :scm, :git
set :repository,  "ssh://user@ec2-repository.compute-1.amazonaws.com/~/repo/cms.git"
set :deploy_via, :remote_cache

set :user, "user"
ssh_options[:keys] = [File.join(ENV["HOME"], "EC2", "admin.pem")]
ssh_options[:forward_agent] = true
set :branch, "master"
set :use_sudo, true

set :location, "ec2-webserver.compute-1.amazonaws.com"
role :web, location
role :app, location
role :db, "cmsinstance.c7r8frl6npxn.us-east-1.rds.amazonaws.com", :primary => true

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

RDS 数据库实例的用户名与此处设置的 SSH 用户名不同,但在我的 database.yml 中定义。我认为这可能没有被 capistrano 阅读,但不知道如何做到这一点。

当我“上限部署”时:

ubuntu@ubuntu-VirtualBox:~/RailsApps/cms$ cap deploy
  * executing `deploy'
  * executing `deploy:update'
 ** transaction: start
  * executing `deploy:update_code'
    updating the cached checkout on all servers
    executing locally: "git ls-remote ssh://user@ec2-repository.compute-1.amazonaws.com/~/repo/cms.git master"
    command finished in 1590ms
  * executing "if [ -d /app-directory/shared/cached-copy ]; then cd /app-directory/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard ffc4ec7762566f801c4a9140aa3980dc71e3d06f && git clean -q -d -x -f; else git clone -q  ssh://user@ec2-repository.amazonaws.com/~/repo/cms.git /app-directory/shared/cached-copy && cd /app-directory/shared/cached-copy && git checkout -q -b deploy ffc4ec7762566f801c4a9140aa3980dc71e3d06f; fi"
    servers: ["ec2-webserver.compute-1.amazonaws.com", "dbinstance.us-east1.rds.amazonaws.com"]
*** [deploy:update_code] rolling back
  * executing "rm -rf /app-directory/releases/20110607161612; true"
    servers: ["ec2-webserver.compute-1.amazonaws.com", "dbinstance.us-east1.rds.amazonaws.com"]
 ** [deploy:update_code] exception while rolling back: Capistrano::ConnectionError, connection failed for: dbinstance.us-east1.rds.amazonaws.com (Errno::ETIMEDOUT: Connection timed out - connect(2))
    connection failed for: dbinstance.us-east1.rds.amazonaws.com (Errno::ETIMEDOUT: Connection timed out - connect(2))

为什么要“更新所有服务器上的缓存结帐”?此时甚至不需要数据库服务器。我很困惑如何解决这个问题。希望有人能指出我正确的方向!

4

1 回答 1

26

I had this exactly problem and struggled with it for what I'm embarrassed to say was a good 5 or 6 hours. In the end, when I realized what the problem was I felt like smacking myself because I knew this once but had forgotten it. Here's the crux of the problem, starting with this part of deploy.rb:

set :location, "ec2-webserver.compute-1.amazonaws.com"
role :web, location
role :app, location
role :db, "cmsinstance.c7r8frl6npxn.us-east-1.rds.amazonaws.com", :primary => true

When you define the machine roles for Capistrano, you're not actually identifying which machines will play a particular role...rather, you're identifying on which machines the Capistrano code will run when applying a deployment recipe for a role. So, when you define the :db role, you want to point to your EC2 instance, not the RDS instance. You can't ssh into the RDS machine, so it's impossible for Capistrano to run a recipe there. Instead, point :db to the same machine as you're pointing :web and :app, i.e.

set :location, "ec2-webserver.compute-1.amazonaws.com"
role :web, location
role :app, location
role :db, location, :primary => true

How does the RDS machine then have any involvement? Well, it's the database.yml file that dictates which machine is actually running the RDBMS where the SQL needs to be executed. You just need to be sure you're setting the host: value for the target database, e.g.:

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: <your_db>_production
  pool: 5
  username: <username>
  password: <password>
  host: cmsinstance.c7r8frl6npxn.us-east-1.rds.amazonaws.com

Make sense?

I hope this save someone else the frustration I experienced.

  • David
于 2011-06-21T18:08:27.620 回答