Created
February 7, 2015 23:49
-
-
Save sczizzo/1f05b3ced5ae884a652f to your computer and use it in GitHub Desktop.
Log 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 https://docs.chef.io/handlers.html#cookbook-versions | |
# and http://dev.nuclearrooster.com/2011/05/10/chef-notifying-and-logging-updated-resources | |
module Handlers | |
class LogHandler < Chef::Handler | |
def initialize opts={} | |
@max_entries = opts[:max_entries] || 25 | |
end | |
def report | |
cookbooks = run_context.cookbook_collection | |
cookbook_versions = cookbooks.keys.inject({}) do |h, k| | |
cb = cookbooks[k] | |
h[cb.name.to_s] = cb.version.to_s | |
h | |
end | |
updates = run_status.updated_resources.map do |resource| | |
{ | |
'time' => Time.now, | |
'cookbook_version' => cookbook_versions[resource.cookbook_name.to_s], | |
'cookbook_name' => resource.cookbook_name, | |
'recipe_name' => resource.recipe_name, | |
'action' => resource.action, | |
'resource' => resource.name, | |
'resource_type' => resource.resource_name | |
} | |
end | |
node.automatic['cookbook_versions'] = cookbook_versions | |
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 'Cookbook Versions: %s' % JSON.pretty_generate(node['cookbook_versions']) | |
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