部署 Capistrano 部署时没有生成 current 目录是什么原因?

hanhor · 2013年03月11日 · 最后由 HanHor 回复于 2013年03月14日 · 4927 次阅读

各位好,最近有个小项目,参考 railscasts 以及 @happypeterhappycasts.net 上面讲的方法,部署在局域网内的一台机器上面。 在执行 cap deploy:check 时,没有发现报错,然后执行 cap deploy:setup , 登录服务器上 apps 下项目的目录查看,只有 releasesshared 目录,没有 currrent 目录,其中 releases 目录为空, shared/cached-copy/ 目录中有项目的代码. 以下是 deploy.rb 的代码:

require "bundler/capistrano"

server "192.168.1.111", :web, :app, :db, primary: true
set :application, "tick"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "[email protected]:HorX/#{application}.git"
set :branch, "master"
default_run_options[:pty] = 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

这个项目是放在 github 上面的私有项目,从局域网内的服务器可以 clone github 上的这个项目。

尝试在执行 cap deploy 时发现最终报错如下

failed: "sh -c 'cd /home/deployer/apps/tick/releases/20130311041014 && bundle install --gemfile /home/deployer/apps/tick/releases/20130311041014/Gemfile --path /home/deployer/apps/tick/shared/bundle --deployment --quiet --without development test'" on 192.168.1.111

折腾了很久了,还是没找出原因,求助各位帮帮忙,谢谢 ~

郁闷啊,还是没找到原因,哪位帮我看看啊。

CAP 太麻烦了,而且也不灵活,最近还是喜欢自己写脚本解决问题。

#2 楼 @happypeter 我看了你的那两期和 railscasts 上面的视频,发现 deployer.rb 里面基本一样,但部署起来就有问题了,找了很久没找到原因。

我是初学 ror,想尝试下用 cap 来部署,之前折腾着玩都是在 appfog,heroku 上面的

@HanHor cap deploy:setup 这个 task 只是建立一些必要的目录,还没有上传代码,所以 releases 目录是空的,current 是链接到当前 release 的一个软链接,所以应该还不存在

报错信息量不足,看不出什么问题

我觉得部署应该不是一个黑盒的过程,所以还是有必要了解一下这个过程的,必要可以去看下源代码

如果失败就会 rollback, 如果第一次部署,current 还没创建就失败了。

#4 楼 @thisiskun 原来current只是软连,我执行 cap eploy:setup' 后执行cap deploy` ,发现有错误输出:

