capistrano / rvm

MIT License
140 stars 47 forks source link

Load capistrano-rvm only in staging environment #49

Open h0jeZvgoxFepBQ2C opened 10 years ago

h0jeZvgoxFepBQ2C commented 10 years ago

Due to historic reasons i use rbenv in production and rvm in staging environment.

The problem is now, that I cannot load capistrano-rvm in the staging.rb cap file:

bundle exec cap staging console
cap aborted!
NoMethodError: undefined method `each' for nil:NilClass
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/capistrano-rvm-0.1.1/lib/capistrano/tasks/rvm.rake:38:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/capistrano-3.2.1/lib/capistrano/dsl/task_enhancements.rb:12:in `block in after'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/Users/user/.rvm/gems/ruby-1.9.3-p448/bin/cap:23:in `load'
/Users/user/.rvm/gems/ruby-1.9.3-p448/bin/cap:23:in `<main>'
/Users/user/.rvm/gems/ruby-1.9.3-p448/bin/ruby_executable_hooks:15:in `eval'
/Users/user/.rvm/gems/ruby-1.9.3-p448/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => rvm:hook
(See full trace by running task with --trace)

But if I move the "require 'capistrano/rvm'" it in the Capfile, it also gets loaded in the production environment:

bundle exec cap production console
DEBUG [3ee05cd7] Running /usr/bin/env [ -d ~/.rvm ] on mywebsite.com
DEBUG [3ee05cd7] Command: [ -d ~/.rvm ]
DEBUG [3ee05cd7] Finished in 1.284 seconds with exit status 1 (failed).
DEBUG [c3bf6f47] Running /usr/bin/env [ -d /usr/local/rvm ] on mywebsite.com
DEBUG [c3bf6f47] Command: [ -d /usr/local/rvm ]
DEBUG [c3bf6f47] Finished in 0.083 seconds with exit status 1 (failed).
DEBUG [dbe7a15a] Running ~/.rvm/bin/rvm version on mywebsite.com
DEBUG [dbe7a15a] Command: ~/.rvm/bin/rvm version
cap aborted!
SSHKit::Command::Failed: rvm exit status: 127
rvm stdout: Nothing written
rvm stderr: Nothing written
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/command.rb:98:in `exit_status='
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:142:in `block (4 levels) in _execute'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/channel.rb:551:in `call'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/channel.rb:551:in `do_request'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:561:in `channel_request'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:205:in `process'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/channel.rb:269:in `wait'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:164:in `block (2 levels) in _execute'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/channel.rb:514:in `call'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:205:in `process'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/net-ssh-2.8.0/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:166:in `block in _execute'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:123:in `tap'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:123:in `_execute'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:76:in `capture'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/capistrano-rvm-0.1.1/lib/capistrano/tasks/rvm.rake:8:in `block (3 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:54:in `run'
/Users/user/.rvm/gems/ruby-1.9.3-p448/gems/sshkit-1.4.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => rvm:check
(See full trace by running task with --trace)

How do I load capistrano-rvm only in staging? Thanks!

skyporter commented 10 years ago

:+1:

deepak commented 10 years ago

can patch Capfile like. as it is a Rakefile

task :use_rvm do
  require 'capistrano/rvm'
end

task 'staging' => [:use_rvm]

my full Capfile is:

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

task :use_rvm do
  require 'capistrano/rvm'
end

task 'staging' => [:use_rvm]

require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

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

rake version is 10.1.0

notalex commented 9 years ago

Using the task pattern described before, capistrano/rvm gets required after capistrano/bundler. This makes capistrano use the command bundle exec ruby --version. This command would fail on the server when it is run inside a directory without a Gemfile.

There are two ways to make this work:

1) Avoid using bundle exec for all ruby commands:

Capfile
task :require_rvm do
  require 'capistrano/rvm'
end
task 'staging' => [:require_rvm]

require 'capistrano/bundler'
config/deploy/staging.rb
set :bundle_bins, %w(gem rake rails)

2) Require bundler the same way we require rvm:

Capfile
task :require_rvm do
  require 'capistrano/rvm'end

task :require_bundler do
  require 'capistrano/bundler'
end

task 'staging' => [:require_rvm, :require_bundler]
task 'production' => [:require_bundler]
Startouf commented 5 years ago

I have been adding no_hooks to several capistrano gems, so you can load many capistrano plugins on a per-deploy task without any dirty hacks like this one, I'll add a PR soon