- From: Adam Jacob <
>
- To:
- Subject: [chef] Re: Embedded Chef
- Date: Tue, 26 Oct 2010 14:20:24 -0700
On Tue, Oct 26, 2010 at 8:45 AM, Jonathan Weiss
<
>
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:
Archive powered by MHonArc 2.6.16.