简体   繁体   English

如何在安装了Capistrano 3和rvm的生产环境中运行Rails命令

[英]How to run rails commands in production with capistrano 3 and rvm installed

I'm struggling to get a decent understanding of capistrano. 我正在努力获得对Capistrano的体面了解。 I want to run rails commands in production but it seems that the corresponding binstub is nowhere to be found. 我想在生产环境中运行rails命令,但似乎找不到相应的binstub。 As a matter of fact, I have the current/ and shared/ directories under my app name, but none of both has a bin/ directory with a rails binstub. 实际上,我的应用程序名称下有current/shared/目录,但是两者都没有带有rails binstub的bin/目录。

I'm also a complete newbie at building capistrano tasks. 我还是构建Capistrano任务的新手。 I found out this gem for example to run rails c with capistrano, but it requires the rails binstub in the current/bin directory, which of course I don't have. 例如,我发现此gem可以与capistrano一起运行rails c ,但是它需要current/bin目录中的rails binstub,当然我没有。

EDIT : I tried the capistrano-rails-console gem but even if I add the ssh_options like this , I end up with: 编辑 :我试过了Capistrano的护栏控制台宝石 ,但即使我添加ssh_options这样 ,我结束了:

00:00 rails:console
      01 ~/.rvm/bin/rvm default do bundle exec rails console production
Usage:
  rails new APP_PATH [options]

Options:
  -r, [--ruby=PATH]                                      # Path to the Ruby binary of your choice
                                                         # Default: /home/ubuntu/.rvm/rubies/ruby-2.3.1/bin/ruby
...

as if any binstub is not recognized. 好像没有任何垃圾桶。

I also followed the answers of this question , but none of the approaches seems to work for me. 我也遵循了这个问题的答案,但是似乎没有一种方法适合我。 I run the app on Linux so iterm is not an option for me, and the GitHub snippets linked in the other answers either end up with: 我在Linux上运行该应用程序,因此iterm对我来说不是一个选择,而其他答案中链接的GitHub代码片段都以:

cap aborted!
NameError: undefined local variable or method `current_task' for #<SSHKit::Backend::Netssh:0x00000001f15800>
Did you mean?  current_path

or: 要么:

00:00 rails:console
      Connecting with <my_username>@<my_host>
bash: bundle: command not found
Connection to <my_host> closed.

So I believe it's an rvm problem, but I totally don't know how to cope with it. 因此,我认为这是一个rvm问题,但我完全不知道如何解决。

I noticed that, when I run cap production deploy , following commands are run among others: 我注意到,当我运行cap production deploy ,将运行以下命令:

~/.rvm/bin/rvm default do bundle exec rake assets:precompile
~/.rvm/bin/rvm default do bundle exec rake db:migrate

but if I run them on my production server, I get the following response: 但是,如果我在生产服务器上运行它们,则会收到以下响应:

Could not locate Gemfile or .bundle/ directory

For your reference, here's my config/deploy.rb : 供您参考,这是我的config/deploy.rb

set :scm,             :git
set :repo_url,        '<git_repo>'
set :application,     '<app_name>'
set :user,            '<production_user>'
set :puma_threads,    [4, 16]
set :puma_workers,    0
set :pty,             true
set :use_sudo,        false
set :stage,           :production
set :deploy_via,      :remote_cache
set :deploy_to,       "/home/#{fetch(:user)}/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord

## Defaults:
# set :branch,        :master
# set :format,        :pretty
# set :log_level,     :debug
# set :keep_releases, 5

## Linked Files & Directories (Default None):
set :linked_files, %w{config/application.yml config/database.yml config/secrets.yml}
set :linked_dirs,  %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}

# Bonus! Colors are pretty!
def red(str)
  "\e[31m#{str}\e[0m"
end

# Figure out the name of the current local branch
def current_git_branch
  branch = `git symbolic-ref HEAD 2> /dev/null`.strip.gsub(/^refs\/heads\//, '')
  puts "Deploying branch #{red branch}"
  branch
end

# Set the deploy branch to the current branch
set :branch, current_git_branch

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :assets do
  desc "compile assets locally and upload before finalize_update"
  task :deploy do
      %x[bundle exec rake assets:clean && bundle exec rake assets:precompile]
      ENV['COMMAND'] = " mkdir '#{release_path}/public/assets'"
      invoke
      upload '/#{app_dir}/public/assets', "#{release_path}/public/assets", {:recursive => true}
  end
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) 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
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  task :fix_absent_manifest_bug do
    on roles(:web) do
      within release_path do  execute :touch,
        release_path.join('public', fetch(:assets_prefix), 'manifest-fix.temp')
      end
   end
  end


  # desc 'Restart application'
  # task :restart do
  #   on roles(:app), in: :sequence, wait: 5 do
  #     invoke 'puma:restart'
  #   end
  # end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
  after  :updating, 'deploy:fix_absent_manifest_bug'
end

# ps aux | grep puma    # Get puma pid
# kill -s SIGUSR2 pid   # Restart puma
# kill -s SIGTERM pid   # Stop puma

and my Capfile : 和我的Capfile

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

require 'capistrano/setup'
require 'capistrano/deploy'

require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'

require 'capistrano/rails/collection'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

Am I missing something? 我想念什么吗? Thanks in advance 提前致谢

Solved with the help of this Upwork freelancer . 这位Upwork自由职业者的帮助下解决了。

The solution steps were: 解决方法是:

  • remove the binstubs locally 在本地删除垃圾桶
  • set :bundle_binstubs, nil in config/deploy.rb set :bundle_binstubs, nilconfig/deploy.rb set :bundle_binstubs, nil
  • remove the bin directory from the :linked_dirs list (adding also /bin in .gitignore ) :linked_dirs列表中删除bin目录(还在.gitignore添加/bin
  • push the changes and run cap production deploy 推动变更并进行cap production deploy
  • recreate the binstubs with rake rails:update:bin rake rails:update:bin重新创建rake rails:update:bin
  • comment out the set :bundle_binstubs, nil line 注释掉set :bundle_binstubs, nil
  • add the bin directory in :linked_dirs again 再次将bin目录添加到:linked_dirs
  • modify the config/deploy.rb file like this: 修改config/deploy.rb文件,如下所示:
namespace :deploy do
  task :regenerate_bins do
    on roles(:web) do
      within release_path do
        execute :bundle, 'exec rake rails:update:bin'
      end
    end
  end
...

...
after  :finishing,    :regenerate_bins
...
  • uncomment set :bundle_binstubs, nil and remove bin from :linked_dirs once more 取消注释set :bundle_binstubs, nil ,将其取消set :bundle_binstubs, nil并再次从:linked_dirs删除bin
  • push changes and deploy 推动变更并部署

After this, the binstubs are found in the current/bin directory instead of the shared/bin one (in Rails 4 and 5) 之后,在current/bin目录current/bin目录中找到binstub,而不是shared/bin目录current/bin目录中的一个(在Rails 4和5中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM