Class: CookbookCreator::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/cookbook_creator/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(cookbook_path, run_list) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
# File 'lib/cookbook_creator/runner.rb', line 8

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

Instance Method Details

#configureObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/cookbook_creator/runner.rb', line 39

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



15
16
17
18
19
20
21
22
23
# File 'lib/cookbook_creator/runner.rb', line 15

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



58
59
60
61
62
63
# File 'lib/cookbook_creator/runner.rb', line 58

def formatter
  @formatter ||=
    Chef::EventDispatch::Dispatcher.new.tap do |d|
      d.register(Chef::Formatters.new(:doc, stdout, stderr))
    end
end

#ohaiObject



50
51
52
53
54
55
56
# File 'lib/cookbook_creator/runner.rb', line 50

def ohai
  return @ohai if @ohai

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

#policyObject



29
30
31
32
33
34
35
36
37
# File 'lib/cookbook_creator/runner.rb', line 29

def policy
  return @policy_builder if @policy_builder
  @policy_builder = Chef::PolicyBuilder::Dynamic.new("generator-cookbook", 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



25
26
27
# File 'lib/cookbook_creator/runner.rb', line 25

def run_context
  @run_context ||= policy.setup_run_context
end

#stderrObject



69
70
71
# File 'lib/cookbook_creator/runner.rb', line 69

def stderr
  $stderr
end

#stdoutObject



65
66
67
# File 'lib/cookbook_creator/runner.rb', line 65

def stdout
  $stdout
end