Class: Chef::Formatters::NavoFormatter

Inherits:
Formatters::Base
  • Object
show all
Defined in:
lib/navo/chef_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(out, err) ⇒ NavoFormatter

Returns a new instance of NavoFormatter.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/navo/chef_formatter.rb', line 6

def initialize(out, err)
  super

  @up_to_date_resources = 0
  @updated_resources = 0
  @skipped_resources = 0
  @resource_stack = []
  @resource_action_times = Hash.new { |hash, key| hash[key] = [] }

  @deprecations = {}
end

Instance Method Details

#attribute_load_completeObject



70
71
72
73
# File 'lib/navo/chef_formatter.rb', line 70

def attribute_load_complete
  elapsed = Time.now - @load_start_time
  puts_line "Loaded cookbook attributes (#{elapsed}s)", :cyan
end

#attribute_load_start(file_count) ⇒ Object



64
65
66
67
68
# File 'lib/navo/chef_formatter.rb', line 64

def attribute_load_start(file_count)
  @load_start_time = Time.now
  puts_line ''
  puts_line 'Loading cookbook attributes...', :cyan
end

#converge_completeObject



117
118
119
120
121
# File 'lib/navo/chef_formatter.rb', line 117

def converge_complete
  unindent while @resource_stack.pop
  puts_line ''
  puts_line 'Converge completed', :green
end

#converge_failed(e) ⇒ Object



123
124
125
126
127
# File 'lib/navo/chef_formatter.rb', line 123

def converge_failed(e)
  unindent while @resource_stack.pop
  puts_line ''
  puts_line "Converge failed: #{e}", :red
end

#converge_start(run_context) ⇒ Object



112
113
114
115
# File 'lib/navo/chef_formatter.rb', line 112

def converge_start(run_context)
  puts_line ''
  puts_line "Converging #{run_context.resource_collection.all_resources.size} resources..."
end

#definition_load_completeObject



92
93
94
95
# File 'lib/navo/chef_formatter.rb', line 92

def definition_load_complete
  elapsed = Time.now - @load_start_time
  puts_line "Loaded definitions (#{elapsed}s)", :cyan
end

#definition_load_start(file_count) ⇒ Object



86
87
88
89
90
# File 'lib/navo/chef_formatter.rb', line 86

def definition_load_start(file_count)
  @load_start_time = Time.now
  puts_line ''
  puts_line 'Loading definitions...', :cyan
end

#deprecation(message, location = ) ⇒ Object



231
232
233
234
235
236
237
238
239
# File 'lib/navo/chef_formatter.rb', line 231

def deprecation(message, location=caller(2..2)[0])
  if Chef::Config[:treat_deprecation_warnings_as_errors]
    super
  end

  # Save deprecations to the screen until the end
  @deprecations[message] ||= Set.new
  @deprecations[message] << location
end

#file_loaded(path) ⇒ Object



108
109
110
# File 'lib/navo/chef_formatter.rb', line 108

def file_loaded(path)
  puts_line "Loaded #{path}"
end

#handler_executed(handler) ⇒ Object



223
224
225
# File 'lib/navo/chef_formatter.rb', line 223

def handler_executed(handler)
  puts_line "- #{handler.class.name}"
end

#handlers_completedObject



227
228
229
# File 'lib/navo/chef_formatter.rb', line 227

def handlers_completed
  puts_line 'Running handlers complete' unless @handler_count == 0
end

#handlers_start(handler_count) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/navo/chef_formatter.rb', line 213

def handlers_start(handler_count)
  @handler_count = handler_count
  puts_line ''
  if @handler_count > 0
    puts_line "Running #{handler_count} handlers:", :cyan
  else
    puts_line 'No registered handlers to run', :cyan
  end
end

#library_load_completeObject



59
60
61
62
# File 'lib/navo/chef_formatter.rb', line 59

def library_load_complete
  elapsed = Time.now - @load_start_time
  puts_line "Loaded cookbook libraries (#{elapsed}s)", :cyan
end

#library_load_start(file_count) ⇒ Object



53
54
55
56
57
# File 'lib/navo/chef_formatter.rb', line 53

def library_load_start(file_count)
  @load_start_time = Time.now
  puts_line ''
  puts_line 'Loading cookbook libraries...', :cyan
end

#lwrp_load_completeObject



81
82
83
84
# File 'lib/navo/chef_formatter.rb', line 81

def lwrp_load_complete
  elapsed = Time.now - @load_start_time
  puts_line "Loaded custom resources (#{elapsed}s)", :cyan
end

#lwrp_load_start(file_count) ⇒ Object



75
76
77
78
79
# File 'lib/navo/chef_formatter.rb', line 75

def lwrp_load_start(file_count)
  @load_start_time = Time.now
  puts_line ''
  puts_line 'Loading custom resources...', :cyan
end

#ohai_completed(node) ⇒ Object



48
49
50
51
# File 'lib/navo/chef_formatter.rb', line 48

def ohai_completed(node)
  puts_line ''
  puts_line 'Ohai run completed', :cyan
end


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/navo/chef_formatter.rb', line 22

