Created
June 12, 2012 23:06
-
-
Save danielsdeleo/2920702 to your computer and use it in GitHub Desktop.
Eval and Converge a Single Chef Recipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
recipe_path = ARGV[0] | |
if recipe_path.nil? | |
STDERR.puts "usage: chef-apply RECIPE_FILE" | |
exit 1 | |
end | |
recipe_path = File.expand_path(recipe_path) | |
if !File.exist?(recipe_path) | |
STDERR.puts "No file #{recipe_path}" | |
exit 1 | |
end | |
require 'rubygems' | |
require 'chef/client' | |
require 'chef/providers' | |
require 'chef/resources' | |
Chef::Log.level = :info | |
class Chef::Client | |
attr_reader :events | |
end | |
# expedient. | |
Chef::Config[:solo] = true | |
client = Chef::Client.new | |
client.run_ohai | |
client.build_node | |
run_context = if client.events.nil? | |
Chef::RunContext.new(client.node, {}) | |
else | |
Chef::RunContext.new(client.node, {}, client.events) | |
end | |
recipe = Chef::Recipe.new("(chef-apply cookbook)", "(chef-apply recipe)", run_context) | |
recipe.from_file(recipe_path) | |
runner = Chef::Runner.new(run_context) | |
runner.converge | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Chef
11.18.12
I also needed to callclient.load_node
beforeclient.build_node
. I think that will be the case for newer chefs too, according to what chef-shell does to create a solo session, e.g. https://github.com/chef/chef/blob/master/lib/chef/shell/shell_session.rb#L183