Last active
August 29, 2015 14:14
-
-
Save sczizzo/09411ffe94d627523dae to your computer and use it in GitHub Desktop.
Updated Resources Report Handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/handler' | |
require 'chef/log' | |
require 'json' | |
# Based on http://dev.nuclearrooster.com/2011/05/10/chef-notifying-and-logging-updated-resources/ | |
module Handlers | |
class UpdatedResources < Chef::Handler | |
def initialize opts={} | |
@max_entries = opts[:max_entries] || 25 | |
end | |
def report | |
updates = run_status.updated_resources.map do |resource| | |
{ | |
'time' => Time.now, | |
'cookbook_name' => resource.cookbook_name, | |
'recipe_name' => resource.recipe_name, | |
'action' => resource.action, | |
'resource' => resource.name, | |
'resource_type' => resource.resource_name, | |
# If you're also running the Cookbook Versions handler... | |
# (https://gist.github.com/sczizzo/c24b1a7bb76e770c5e95) | |
# 'cookbook_version' => node['cookbooks'][resource.cookbook_name] | |
} | |
end | |
node.automatic['log'] ||= [] | |
log = node.automatic['log'].to_a | |
updates.each do |update| | |
log.unshift update | |
end | |
node.automatic['log'] = log.take @max_entries | |
node.save | |
Chef::Log.debug 'Updated Resources: %s' % JSON.pretty_generate(node['log'].reverse) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment