]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/parselocalconfig/lib/puppet/face/catalog/print.rb
Add a new puppet face
[dsa-puppet.git] / modules / parselocalconfig / lib / puppet / face / catalog / print.rb
1 require 'puppet/face'
2
3 Puppet::Face.define(:catalog, '0.0.1') do
4   action :print do
5     summary "Displays the contents of a catalog"
6
7     option "--catalog CATALOG" do
8       summary "Path to a specific catalog to print"
9     end
10
11     option "--limit TYPE" do
12       summary "Limits the display to a certain type"
13     end
14
15     option "--no-classes" do
16       summary "Do not show any classes"
17     end
18
19     option "--no-resources" do
20       summary "Do not show resources list"
21     end
22
23     option "--no-tags" do
24       summary "Do not show any tags"
25     end
26
27
28     when_invoked do |options|
29       Puppet.settings.preferred_run_mode = "agent"
30
31       catalog_file = options.fetch(:catalog, File.join([Puppet[:client_datadir], "catalog", "%s.json" % Puppet[:certname]]))
32
33       catalog = PSON.parse(File.read(catalog_file))
34
35       unless options[:no_classes] == false
36         puts("Classes included on this node:")
37         catalog.classes.each do |klass|
38           puts("\t#{klass}")
39         end
40
41         puts("\n\n")
42       end
43
44       unless options[:no_tags] == false
45         puts("Tags for this node:")
46         catalog.tags.each do |tag|
47           puts("\t#{tag}")
48         end
49
50         puts("\n\n")
51       end
52
53       unless options[:no_resources] == false
54         puts("Resources managed by puppet on this node:")
55         printresource(catalog, options[:limit])
56       end
57
58       nil
59     end
60   end
61
62   def printresource(resource, limit)
63     if resource.class == Puppet::Resource::Catalog
64       resource.edges.each do |b|
65         printresource(b, limit)
66       end
67     elsif resource.class == Puppet::Relationship and resource.target.class == Puppet::Resource and resource.target.title != nil and resource.target.file != nil
68       target = resource.target
69       manifestfile = target.file.gsub("/etc/puppet/manifests/", "")
70
71       if limit
72         if target.type.downcase == limit.downcase
73           puts "\t#{target.type} { #{target.title}: }\n\t\tdefined in #{manifestfile}:#{target.line}\n\n"
74         end
75       else
76           puts "\t#{target.type} { #{target.title}: }\n\t\tdefined in #{manifestfile}:#{target.line}\n\n"
77       end
78     end
79   end
80 end