-
Notifications
You must be signed in to change notification settings - Fork 11
/
SANMAP.py
185 lines (156 loc) · 4.15 KB
/
SANMAP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
146
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
179
180
181
182
183
184
185
#!/usr/bin/env python
# Author Dario Clavijo 2016
# GPLv3
import graphviz as gv
import functools
# creamos algunos estilos
styles = {
"graph": {
"label": "FC SAN MAP",
"fontsize": "16",
"fontcolor": "white",
"bgcolor": "#333333",
"rankdir": "BT",
},
"nodes": {
"fontname": "Helvetica",
"shape": "hexagon",
"fontcolor": "white",
"color": "white",
"style": "filled",
"fillcolor": "#006699",
},
"edges": {
"style": "dashed",
"color": "white",
"arrowhead": "open",
"fontname": "Courier",
"fontsize": "12",
"fontcolor": "white",
},
}
# funcion de ayuda para crear nodos
def add_nodes(graph, nodes):
for n in nodes:
if isinstance(n, tuple):
graph.node(n[0], **n[1])
else:
graph.node(n)
return graph
# funcion de ayuda para cread conexiones
def add_edges(graph, edges):
for e in edges:
if isinstance(e[0], tuple):
graph.edge(*e[0], **e[1])
else:
graph.edge(*e)
return graph
# funcion de ayuda para aplicar estilos
def apply_styles(graph, styles):
graph.graph_attr.update(("graph" in styles and styles["graph"]) or {})
graph.node_attr.update(("nodes" in styles and styles["nodes"]) or {})
graph.edge_attr.update(("edges" in styles and styles["edges"]) or {})
return graph
# funcion de ayuda para crear un enlace con label y color
def link(A, B, label, color):
return (
(A, B),
{
"label": label,
"style": "dashed",
"color": color,
"arrowhead": "open",
"fontname": "Courier",
"fontsize": "12",
"fontcolor": "white",
},
)
# funcion de ayuda para crear un switch
def addsw(name):
return (
name,
{
"fontname": "Helvetica",
"shape": "hexagon",
"fontcolor": "white",
"color": "white",
"style": "filled",
"fillcolor": "#006699",
},
)
# funcion de ayuda para crear un storage
def addstg(name):
return (
name,
{
"fontname": "Helvetica",
"shape": "cylinder",
"fontcolor": "white",
"color": "white",
"style": "filled",
"fillcolor": "#660000",
},
)
# funcion de ayuda para crear un host
def addhost(name):
return (
name,
{
"fontname": "Helvetica",
"shape": "square",
"fontcolor": "white",
"color": "white",
"style": "filled",
"fillcolor": "#0000FF",
},
)
# arreglo de switches
switches = [
addsw("switchcore1"),
addsw("switchcore2"),
addsw("switch1"),
addsw("switch2"),
]
# arreglo de storages
storages = [
addstg("stg1"),
addstg("stg2"),
]
# arreglo de hosts
hosts = [
addhost("VMWHOSTX"),
]
# arreglo general
nodes = switches + storages + hosts
# arreglo de conexiones entre switches
switches = [
link("switchcore1", "switch1", "isl", "blue"),
link("switchcore2", "switch2", "isl", "blue"),
link("switchcore1", "switch1", "isl", "blue"),
link("switchcore2", "switch2", "isl", "blue"),
]
# arreglo de conexiones entre storages y switches
storages = [
link("stg1", "switchcore1", "", "red"),
link("stg1", "switchcore2", "", "red"),
link("stg2", "switchcore1", "", "red"),
link("stg2", "switchcore2", "", "red"),
]
# arreglo de conexiones entre hosts y switches
hosts = [
link("VMWHOSTX", "switch1", "", "white"),
link("VMWHOSTX", "switch2", "", "white"),
]
# arreglo general
links = switches + storages + hosts
def make_graph(nodes, edges, filename, format):
# inicializamos la lib
graph = functools.partial(gv.Graph, format=format)
digraph = functools.partial(gv.Digraph, format=format)
# parseo y renderizado del mapa
g1 = add_edges(add_nodes(digraph(), nodes), edges)
g1 = apply_styles(g1, styles)
g1.render(filename)
# renderizamos en formato png y svg
make_graph(nodes, links, "img/fcsw", "svg")
make_graph(nodes, links, "img/fcsw", "png")