【问题标题】:How to setup a server for deployment and to do a cold deploy with Capistrano?如何设置服务器进行部署并使用 Capistrano 进行冷部署?
【发布时间】:2013-02-28 20:14:17
【问题描述】:

deploy:setup 和使用 Capistrano 进行冷部署的正确方法是什么?

使用

  • this deploy.rb
  • Capistrano v2.14.2
  • Vagrant 虚拟化我的服务器,

这是我的场景:

  1. 在运行deploy:setup 时,Capistrano 使用 root 权限来准备目录结构以进行部署:

    $ cap deploy:setup
      * 2013-02-28 14:50:21 executing `deploy:setup'
      * executing "sudo -p 'sudo password: ' mkdir -p /home/vagrant/example /home/vagrant/example/releases /home/vagrant/example/shared /home/vagrant/example/shared/system /home/vagrant/example/shared/log /home/vagrant/example/shared/pids"
        servers: ["example.com"]
        [example.com] executing command
        command finished in 29ms
      * executing "sudo -p 'sudo password: ' chmod g+w /home/vagrant/example /home/vagrant/example/releases /home/vagrant/example/shared /home/vagrant/example/shared/system /home/vagrant/example/shared/log /home/vagrant/example/shared/pids"
        servers: ["example.com"]
        [example.com] executing command
        command finished in 11ms
    
  2. 然而在deploy:cold Capistrano 尝试结帐(在这种情况下从 git)并以vagrant 用户的身份写入 - 在deploy.rb 中指定的用户:

    $ cap deploy:cold
      * 2013-02-28 14:50:47 executing `deploy:cold'
      * 2013-02-28 14:50:47 executing `deploy:update'
     ** transaction: start
      * 2013-02-28 14:50:47 executing `deploy:update_code'
        updating the cached checkout on all servers
        executing locally: "git ls-remote git@github.com:mariusbutuc/realtime-faye.git master"
        command finished in 2360ms
      * executing "if [ -d /home/vagrant/example/shared/cached-copy ]; then cd /home/vagrant/example/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard a7c05516bc31c2c18f89057c02f84bfad83a6b59 && git clean -q -d -x -f; else git clone -q git@github.com:mariusbutuc/realtime-faye.git /home/vagrant/example/shared/cached-copy && cd /home/vagrant/example/shared/cached-copy && git checkout -q -b deploy a7c05516bc31c2c18f89057c02f84bfad83a6b59; fi"
        servers: ["example.com"]
        [example.com] executing command
     ** [example.com :: out] fatal: could not create work tree dir '/home/vagrant/example/shared/cached-copy'.: Permission denied
        command finished in 26ms
    *** [deploy:update_code] rolling back
      * executing "rm -rf /home/vagrant/example/releases/20130228195049; true"
        servers: ["example.com"]
        [example.com] executing command
        command finished in 7ms
    failed: "sh -c 'if [ -d /home/vagrant/example/shared/cached-copy ]; then cd /home/vagrant/example/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard a7c05516bc31c2c18f89057c02f84bfad83a6b59 && git clean -q -d -x -f; else git clone -q git@github.com:mariusbutuc/realtime-faye.git /home/vagrant/example/shared/cached-copy && cd /home/vagrant/example/shared/cached-copy && git checkout -q -b deploy a7c05516bc31c2c18f89057c02f84bfad83a6b59; fi'" on example.com
    
  3. 当然,deploy:check 报告并不令人意外:vagrant 用户无法写入在 deploy:setup 期间创建的目录,因为这两个用户属于不同的组 - root:rootvagrant:vagrant

    $ cap deploy:check
      [...]
    The following dependencies failed. Please check them and try again:
    --> You do not have permissions to write to `/home/vagrant/example'. (example.com)
    --> You do not have permissions to write to `/home/vagrant/example/releases'. (example.com)
    --> `/home/vagrant/example/shared' is not writable (example.com)
    

这背后的原因是什么,什么先决条件还没有满足,所以部署通过了这个问题?

【问题讨论】:

  • 您不想手动更改权限?
  • 我遇到了完全相同的问题...我不明白为什么这些目录是作为 root 用户而不是 deploy.rb 文件中指定的用户创建的。我假设这是一个错误?我最终只是通过 SSH 登录并手动更改权限。

标签: ruby-on-rails ruby deployment capistrano


【解决方案1】:

deploy:setup 任务可能不应该使用sudo 来创建应用目录,因为这很可能导致它归root 所有。

您可以在 deploy.rb 文件中关闭它:

set :use_sudo, false

【讨论】:

  • 谢谢。它有效,而且比我的解决方法优雅得多。
【解决方案2】:

由于有no group setting in Capistrano,我的解决方法是扩展这样的设置,例如:

set :user,  'vagrant'
set :group, 'vagrant'

然后在运行deploy:setup后创建一个任务来“修复”所有权:

after "deploy:setup", :setup_ownership
task :setup_ownership do
  run "#{sudo} chown -R #{user}:#{group} #{deploy_to} && chmod -R g+s #{deploy_to}"
end

但唯一比解决问题更好的方法是一开始就没有它,所以Stuart's answer 更聪明也更优雅。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    相关资源
    最近更新 更多