This repository has been archived by the owner on Oct 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
pm-grep
90 lines (69 loc) · 2.34 KB
/
pm-grep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/ruby
require 'puppet'
require 'optparse'
def help
appname = 'pm-grep'
puts <<-EOH
#{appname} -t <type> -p <property> -v [value] <puppet_manifest.pp>
Examples
Show all file resources with a mode of 644
#{appname} -t file -p mode -v 644 files.pp
Show all host resources with an alias of any value
#{appname} -t host -p host_aliases hosts.pp
Check a number of pp files at once
find /etc/puppet/modules/ -name "*.pp" | xargs -n 1 #{appname}
EOH
exit 1
end
options = {}
OptionParser.new do |opts|
opts.on("-p", "--property PROPERTY") { |property| options['property'] = property.to_s }
opts.on("-t", "--type TYPE") { |type| options['type'] = type.to_s }
opts.on("-v", "--value VALUE") { |value| options['value'] = value.to_s }
opts.on("-h", "--help") { help }
end.parse!
help unless options['type']
# doesn't work with global settings (Package { foo }), wrong stage for
# that.
# grab the file to check inside
manifest = ARGV.shift
help unless manifest
Puppet[:manifest] = manifest
begin
loaded = Puppet::Node::Environment.new(Puppet[:environment]).known_resource_types
rescue => detail
Puppet.err detail
exit 1
end
loaded.hostclasses.keys.each do |hc|
loaded.hostclasses[hc].code.each do |r|
resource = nil
if r.class.to_s == "Puppet::Parser::AST::Resource"
resource = r
elsif r.class.to_s == "Puppet::Parser::AST::ASTArray"
resource = r[0]
else
next
end
next unless resource.type == options['type']
# show all matching resources of 'type' if no other options are given
unless options['property'] || options['value']
puts "Matched #{options['type']} #{resource.title} in #{Puppet[:manifest]}"
next
end
params = {}
resource.parameters.each do | param |
property, value = param.to_a
params[property.to_s] = value.to_s.gsub(/"/, '')
end
next unless params.key? options['property']
# show all matching resources of 'type' with the given property of any
# value
unless options['value']
puts "Matched #{options['type']} #{resource.title} - has #{options['property']} in #{Puppet[:manifest]}"
next
end
next unless params[options['property']] == options['value']
puts "Matched #{options['type']} #{resource.title} in #{Puppet[:manifest]}"
end
end