I have a rails web application that allows users to run automated tasks with multiple servers. I wired up a rake task to run every 5 minutes (whenever gem) and check for jobs that need to be executed and spin up the servers with the appropriate options.
My trouble is in actually running chef’s knife command. I currently do:
sh %{bash -c -l 'cd ~/opscode/FTW && source ~/.rvm/scripts/rvm && rvm use system && knife rackspace server list'} do |ok, res|
if ! ok
puts "meh? (status = #{res.exitstatus})"
puts res
end
end
this gets me halfway there. It switches to the appropriate gemset (system’s) without any trouble but on execution of knife i get the following:
Could not find multi_json-1.1.0 in any of the sources
Run `bundle install` to install missing gems.
I don’t have bundler installed in the system gems… so I’m pretty confused. multi-json-1.1.0 is required by my web application. My installation of chef seems to require multi_json 1.0.3 so there seems to be a mixup in the gem requirements.
the command runs from bash no problem… it only fails in rake at the knife part
any thoughts?
EDIT:
using mpapis suggestion i used the RVM gem and everything works great in IRB. I do the following
RVM.use! 'system'
env = RVM.current
env.shell_wrapper.run_command("cd /my/path/to/opscode/FTW && knife rackspace server list")
however when running the same code in rails console or from rake i have issues. Rails console essentially ignores my RVM.use! and rake blows up… does it have something to do with bundler interfering?
SOLUTION:
mpapis built a phenomenal gem https://github.com/mpapis/rvm-with that allows you to execute code within a particular ruby version.
RVM.with '1.8.7' do |r|
puts r.execute "unset RUBYOPT"
puts r.execute "cd /home/hunter/opscode/FTW && knife rackspace server list"
#puts r.execute "ruby --version"
end
SOLUTION: mpapis built a phenomenal gem https://github.com/mpapis/rvm-with that allows you to execute code within a particular ruby version.