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.



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

def initialize(cookbook_path, run_list)
  @cookbook_path = File.expand_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.



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

def cookbook_path
  @cookbook_path
end

#run_listObject (readonly)

Returns the value of attribute run_list.



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

def run_list
  @run_list
end

Instance Method Details

#configureObject



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

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

  # atomic file operations on Windows require Administrator privileges to be able to read the SACL from a file
  # Using file_staging_uses_destdir(true) will get us inherited permissions indirectly on tempfile creation
  Chef::Config.file_atomic_update = false if Chef::Platform.windows?
  Chef::Config.file_staging_uses_destdir = true # Default in Chef 12+
end

#convergeObject



35
36
37
38
39
40
41
42
43
# File 'lib/chef-dk/chef_runner.rb', line 35

def converge
  configure
  Chef::Runner.new(run_context).converge
rescue Chef::Exceptions::CookbookNotFound => e
  message = "Could not find cookbook(s) to satisfy run list #{run_list.inspect} in #{cookbook_path}"
  raise CookbookNotFound.new(message, e)
rescue => e
  raise ChefConvergeError.new("Chef failed to converge: #{e}", e)
end

#formatterObject



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

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

#ohaiObject



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

def ohai
  return @ohai if @ohai

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

#policyObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/chef-dk/chef_runner.rb', line 49

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



45
46
47
# File 'lib/chef-dk/chef_runner.rb', line 45

def run_context
  @run_context ||= policy.setup_run_context
end

#stderrObject



88
89
90
# File 'lib/chef-dk/chef_runner.rb', line 88

def stderr
  $stderr
end

#stdoutObject



84
85
86
# File 'lib/chef-dk/chef_runner.rb', line 84

def stdout
  $stdout
end