首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改变Rails.root路径

改变Rails.root路径
EN

Stack Overflow用户
提问于 2015-07-30 16:02:08
回答 2查看 2.2K关注 0票数 0

Rails 3.2

ruby 1.9.3

这就是我的版本存储在服务器上的地方。

代码语言:javascript
复制
deploy@production-web:/var/www/mrd_production/releases$ ls
20150729224354  20150730101637  20150730110152  20150730122336

这就是我当前产品存储在服务器中的地方。

代码语言:javascript
复制
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进行部署:

代码语言:javascript
复制
bundle exec cap production deploy

deploy.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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 .

代码语言:javascript
复制
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帽生产部署日志:

代码语言:javascript
复制
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>
EN

回答 2

Stack Overflow用户

发布于 2015-07-30 17:06:27

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

我不知道有一个链接到他们的文档,它描述了我在说什么,但我相当肯定,您应该对您想要运行的任何东西都没有意见。

编辑:

此页描述了capistrano目录层次结构。如果您专门查看current上的部分,它会读到:

current是指向最新版本的符号链接。此符号链接在成功部署结束时更新。如果部署在任何步骤中失败,当前符号链接仍然指向旧版本。 版本将所有部署保存在一个时间戳文件夹中。这些文件夹是当前符号链接的目标。

票数 1
EN

Stack Overflow用户

发布于 2015-07-31 00:36:16

看来在部署之后,您仍然处于“旧”当前符号链接中。cd出当前,然后返回,然后跳回Rails控制台,您应该在新的正确的发布路径中。

这一行显示当前与最新发布目录的符号链接是正确的。

代码语言:javascript
复制
INFO [36cbdf44] Running /usr/bin/env ln -s /var/www/mrd_production/releases/20150730220743 /var/www/mrd_production/releases/current as deploy@<server-ip>

当部署发生时,您不能停留在当前状态,否则,符号链接就会从您的下面改变,shell也不知道。

其他值得注意的事项:

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

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31728736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档