nwops / puppet-debugger

A interactive live debugger and REPL for the puppet language
https://gitlab.com/puppet-debugger/puppet-debugger
MIT License
121 stars 11 forks source link

render relationship graph #54

Closed binford2k closed 7 years ago

binford2k commented 7 years ago

there should be a command in the web REPL to display the relationship graph in an overlay or popup dialog or some such. This code will render the graph into an SVG, which can be included directly in a webpage for any modern browser.

        node    = Puppet::Node.indirection.find('validator')
        catalog = Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)

        # Remove some of the noise -- we don't need these nodes rendered
        # These calls are failing due to an internal method not being available in 2 & 3.x. Suspect
        # that it's related to the compiler not being set up fully?
        catalog.remove_resource(catalog.resource("Stage", :main)) rescue nil
        catalog.remove_resource(catalog.resource("Class", :settings)) rescue nil

        dotfile  = catalog.to_ral.relationship_graph.to_dot

        # SVG rendition of the graph
        svg = GraphViz.parse_string(dotfile) do |graph|
          graph[:label] = 'Resource Relationships'

          # change the whits into something readable
          graph.each_node do |name, node|
            next unless name.start_with? 'Whit'
            newname = name.dup
            newname.sub!('Admissible_class', 'Starting Class')
            newname.sub!('Completed_class', 'Finishing Class')
            node[:label] = newname[5..-2]
          end
        end.output(:svg => String)

Simply render into a web page:

<html>
<head>
  <title>Relationship Graph</title>
</head>
  <body>
        <div class="relationships"><%= svg %></div>
  </body>
</html>
logicminds commented 7 years ago

@binford2k had to add a lot of new code to get this working. v0.7.0 now has a plugin and hook system as a result.

This graph plugin allows for live updates which I think is pretty killer.

See the usage for a live container demo. The graph gets updated as you add resources which is pretty neat, but I think the presentation could be so much better.

https://github.com/logicminds/puppet-debugger-graph#usage

Use the container instructions since it lowers the bar.

logicminds commented 7 years ago

closing since I moved to a plugin.