使用capistrano部署时隐藏mysql2密码的好方法是什么?

时间:2013-03-13 06:27:58

标签: ruby-on-rails ruby-on-rails-3 deployment capistrano

所以,这是我的capistrano文件

load 'deploy/assets'
require "bundler/capistrano" 
set :application, "XXXXXX"
set :repository,  "XXXXXX"

set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :repository , "XXXXXX"

role :web, "XXXXXX"                          # Your HTTP server, Apache/etc
role :app, "XXXXXX"                          # This may be the same as your `Web` server
role :db,  "XXXXXX", :primary => true # This is where Rails migrations will run
#role :db,  "your slave db-server here"

set :user, 'root'
set :use_sudo, false
set :deploy_to, "/var/www/#{application}"
set :deploy_via, :remote_cache
set :normalize_asset_timestamps, false

# if you want to clean up old releases on each deploy uncomment this:
# after "deploy:restart", "deploy:cleanup"

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# 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 "touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

现在,当我运行cap deploy时,我收到错误

Access denied for user 'root'@'localhost' (using password: NO)

我假设那是因为我的database.yml文件是

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
    adapter: mysql2
    encoding: utf8
    database: XXXXX
    username: root
    password: 
    socket: /tmp/mysql.sock

现在我有一个公共github帐户。我不想传递我的密码并将其发布到github。如果我没有传递密码,我无法部署应用程序。

处理这个问题的好方法是什么?

由于

2 个答案:

答案 0 :(得分:5)

您还需要确保SSH系统安全可靠,以防止人们以Capistrano机器人身份登录。我建议限制访问受密码保护的密钥对。

加密服务器上的.yml文件是没用的,因为你必须给机器人一个密钥,它将被存储。 。 。在同一台服务器上。在您的机器上加密它可能是一个好主意。 Capistrano可以在发送之前对其进行解密。

我解决这个问题的方法是将数据库密码放在一个文件中,该文件只对我运行应用程序的用户具有读取权限。然后,在database.yml中,我使用ERB来读取文件:

production:
  adapter: mysql
  database: my_db
  username: db_user
  password: <%= begin IO.read("/home/my_deploy_user/.db") rescue "" end %>

答案 1 :(得分:1)

我会推荐以下内容:

  1. config/database.yml移至您的仓库中的config/database.yml.sample
  2. 删除config/database.yml.sample中的所有敏感信息,例如密码和
    将“sample”配置文件提交到您的仓库。
  3. config/database.yml添加到您的.gitignore文件中,因此无法将其提交给回购
  4. 在您的服务器上,手动config/database.yml.sample复制到Capistrano为您创建的config/database.yml目录中的shared/。这应该在之后运行cap deploy:setup命令,这将创建顶级sharedreleases目录。在设置应用程序时,这应该只需手动完成一次。
  5. 在服务器上的shared/config/database.yml中,填写实际数据库详细信息,包括密码。 chmod该文件因此无权访问的人无法读取。
  6. 将以下内容添加到部署脚本中:

    namespace(:customs) do
       task :symlink_db, :roles => :app do
        run <<-CMD
          ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml
        CMD
      end
    end
    after "deploy:update_code", "customs:symlink_db"