![](/img/trans.png)
[英]How can I switch a production rails app (passenger/nginx/capistrano) to use RVM on Ubuntu?
[英]How to run rails commands in production with capistrano 3 and rvm installed
我正在努力获得对Capistrano的体面了解。 我想在生产环境中运行rails命令,但似乎找不到相应的binstub。 实际上,我的应用程序名称下有current/
和shared/
目录,但是两者都没有带有rails binstub的bin/
目录。
我还是构建Capistrano任务的新手。 例如,我发现此gem可以与capistrano一起运行rails c
,但是它需要current/bin
目录中的rails binstub,当然我没有。
编辑 :我试过了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
...
好像没有任何垃圾桶。
我也遵循了这个问题的答案,但是似乎没有一种方法适合我。 我在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 }
我想念什么吗? 提前致谢
在这位Upwork自由职业者的帮助下解决了。
解决方法是:
set :bundle_binstubs, nil
在config/deploy.rb
set :bundle_binstubs, nil
:linked_dirs
列表中删除bin目录(还在.gitignore
添加/bin
) cap production deploy
rake rails:update:bin
重新创建rake rails:update:bin
set :bundle_binstubs, nil
行 :linked_dirs
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
,将其取消set :bundle_binstubs, nil
并再次从:linked_dirs
删除bin 之后,在current/bin
目录current/bin
目录中找到binstub,而不是shared/bin
目录current/bin
目录中的一个(在Rails 4和5中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.