Capistrano改变了Rails.root路径

时间:2015-07-30 16:02:09

标签: ruby-on-rails deployment capistrano

Rails 3.2
红宝石1.9.3

我的版本存储在服务器

的位置
deploy@production-web:/var/www/mrd_production/releases$ ls
20150729224354  20150730101637  20150730110152  20150730122336

我目前的作品存储在服务器

中的位置
deploy@production-web:/var/www/mrd_production/current$ ls
app                     config.ru  Gemfile       public    spec
assets_manifest_backup  db         Gemfile.lock  Rakefile  tmp
bin                     doc        Guardfile     README    Vagrantfile
Capfile                 features   lib           REVISION
config                  fixtures   log           script

所以,我使用capistrano进行部署:

bundle exec cap production deploy

deploy.rb

lock '3.4.0'

set :application, 's2yd'

set :repo_url, 'git@bitbucket.org:mrdelivery/mxad-mr.-delivery.git'

set :keep_releases, 3
set :log_level, :info
set :linked_files, %w{.env}
set :linked_dirs, %w{public/system}
set :migration_role, :app
set :rbenv_type, :user
set :rbenv_ruby, '1.9.3-p484'

namespace :deploy do
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 3 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, :restart
end

    namespace :rails do
      desc 'Open the rails console on the primary remote server'
      task :console do
        on roles(:app), primary: true do |host|
          command = "cd #{deploy_to}/current && /home/#{host.user}/.rbenv/shims/bundle exec rails console #{fetch(:stage)}"
          exec "ssh -l #{host.user} #{host.hostname} -p #{host.port || 22} -t 'cd #{deploy_to}/current && #{command}'"
        end
      end
    end

config / deploy / production.rb

set :deploy_to, '/var/www/mrd_production' # Change this to match your deploy_dir in vars/app.yml

server '104.130.31.250', user: 'deploy', roles: %w{web app}

namespace :deploy do
  desc 'generate_ymls'
  task :generate_ymls do
    on roles(:app) do
      upload! File.expand_path('../../database.yml.production',  __FILE__), "#{release_path}/config/database.yml"
    end
  end
end
before "deploy:migrate", 'deploy:generate_ymls'

部署后,我输入我的应用所在的文件夹/var/www/mrd_production/current ...

deploy@production-web:/var/www/mrd_production/current$ RAILS_ENV=production rails c

irb(main):001:0> Rails.root
=> #<Pathname:/var/www/mrd_production/releases/20150730122336>
irb(main):002:0> 

问题是Rails.root指向/var/www/mrd_production/releases/20150730122336,但我需要将其指向我所在的文件夹/var/www/mrd_production/current

如何更改Rails.root?

修改
捆绑exec上限生产部署日志:

