Class: Fhcap::ChefRunner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookbook_path, run_list, dry_run = false, node_attrs = {}, private_key_paths = []) ⇒ ChefRunner

Returns a new instance of ChefRunner.



23
24
25
26
27
28
29
30
31
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 23

def initialize(cookbook_path, run_list, dry_run=false, node_attrs={}, private_key_paths=[])
  @cookbook_path = File.expand_path(cookbook_path)
  @run_list = run_list
  @private_key_paths = private_key_paths
  @node_attrs = node_attrs
  @formatter = nil
  @ohai = nil
  @dry_run = dry_run
end

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



17
18
19
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 17

def cookbook_path
  @cookbook_path
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



21
22
23
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 21

def dry_run
  @dry_run
end

#node_attrsObject (readonly)

Returns the value of attribute node_attrs.



20
21
22
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 20

def node_attrs
  @node_attrs
end

#private_key_pathsObject (readonly)

Returns the value of attribute private_key_paths.



19
20
21
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 19

def private_key_paths
  @private_key_paths
end

#run_listObject (readonly)

Returns the value of attribute run_list.



18
19
20
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 18

def run_list
  @run_list
end

Instance Method Details

#configureObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 67

def configure
  Chef::Config.solo = true
  Chef::Config.log_level = :error
  Chef::Config.ssl_verify_mode = :verify_none
  Chef::Config.cookbook_path = cookbook_path
  Chef::Config.private_key_paths = (Chef::Config.private_key_paths + private_key_paths).compact.uniq
  Chef::Config.color = true
  Chef::Config.diff_disabled = true
  Chef::Config.why_run = dry_run

  # 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



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

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)
rescue => e
  test = ChefConvergeError.new("Chef failed to converge: #{e}")
  puts e.backtrace
  raise test
end

#formatterObject



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

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

#ohaiObject



83
84
85
86
87
88
89
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 83

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/fhcap/chef-dk/chef_runner.rb', line 49

def policy
  return @policy_builder if @policy_builder

  @policy_builder = Chef::PolicyBuilder::ExpandNodeObject.new("fhcap-cli", ohai.data, node_attrs, 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/fhcap/chef-dk/chef_runner.rb', line 45

def run_context
  @run_context ||= policy.setup_run_context
end

#stderrObject



95
96
97
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 95

def stderr
  $stderr
end

#stdoutObject



91
92
93
# File 'lib/fhcap/chef-dk/chef_runner.rb', line 91

def stdout
  $stdout
end