Hunter Barrington
Hunter Barrington

Reputation: 143

Run Chef's Knife from Rake

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    

Upvotes: 0

Views: 1722

Answers (3)

Hunter Barrington
Hunter Barrington

Reputation: 143

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    

Upvotes: 1

jtimberman
jtimberman

Reputation: 8258

The multi_json gem is actually required by fog, not Chef. The knife-rackspace plugin depends on fog, which brings in the multi_json gem.

The current version of fog (1.3.0) depends on multi_json ~> 1.0, so it won't be able to activate the gem using 1.1.0.

If you're set on using Rake for this, I suggest using a gemset for the knife-rackspace gems.

Upvotes: 0

mpapis
mpapis

Reputation: 53178

You problem is that you start the shell via 'sh' command bash will inherit the sh behavior and rvm will not be sourced properly.

sh was never supported by RVM but we had to disable it explicitly after problems with starting X server on Fedora 16.

Upvotes: 1

Related Questions