Capistrano 3:运行自定义 shell 命令时无法识别捆绑器

Posted

技术标签:

【中文标题】Capistrano 3:运行自定义 shell 命令时无法识别捆绑器【英文标题】:Capistrano 3: bundler not recognized when running custom shell command 【发布时间】:2014-12-09 21:06:32 【问题描述】:

我在 RVM 上使用 jruby 实施 Capistrano 部署时遇到问题。我使用 PUMA 作为应用服务器,这需要我在 Gemfile 中添加 capistrano3-puma。 总而言之,一切似乎都运行良好,我的服务器代码从 git repo 得到了最新的,并且很好地存档了以前版本的代码。 除了,每次我尝试从 Capistrano 任务运行 shell 命令时, 比如“bundle exec pumactl -F config/puma.rb start”,比如Capistrano似乎无法识别bundle命令。

我的真实案例示例如下所示,当我在覆盖的 deploy:start 方法中运行上述命令时,它返回错误“bundle: command not found”。当我通过 SSH 登录到服务器并直接从 app 文件夹启动它时,我已经测试过相同的命令可以正常工作。

jruby 1.7.16.1 (1.9.3p392) 2014-10-28 4e93f31 on OpenJDK 64-Bit Server VM 1.7.0_65-b32 +jit [linux-amd64]
** Invoke deploy:start (first_time)
** Execute deploy:start
INFO[fc57a7d8] Running /usr/bin/env cd /var/appname/current &&  bundle exec pumactl -F config/puma.rb start on 192.168.2.6
DEBUG[fc57a7d8] Command: cd /var/appname/current &&  bundle exec pumactl -F config/puma.rb start
bind address: 
port: 22
host: 192.168.2.6
options: :auth_methods=>["none", "publickey", "password", "keyboard-interactive"], :send_env=>[/^LANG$/, /^LC_.*$/], :user=>"root
", :forward_agent=>true, :logger=>#<Logger:0x14c7b324 @logdev=#<Logger::LogDevice:0x42c20b24 @filename=nil, @shift_age=nil, @dev=#
<IO:fd 2>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x51698ab9 @mon_count=0, @mon_mutex=#<Mutex:0x6812a170>, @mon_owner=nil>, @s
hift_size=nil>, @formatter=nil, @progname=nil, @default_formatter=#<Logger::Formatter:0x1f24f571 @datetime_format=nil>, @level=4>
DEBUG[fc57a7d8]         ***bash: bundle: command not found***
cap aborted!
Exception while executing on host 192.168.2.6: cd /var/appname/current &&  bundle exec pumactl -F config/puma.rb start exi
t status: 127
cd /var/appname/current &&  bundle exec pumactl -F config/puma.rb start stdout: Nothing written
cd /var/appname/current &&  bundle exec pumactl -F config/puma.rb start stderr: Nothing written
/Users/dev/.rvm/gems/jruby-1.7.16.1@rails3.2.8/gems/sshkit-1.5.1/lib/sshkit/command.rb:97:in `exit_status='
/Users/dev/.rvm/gems/jruby-1.7.16.1@rails3.2.8/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:148:in `_execute'
org/jruby/RubyProc.java:271:in `call'

这是我的 Capfile

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

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

Dir.glob('lib/capistrano/tasks/*.rake').each  |r| import r 

部署.rb

server '192.168.2.6', port: 22, roles: [:app], primary: true

set :repo_url,        'git@bitbucket.org:username/appname.git'
set :application,     'appname'
set :user,            'username'
set :puma_threads,    [4, 16]
set :puma_workers,    0
set :shared_children,  []

set :default_env,  rvm_bin_path: '~/.rvm/bin' 
set :default_shell, '/bin/bash -l'
set :pty,             true
set :use_sudo,        false
set :stage,           :staging
set :deploy_via,      :remote_cache
set :deploy_to,       "/var/#fetch(:application)"

set :linked_dirs, %wtmp/pids tmp/sockets log
set :puma_bind,       "unix:///#fetch(:deploy_to)/tmp/sockets/puma.sock"
set :puma_state,      "#fetch(:deploy_to)/tmp/pids/puma.state"
set :puma_pid,        "#fetch(:deploy_to)/tmp/pids/puma.pid"
set :puma_access_log, "#fetch(:deploy_to)/log/puma.error.log"
set :puma_error_log,  "#fetch(:deploy_to)/log/puma.access.log"
set :ssh_options,      forward_agent: true, user: fetch(:user) 
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, false  # Change to true if using ActiveRecord


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 :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

  desc "Start the application"
  task :start do
    on "root@192.168.2.6", in: :sequence, wait: 5  do
      with :rails_env => fetch(:rails_env) do
        execute "cd #current_path && /bin/bash -l bundle exec pumactl -F config/puma.rb start"
      end
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
end

staging.rb

set :stage, :staging
set :branch, 'master'

宝石文件

source 'http://rubygems.org'

gem 'rails', '3.2.8'
gem 'rubyzip', '< 1.0.0'
gem 'roo','1.12.1'
gem 'jdbc-mysql', platform: :jruby
gem 'activerecord-jdbcmysql-adapter', platform: :jruby
gem 'jquery-rails', '2.1.2'

gem 'haml', '3.1.7'
gem 'puma'

gem 'devise', '2.1.2'
gem 'devise-async', '0.5.0'
gem 'cancan', '1.6.8'
gem 'simple_form', '2.0.4'
gem 'cocoon', '1.1.1'
gem 'inherited_resources', '1.3.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'mechanize', '2.5.1'
gem 'delayed_job', '3.0.4'
gem 'paperclip', '3.4.0'
gem 'spreadsheet', '0.6.4.1'
gem 'geocoder', '1.1.6'
gem 'whenever', '0.8.2'
gem 'american_date', '1.0.0'
gem 'money','5.1.1'
gem 'rets','0.5.1'
gem 'haversine','0.3.0'

group :assets do
  gem 'stylus', '0.7.1'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.3.0'
end

group :development do
  gem 'capistrano',         require: false
  gem 'capistrano-rvm',     require: false
  gem 'capistrano-rails',   require: false
  gem 'capistrano-bundler', require: true
  gem 'capistrano3-puma', github: 'seuros/capistrano-puma',   require: false
end

任何帮助我解决问题的建议都将非常非常感谢。

更新

按照 rubish 的建议编辑代码后, 现在我发出这样的命令 => 执行“cd #current_path && /bin/bash -l bundle exec pumactl -F config/puma.rb start” 现在,它开始抱怨全局 rvm 路径下的捆绑程序。确切的新错误如下所示。

DEBUG[df47d6e1] Finished in 0.463 seconds with exit status 0 (successful).
jruby 1.7.16.1 (1.9.3p392) 2014-10-28 4e93f31 on OpenJDK 64-Bit Server VM 1.7.0_65-b32 +jit [linux-amd64]
** Invoke deploy:start (first_time)
** Execute deploy:start
INFO[93744fc5] Running /usr/bin/env cd /var/appname/current && /bin/bash -l bundle exec pumactl -F config/puma.rb start on
 192.168.2.6
DEBUG[93744fc5] Command: cd /var/appname/current && /bin/bash -l bundle exec pumactl -F config/puma.rb start
bind address: 
port: 22
host: 192.168.2.6
options: :auth_methods=>["none", "publickey", "password", "keyboard-interactive"], :send_env=>[/^LANG$/, /^LC_.*$/], :user=>"root
", :forward_agent=>true, :logger=>#<Logger:0x4c860e93 @logdev=#<Logger::LogDevice:0x7526fc24 @filename=nil, @shift_age=nil, @dev=#
<IO:fd 2>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x5273db92 @mon_count=0, @mon_mutex=#<Mutex:0x34547888>, @mon_owner=nil>, @s
hift_size=nil>, @formatter=nil, @progname=nil, @default_formatter=#<Logger::Formatter:0x15b5438f @datetime_format=nil>, @level=4>
DEBUG[93744fc5]         /usr/local/rvm/gems/jruby-1.7.16.1@global/bin/bundle: line 9: require: command not found
DEBUG[93744fc5]         /usr/local/rvm/gems/jruby-1.7.16.1@global/bin/bundle: line 11: version: command not found
DEBUG[93744fc5]         /usr/local/rvm/gems/jruby-1.7.16.1@global/bin/bundle: bundle: line 15: syntax error near unexpected token 
`('
DEBUG[93744fc5]         /usr/local/rvm/gems/jruby-1.7.16.1@global/bin/bundle: bundle: line 15: `  str = str.dup.force_encoding("BI
NARY") if str.respond_to? :force_encoding'
cap aborted!
Exception while executing on host 192.168.2.6: cd /var/appname/current && /bin/bash -l bundle exec pumactl -F config/puma.
rb start exit status: 2
cd /var/appname/current && /bin/bash -l bundle exec pumactl -F config/puma.rb start stdout: Nothing written
cd /var/appname/current && /bin/bash -l bundle exec pumactl -F config/puma.rb start stderr: Nothing written

【问题讨论】:

对于你的最后一个问题,你需要确保你从你的应用程序目录中调用 bundle exec 以便 RVM 可以获取 ".ruby-gemset" 和 ".ruby-version" 文件,否则它将使用您真正不想要的全局 gemset。 【参考方案1】:

通常 RVM 仅在登录 shell 中初始化,因此 capistrano 命令不知道 RVM。只需更改您的 shell 命令即可解决此问题:

# play nice with rvm
set :default_shell, '/bin/bash -l'

【讨论】:

arhh,谢谢@rubish,我已按照建议更新了我的代码。至少,错误已经改变。我改为这样做 => 执行 "cd #current_path && /bin/bash -l bundle exec pumactl -F config/puma.rb start" DEBUG[93744fc5] /usr/local/rvm/gems/jruby-1.7.16.1 @global/bin/bundle:第 9 行:要求:找不到命令调试 [93744fc5] /usr/local/rvm/gems/jruby-1.7.16.1@global/bin/bundle:第 11 行:版本:找不到命令调试 [ 93744fc5] /usr/local/rvm/gems/jruby- 请参考我在底部更新的问题中的完整错误。 @SarunSermsuwan 看来,无法识别的捆绑程序已解决。如果它帮助您解决问题,请将此答案标记为已接受。您的更新完全是一个新问题,请为其创建一个新问题。【参考方案2】:

所以你几乎遇到了我在制作 RoR + JRuby + Puma + RVM 应用程序时遇到的一些相同问题。我不记得所有的细节,但你需要做一些事情

1 - 我必须使用 git 中的 capistrano3-puma 并没有损坏:

gem 'capistrano3-puma', github: 'seuros/capistrano-puma'

2 - 在 rake 任务中包含 rails env + path:

 on roles(:db), in: :sequence, wait: 3 do
    within current_path do
      with :rails_env => fetch(:rails_env) do
        rake "some:task"
      end
    end
  end

3 - 最后 - 这是真正搞砸的部分,因为 Puma 不读取~/.bashrc 来加载 RVM 路径:

将 RVM 路径的加载也放在 ~/.bash_profile 中

我还必须做很多其他事情才能让一切正常运行,但这应该是不是特定于实现的大事。另外,别忘了将 Puma 变成一项服务,否则您将不得不停机。

【讨论】:

感谢 Srdjan。我已经按照建议进行了尝试。没有运气,仍然是同样的错误,但我一直在改变,让 Capistrano 看到 bundler【参考方案3】:

尝试在您的配置文件中添加以下内容(staging.rbdeploy.rb

set :rvm_map_bins, fetch(:rvm_map_bins).to_a.concat(%w puma pumactl )

capistrano-puma gem 似乎并不总是将其捆绑器映射到 rvm。

【讨论】:

以上是关于Capistrano 3:运行自定义 shell 命令时无法识别捆绑器的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Capistrano 运行 rake 任务?

“运行 3 个自定义 shell 脚本中的 2 个”Xcode 8.2

shell 自定义带参数函数

XCode 8 卡在运行 4 个自定义 Shell 脚本中的 2 个

Xcode 自定义 shell 脚本正在减慢编译时间

Rails 3 -- Bundler/Capistrano 错误