Class: EvilChef::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/evil-chef.rb

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



6
7
8
9
10
11
12
# 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

Instance Method Details

#init_run_contextObject



14
15
16
17
18
19
20
# 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 = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

#nodeObject



35
36
37
# File 'lib/evil-chef.rb', line 35

def node
	@chef_client.node
end

#recipe_eval(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# 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