3

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

使用

这是我的场景:

  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:coldCapistrano 尝试结帐(在这种情况下从 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:setuproot: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)
    

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

4

2 回答 2

1

deploy:setup任务可能不应该sudo用于创建 app 目录,因为这可能会导致它归root.

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

set :use_sudo, false
于 2013-03-04T07:12:40.400 回答
0

由于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

但唯一比解决问题更好的事情是一开始就没有,所以斯图尔特的回答既明智又优雅。

于 2013-03-05T00:44:57.560 回答