Class: ChefCLI::ChefRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/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.



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

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.



31
32
33
# File 'lib/chef-cli/chef_runner.rb', line 31

def cookbook_path
  @cookbook_path
end

#run_listObject (readonly)

Returns the value of attribute run_list.



32
33
34
# File 'lib/chef-cli/chef_runner.rb', line 32

def run_list
  @run_list
end

Instance Method Details

#configureObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef-cli/chef_runner.rb', line 77

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

  # If the user has set policyfile configuration in the workstation config
  # file, the underlying chef-client code may enable policyfile mode and
  # then fail because chef-solo doesn't support policyfiles.
  Chef::Config.use_policyfile = false
  Chef::Config.policy_name = nil
  Chef::Config.policy_group = nil
  Chef::Config.deployment_group = nil

  # 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



41
42
43
44
45
46
47
48
49
# File 'lib/chef-cli/chef_runner.rb', line 41

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("#{ChefCLI::Dist::INFRA_PRODUCT} failed to converge: #{e} from file #{e.backtrace.first}", e)
end

#doc_formatterObject



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

def doc_formatter
  Chef::Formatters.new(:chefcli_doc, stdout, stderr)
end

#event_dispatcherObject



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

def event_dispatcher
  @event_dispatcher ||=
    Chef::EventDispatch::Dispatcher.new.tap do |d|
      d.register(doc_formatter)
    end
end

#ohaiObject



97
98
99
100
101
102
103
# File 'lib/chef-cli/chef_runner.rb', line 97

def ohai
  return @ohai if @ohai

  @ohai = Ohai::System.new
  @ohai.all_plugins(%w{platform platform_version})
  @ohai
end

#policyObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/chef-cli/chef_runner.rb', line 55

def policy
  return @policy_builder if @policy_builder

  @policy_builder = Chef::PolicyBuilder::Dynamic.new("chef-cli", ohai.data, {}, nil, event_dispatcher)
  @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



51
52
53
# File 'lib/chef-cli/chef_runner.rb', line 51

def run_context
  @run_context ||= policy.setup_run_context
end

#stderrObject



109
110
111
# File 'lib/chef-cli/chef_runner.rb', line 109

def stderr
  $stderr
end

#stdoutObject



105
106
107
# File 'lib/chef-cli/chef_runner.rb', line 105

def stdout
  $stdout
end