ihor@work:/www/tick$ [master] cap deploy
  * 2013-03-12 12:27:08 executing `deploy'
    triggering before callbacks for `deploy'
  * 2013-03-12 12:27:08 executing `deploy:check_revision'
  * 2013-03-12 12:27:08 executing `deploy:update'
 ** transaction: start
  * 2013-03-12 12:27:08 executing `deploy:update_code'
    updating the cached checkout on all servers
    executing locally: "git ls-remote [email protected]:HorX/tick.git master"
    command finished in 4366ms
  * executing "if [ -d /home/deployer/apps/tick/shared/cached-copy ]; then cd /home/deployer/apps/tick/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard aef5b8ce4a532b0d7c9b9a04f5ce0d07563829a1 && git clean -q -d -x -f; else git clone -q [email protected]:HorX/tick.git /home/deployer/apps/tick/shared/cached-copy && cd /home/deployer/apps/tick/shared/cached-copy && git checkout -q -b deploy aef5b8ce4a532b0d7c9b9a04f5ce0d07563829a1; fi"
    servers: ["new.dev.com"]
    [new.dev.com] executing command
    command finished in 8877ms
    copying the cached version to /home/deployer/apps/tick/releases/20130312042724
  * executing "cp -RPp /home/deployer/apps/tick/shared/cached-copy /home/deployer/apps/tick/releases/20130312042724 && (echo aef5b8ce4a532b0d7c9b9a04f5ce0d07563829a1 > /home/deployer/apps/tick/releases/20130312042724/REVISION)"
    servers: ["new.dev.com"]
    [new.dev.com] executing command
    command finished in 854ms
  * 2013-03-12 12:27:25 executing `deploy:finalize_update'
    triggering before callbacks for `deploy:finalize_update'
  * 2013-03-12 12:27:25 executing `deploy:assets:symlink'
  * executing "rm -rf /home/deployer/apps/tick/releases/20130312042724/public/assets && mkdir -p /home/deployer/apps/tick/releases/20130312042724/public && mkdir -p /home/deployer/apps/tick/shared/assets && ln -s /home/deployer/apps/seearm/shared/assets /home/deployer/apps/tick/releases/20130312042724/public/assets"
    servers: ["new.dev.com"]
    [new.dev.com] executing command
    command finished in 845ms
  * 2013-03-12 12:27:26 executing `bundle:install'
  * executing "cd /home/deployer/apps/tick/releases/20130312042724 && bundle install --gemfile /home/deployer/apps/tick/releases/20130312042724/Gemfile --path /home/deployer/apps/tick/shared/bundle --deployment --quiet --without development test"
    servers: ["new.dev.com"]
    [new.dev.com] executing command
 ** [out :: new.dev.com] sh: 1:
 ** [out :: new.dev.com] bundle: not found
 ** [out :: new.dev.com] 
    command finished in 858ms
*** [deploy:update_code] rolling back
  * executing "rm -rf /home/deployer/apps/tick/releases/20130312042724; true"
    servers: ["new.dev.com"]
    [new.dev.com] executing command
    command finished in 845ms
failed: "sh -c 'cd /home/deployer/apps/tick/releases/20130312042724 && bundle install --gemfile /home/deployer/apps/tick/releases/20130312042724/Gemfile --path /home/deployer/apps/tick/shared/bundle --deployment --quiet --without development test'" on new.dev.com

最后面的错误,上服务器取看,发现releases目录没有 20130312042724这个 release

#6 楼 @HanHor 如果部署失败会回滚的,会把项目目录删除(目录名类似 20130312042724) 你这部署问题应该是 bundle install 出错!

@HanHor before "bundle:install", "deploy:symlink_config",原来这个问题我也碰到过的。。

#9 楼 @thisiskun 你是怎么解决的呢?

是由于这里没有生成当前的 release 的原因吗?

executing "if [ -d /home/deployer/apps/tick/shared/cached-copy ]; then cd /home/deployer/apps/tick/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard aef5b8ce4a532b0d7c9b9a04f5ce0d07563829a1 && git clean -q -d -x -f; else git clone -q [email protected]:HorX/tick.git /home/deployer/apps/tick/shared/cached-copy && cd /home/deployer/apps/tick/shared/cached-copy && git checkout -q -b deploy aef5b8ce4a532b0d7c9b9a04f5ce0d07563829a1; fi"

@HanHor bundle install 之前没有 database.yml 文件

出错原因是 bundle 命令找不到,你用 rvm 还是啥,railscasts 上面是 rbenv.

#12 楼 @hisea 我用的是 rvm,bundle 命令找不到的话我从 github 上 checkout 的代码 目录中执行 bundle 都可以

#11 楼 @thisiskun 不是 deploy.rb 中有这句吗?

put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"

我进 #{shared_path}/config/ 中发现有 database.yml ,你说的是指这个吗?

@HanHorafter "deploy:finalize_update", "deploy:symlink_config" 改成 before "bundle:install", "deploy:symlink_config" 能解决问题吗?

#2 楼 @happypeter

哥,你别这么说。

我们刚学会 cap, 你就抛弃不用了。

-_-

#15 楼 @thisiskun 还是同样的错误

#16 楼 @xiaoronglv 你刚才的问题解决了吗?

#18 楼 @HanHor

sorry, 我没碰到你这个问题。

@HanHor bundle: not found ruby 没装好吧

#20 楼 @thisiskun 但是我 clone 一份在那台服务机上没问题啊, ruby -v 也输出我安装的 ruby 版本

@HanHor 问题应该还是如 #7 楼 所说的,之前你 log 没传上来,把问题搞复杂了

#22 楼 @thisiskun #12 楼 @hisea 我发现是在这里删除了当前生成的 release

*** [deploy:update_code] rolling back
  * executing "rm -rf /home/deployer/apps/tick/releases/20130312091522; true"

我一边部署,以便观察/home/deployer/apps/tick/releases/目录,发现刚开始生成了 20130312091522 这个 release. 之后就发现被删除了,终端里面看见输出信息里面的上面一段,有删除操作。 服务器环境是没问题的,手动取部署后通过浏览器正常访问。 难道我部署的步骤有问题吗? 我是这样部署操作的:

  1. cap deploy:setup
  2. 登录服务器取修改 apps/tick/shared/config/database.yml 文件,设置为服务商对应数据库的信息,同时登录 mysql,创建 production 数据库
  3. 执行 cap deploy ,然后就报 #6 楼 的错误

看起来像是 ruby 在 non-interactive mode 没加载 把 [ -z "$PS1" ] && return 扔到 .bashrc 最后试试

谢谢各位,解决了,果然是 bundle 的原因。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号