Class: ChefDK::ChefRunner

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

Overview

An adapter to chef’s APIs to kick off a chef-client run.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookbook_path, run_list) ⇒ ChefRunner

Returns a new instance of ChefRunner.



27
28
29
30
31
32
# File 'lib/chef-dk/chef_runner.rb', line 27

def initialize(cookbook_path, run_list)
  @cookbook_path = cookbook_path
  @run_list = run_list
  @formatter = nil
  @ohai = nil
end

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



24
25
26
# File 'lib/chef-dk/chef_runner.rb', line 24

def cookbook_path
  @cookbook_path
end

#run_listObject (readonly)

Returns the value of attribute run_list.



25
26
27
# File 'lib/chef-dk/chef_runner.rb', line 25

def run_list
  @run_list
end

Instance Method Details

#configureObject



58
59
60
61
62
63
# File 'lib/chef-dk/chef_runner.rb', line 58

def configure
  Chef::Config.solo = true
  Chef::Config.cookbook_path = cookbook_path
  Chef::Config.color = true
  Chef::Config.diff_disabled = true
end

#convergeObject



34
35
36
37
# File 'lib/chef-dk/chef_runner.rb', line 34

def converge
  configure
  Chef::Runner.new(run_context).converge
end

#formatterObject



54
55
56
# File 'lib/chef-dk/chef_runner.rb', line 54

def formatter
  @formatter ||= Chef::Formatters.new(:doc, stdout, stderr)
end

#ohaiObject



65
66
67
68
69
70
71
# File 'lib/chef-dk/chef_runner.rb', line 65

def ohai
  return @ohai if @ohai

  @ohai = Ohai::System.new
  @ohai.all_plugins(["platform", "platform_version"])
  @ohai
end

#policyObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/chef-dk/chef_runner.rb', line 43

def policy
  return @policy_builder if @policy_builder

  @policy_builder = Chef::PolicyBuilder::ExpandNodeObject.new("chef-dk", ohai.data, {}, nil, formatter)
  @policy_builder.load_node
  @policy_builder.build_node
  @policy_builder.node.run_list(*run_list)
  @policy_builder.expand_run_list
  @policy_builder
end

#run_contextObject



39
40
41
# File 'lib/chef-dk/chef_runner.rb', line 39

def run_context
  @run_context ||= policy.setup_run_context
end

#stderrObject



77
78
79
# File 'lib/chef-dk/chef_runner.rb', line 77

def stderr
  $stderr
end

#stdoutObject



73
74
75
# File 'lib/chef-dk/chef_runner.rb', line 73

def stdout
  $stdout
end