Class: Chef::RunStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/run_status.rb

Overview

Chef::RunStatus

Tracks various aspects of a Chef run, including the Node and RunContext, start and end time, and any Exception that stops the run. RunStatus objects are passed to any notification or exception handlers at the completion of a Chef run.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, events) ⇒ RunStatus



44
45
46
47
# File 'lib/chef/run_status.rb', line 44

def initialize(node, events)
  @node = node
  @events = events
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time



34
35
36
# File 'lib/chef/run_status.rb', line 34

def end_time
  @end_time
end

#eventsObject (readonly)

Returns the value of attribute events



26
27
28
# File 'lib/chef/run_status.rb', line 26

def events
  @events
end

#exceptionObject

Returns the value of attribute exception



36
37
38
# File 'lib/chef/run_status.rb', line 36

def exception
  @exception
end

#nodeObject

Returns the value of attribute node



42
43
44
# File 'lib/chef/run_status.rb', line 42

def node
  @node
end

#run_contextObject

Returns the value of attribute run_context



28
29
30
# File 'lib/chef/run_status.rb', line 28

def run_context
  @run_context
end

#run_idObject

Returns the value of attribute run_id



40
41
42
# File 'lib/chef/run_status.rb', line 40

def run_id
  @run_id
end

#start_timeObject (readonly)

Returns the value of attribute start_time



32
33
34
# File 'lib/chef/run_status.rb', line 32

def start_time
  @start_time
end

Instance Method Details

#all_resourcesObject

The list of all resources in the current run context's resource_collection



71
72
73
# File 'lib/chef/run_status.rb', line 71

def all_resources
  @run_context && @run_context.resource_collection.all_resources
end

#backtraceObject

The backtrace from exception, if any



82
83
84
# File 'lib/chef/run_status.rb', line 82

def backtrace
  @exception && @exception.backtrace
end

#elapsed_timeObject

The elapsed time between start_time and end_time. Returns nil if either value is not set.



62
63
64
65
66
67
68
# File 'lib/chef/run_status.rb', line 62

def elapsed_time
  if @start_time && @end_time
    @end_time - @start_time
  else
    nil
  end
end

#failed?Boolean

Did the Chef run fail?



87
88
89
# File 'lib/chef/run_status.rb', line 87

def failed?
  !success?
end

#formatted_exceptionObject

Returns a string of the format “ExceptionClass: message” or nil if no exception is set.



124
125
126
# File 'lib/chef/run_status.rb', line 124

def formatted_exception
  @exception && "#{@exception.class.name}: #{@exception.message}"
end

#start_clockObject

sets start_time to the current time.



50
51
52
# File 'lib/chef/run_status.rb', line 50

def start_clock
  @start_time = Time.now
end

#stop_clockObject

sets end_time to the current time



55
56
57
58
# File 'lib/chef/run_status.rb', line 55

def stop_clock
  @start_time ||= Time.now # if we failed so early we didn't get a start time
  @end_time = Time.now
end

#success?Boolean

Did the chef run succeed? returns true if no exception has been set.



92
93
94
# File 'lib/chef/run_status.rb', line 92

def success?
  @exception.nil?
end

#to_hObject Also known as: to_hash

A Hash representation of the RunStatus, with the following (Symbol) keys:

  • :node

  • :success

  • :start_time

  • :end_time

  • :elapsed_time

  • :all_resources

  • :updated_resources

  • :exception

  • :backtrace



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/run_status.rb', line 106

def to_h
  # use a flat hash here so we can't errors from intermediate values being nil
  { node: node,
    success: success?,
    start_time: start_time,
    end_time: end_time,
    elapsed_time: elapsed_time,
    all_resources: all_resources,
    updated_resources: updated_resources,
    exception: formatted_exception,
    backtrace: backtrace,
    run_id: run_id }
end

#updated_resourcesObject

The list of all resources in the current run context's resource_collection that are marked as updated



77
78
79
# File 'lib/chef/run_status.rb', line 77

def updated_resources
  @run_context && @run_context.resource_collection.select { |r| r.updated }
end