Created
February 8, 2015 01:28
-
-
Save sczizzo/2b6dbd4b1a5b6929b7ee to your computer and use it in GitHub Desktop.
Node Log and Cookbooks Knife Plugin
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/knife' | |
module Plugins | |
class NodeCookbooks < Chef::Knife | |
banner "knife node cookbooks NODE" | |
deps do | |
require 'chef/search/query' | |
require 'chef/knife/search' | |
require 'highline' | |
end | |
def h | |
@highline ||= HighLine.new | |
end | |
def run | |
unless @node_name = name_args[0] | |
ui.error 'Please specify a node' | |
exit 1 | |
end | |
searcher = Chef::Search::Query.new | |
result = searcher.search(:node, "name:#{@node_name}") | |
node = result.first.first | |
if node.nil? | |
ui.error 'Could find the given node' | |
exit 1 | |
end | |
$stdout.sync = true | |
unless cookbook_versions = node['cookbook_versions'] | |
ui.error 'No cookbooks to report' | |
exit 1 | |
end | |
log_entries = [ | |
h.color('Cookbook', :bold), | |
h.color('Version', :bold) | |
] | |
num_columns = log_entries.length | |
cookbook_versions.to_a.sort_by(&:first).each do |(name, version)| | |
log_entries << name | |
log_entries << version | |
end | |
puts h.list(log_entries, :columns_across, num_columns) | |
end | |
end | |
class NodeLog < Chef::Knife | |
banner "knife node log NODE" | |
deps do | |
require 'chef/search/query' | |
require 'chef/knife/search' | |
require 'highline' | |
end | |
def h | |
@highline ||= HighLine.new | |
end | |
def recipe entry | |
"%s@%s::%s" % [ | |
entry['cookbook_name'], | |
entry['cookbook_version'], | |
entry['recipe_name'] | |
] | |
end | |
def singularize array | |
return array.first if array.length == 1 | |
array | |
end | |
def run | |
unless @node_name = name_args[0] | |
ui.error 'Please specify a node' | |
exit 1 | |
end | |
searcher = Chef::Search::Query.new | |
result = searcher.search(:node, "name:#{@node_name}") | |
node = result.first.first | |
if node.nil? | |
ui.error 'Could find the given node' | |
exit 1 | |
end | |
$stdout.sync = true | |
unless node['log'] | |
ui.error 'No log to report' | |
exit 1 | |
end | |
log_entries = [ | |
h.color('Time', :bold), | |
h.color('Recipe', :bold), | |
h.color('Action', :bold), | |
h.color('Resource Type', :bold), | |
h.color('Resource', :bold) | |
] | |
num_columns = log_entries.length | |
node['log'].each do |log_entry| | |
log_entries << log_entry['time'].to_s | |
log_entries << recipe(log_entry) | |
log_entries << singularize(log_entry['action']).to_s | |
log_entries << log_entry['resource_type'].to_s | |
log_entries << log_entry['resource'].to_s | |
end | |
puts h.list(log_entries, :columns_across, num_columns) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment