Sha256: b1f920f076201a9de7930ab52cee94080183091750e75f4f359a144dbd307a87

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'graphviz'

module KnifeDraw
  class ChefGraph
    attr_reader :graph

    NODE_COLOR = "#66c2a5"
    ROLE_COLOR = "#8da0cb"
    RUNLIST_COLOR = "#fc8d62"
    EDGE_COLOR = "gray52" #"#bebada"
    FONT_NAME = "Helvetica"

    def initialize(cluster_environments: false)
      @graph =  GraphViz.new(:KnifeDraw, rankdir: :LR, strict: true, fontname: FONT_NAME)
      @cluster_environments = cluster_environments
    end

    def env_prefix
      @cluster_environments ? "cluster" : "env_"
    end

    def environments
      @environments ||= Hash.new {|hash, key|
        hash[key] = graph.add_graph("#{env_prefix}#{key}", label: key, fontname: FONT_NAME)
      }
    end

    def draw_node(name, environment)
      environments[environment.to_s].add_nodes(name, shape: :box3d, fillcolor: NODE_COLOR, style: :filled, fontname: FONT_NAME)
    end

    def draw_role(name)
      graph.add_nodes(name, shape: :component, fillcolor: ROLE_COLOR, style: :filled, fontname: FONT_NAME)
    end

    def draw_runlist(name)
      graph.add_nodes(name, shape: :note, fillcolor: RUNLIST_COLOR, style: :filled, fontname: FONT_NAME)
    end

    def connect(source, target)
      graph.add_edge(source, target, color: EDGE_COLOR)
    end

    def draw!(outputfile="output.png")
      filename=outputfile
      format = File.extname(filename)[1..-1]
      graph.output format => filename
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
knife-draw-0.9.0 lib/knife_draw/chef_graph.rb