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, #compliance_input_enabled, #compliance_input_loaded, #compliance_load_complete, #compliance_load_start, #compliance_profile_enabled, #compliance_profile_loaded, #compliance_waiver_enabled, #compliance_waiver_loaded, #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, #inputs_load_complete, #inputs_load_start, #key_migration_status, #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, #profiles_load_complete, #profiles_load_start, #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, #waivers_load_complete, #waivers_load_start, #whyrun_assumption

Constructor Details

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

Returns a new instance of ActionCollection.



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

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

Instance Attribute Details

#action_recordsObject (readonly)

Returns the value of attribute action_records.



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

def action_records
  @action_records
end

#eventsObject (readonly)

Returns the value of attribute events.



90
91
92
# File 'lib/chef/action_collection.rb', line 90

def events
  @events
end

#pending_updatesObject (readonly)

Returns the value of attribute pending_updates.



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

def pending_updates
  @pending_updates
end

#run_contextObject (readonly)

Returns the value of attribute run_context.



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

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#)



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

def converge_failed(exception)
  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.

(see EventDispatch::Base#)



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

def cookbook_compilation_start(run_context)
  run_context.action_collection = self
  # this hook is now poorly named since it is just a callback that lets other consumers snag a reference to the action_collection
  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



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

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.



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

def register(object)
  Chef::Log.warn "the action collection no longer requires registration at #{caller[0]}"
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#)



157
158
159
# File 'lib/chef/action_collection.rb', line 157

def resource_action_start(new_resource, action, notification_type = nil, notifier = nil)
  pending_updates << ActionRecord.new(new_resource, action, pending_updates.length)
end

#resource_after_state_loaded(new_resource, action, after_resource) ⇒ Object

Hook called after an after resource is loaded. If load_after_resource fails, this hook will not be called and after_resource will be nil, and the resource_failed hook will be called.

(see EventDispatch::Base#)



175
176
177
# File 'lib/chef/action_collection.rb', line 175

def resource_after_state_loaded(new_resource, action, after_resource)
  current_record.after_resource = after_resource
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#)



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/chef/action_collection.rb', line 218

def resource_completed(new_resource)
  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 current 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#)



166
167
168
# File 'lib/chef/action_collection.rb', line 166

def resource_current_state_loaded(new_resource, action, current_resource)
  current_record.current_resource = current_resource
end

#resource_failed(new_resource, action, exception) ⇒ Object

Hook called after an action fails.

(see EventDispatch::Base#)



208
209
210
211
212
# File 'lib/chef/action_collection.rb', line 208

def resource_failed(new_resource, action, exception)
  current_record.status = :failed
  current_record.exception = exception
  current_record.error_description = Formatters::ErrorMapper.resource_failed(new_resource, action, exception).for_json
end

#resource_skipped(resource, action, conditional) ⇒ Object

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

(see EventDispatch::Base#)



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

def resource_skipped(resource, action, conditional)
  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#)



183
184
185
# File 'lib/chef/action_collection.rb', line 183

def resource_up_to_date(new_resource, action)
  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
# File 'lib/chef/action_collection.rb', line 200

def resource_updated(new_resource, action)
  current_record.status = :updated
end

#resourcesObject



119
120
121
# File 'lib/chef/action_collection.rb', line 119

def resources
  action_records.map(&:new_resource)
end