Module: Construqt::Flavour::Plantuml
- Defined in:
- lib/construqt/flavour/plantuml/plantuml.rb
Defined Under Namespace
Classes: Node
Class Method Summary collapse
- .add_node_factory(type, host, *args) ⇒ Object
- .build_tree ⇒ Object
- .call(type, *args) ⇒ Object
- .clean_name(name) ⇒ Object
- .connect(node, out, path) ⇒ Object
- .draw(node, out, path, flat) ⇒ Object
- .ident(path, content) ⇒ Object
- .name ⇒ Object
- .render_object_address(iface) ⇒ Object
- .simple(clazz) ⇒ Object
Class Method Details
.add_node_factory(type, host, *args) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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 205 206 207 208 209 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 157 def self.add_node_factory(type, host, *args) factory = { "BondDelegate.build_config" => lambda do |type, host, *args| args.first end, "BridgeDelegate.build_config" => lambda do |type, host, *args| args.first end, "DeviceDelegate.build_config" => lambda do |type, host, *args| #Construqt.logger.debug("DeviceDelegate.build_config:#{host.class.name} #{args.map{|i| i.name}}") args.first end, "HostDelegate.build_config" => lambda do |type, host, *args| #Construqt.logger.debug("Planuml:HostDelegate.build_config:#{host.name}") #binding.pry args.first end, "InterfaceDelegate.build_config" => lambda do |type, host, *args| nil end, "OpvnDelegate.build_config" => lambda do |type, host, *args| args.first end, "VlanDelegate.build_config" => lambda do |type, host, *args| args.first end, "IpsecDelegate.build_config" => lambda do |type, host, *args| args.first.cfg end, "VrrpDelegate.build_config" => lambda do |type, host, *args| args.first end, "GreDelegate.build_config" => lambda do |type, host, *args| args.first end, "BgpDelegate.build_config" => lambda do |type, host, *args| args.first.cfg end } method = factory[type] if method obj = method.call(type, host, *args) if obj ident = obj.ident throw "A object needs a ident #{obj.class.name}" unless ident @tree[ident] ||= Node.new(obj) end else Construqt.logger.debug "Planuml:add_node_factory type not found #{type}" end end |
.build_tree ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 211 def self.build_tree #binding.pry @tree.each do |ident,node| #binding.pry # Construqt.logger.debug "Planuml:build_tree=#{node.reference.class.name}=#{simple(node.reference.class)}" { "Vrrp" => lambda do |node| node.reference.delegate.interfaces.each do |i| node.connect @tree[i.ident] end end, "Vlan" => lambda do |node| node.reference.interfaces.each do |vlan_iface| node.connect @tree[vlan_iface.ident] end end, "Bond" => lambda do |node| node.reference.delegate.interfaces.each do |i| #Construqt.logger.debug(">>>>>>>>>> BOND -> #{simple(i.clazz)} #{i.name}") node.connect @tree[i.ident] end end, "Bridge" => lambda do |node| node.reference.delegate.interfaces.each do |i| #binding.pry node.connect @tree[i.ident] end end, "Device" => lambda do |node| if node.reference.cable node.connect @tree[node.reference.cable.other.ident] end end, "Template" => lambda do |node| # iface.interface.delegate.vlans.each do |i| # iface.connect tree[simple(i.clazz)][i.name] # end end, "Gre" => lambda do |node| # binding.pry interface = node.reference.delegate.local.interface node.connect @tree[interface.ident] end, "Opvn" => lambda do |node| end, "Ipsec" => lambda do |node| [node.reference.left, node.reference.right].each do |iface| binding.pry unless @tree[iface.interface.ident] node.connect @tree[iface.interface.ident] end end, "Bgp" => lambda do |node| #binding.pry [node.reference.left, node.reference.right].each do |iface| node.connect @tree[iface.my.ident] end end, "Host" => lambda do |node| node.reference.interfaces.values.each do |iface| next if simple(iface.class) == "Vrrp" #Construqt.logger.debug "Planuml:Host:#{iface.name}:#{iface.ident}:#{simple(iface.class)}" node.connect @tree[iface.ident] end end }[simple(node.reference.class)].call(node) end end |
.call(type, *args) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 296 def self.call(type, *args) add_node_factory(type, *args) factory = { #X "host.commit" => lambda do |type, host, *args| #X #binding.pry #X # vrrp -> bridge -> vlan -> bond -> device #X # vrrp1 #X # vlan1 vlan2 #X # bond #X # device0 device1 #X # #X out = [] #X #X out << <<UML #X package "#{host.name}" { #X UML #X tree = {} #X host.interfaces.each do |k,v| #X key = simple(v.clazz) #X tree[key] ||= {} #X ident = "#{clean_name(host.name)}_#{key}_#{clean_name(v.name)}" #X tree[key][v.name] = Node.new(key, ident, v) #X out << <<UML #X object #{ident} { #X #{render_object_address(v)} #X } #X UML #X end #X #X tree.each do |k,ifaces| #X # puts "K=>#{k}" #X ifaces.each do |name, iface| #X # binding.pry if k == 'Bond' #X { #X "Vrrp" => lambda do |iface| #X interface = iface.interface.delegate.interface #X iface.connect tree[simple(interface.clazz)][interface.name] #X end, #X "Vlan" => lambda do |iface| #X iface.interface.interfaces.each do |vlan_iface| #X iface.connect tree[simple(vlan_iface.clazz)][vlan_iface.name] #X end #X end, #X "Bond" => lambda do |iface| #X iface.interface.delegate.interfaces.each do |i| #X #Construqt.logger.debug(">>>>>>>>>> BOND -> #{simple(i.clazz)} #{i.name}") #X iface.connect tree[simple(i.clazz)][i.name] #X end #X end, #X "Bridge" => lambda do |iface| #X iface.interface.delegate.interfaces.each do |i| #X #binding.pry #X iface.connect tree[simple(i.clazz)][i.name] #X end #X end, #X "Device" => lambda do |iface| #X end, #X "Template" => lambda do |iface| #X # iface.interface.delegate.vlans.each do |i| #X # iface.connect tree[simple(i.clazz)][i.name] #X # end #X end, #X "Gre" => lambda do |iface| #X interface = iface.interface.delegate.local.interface #X # puts ">>>>>>GRE #{interface.host.name} #{interface.name}" #X iface.connect tree[simple(interface.clazz)][interface.name] #X end, #X "Opvn" => lambda do |iface| #X end #X }[k].call(iface) #X end #X end #X #X #render_matrixs = [] #X #[i"Vrrp","Vlan", "Bridge", "Bond", "Device"].each do |clazz| #X tree.keys.each do |clazz| #X (tree[clazz]||{}).values.each do |node| #X next unless node.in_links.empty? #X next if node.drawed? #X draw(node, out, [node.reference.name]) #X end #X end #X out << <<UML #X } #X UML "completed" => lambda do |type, *args| build_tree out = [] # @tree.values.each do |node| #binding.pry if node.reference.name == "s2b-l3-m2" # out << <<UML #object #{node.ident} { # #{render_object_address(node.reference)} #} #UML # end @tree.values.each do |node| # next unless node.in_links.empty? draw(node, out, [node.reference.name], ['Vrrp', 'Ipsec', 'Bgp'].include?(simple(node.reference.class))) end @tree.values.each { |n| n.drawed = false } @tree.values.each do |node| # next unless node.in_links.empty? connect(node, out, [node.reference.name]) end File.open("cfgs/world.puml", 'w') do |file| file.puts(<<UML) @startuml skinparam object { ArrowColor<<Gre>> MediumOrchid BackgroundColor<<Gre>> MediumOrchid ArrowColor<<Bgp>> MediumSeaGreen BackgroundColor<<Bgp>> MediumSeaGreen ArrowColor<<Ipsec>> LightSkyBlue BackgroundColor<<Ipsec>> LightSkyBlue ArrowColor<<Vrrp>> OrangeRed BackgroundColor<<Vrrp>> OrangeRed ArrowColor<<Device>> YellowGreen BackgroundColor<<Device>> YellowGreen ArrowColor<<Bond>> Orange BackgroundColor<<Bond>> Orange ArrowColor<<Vlan>> Yellow BackgroundColor<<Vlan>> Yellow ArrowColor<<Bridge>> Pink BackgroundColor<<Bridge>> Pink } skinparam stereotypeBackgroundColor<<Gre>> MediumOrchid skinparam stereotypeBackgroundColor<<Bgp>> MediumSeaGreen skinparam stereotypeBackgroundColor<<Ipsec>> LightSkyBlue skinparam stereotypeBackgroundColor<<Vrrp>> OrangeRed skinparam stereotypeBackgroundColor<<Device>> YellowGreen skinparam stereotypeBackgroundColor<<Bond>> Orange skinparam stereotypeBackgroundColor<<Vlan>> Yellow skinparam stereotypeBackgroundColor<<Bridge>> Pink UML file.write(out.join("\n") + "\n") file.puts("@enduml") end if File.exists?("/cygdrive/c/Program Files/cygwin/bin/dot.exe") dot = "C:\\Program Files\\cygwin\\bin\\dot.exe" elsif File.exists?("/usr/bin/dot") dot = "/usr/bin/dot" else dot = "$(which dot)" end if File.exists?("#{ENV['HOMEPATH']}/Downloads/plantuml.jar") plantuml_jar = "#{ENV['HOMEPATH']}/Downloads/plantuml.jar" else plantuml_jar = "$HOME/Downloads/plantuml.jar" end system("java -jar \"#{plantuml_jar}\" -Djava.awt.headless=true -graphvizdot \"#{dot}\" -tsvg cfgs/world.puml") end } Construqt.logger.debug "Planuml:#{type}" action = factory[type] if action action.call(type, *args) end end |
.clean_name(name) ⇒ Object
152 153 154 155 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 152 def self.clean_name(name) #name = name.gsub(/\s+/, '_') name.gsub(/[^0-9a-zA-Z_]/, '_') end |
.connect(node, out, path) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 73 def self.connect(node, out, path) return if node.drawed! # Construqt.logger.debug("planuml.draw:#{node.reference.name} #{node.ident} ") node.out_links.each do |n| # Construqt.logger.debug("planuml.draw:Out:#{node.reference.name} #{node.ident}:#{n.ident}") unless simple(node.reference.class) == "Host" out << "#{node.ident} .. #{n.ident}" end connect(n, out, path + [n.reference.name]) end end |
.draw(node, out, path, flat) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 91 def self.draw(node, out, path, flat) return if node.drawed! n_kind = simple(node.reference.class) if n_kind == "Host" out << ident(path, "package \"#{node.ident}\" <<Node>> #DDDDDD {") else out << ident(path, <<UML) object #{node.ident} <<#{n_kind}>> { #{render_object_address(node.reference)} } UML end !flat && n_kind != 'Device' && node.out_links.each do |n| draw(n, out, path + [n.reference.name], flat) end if n_kind == "Host" out << ident(path, "}") end end |
.ident(path, content) ⇒ Object
86 87 88 89 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 86 def self.ident(path, content) ident = (0...path.length-1).to_a.map{ " " }.join('') content.lines.map{|i| ident+i }.join('') end |
.name ⇒ Object
6 7 8 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 6 def self.name 'plantuml' end |
.render_object_address(iface) ⇒ Object
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 146 147 148 149 150 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 113 def self.render_object_address(iface) = [] out = [] out << "name = \"#{iface.name}\"" out << "desc = \"#{iface.description}\"" if iface.description if iface.address [iface.address.v4s, iface.address.v6s].each do |ips| next unless ips.first prefix = ips.first.ipv4? ? "ipv4" : "ipv6" ips.each_with_index do |ip, idx| += Construqt::Tags.from(ip)||[] out << "#{prefix}(#{idx}) = #{ip.to_string}" end end if iface.address.dhcpv4? out << "dhcpv4 = client" end if iface.address.dhcpv6? out << "dhcpv6 = client" end iface.address.routes.each_with_index do |route, idx| out << "route(#{idx}) = \"#{route.dst.to_string} via #{route.via.to_s}\"" end end iface.delegate.firewalls && iface.delegate.firewalls.each_with_index do |fw, idx| out << "fw(#{idx}) = \"#{fw.name}\"" end (iface.+).sort.uniq.each_with_index do |tag, idx| out << "tag(#{idx}) = \"#{tag}\"" end out.join("\n") end |
.simple(clazz) ⇒ Object
11 12 13 14 |
# File 'lib/construqt/flavour/plantuml/plantuml.rb', line 11 def self.simple(clazz) # clazz clazz.name[clazz.name.rindex(':')+1..-1].gsub(/Delegate$/,'') end |