Class: Chef::ActionCollection

Inherits:
EventDispatch::Base show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/chef/action_collection.rb

Defined Under Namespace

Classes: ActionRecord

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from EventDispatch::Base

#action_collection_registration, #attribute_changed, #attribute_file_load_failed, #attribute_file_loaded, #attribute_load_complete, #attribute_load_start, #converge_complete, #converge_start, #cookbook_clean_complete, #cookbook_clean_start, #cookbook_compilation_complete, #cookbook_gem_failed, #cookbook_gem_finished, #cookbook_gem_installing, #cookbook_gem_start, #cookbook_gem_using, #cookbook_resolution_complete, #cookbook_resolution_failed, #cookbook_resolution_start, #cookbook_sync_complete, #cookbook_sync_failed, #cookbook_sync_start, #definition_file_load_failed, #definition_file_loaded, #definition_load_complete, #definition_load_start, #deprecation, #handler_executed, #handlers_completed, #handlers_start, #library_file_load_failed, #library_file_loaded, #library_load_complete, #library_load_start, #lwrp_file_load_failed, #lwrp_file_loaded, #lwrp_load_complete, #lwrp_load_start, #msg, #node_load_completed, #node_load_failed, #node_load_start, #node_load_success, #ohai_completed, #ohai_plugin_file_load_failed, #ohai_plugin_file_loaded, #ohai_plugin_load_complete, #ohai_plugin_load_start, #policyfile_loaded, #provider_requirement_failed, #recipe_file_load_failed, #recipe_file_loaded, #recipe_load_complete, #recipe_load_start, #recipe_not_found, #registration_completed, #registration_failed, #registration_start, #removed_cookbook_file, #resource_bypassed, #resource_current_state_load_bypassed, #resource_failed_retriable, #resource_update_applied, #resource_update_progress, #run_completed, #run_failed, #run_list_expand_failed, #run_list_expanded, #run_start, #run_started, #skipping_registration, #stream_closed, #stream_opened, #stream_output, #synchronized_cookbook, #updated_cookbook_file, #whyrun_assumption

Constructor Details

#initialize(events, run_context = nil, action_records = []) ⇒ ActionCollection

Returns a new instance of ActionCollection.



90
91
92
93
94
95
96
# File 'lib/chef/action_collection.rb', line 90

def initialize(events, run_context = nil, action_records = [])
  @action_records  = action_records
  @pending_updates = []
  @consumers       = []
  @events          = events
  @run_context     = run_context
end

Instance Attribute Details

#action_recordsObject (readonly)

Returns the value of attribute action_records.



84
85
86
# File 'lib/chef/action_collection.rb', line 84

def action_records
  @action_records
end

#consumersObject (readonly)

Returns the value of attribute consumers.



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

def consumers
  @consumers
end

#eventsObject (readonly)

Returns the value of attribute events.



88
89
90
# File 'lib/chef/action_collection.rb', line 88

def events
  @events
end

#pending_updatesObject (readonly)

Returns the value of attribute pending_updates.



85
86
87
# File 'lib/chef/action_collection.rb', line 85

def pending_updates
  @pending_updates
end

#run_contextObject (readonly)

Returns the value of attribute run_context.



86
87
88
# File 'lib/chef/action_collection.rb', line 86

def run_context
  @run_context
end

Instance Method Details

#converge_failed(exception) ⇒ Object

End of an unsuccessful converge used to fire off detect_unprocessed_resources.