def print_deprecations
  return if @deprecations.empty?
  puts_line 'Deprecated features used!'

  @deprecations.each do |message, locations|
    if locations.size == 1
      puts_line "  #{message} at one location:"
    else
      puts_line "  #{message} at #{locations.size} locations:"
    end
    locations.each do |location|
      prefix = '    - '
      Array(location).each do |line|
        puts_line "#{prefix}#{line}"
        prefix = '      '
      end
    end
  end
  puts_line ''
end

#recipe_load_completeObject



103
104
105
106
# File 'lib/navo/chef_formatter.rb', line 103

def recipe_load_complete
  elapsed = Time.now - @load_start_time
  puts_line "Recipes loaded (#{elapsed}s)", :cyan
end

#recipe_load_start(recipes) ⇒ Object



97
98
99
100
101
# File 'lib/navo/chef_formatter.rb', line 97

def recipe_load_start(recipes)
  @load_start_time = Time.now
  puts_line ''
  puts_line "Loading recipes...", :cyan
end

#resource_action_start(resource, action, notification_type = nil, notifier = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/navo/chef_formatter.rb', line 129

def resource_action_start(resource, action, notification_type = nil, notifier = nil)
  # Track the current recipe so we update it only when it changes
  # (i.e. when descending into another recipe via include_recipe)
  if resource.cookbook_name && resource.recipe_name
    current_recipe = "#{resource.cookbook_name}::#{resource.recipe_name}"

    unless current_recipe == @current_recipe
      @current_recipe = current_recipe
      puts_line current_recipe, :magenta
    end
  end

  # Record the resource and the time we started so we can figure out how
  # long it took to complete
  @resource_stack << [resource, Time.now]
  indent

  puts_line "#{resource} action #{action}"
end

#resource_failed(resource, action, exception) ⇒ Object

Called when a resource fails and will not be retried.



154
155
156
157
158
159
160
# File 'lib/navo/chef_formatter.rb', line 154

def resource_failed(resource, action, exception)
  _, start_time = @resource_stack.pop
  elapsed = Time.now - start_time
  @resource_action_times[[resource.to_s, action.to_s]] << elapsed
  puts_line "#{resource} action #{action} (#{elapsed}s) FAILED: #{exception}", :red
  unindent
end

#resource_failed_retriable(resource, action, retry_count, exception) ⇒ Object



149
150
151
# File 'lib/navo/chef_formatter.rb', line 149

def resource_failed_retriable(resource, action, retry_count, exception)
  puts_line "#{resource} action #{action} FAILED; retrying...", :yellow
end

#resource_skipped(resource, action, conditional) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/navo/chef_formatter.rb', line 162

def resource_skipped(resource, action, conditional)
  @skipped_resources += 1
  _, start_time = @resource_stack.pop
  elapsed = Time.now - start_time
  @resource_action_times[[resource.to_s, action.to_s]] << elapsed
  puts_line "#{resource} action #{action} (#{elapsed}s) SKIPPED due to: #{conditional.short_description}"
  unindent
end

#resource_up_to_date(resource, action) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/navo/chef_formatter.rb', line 171

def resource_up_to_date(resource, action)
  @up_to_date_resources += 1
  _, start_time = @resource_stack.pop
  elapsed = Time.now - start_time
  @resource_action_times[[resource.to_s, action.to_s]] << elapsed
  puts_line "#{resource} action #{action} up-to-date (#{elapsed}s)"
  unindent
end

#resource_update_applied(resource, action, update) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/navo/chef_formatter.rb', line 180

def resource_update_applied(resource, action, update)
  indent

  Array(update).compact.each do |line|
    if line.is_a?(String)
      puts_line "- #{line}", :green
    elsif line.is_a?(Array)
      # Expanded output delta
      line.each do |detail|
        if detail =~ /^\+(?!\+\+ )/
          color = :green
        elsif detail =~ /^-(?!-- )/
          color = :red
        else
          color = :cyan
        end
        puts_line detail, color
      end
    end
  end

  unindent
end

#resource_updated(resource, action) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/navo/chef_formatter.rb', line 204

def resource_updated(resource, action)
  @updated_resources += 1
  _, start_time = @resource_stack.pop
  elapsed = Time.now - start_time
  @resource_action_times[[resource.to_s, action.to_s]] << elapsed
  puts_line "#{resource} action #{action} updated (#{elapsed}s)"
  unindent
end

#run_completed(node) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/navo/chef_formatter.rb', line 241

def run_completed(node)
  @end_time = Time.now

  print_deprecations

  puts_line ''
  print_resource_summary
  puts_line "Chef client finished in #{@end_time - @start_time} seconds", :cyan
end

#run_start(version) ⇒ Object



43
44
45
46
# File 'lib/navo/chef_formatter.rb', line 43

def run_start(version)
  @start_time = Time.now
  puts_line "Starting Chef client #{version}...", :cyan
end

#total_resourcesObject



18
19
20
# File 'lib/navo/chef_formatter.rb', line 18

def total_resources
  @up_to_date_resources + @updated_resources + @skipped_resources
end