Class: KnifeGraph::Graph

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/graph.rb

Instance Method Summary collapse

Instance Method Details

#runObject



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
# File 'lib/chef/knife/graph.rb', line 24

def run
  if config[:environment] and not Chef::Environment.list.find { |e, url| e == config[:environment] }
    raise "Environment '#{config[:environment]}' not found"
  end

  graph = ::Graph.new

  Chef::Role.list.each do |r, url|
    r_node = "role:#{r}"

    graph.node(r_node, r)
    graph.square << graph.node(r_node)

    role = Chef::Role.load(r)

    role.run_list_for(config[:environment]).recipe_names.each do |recipe|
      graph.edge r_node, "recipe:#{recipe}"
      graph.node("recipe:#{recipe}", recipe)
    end

    role.run_list_for(config[:environment]).role_names.each do |role_name|
      role_node = "role:#{role_name}"
      graph.node(role_node, role_name)
      graph.square << graph.node(role_node)
      graph.edge r_node, role_node
    end
  end

  # Graph dot data
  dot_str = graph.to_s

  # Determine the requested output type
  output_type = if config[:output] == '-'
                  config[:type]
                else
                  File.extname(config[:output]).split('.').last
                end

  # Determine the right output
  if output_type == 'dot'
    graph_data = dot_str
  else
    dot = Mixlib::ShellOut.new("dot -T#{output_type}", :input => dot_str)
    graph_data = dot.run_command.stdout
  end

  # Write the data to STDOUT or file
  if config[:output] == '-'
    print graph_data
  else
    File.open config[:output], 'w' do |f|
      f.print graph_data
    end
  end

  if config[:verbosity] > 0 and config[:output] != '-'
    ui.info("Generated #{config[:output]} for environment #{config[:environment] ? config[:environment] : '_default'}")
  end
end