Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 14009

Executing capistrano from a different application

I need to trigger a capistrano deployment from within an existing ruby scrpit.

For example, assume I want to start service that will listen to some messages, and on some messages I want to trigger a capistrano deployment of another app using cap staging deploy (which does work if I run this command directly from a shell).

~/app-to-deploy
  - Gemfile       #including many dependencies
  - config/deploy # capistrano files
~/my-service
  - Gemfile       # including different dependencies than `app-to-deploy`
  - my_script.rb  # that wishes to call `cap app-to-deploy deploy`

I was thinking I could run the capistrano bash script from my service using something like

# my_service.rb
Dir.chdir(ENV['HOME'] + '/app-to-deploy') do
  result = system('bash', '-c', "cap staging deploy")
end

If I run it with or without bundle exec this gives something like

cap aborted!
LoadError: cannot load such file -- active_support/all
/deploy/hermes/Capfile:2:in `require'
/deploy/hermes/Capfile:2:in `<top (required)>'
(See full trace by running task with --trace)

(I have ActiveSupport as a gem in my app-to-deploy but I don't have it in the Gemfile of my-service)

Seems like I have a lib dependency problem, but I'm not sure how to solve it...

Anyone has ideas or ways to go around the problem ? (or even better a way to call capistrano without using bash, assuming I may still have different dependencies ?)

(Note that app-to-deploy is an actual Rails app with many capistrano scripts that I don't want to touch if possible...)

Upvotes: 0

Views: 78

Answers (1)

GorillaApe
GorillaApe

Reputation: 3641

The problem is that in unix systems environment is inherited. So when you call Capistrano from your rails app you don't have access to other gems. bash -c doesn't seem to clean env. You can with su -l probably.

One solution is to use

Bundler.with_clean_env do
  Dir.chdir(ENV['HOME'] + '/app-to-deploy') do      
    result = system('bundle', 'exec', "cap", "staging", "deploy")
  end
end

Just for reference: http://bundler.io/man/bundle-exec.1.html#Shelling-out

Upvotes: 2

Related Questions