-
Notifications
You must be signed in to change notification settings - Fork 6
/
punch.rb
executable file
·140 lines (121 loc) · 3.41 KB
/
punch.rb
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env ruby -W0
# -*- coding: utf-8 -*-
#
# Parse Puppet manifests into JSON
#
# Usage: punch.rb [--hostclass HOSTCLASS] manifest.pp
# --help, -h Show this help and exit
# --ignore-import Do not attempt to parse depdencies
# --hostclass, -c Output only this host class (default: all)
#
# Example:
# punch -c 'role::cache::configuration' site.pp
#
# Author: Ori Livneh <[email protected]>
#
require 'getoptlong'
require 'json'
require 'puppet'
include Puppet::Parser
def parse_file(manifest, ignore_import=false)
pp = Puppet::Parser::Parser.new('')
if ignore_import
pp.file = manifest
Puppet[:ignoreimport] = true
ast = pp.parse
pp.known_resource_types.import_ast(ast, manifest)
else
pp.import(manifest)
end
return pp
end
# Public: Recursively convert a Puppet AST object to primitive Ruby
# types.
#
# When no equivalent primitive exists, converts to a hash of instance
# variables, omitting :@line, :@file_index and :@children, and adding
# a key, "type", with the node's class name as the value.
#
# Returned object is JSON-serializable.
#
def primitive(node)
case node
when Array
node.map { |child| primitive(child) }
when Hash
hash = {}
node.each { |k,v| hash[primitive(k)] = primitive(v) }
hash
when Regexp
node.to_s
when Puppet::Resource::Type
primitive(node.code)
when AST::ASTArray
primitive(node.children)
when AST::Boolean
node.value == "true"
when AST::Concat, AST::ASTHash
primitive(node.value)
when AST::Hostclass
{ node.name => primitive(node.code) }
when AST::Name, AST::String, AST::Type
node.value
when AST
hash = { 'type' => node.class.to_s }
vars = node.instance_variables - [:@line, :@file_index, :@children]
vars.each do |var|
k = var.to_s[1..-1]
v = node.instance_variable_get(var)
hash[k] = primitive(v)
end
hash
else
node
end
end
def show_usage
puts <<-END.gsub(/^ {4}/, '')
Parse Puppet manifests into JSON
Usage: #{$0} [--hostclass HOSTCLASS] manifest.pp
--help, -h Show this help and exit
--ignore-import Do not attempt to parse depdencies
--hostclass, -c Output only this host class (default: all)
Example:
punch -c 'role::cache::configuration' site.pp
END
exit 1
end
if __FILE__ == $0
hclass = nil
ignore_import = false
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--hostclass', '-c', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--ignore-import', '-i', GetoptLong::NO_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--help'
show_usage
when '--hostclass'
hclass = arg
when '--ignore-import'
ignore_import = true
end
end
manifest = ARGV.shift
show_usage unless manifest
begin
pp = parse_file(manifest, ignore_import)
rescue Puppet::ParseError => e
$stderr.puts e.message
abort( "Invoke punch.rb with \"--ignore-import\" to override." )
end
if hclass != nil
root = pp.find_hostclass('', hclass)
else
root = pp.known_resource_types.hostclasses
end
prim = primitive(root)
puts JSON.pretty_generate(prim, :max_nesting => false)
end