Class: Cheffish::ChefRun

Inherits:
Object
  • Object
show all
Defined in:
lib/cheffish/chef_run.rb

Direct Known Subclasses

RSpec::RecipeRunWrapper

Defined Under Namespace

Classes: EventSink, StringIOTee

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chef_config = {}) ⇒ ChefRun

Returns a new instance of ChefRun.

Parameters:

  • chef_config (defaults to: {})

    A hash with symbol keys that looks suspiciously similar to ‘Chef::Config`. Some possible options:

    • stdout: <IO object> - where to stream stdout to

    • stderr: <IO object> - where to stream stderr to

    • log_level: :debug|:info|:warn|:error|:fatal

    • log_location: <path|IO object> - where to stream logs to

    • verbose_logging: true|false - true if you want verbose logging in :debug



14
15
16
# File 'lib/cheffish/chef_run.rb', line 14

def initialize(chef_config = {})
  @chef_config = chef_config || {}
end

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



18
19
20
# File 'lib/cheffish/chef_run.rb', line 18

def chef_config
  @chef_config
end

Instance Method Details

#clientObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cheffish/chef_run.rb', line 34

def client
  @client ||= begin
    chef_config = self.chef_config.dup
    chef_config[:log_level] ||= :debug if !chef_config.key?(:log_level)
    chef_config[:verbose_logging] = false if !chef_config.key?(:verbose_logging)
    chef_config[:stdout] = StringIOTee.new(chef_config[:stdout])
    chef_config[:stderr] = StringIOTee.new(chef_config[:stderr])
    chef_config[:log_location] = StringIOTee.new(chef_config[:log_location])
    @client = ::Cheffish::BasicChefClient.new(nil,
      [ event_sink, Chef::Formatters.new(:doc, chef_config[:stdout], chef_config[:stderr]) ],
      chef_config
    )
  end
end

#compile_recipe(&recipe) ⇒ Object



84
85
86
# File 'lib/cheffish/chef_run.rb', line 84

def compile_recipe(&recipe)
  client.load_block(&recipe)
end

#convergeObject



88
89
90
91
92
93
94
# File 'lib/cheffish/chef_run.rb', line 88

def converge
  client.converge
  @converged = true
rescue RuntimeError => e
  @raised_exception = e
  raise
end

#converge_failed?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/cheffish/chef_run.rb', line 109

def converge_failed?
  @raised_exception.nil? ? false : true
end

#converged?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/cheffish/chef_run.rb', line 105

def converged?
  !!@converged
end

#event_sinkObject



49
50
51
# File 'lib/cheffish/chef_run.rb', line 49

def event_sink
  @event_sink ||= EventSink.new
end

#logged_errorsObject



72
73
74
# File 'lib/cheffish/chef_run.rb', line 72

def logged_errors
  logs.lines.select { |l| l =~ /^\[[^\]]*\] ERROR:/ }.join("\n")
end

#logged_infoObject



76
77
78
# File 'lib/cheffish/chef_run.rb', line 76

def logged_info
  logs.lines.select { |l| l =~ /^\[[^\]]*\] INFO:/ }.join("\n")
end

#logged_warningsObject



68
69
70
# File 'lib/cheffish/chef_run.rb', line 68

def logged_warnings
  logs.lines.select { |l| l =~ /^\[[^\]]*\] WARN:/ }.join("\n")
end

#logsObject



64
65
66
# File 'lib/cheffish/chef_run.rb', line 64

def logs
  @client ? client.chef_config[:log_location].string : nil
end

#output_for_failure_messageObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cheffish/chef_run.rb', line 121

def output_for_failure_message
  message = ""
  if stdout && !stdout.empty?
    message << "---                    ---\n"
    message << "--- Chef Client Output ---\n"
    message << "---                    ---\n"
    message << stdout
    message << "\n" if !stdout.end_with?("\n")
  end
  if stderr && !stderr.empty?
    message << "---                          ---\n"
    message << "--- Chef Client Error Output ---\n"
    message << "---                          ---\n"
    message << stderr
    message << "\n" if !stderr.end_with?("\n")
  end
  if logs && !logs.empty?
    message << "---                  ---\n"
    message << "--- Chef Client Logs ---\n"
    message << "---                  ---\n"
    message << logs
  end
  message
end

#resetObject



96
97
98
99
100
101
102
103
# File 'lib/cheffish/chef_run.rb', line 96

def reset
  @client = nil
  @converged = nil
  @stdout = nil
  @stderr = nil
  @logs = nil
  @raised_exception = nil
end

#resourcesObject



80
81
82
# File 'lib/cheffish/chef_run.rb', line 80

def resources
  client.run_context.resource_collection
end

#stderrObject



60
61
62
# File 'lib/cheffish/chef_run.rb', line 60

def stderr
  @client ? client.chef_config[:stderr].string : nil
end

#stdoutObject

output



56
57
58
# File 'lib/cheffish/chef_run.rb', line 56

def stdout
  @client ? client.chef_config[:stdout].string : nil
end

#up_to_date?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/cheffish/chef_run.rb', line 117

def up_to_date?
  !client.updated?
end

#updated?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/cheffish/chef_run.rb', line 113

def updated?
  client.updated?
end