Class: Sfn::Command::Graph
- Inherits:
-
Sfn::Command
- Object
- Bogo::Cli::Command
- Sfn::Command
- Sfn::Command::Graph
- Defined in:
- lib/sfn/command/graph.rb
Overview
Graph command
Defined Under Namespace
Classes: GraphProcessor
Constant Summary collapse
- GRAPH_STYLES =
Valid graph styles
[ 'creation', 'dependency' ]
Constants included from Sfn::CommandModule::Template
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
- #colorize(string) ⇒ Object
- #edge_detection(template, graph, name = '', resource_names = []) ⇒ Object
-
#execute! ⇒ Object
Generate graph.
- #generate_graph(template, args = {}) ⇒ Object
- #output_discovery(template, outputs, resource_name, parent_template, name = '') ⇒ Object
- #resource_dependencies(data, names) ⇒ Object
- #validate_graph_style! ⇒ Object
Methods included from Sfn::CommandModule::Stack
Methods included from Sfn::CommandModule::Template
Methods included from Sfn::CommandModule::Base
Methods inherited from Sfn::Command
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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/sfn/command/graph.rb', line 206 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 |
#edge_detection(template, graph, name = '', resource_names = []) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/sfn/command/graph.rb', line 147 def edge_detection(template, graph, name = '', resource_names = []) resources = template.fetch('Resources', {}) node_prefix = name resources.each do |resource_name, resource_data| node_name = [node_prefix, resource_name].join if(resource_data['Type'] == 'AWS::CloudFormation::Stack') graph.subgraph << generate_graph( resource_data['Properties'].delete('Stack'), :name => resource_name, :type => resource_data['Type'], :resource_names => resource_names ) next else graph.node(node_name).attributes << graph.fillcolor(colorize(node_prefix.empty? ? config[:file] : node_prefix).inspect) graph.box3d << graph.node(node_name) end graph.filled << graph.node(node_name) graph.node(node_name).label "#{resource_name}\n<#{resource_data['Type']}>\n#{name}" resource_dependencies(resource_data, resource_names + resources.keys).each do |dep_name| if(resources.keys.include?(dep_name)) dep_name = [node_prefix, dep_name].join end if(config[:graph_style] == 'creation') @root_graph.edge(dep_name, node_name) else @root_graph.edge(node_name, dep_name) end end end resource_names.concat resources.keys.map{|r_name| [node_prefix, r_name].join} end |
#execute! ⇒ Object
Generate graph
20 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 |
# File 'lib/sfn/command/graph.rb', line 20 def execute! config[:print_only] = true validate_graph_style! file = load_template_file file = parameter_scrub!(file.sparkle_dump) @outputs = Smash.new file = file.to_smash ui.info "Template resource graph generation - Style: #{ui.color(config[:graph_style], :bold)}" if(config[:file]) ui.puts " -> path: #{config[:file]}" end run_action 'Pre-processing template for graphing' do output_discovery(file, @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(file.to_smash) 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 |file| file.puts graph.to_s end else graph.save config[:output_file], config[:output_type] end nil end end |
#generate_graph(template, args = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sfn/command/graph.rb', line 57 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 |
#output_discovery(template, outputs, resource_name, parent_template, name = '') ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/sfn/command/graph.rb', line 76 def output_discovery(template, outputs, resource_name, parent_template, name='') if(template['Resources']) template['Resources'].each_pair do |r_name, r_info| if(r_info['Type'] == 'AWS::CloudFormation::Stack') output_discovery(r_info['Properties']['Stack'], outputs, r_name, template, r_name) end end end if(parent_template) ui.debug "Pre-processing stack resource `#{resource_name}`" substack_parameters = Smash[ parent_template.fetch('Resources', resource_name, 'Properties', 'Parameters', {}).map do |key, value| result = [key, value] if(value.is_a?(Hash)) v_key = value.keys.first v_value = value.values.first if(v_key == 'Fn::GetAtt' && parent_template.fetch('Resources', {}).keys.include?(v_value.first) && v_value.last.start_with?('Outputs.')) output_key = v_value.first + '__' + v_value.last.split('.', 2).last ui.debug "Output key for check: #{output_key}" if(outputs.key?(output_key)) new_value = outputs[output_key] result = [key, new_value] ui.debug "Parameter for output swap `#{key}`: #{value} -> #{new_value}" end end end result end ] ui.debug "Generated internal parameters for `#{resource_name}`: #{substack_parameters}" processor = GraphProcessor.new({}, :parameters => substack_parameters ) template['Resources'] = processor.dereference_processor( template['Resources'], ['Ref'] ) template['Outputs'] = processor.dereference_processor( template['Outputs'], ['Ref'] ) rename_processor = GraphProcessor.new({}, :parameters => Smash[ template.fetch('Resources', {}).keys.map do |r_key| [r_key, {'Ref' => [name, r_key].join}] end ] ) derefed_outs = rename_processor.dereference_processor( template.fetch('Outputs', {}) ) || {} derefed_outs.each do |o_name, o_data| o_key = [name, o_name].join('__') outputs[o_key] = o_data['Value'] end end outputs.dup.each do |key, value| if(value.is_a?(Hash)) v_key = value.keys.first v_value = value.values.first if(v_key == 'Fn::GetAtt' && v_value.last.start_with?('Outputs.')) output_key = v_value.first << '__' << v_value.last.split('.', 2).last if(outputs.key?(output_key)) outputs[key] = outputs[output_key] end end end end end |
#resource_dependencies(data, names) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/sfn/command/graph.rb', line 180 def resource_dependencies(data, names) case data when Hash data.map do |key, value| if(key == 'Ref' && names.include?(value)) value elsif(key == 'DependsOn') [value].flatten.compact.find_all do |dependson_name| names.include?(dependson_name) end elsif(key == 'Fn::GetAtt' && names.include?(res = [value].flatten.compact.first)) res else resource_dependencies(key, names) + resource_dependencies(value, names) end end.flatten.compact.uniq when Array data.map do |item| resource_dependencies(item, names) end.flatten.compact.uniq else [] end end |
#validate_graph_style! ⇒ Object
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/sfn/command/graph.rb', line 227 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 |