INFO [8ba777a3] Running /usr/bin/env mkdir -p /tmp/s2yd/ as deploy@<server-ip>
INFO [8ba777a3] Finished in 0.269 seconds with exit status 0 (successful).
INFO Uploading /tmp/s2yd/git-ssh.sh 100.0%
INFO [867961a0] Running /usr/bin/env chmod +x /tmp/s2yd/git-ssh.sh as deploy@<server-ip>
INFO [867961a0] Finished in 0.267 seconds with exit status 0 (successful).
INFO [6be86a24] Running /usr/bin/env git ls-remote --heads git@bitbucket.org:mrdelivery/mxad-mr.-delivery.git as deploy@<server-ip>
INFO [6be86a24] Finished in 0.900 seconds with exit status 0 (successful).
INFO [e6f437c4] Running /usr/bin/env mkdir -p /var/www/mrd_production/shared /var/www/mrd_production/releases as deploy@<server-ip>
INFO [e6f437c4] Finished in 0.334 seconds with exit status 0 (successful).
INFO [37165e60] Running /usr/bin/env mkdir -p /var/www/mrd_production/shared/public/system as deploy@<server-ip>
INFO [37165e60] Finished in 0.266 seconds with exit status 0 (successful).
INFO [3c9b9d10] Running /usr/bin/env mkdir -p /var/www/mrd_production/shared as deploy@<server-ip>
INFO [3c9b9d10] Finished in 0.265 seconds with exit status 0 (successful).
INFO The repository mirror is at /var/www/mrd_production/repo
INFO [0002d83c] Running /usr/bin/env git remote update as deploy@<server-ip>
INFO [0002d83c] Finished in 2.495 seconds with exit status 0 (successful).
INFO [869b66a7] Running /usr/bin/env mkdir -p /var/www/mrd_production/releases/20150730220743 as deploy@<server-ip>
INFO [869b66a7] Finished in 0.271 seconds with exit status 0 (successful).
INFO [1b9871ea] Running /usr/bin/env git archive master | tar -x -f - -C /var/www/mrd_production/releases/20150730220743 as deploy@<server-ip>
INFO [1b9871ea] Finished in 0.528 seconds with exit status 0 (successful).
INFO [c28030f4] Running /usr/bin/env echo "6ce11b0" >> REVISION as deploy@<server-ip>
INFO [c28030f4] Finished in 0.266 seconds with exit status 0 (successful).
INFO [0af78fb4] Running /usr/bin/env mkdir -p /var/www/mrd_production/releases/20150730220743 as deploy@<server-ip>
INFO [0af78fb4] Finished in 0.273 seconds with exit status 0 (successful).
INFO [86aef2af] Running /usr/bin/env ln -s /var/www/mrd_production/shared/.env /var/www/mrd_production/releases/20150730220743/.env as deploy@<server-ip>
INFO [86aef2af] Finished in 0.267 seconds with exit status 0 (successful).
INFO [80cc46a1] Running /usr/bin/env mkdir -p /var/www/mrd_production/releases/20150730220743/public as deploy@<server-ip>
INFO [80cc46a1] Finished in 0.268 seconds with exit status 0 (successful).
INFO [bbfef99c] Running /usr/bin/env rm -rf /var/www/mrd_production/releases/20150730220743/public/system as deploy@<server-ip>
INFO [bbfef99c] Finished in 0.270 seconds with exit status 0 (successful).
INFO [66f90632] Running /usr/bin/env ln -s /var/www/mrd_production/shared/public/system /var/www/mrd_production/releases/20150730220743/public/system as deploy@<server-ip>
INFO [66f90632] Finished in 0.268 seconds with exit status 0 (successful).
INFO [e63b7bd2] Running ~/.rbenv/bin/rbenv exec bundle install --path /var/www/mrd_production/shared/bundle --without development test --deployment --quiet as deploy@<server-ip>
INFO [e63b7bd2] Finished in 0.701 seconds with exit status 0 (successful).
INFO [89bd1320] Running ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile as deploy@<server-ip>
INFO [89bd1320] Finished in 15.517 seconds with exit status 0 (successful).
INFO [8e1765be] Running /usr/bin/env mkdir -p /var/www/mrd_production/releases/20150730220743/assets_manifest_backup as deploy@<server-ip>
INFO [8e1765be] Finished in 0.266 seconds with exit status 0 (successful).
INFO [30408583] Running /usr/bin/env cp /var/www/mrd_production/releases/20150730220743/public/assets/manifest.yml /var/www/mrd_production/releases/20150730220743/assets_manifest_backup as deploy@<server-ip>
INFO [30408583] Finished in 0.270 seconds with exit status 0 (successful).
INFO Uploading /home/drobazko/www/mxad-mr.-delivery/config/database.yml.production 100.0%
INFO [deploy:migrate] Run `rake db:migrate`
INFO [e0eabe3f] Running ~/.rbenv/bin/rbenv exec bundle exec rake db:migrate as deploy@<server-ip>
INFO [e0eabe3f] Finished in 14.161 seconds with exit status 0 (successful).
INFO [36cbdf44] Running /usr/bin/env ln -s /var/www/mrd_production/releases/20150730220743 /var/www/mrd_production/releases/current as deploy@<server-ip>
INFO [36cbdf44] Finished in 0.270 seconds with exit status 0 (successful).
INFO [f261e897] Running /usr/bin/env mv /var/www/mrd_production/releases/current /var/www/mrd_production as deploy@<server-ip>
INFO [f261e897] Finished in 0.266 seconds with exit status 0 (successful).
INFO [47336a26] Running /usr/bin/env touch /var/www/mrd_production/releases/20150730220743/tmp/restart.txt as deploy@<server-ip>
INFO [47336a26] Finished in 0.267 seconds with exit status 0 (successful).
INFO Keeping 3 of 4 deployed releases on <server-ip>
INFO [81cb4cbd] Running /usr/bin/env rm -rf /var/www/mrd_production/releases/20150730162232 as deploy@<server-ip>
INFO [81cb4cbd] Finished in 0.384 seconds with exit status 0 (successful).
INFO [673ac3d6] Running /usr/bin/env echo "Branch master (at 6ce11b0) deployed as release 20150730220743 by drobazko" >> /var/www/mrd_production/revisions.log as deploy@<server-ip>

2 个答案:

答案 0 :(得分:1)

如果我没弄错的话,当Capistrano创建current文件夹时,它实际上是最新版本的“快捷方式”(缺少更好的术语)(也许符号链接目录更好)。

我不知道他们的文档的链接描述了我正在谈论的内容,但我相当肯定你应该对你想要的任何事情都没事。

编辑:

this page描述了capistrano目录层次结构的结构。如果您具体查看current上的部分,请阅读:

  

当前是指向最新版本的符号链接。在成功部署结束时更新此符号链接。如果部署在任何步骤中失败,则当前符号链接仍指向旧版本。

     

版本保存时间戳文件夹中的所有部署。这些文件夹是当前符号链接的目标。

答案 1 :(得分:1)

看起来你还在#34; old&#34;部署后的当前符号链接。 cd退出当前然后重新进入,然后跳回Rails控制台,你应该进入新的正确释放路径。

此行显示当前到最新版本目录的符号链接正确

INFO [36cbdf44] Running /usr/bin/env ln -s /var/www/mrd_production/releases/20150730220743 /var/www/mrd_production/releases/current as deploy@<server-ip>

在部署发生时你不能保持最新状态,否则符号链接会从你的下方改变而外壳并不知道。

其他注意事项:

要像在deploy.rb中一样打开shell,只需使用https://github.com/marshall-lee/capistrano-shell

另外,不要通过字符串连接创建shell命令,例如使用DSL。

task :do_something do
  on roles(:web) do
    within release_path do
      with rails_env: fetch(:rails_env) do
        execute :rake, 'simple:task'
      end
    end
  end
end