Class: PuppetGraph::Grapher

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-graph/grapher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code.



5
6
7
# File 'lib/puppet-graph/grapher.rb', line 5

def code
  @code
end

#fact_overridesObject

Returns the value of attribute fact_overrides.



3
4
5
# File 'lib/puppet-graph/grapher.rb', line 3

def fact_overrides
  @fact_overrides
end

#modulepathObject

Returns the value of attribute modulepath.



4
5
6
# File 'lib/puppet-graph/grapher.rb', line 4

def modulepath
  @modulepath
end

Instance Method Details

#catalogueObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet-graph/grapher.rb', line 43

def catalogue
  with_tmpdir do |dir|
    Puppet[:modulepath] = modulepath
    Puppet[:vardir] = dir
    Puppet[:confdir] = dir
    Puppet[:logdir] = dir
    Puppet[:rundir] = dir
    Puppet[:code] = code

    stub_facts

    Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)
  end
end

#default_factsObject



72
73
74
75
76
77
78
# File 'lib/puppet-graph/grapher.rb', line 72

def default_facts
  @default_facts ||= {
    'hostname' => node.name.split('p').first,
    'fqdn'     => node.name,
    'domain'   => node.name.split('.').last,
  }
end

#draw(format, output_file) ⇒ Object



7
8
9
# File 'lib/puppet-graph/grapher.rb', line 7

def draw(format, output_file)
  self.send("draw_#{format}_graph", output_file)
end

#draw_dot_graph(output_file) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/puppet-graph/grapher.rb', line 25

def draw_dot_graph(output_file)
  dot_graph = relationship_graph.to_dot('label' => code)

  File.open(output_file, 'wb') do |f|
    f.puts dot_graph
  end
end

#draw_png_graph(output_file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/puppet-graph/grapher.rb', line 11

def draw_png_graph(output_file)
  dot_graph = relationship_graph.to_dot('label' => code)

  tmp_dot_file = Tempfile.new('puppet-graph')
  begin
    tmp_dot_file.write dot_graph
    tmp_dot_file.flush
    `dot -o#{output_file} -Tpng #{tmp_dot_file.path}`
  ensure
    tmp_dot_file.close
    tmp_dot_file.unlink
  end
end

#nodeObject



89
90
91
# File 'lib/puppet-graph/grapher.rb', line 89

def node
  @node ||= Puppet::Node.new('testnode')
end

#relationship_graphObject



33
34
35
36
37
38
39
40
41
# File 'lib/puppet-graph/grapher.rb', line 33

def relationship_graph
  @relationship_graph ||= lambda {
    graph = catalogue.to_ral.relationship_graph
    graph.vertices.select { |r| r.to_s == 'Class[Settings]' }.each do |vertex|
      graph.remove_vertex! vertex
    end
    graph
  }.call
end

#stub_factsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puppet-graph/grapher.rb', line 58

def stub_facts
  if fact_overrides.nil?
    facts = default_facts
  else
    facts = default_facts.merge(fact_overrides)
  end

  facts.each do |key, value|
    Facter.add(key) { setcode { value } }
  end

  node.merge(facts)
end

#with_tmpdirObject



80
81
82
83
84
85
86
87
# File 'lib/puppet-graph/grapher.rb', line 80

def with_tmpdir
  dir = Dir.mktmpdir
  begin
    yield dir
  ensure
    FileUtils.remove_entry_secure dir
  end
end