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.shift 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' require 'date' require 'time' end def h @highline ||= HighLine.new end def recipe entry "%s@%s::%s" % [ entry['cookbook_name'], entry['cookbook_version'], entry['recipe_name'] ] end def join array array.join(',') rescue array end def iso8601 date_string DateTime.strptime(date_string).to_time.iso8601 rescue date_string end def run unless node_name = name_args.shift 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 << iso8601(log_entry['time']) log_entries << recipe(log_entry) log_entries << join(log_entry['action']) 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