class EvilChef::Runner

Public Class Methods

new() click to toggle source
# File lib/evil-chef.rb, line 6
def initialize
    ::Chef::Config[:solo] = true
    @chef_client = ::Chef::Client.new
    @chef_client.run_ohai
    @chef_client.load_node
    @chef_client.build_node
end

Public Instance Methods

init_run_context() click to toggle source
# File lib/evil-chef.rb, line 14
def init_run_context
    run_context = if @chef_client.events.nil?
                    ::Chef::RunContext.new(@chef_client.node, {})
                  else
                    ::Chef::RunContext.new(@chef_client.node, {}, @chef_client.events)
                  end
end
manage_resource(type_symbol, name, action, opts={}) click to toggle source
# File lib/evil-chef.rb, line 39
def manage_resource(type_symbol, name, action, opts={})
        run_context = init_run_context
        resource_class = Chef::Resource.resource_for_node(type_symbol, run_context.node)
        resource = resource_class.new(name, run_context)
        opts.each do |opt, val|
                resource.send(opt, val)
        end
        begin
                resource.run_action(action)
                true
        rescue Exception => e
                false
        end
end
node() click to toggle source
# File lib/evil-chef.rb, line 35
def node
        @chef_client.node
end
recipe_eval(&block) click to toggle source
# File lib/evil-chef.rb, line 22
def recipe_eval(&block)
        run_context = init_run_context
        recipe = ::Chef::Recipe.new("(evil-chef cookbook)", "(evil-chef recipe)", run_context)
        recipe.instance_eval(&block)
        runner = ::Chef::Runner.new(run_context)
        begin
                runner.converge
        rescue Exception => e
                @chef_client.run_status.exception = e
        end
        @chef_client.run_status
end