(see EventDispatch::Base#)



146
147
148
149
150
# File 'lib/chef/action_collection.rb', line 146

def converge_failed(exception)
  return if consumers.empty?

  detect_unprocessed_resources
end

#cookbook_compilation_start(run_context) ⇒ Object

This hook gives us the run_context immediately after it is created so that we can wire up this object to it.

This also causes the action_collection_registration event to fire, all consumers that have not yet registered with the action_collection must register via this callback. This is the latest point before resources actually start to get evaluated.

(see EventDispatch::Base#)



126
127
128
129
130
131
# File 'lib/chef/action_collection.rb', line 126

def cookbook_compilation_start(run_context)
  run_context.action_collection = self
  # fire the action_colleciton_registration hook after cookbook_compilation_start -- last chance for consumers to register
  run_context.events.enqueue(:action_collection_registration, self)
  @run_context = run_context
end

#filtered_collection(max_nesting: nil, up_to_date: true, skipped: true, updated: true, failed: true, unprocessed: true) ⇒ Chef::ActionCollection

Allows getting at the action_records collection filtered by nesting level and status.

TODO: filtering by resource type+name



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

def filtered_collection(max_nesting: nil, up_to_date: true, skipped: true, updated: true, failed: true, unprocessed: true)
  subrecords = action_records.select do |rec|
    ( max_nesting.nil? || rec.nesting_level <= max_nesting ) &&
      ( rec.status == :up_to_date && up_to_date ||
        rec.status == :skipped && skipped ||
        rec.status == :updated && updated ||
        rec.status == :failed && failed ||
        rec.status == :unprocessed && unprocessed )
  end
  self.class.new(events, run_context, subrecords)
end

#register(object) ⇒ Object

Consumers must call register – either directly or through the action_collection_registration hook. If nobody has registered any interest, then no action tracking will be done.



138
139
140
# File 'lib/chef/action_collection.rb', line 138

def register(object)
  consumers << object
end

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

Hook to start processing a resource. May be called within processing of an outer resource so the pending_updates array forms a stack that sub-resources are popped onto and off of. This is always called.

(see EventDispatch::Base#)



158
159
160
161
162
# File 'lib/chef/action_collection.rb', line 158

def resource_action_start(new_resource, action, notification_type = nil, notifier = nil)
  return if consumers.empty?

  pending_updates << ActionRecord.new(new_resource, action, pending_updates.length)
end

#resource_completed(new_resource) ⇒ Object

Hook called after an action is completed. This is always called, even if the action fails.

(see EventDispatch::Base#)



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/chef/action_collection.rb', line 221

def resource_completed(new_resource)
  return if consumers.empty?

  current_record.elapsed_time = new_resource.elapsed_time

  # Verify if the resource has sensitive data and create a new blank resource with only
  # the name so we can report it back without sensitive data
  # XXX?: what about sensitive data in the current_resource?
  # FIXME: this needs to be display-logic
  if current_record.new_resource.sensitive
    klass = current_record.new_resource.class
    resource_name = current_record.new_resource.name
    current_record.new_resource = klass.new(resource_name)
  end

  action_records << pending_updates.pop
end

#resource_current_state_loaded(new_resource, action, current_resource) ⇒ Object

Hook called after a resource is loaded. If load_current_resource fails, this hook will not be called and current_resource will be nil, and the resource_failed hook will be called.

(see EventDispatch::Base#)



169
170
171
172
173
# File 'lib/chef/action_collection.rb', line 169

def resource_current_state_loaded(new_resource, action, current_resource)
  return if consumers.empty?

  current_record.current_resource = current_resource
end

#resource_failed(new_resource, action, exception) ⇒ Object

Hook called after an action fails.

(see EventDispatch::Base#)



210
211
212
213
214
215
# File 'lib/chef/action_collection.rb', line 210

def resource_failed(new_resource, action, exception)
  return if consumers.empty?

  current_record.status = :failed
  current_record.exception = exception
end

#resource_skipped(resource, action, conditional) ⇒ Object

Hook called after an action is determined to be skipped due to a conditional.

(see EventDispatch::Base#)



189
190
191
192
193
194
# File 'lib/chef/action_collection.rb', line 189

def resource_skipped(resource, action, conditional)
  return if consumers.empty?

  current_record.status = :skipped
  current_record.conditional = conditional
end

#resource_up_to_date(new_resource, action) ⇒ Object

Hook called after an action is determined to be up to date.

(see EventDispatch::Base#)



179
180
181
182
183
# File 'lib/chef/action_collection.rb', line 179

def resource_up_to_date(new_resource, action)
  return if consumers.empty?

  current_record.status = :up_to_date
end

#resource_updated(new_resource, action) ⇒ Object

Hook called after an action modifies the system and is marked updated.

(see EventDispatch::Base#)



200
201
202
203
204
# File 'lib/chef/action_collection.rb', line 200

def resource_updated(new_resource, action)
  return if consumers.empty?

  current_record.status = :updated
end