如何在安装了 capistrano 3 和 rvm 的情况下 运行 rails 命令
How to run rails commands in production with capistrano 3 and rvm installed
我正在努力获得对 Capistrano 的正确理解。我想在生产中使用 运行 rails 命令,但似乎找不到相应的 binstub。事实上,我的应用程序名称下有 current/
和 shared/
目录,但两者的 none 都有一个 bin/
目录和 rails binstub .
我也是构建 Capistrano 任务的新手。我发现 this gem 例如 运行 rails c
与 capistrano,但它需要 current/bin
目录中的 rails binstub,当然我不需要有。
编辑:我尝试了 capistrano-rails-console gem but even if I add the ssh_options
like this,我最终得到:
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
...
好像没有识别任何 binstub。
我也遵循了 this question 的答案,但是 none 的方法似乎对我有用。我 运行 Linux 上的应用程序所以 iterm 不是我的选择,其他答案中链接的 GitHub 片段要么以:
结尾
cap aborted!
NameError: undefined local variable or method `current_task' for #<SSHKit::Backend::Netssh:0x00000001f15800>
Did you mean? current_path
或:
00:00 rails:console
Connecting with <my_username>@<my_host>
bash: bundle: command not found
Connection to <my_host> closed.
所以我认为这是一个 rvm
问题,但我完全不知道如何处理它。
我注意到,当我 运行 cap production deploy
时,以下命令是 运行 等:
~/.rvm/bin/rvm default do bundle exec rake assets:precompile
~/.rvm/bin/rvm default do bundle exec rake db:migrate
但是如果我 运行 它们在我的生产服务器上,我会得到以下响应:
Could not locate Gemfile or .bundle/ directory
供您参考,这是我的 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
和我的 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 }
我错过了什么吗?提前致谢
在 this Upwork freelancer 的帮助下解决了。
解决步骤是:
- 删除本地的 binstubs
set :bundle_binstubs, nil
在 config/deploy.rb
- 从
:linked_dirs
列表中删除 bin 目录(在 .gitignore
中添加 /bin
)
- 推送更改并 运行
cap production deploy
- 使用
rake rails:update:bin
重新创建 binstub
- 注释掉
set :bundle_binstubs, nil
行
- 再次添加
:linked_dirs
中的bin目录
- 像这样修改
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
...
- 取消注释
set :bundle_binstubs, nil
并再次从 :linked_dirs
中删除 bin
- 推送更改并部署
在此之后,binstub 位于 current/bin
目录中,而不是 shared/bin
目录中(在 Rails 4 和 5 中)
我正在努力获得对 Capistrano 的正确理解。我想在生产中使用 运行 rails 命令,但似乎找不到相应的 binstub。事实上,我的应用程序名称下有 current/
和 shared/
目录,但两者的 none 都有一个 bin/
目录和 rails binstub .
我也是构建 Capistrano 任务的新手。我发现 this gem 例如 运行 rails c
与 capistrano,但它需要 current/bin
目录中的 rails binstub,当然我不需要有。
编辑:我尝试了 capistrano-rails-console gem but even if I add the ssh_options
like this,我最终得到:
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
...
好像没有识别任何 binstub。
我也遵循了 this question 的答案,但是 none 的方法似乎对我有用。我 运行 Linux 上的应用程序所以 iterm 不是我的选择,其他答案中链接的 GitHub 片段要么以:
结尾cap aborted!
NameError: undefined local variable or method `current_task' for #<SSHKit::Backend::Netssh:0x00000001f15800>
Did you mean? current_path
或:
00:00 rails:console
Connecting with <my_username>@<my_host>
bash: bundle: command not found
Connection to <my_host> closed.
所以我认为这是一个 rvm
问题,但我完全不知道如何处理它。
我注意到,当我 运行 cap production deploy
时,以下命令是 运行 等:
~/.rvm/bin/rvm default do bundle exec rake assets:precompile
~/.rvm/bin/rvm default do bundle exec rake db:migrate
但是如果我 运行 它们在我的生产服务器上,我会得到以下响应:
Could not locate Gemfile or .bundle/ directory
供您参考,这是我的 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
和我的 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 }
我错过了什么吗?提前致谢
在 this Upwork freelancer 的帮助下解决了。
解决步骤是:
- 删除本地的 binstubs
set :bundle_binstubs, nil
在config/deploy.rb
- 从
:linked_dirs
列表中删除 bin 目录(在.gitignore
中添加/bin
) - 推送更改并 运行
cap production deploy
- 使用
rake rails:update:bin
重新创建 binstub
- 注释掉
set :bundle_binstubs, nil
行 - 再次添加
:linked_dirs
中的bin目录 - 像这样修改
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
...
- 取消注释
set :bundle_binstubs, nil
并再次从:linked_dirs
中删除 bin - 推送更改并部署
在此之后,binstub 位于 current/bin
目录中,而不是 shared/bin
目录中(在 Rails 4 和 5 中)