Embedded Chef


#1

Cheers,

Is there an easy way to use the various resources from a Ruby script.
This is what I want to be able to do:

pseudo-script

require 'rubygems’
require ‘chef’

Chef::Package(“apache2”, :action => :upgrade)
Chef::Service(“apache2”, :action => [:enable, :start])

Is there a nice API to do this?

Thanks,
Jonathan


Jonathan Weiss
http://blog.innerewut.de
http://twitter.com/jweiss


#2

What does this buy you over using Chef (at least Chef Solo)? directly?

On 26 October 2010 08:45, Jonathan Weiss jw@innerewut.de wrote:

Cheers,

Is there an easy way to use the various resources from a Ruby script.
This is what I want to be able to do:

pseudo-script

require 'rubygems’
require ‘chef’

Chef::Package(“apache2”, :action => :upgrade)
Chef::Service(“apache2”, :action => [:enable, :start])

Is there a nice API to do this?

Thanks,
Jonathan


Jonathan Weiss
http://blog.innerewut.de
http://twitter.com/jweiss


#3

On Tue, Oct 26, 2010 at 7:52 PM, AJ Christensen aj@junglist.gen.nz wrote:

What does this buy you over using Chef (at least Chef Solo)? directly?

I was thinking about writing a small agent for mcollective that would
allow me to query packages and services, much like those puppet
agents:

http://code.google.com/p/mcollective-plugins/wiki/AgentService
http://code.google.com/p/mcollective-plugins/wiki/AgentPackage

I want to be able to know if a certain service is running or if a
certain package is installed. Running Chef-Solo just for this taks
seems like a big overhead and I would have to parse the output as I
want to return the status to the requesting mcollective client.

Jonathan


Jonathan Weiss
http://blog.innerewut.de
http://twitter.com/jweiss


#4

Yo,

On 26 October 2010 13:12, Jonathan Weiss jw@innerewut.de wrote:

On Tue, Oct 26, 2010 at 7:52 PM, AJ Christensen aj@junglist.gen.nz
wrote:

What does this buy you over using Chef (at least Chef Solo)? directly?

I was thinking about writing a small agent for mcollective that would
allow me to query packages and services, much like those puppet
agents:

I’ve seen this done in the past. The provider/resource code for Chef is
definitely reusable enough. I’m not familiar enough with the way Recipes are
converted into Resources/Providers, these days, to be able to point you in
the right direction.

You might look at RightScale’s RightLink (amqp+chef) or Opscode’s
opscode-agent (WIP, nanite+chef).

Maybe someone else will chime in: otherwise try #chef-hacking on
irc.freenode.net

Regards,

AJ

http://code.google.com/p/mcollective-plugins/wiki/AgentService
http://code.google.com/p/mcollective-plugins/wiki/AgentPackage

I want to be able to know if a certain service is running or if a
certain package is installed. Running Chef-Solo just for this taks
seems like a big overhead and I would have to parse the output as I
want to return the status to the requesting mcollective client.

Jonathan


Jonathan Weiss
http://blog.innerewut.de
http://twitter.com/jweiss


#5

On Tue, Oct 26, 2010 at 8:45 AM, Jonathan Weiss jw@innerewut.de wrote:

Cheers,

Is there an easy way to use the various resources from a Ruby script.
This is what I want to be able to do:

pseudo-script

require 'rubygems’
require ‘chef’

Chef::Package(“apache2”, :action => :upgrade)
Chef::Service(“apache2”, :action => [:enable, :start])

Is there a nice API to do this?

Yep.

#!/usr/bin/env ruby

require 'rubygems’
require 'chef’
require 'chef/client’
require ‘chef/run_context’

Chef::Config[:solo] = true
Chef::Config[:log_level] = :info
Chef::Log.level(:info)
client = Chef::Client.new
client.run_ohai
client.build_node

run_context = Chef::RunContext.new(client.node,
Chef::CookbookCollection.new(Chef::CookbookLoader.new))

Use resources directly

Chef::Resource::Execute.new(“echo foo”, run_context).run_action(:run)

Build a recipe programatically, and execute it

recipe = Chef::Recipe.new(“adhoc”, “default”, run_context)
recipe.instance_eval <<-EOH
execute "echo bar"
EOH
Chef::Runner.new(run_context).converge

Best,
Adam


Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com


#6

damn, that’s dope as hell. someone wikify this shit?

On 26 October 2010 14:20, Adam Jacob adam@opscode.com wrote:

On Tue, Oct 26, 2010 at 8:45 AM, Jonathan Weiss jw@innerewut.de wrote:

Cheers,

Is there an easy way to use the various resources from a Ruby script.
This is what I want to be able to do:

pseudo-script

require 'rubygems’
require ‘chef’

Chef::Package(“apache2”, :action => :upgrade)
Chef::Service(“apache2”, :action => [:enable, :start])

Is there a nice API to do this?

Yep.

#!/usr/bin/env ruby

require 'rubygems’
require 'chef’
require 'chef/client’
require ‘chef/run_context’

Chef::Config[:solo] = true
Chef::Config[:log_level] = :info
Chef::Log.level(:info)
client = Chef::Client.new
client.run_ohai
client.build_node

run_context = Chef::RunContext.new(client.node,
Chef::CookbookCollection.new(Chef::CookbookLoader.new))

Use resources directly

Chef::Resource::Execute.new(“echo foo”, run_context).run_action(:run)

Build a recipe programatically, and execute it

recipe = Chef::Recipe.new(“adhoc”, “default”, run_context)
recipe.instance_eval <<-EOH
execute "echo bar"
EOH
Chef::Runner.new(run_context).converge

Best,
Adam


Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com


#7

You might look at RightScale’s RightLink (amqp+chef) or Opscode’s
opscode-agent (WIP, nanite+chef).

We have a similar setup at Scalarium (nanite + Chef-Solo) but I’m
looking for something more lightweight for this current project.
BTW IMHO RightLink is just nanite renamed + Chef.

Jonathan


Jonathan Weiss
http://blog.innerewut.de
http://twitter.com/jweiss


#8

Is there a nice API to do this?

Yep.

Thanks, I’ll get it a try!

Jonathan


Jonathan Weiss
http://blog.innerewut.de
http://twitter.com/jweiss