Class: Sfn::Command::Graph

Inherits:
Sfn::Command
  • Object
show all
Includes:
Sfn::CommandModule::Base, Sfn::CommandModule::Stack, Sfn::CommandModule::Template
Defined in:
lib/sfn/command/graph.rb,
lib/sfn/command/graph/aws.rb,
lib/sfn/command/graph/provider.rb,
lib/sfn/command/graph/terraform.rb

Overview

Graph command

Defined Under Namespace

Modules: Provider

Constant Summary collapse

GRAPH_STYLES =

Valid graph styles

[
  "creation",
  "dependency",
]

Constants included from Sfn::CommandModule::Template

Sfn::CommandModule::Template::DEFAULT_PROVIDER_NAME, Sfn::CommandModule::Template::MAX_PARAMETER_ATTEMPTS, Sfn::CommandModule::Template::TEMPLATE_IGNORE_DIRECTORIES

Constants inherited from Sfn::Command

CONFIG_BASE_NAME, VALID_CONFIG_EXTENSIONS

Instance Method Summary collapse

Methods included from Sfn::CommandModule::Stack

included

Methods included from Sfn::CommandModule::Template

included

Methods included from Sfn::CommandModule::Base

included

Methods inherited from Sfn::Command

#config, #initialize

Methods included from Sfn::CommandModule::Callbacks

#api_action!, #callbacks_for, #run_callbacks_for

Constructor Details

This class inherits a constructor from Sfn::Command

Instance Method Details

#colorize(string) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sfn/command/graph.rb', line 88

def colorize(string)
  hash = string.chars.inject(0) do |memo, chr|
    if memo + chr.ord > 127
      (memo - chr.ord).abs
    else
      memo + chr.ord
    end
  end
  color = "#"
  3.times do |i|
    color << (255 ^ hash).to_s(16)
    new_val = hash + (hash * (1 / (i + 1.to_f))).to_i
    if hash * (i + 1) < 127
      hash = new_val
    else
      hash = hash / (i + 1)
    end
  end
  color
end

#execute!Object

Generate graph



21
22
23
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
# File 'lib/sfn/command/graph.rb', line 21

def execute!
  config[:print_only] = true
  validate_graph_style!
  file = load_template_file
  provider = Bogo::Utility.camel(file.provider).to_sym
  if Provider.constants.include?(provider)
    graph_const = Provider.const_get(provider)
    ui.debug "Loading provider graph implementation - #{graph_const}"
    extend graph_const
    @outputs = Smash.new
    ui.info "Template resource graph generation - Style: #{ui.color(config[:graph_style], :bold)}"
    if config[:file]
      ui.puts "  -> path: #{config[:file]}"
    end
    template_dump = file.compile.sparkle_dump!.to_smash
    run_action "Pre-processing template for graphing" do
      output_discovery(template_dump, @outputs, nil, nil)
      ui.debug "Output remapping results from pre-processing:"
      @outputs.each_pair do |o_key, o_value|
        ui.debug "#{o_key} -> #{o_value}"
      end
      nil
    end
    graph = nil
    run_action "Generating resource graph" do
      graph = generate_graph(template_dump)
      nil
    end
    run_action "Writing graph result" do
      FileUtils.mkdir_p(File.dirname(config[:output_file]))
      if config[:output_type] == "dot"
        File.open("#{config[:output_file]}.dot", "w") do |o_file|
          o_file.puts graph.to_s
        end
      else
        graph.save config[:output_file], config[:output_type]
      end
      nil
    end
  else
    valid_providers = Provider.constants.sort.map { |provider|
      Bogo::Utility.snake(provider)
    }.join("`, `")
    ui.error "Graphing for provider `#{file.provider}` not currently supported."
    ui.error "Currently supported providers: `#{valid_providers}`."
  end
end

#generate_graph(template, args = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sfn/command/graph.rb', line 69

def generate_graph(template, args = {})
  graph = ::Graph.new
  @root_graph = graph unless @root_graph
  graph.graph_attribs << ::Graph::Attribute.new("overlap = false")
  graph.graph_attribs << ::Graph::Attribute.new("splines = true")
  graph.graph_attribs << ::Graph::Attribute.new("pack = true")
  graph.graph_attribs << ::Graph::Attribute.new('start = "random"')
  if args[:name]
    graph.name = "cluster_#{args[:name]}"
    labelnode_key = "cluster_#{args[:name]}"
    graph.plaintext << graph.node(labelnode_key)
    graph.node(labelnode_key).label args[:name]
  else
    graph.name = "root"
  end
  edge_detection(template, graph, args[:name].to_s.sub("cluster_", ""), args.fetch(:resource_names, []))
  graph
end

#validate_graph_style!Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/sfn/command/graph.rb', line 109

def validate_graph_style!
  if config[:luckymike]
    ui.warn "Detected luckymike power override. Forcing `dependency` style!"
    config[:graph_style] = "dependency"
  end
  config[:graph_style] = config[:graph_style].to_s
  unless GRAPH_STYLES.include?(config[:graph_style])
    raise ArgumentError.new "Invalid graph style provided `#{config[:graph_style]}`. Valid: `#{GRAPH_STYLES.join("`, `")}`"
  end
end