From bb8f5554b2a73c95c7eccad6aaebde119fef9666 Mon Sep 17 00:00:00 2001 From: Ricky Zhou Date: Wed, 12 May 2010 15:06:44 -0400 Subject: [PATCH] Add puppetsearch (compatible with 0.25.5). --- scripts/puppetsearch/puppetsearch.rb | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 scripts/puppetsearch/puppetsearch.rb diff --git a/scripts/puppetsearch/puppetsearch.rb b/scripts/puppetsearch/puppetsearch.rb new file mode 100755 index 0000000..dd8df04 --- /dev/null +++ b/scripts/puppetsearch/puppetsearch.rb @@ -0,0 +1,57 @@ +#!/usr/bin/ruby +require "yaml" +require "puppet" +require "optparse" + +def find_resource(catalog, type, query_file) + resource = catalog.resource(type, query_file) + unless resource.nil? + resource.file + end +end + +options = { + :types => [], +} + +OptionParser.new do |opts| + opts.banner = "Usage: puppetsearch [options] title1 title2 ..." + + opts.on("-y", + "--yamlfile YAMLFILE", + "Read catalog from YAMLFILE") do |yamlfile| + options[:yamlfile] = yamlfile + end + + opts.on("-t", + "--types TYPES", + "Comma-separated list of resource types to search for") do |types| + options[:types] = types.split(',') + end +end.parse! + +# Search for files by default. +if options[:types].empty? + options[:types] << "File" +end + +if options[:yamlfile].nil? + require "facter" + + fqdn = Facter.value(:fqdn) + if fqdn.nil? + abort "Error: Could not get FQDN" + end + + options[:yamlfile] = "/var/lib/puppet/client_yaml/catalog/#{fqdn}.yaml" +end + +catalog = YAML::load_file(options[:yamlfile]) + +ARGV.each do |search| + options[:types].each do |type| + manifest = find_resource(catalog, type, search) + puts "#{type}[#{search}] defined in #{manifest}" + end +end +