Skip to content

Instantly share code, notes, and snippets.

@mks-m
Created July 11, 2012 14:59
Show Gist options
  • Save mks-m/3090955 to your computer and use it in GitHub Desktop.
Save mks-m/3090955 to your computer and use it in GitHub Desktop.
puppet repo graph
# Generates infrastructure graph in DOT format
# Usage:
# $ find . -name '*.pp' | while read file; do cat $file; echo; done | \
# ruby graph.rb > puppet.dot
NODE = /^\s*(class|define|node)(\s+)([\w:]+)/
RELATION = /^\s*(require|inherits|include)(\s+)([\w:]+)/
USAGE = /^\s*([\w:]*[\w])(\s*)\{(\s*)/
IGNORE = %w{file package service cron exec Exec line plugin}
$nodes = {}
while line = gets
line.gsub!(/#.*/, '')
if line =~ NODE
current_node = "\"#{$3}\""
$nodes[current_node] = {
"type" => $1,
"uses" => [],
"include" => [],
"require" => [],
"inherits" => []}
end
if line =~ RELATION
$nodes[current_node][$1] << "\"#{$3}\""
end
if line =~ USAGE
$nodes[current_node]["uses"] << "\"#{$1}\"" unless IGNORE.include?($1)
end
end
puts "digraph G {"
$nodes.each do |n, a|
puts "#{n}#{a['type'] == 'node' ? ' [shape=box]' : ''};"
puts "#{n} -> {#{a['uses'].join('; ')}} [style=dotted];" if a['uses'].any?
puts "#{n} -> {#{a['include'].join('; ')}};" if a['include'].any?
puts "#{n} -> {#{a['require'].join('; ')}};" if a['require'].any?
puts "#{n} -> {#{a['inherits'].join('; ')}};" if a['inherits'].any?
end
puts "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment