简体   繁体   中英

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

I'm struggling to get a decent understanding of capistrano. I want to run rails commands in production but it seems that the corresponding binstub is nowhere to be found. 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.

I'm also a complete newbie at building capistrano tasks. 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.

EDIT : I tried the capistrano-rails-console gem but even if I add the ssh_options like this , I end up with:

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:

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.

I noticed that, when I run cap production deploy , following commands are run among others:

~/.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 :

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 :

# 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 .

The solution steps were:

  • remove the binstubs locally
  • set :bundle_binstubs, nil in config/deploy.rb
  • remove the bin directory from the :linked_dirs list (adding also /bin in .gitignore )
  • push the changes and run cap production deploy
  • recreate the binstubs with rake rails:update:bin
  • comment out the set :bundle_binstubs, nil line
  • add the bin directory in :linked_dirs again
  • modify the config/deploy.rb file like this:
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
  • 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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