xentaur
view xentaur.py @ 58:58381d1fe31b
domains_brief support
author | igor |
---|---|
date | Sun Nov 11 19:22:52 2007 +0200 (2007-11-11) |
parents | ec48cf129028 |
children | e7b9761c03e8 |
line source
1 #!/usr/bin/python
4 import sys,os,time
6 xentaur_path=os.environ['HOME']+"/xentaur"
8 sys.path.append('/etc/xen')
9 sys.path.append(xentaur_path)
11 #network='snrs_ipsec_rsa_1'
12 node_object={}
13 link_object={}
14 bridge_object={}
16 network='icnd2'
17 domain='sw1'
18 from xendomain import *
20 bridges_turned_down=[]
22 from IPython.Shell import IPShellEmbed
25 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
27 def run(program, *args):
28 pid = os.fork()
29 if not pid:
30 os.execvp(program, (program,) + args)
31 return os.wait()[0]
33 def run_command(line):
34 #cmds=line.split()
35 #run(cmds[0],*cmds[1:])
36 run("/bin/sh", "-c", line)
38 def run_command_return_stdout(line):
39 p = os.popen(line)
40 output = p.read()
41 p.close()
42 return output
44 ################################################################################
45 #Xentaur command-line commands
47 ## Start
49 def start_bridges():
50 unbound_bridges=set(bridges)-set(real_bridges)
51 script=""
52 script="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges))
53 script+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges))
54 script+="\n"+"\n".join(map(lambda x: "sudo ip link set "+x+" up", unbound_bridges))
56 print """#!/bin/sh
57 # create unbound bridges
58 %s
59 """ % (script)
61 def start_domain(domain):
62 print "sudo xm create "+xentaur_path+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo xm sched-credit -d $(sudo xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
64 def start_domains(doms=domains):
65 for domain in doms:
66 if not domain in real_nodes:
67 start_domain(domain)
69 def start_all():
70 graph()
71 screen()
72 start_bridges()
73 start_domains()
75 ## Stop
77 def stop_domain(domain,wait=0):
78 if wait:
79 print "sudo xm shutdown -w "+domain
80 else:
81 print "sudo xm shutdown "+domain
83 def stop_domains(doms=domains, wait=0):
84 for domain in doms:
85 if not domain in real_nodes:
86 stop_domain(domain,wait)
88 def stop_bridges():
89 ###FIXME###
90 return 0
92 def stop_all(wait=0):
93 stop_domains(domains, wait)
94 stop_bridges()
96 def restart_all():
97 stop_all(1)
98 start_all()
100 ####################################################
102 def create_objects():
103 create_node_objects()
104 create_bridge_objects()
105 create_link_objects()
107 def create_node_objects():
108 for dom in domains:
109 node_object[dom]=Node(dom)
111 def create_bridge_objects():
112 for bridge in bridges:
113 bridge_object[bridge]=Bridge(bridge)
115 def create_link_objects():
117 for node, bridges_raw in vbridges_table.iteritems():
118 interface=0
119 j=0
120 for this_bridge in bridges_raw:
121 int_label=""
122 if this_bridge.find(':') != -1:
123 res = this_bridge.split(':')
124 this_bridge= res[0]
125 bridges_raw[j] = this_bridge
126 int_label = res[1]
127 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
128 name="%s %s %s" % (node,interface,this_bridge)
129 link_object[name]=Link(name,node,interface,this_bridge,int_label)
130 interface+=1
131 vbridges_table[node]=bridges_raw
133 for node, bridges_raw in bridge_bridge_table.iteritems():
134 interface=0
135 j=0
136 for this_bridge in bridges_raw:
137 int_label=""
138 if this_bridge.find(':') != -1:
139 res = this_bridge.split(':')
140 this_bridge= res[0]
141 bridges_raw[j] = this_bridge
142 int_label = res[1]
143 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
144 name="%s %s %s" % (node,interface,this_bridge)
145 link_object[name]=Link(name,node,interface,this_bridge,int_label)
146 interface+=1
147 bridge_bridge_table[node]=bridges_raw
149 for node,interface,bridge in temporary_links:
150 name="%s %s %s" % (node,interface,bridge)
151 link_object[name]=Link(name,node,interface,this_bridge)
153 for node,interface,bridge in broken_links:
154 name="%s %s %s" % (node,interface,bridge)
155 link_object[name]=Link(name,node,interface,this_bridge)
158 ####################################################
160 def screen():
161 wait_seconds=5
162 screens=[]
163 for domain in domains:
164 screens.append("screen -t %s %s sh -c 'while true; do %s ; echo Retrying in %s secods...; sleep %s ; clear; done'" %
165 (domain,domains.index(domain),node_object[domain].console_string(),wait_seconds,wait_seconds))
166 screenlist="\n".join(screens)
168 hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
170 f=open(screenrc, "w");
171 f.write("""
172 hardstatus on
173 hardstatus alwayslastline
174 %s
176 screen -t console 0 bash
177 %s
178 """ % (hardstatus,screenlist))
179 f.close()
180 print "# GNU Screen config file is written to: %s" % screenrc
182 def graph():
183 nodelist=""
184 bridgelist=""
185 linklist=""
186 physicallist=""
187 networklist=""
189 nodelist=";\n ".join(map(lambda node: node_object[node].graphviz_string(),nodes))
190 if nodelist: nodelist += ";"
191 bridgelist=";\n ".join(map(lambda bridge: bridge_object[bridge].graphviz_string(),bridges))
192 if bridgelist: bridgelist += ";"
193 linklist=";\n ".join(map(lambda link: link_object[link].graphviz_string(),link_object.keys()))
194 if linklist: linklist += ";"
196 f = open(network+".dot", "w");
197 f.write ("""
198 graph G {
199 edge [len=1.25];
200 splines=true;
201 // nodes
202 // node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
203 %s
205 // bridges
206 // node [shape=none,shapefile="shapes/all/switch.png"];
207 %s
209 // physical
210 node [shape=rectangle,color=blue];
211 %s
213 // networks (not bridges, not physical)
214 node [shape=rectangle,color=green];
215 %s
217 // links (between nodes and bridges)
218 %s
220 };
221 """ % (nodelist, bridgelist, physicallist, networklist, linklist))
222 f.close()
223 run_command("neato -Tpng -o %s.png %s.dot "%(network,network))
224 run_command("neato -Tjpg -o %s.jpg %s.dot "%(network,network))
225 run_command("neato -Tsvg -o %s.svg %s.dot "%(network,network))
226 run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(network,network))
227 print "# Network map is written to files: %s.{png,svg,jpg,dot}" % network
229 def autoredraw():
230 graph()
232 def shell():
233 autoredraw()
234 ipshell = IPShellEmbed()
235 ipshell()
237 def show_usage():
238 print """Usage:
239 xentaur <command> [<argument>]
241 Commands:
242 start-all -- start bridges and domains
243 start-domains -- start domains only
244 start-bridges -- start bridges only
245 stop-all -- stop bridges and domains
246 stop-domains -- stop domains only
247 stop-bridges -- stop bridges only (domains have to be stopped already)
248 restart-all -- restart bridges and domains
250 start <domain> -- start the <domain>
251 stop <domain> -- stop the <domain>
253 graph -- generate network scheme (result is in <network>.{png,jpg,svg})
254 screen -- generate GNU Screen config file (~/.screenrc_xentaur)
255 shell -- run Xentaur shell
257 """
259 def save():
260 print "network =", xen_config_name
261 print "domains =", domains
262 print "domain_types =", domain_types
263 print "bridges =", bridges
264 print "vbridges_table =", vbridges_table
265 print "hidden_bridges =", hidden_bridges
266 print "broken_links =", broken_links
267 print "temporary_links =", temporary_links
268 print "bridges_turned_down =", bridges_turned_down
270 #-----------------------------------------------------------------------
271 # CLASSES
273 class Bridge:
274 def __init__ (self,name):
275 self.name=name
276 def up(self):
277 bridge_up(self.name)
278 def down(self):
279 bridge_down(self.name)
280 def show(self):
281 show_bridge(self.name)
282 def dump_start(self,filter=""):
283 dump_start(self.name,filter)
285 def is_hidden(self):
286 return self.name in hidden_bridges
287 def is_real(self):
288 return self.name in real_bridges
289 def is_turned_down(self):
290 return self.name in bridges_turned_down
291 def is_cross(self):
292 return self.name in cross_bridges
294 def graphviz_string(self):
295 if self.is_hidden():
296 return ""
297 elif self.is_cross():
298 return "%s [shape=circle,height=0.03,color=black,fillcolor=black,style=filled,label=\"\"]" % (self.name)
299 elif self.is_real():
300 return "%s [color=white,shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (self.name)
301 elif self.is_turned_down():
302 return "%s [color=white,shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (self.name)
303 else:
304 return "%s [color=white,shape=none,shapefile=\"shapes/all/switch.png\"]" % (self.name)
307 class Node:
308 def __init__ (self,name):
309 self.name=name
310 self.type=domain_types[domains.index(name)]
311 def start(self):
312 return ""
313 def stop(self):
314 return ""
315 def start_commandline(self):
316 return ""
317 def get_domain_id(self):
318 return get_domain_id(self.name)
319 def graphviz_string(self):
320 return self.name+" [color=white,shape=plaintext,label=\" "+self.name+"\",shapefile=\"shapes/all/"+\
321 domain_types[domains.index(self.name)]+".png\",fontcolor=black,fontsize=16,target=\"http://google.com\"]"
322 def console_string(self):
323 if self.type == 'quagga' or self.type == 'xenomips':
324 return "sudo xm console "+self.name
325 elif self.name in real_bridges or self.name in real_nodes:
326 return "echo Press enter to connect; read line; "+connection_table[self.name]
329 class Link:
330 def __init__ (self,name,node,interface,bridge,label=""):
331 self.name=name
332 self.node=node
333 self.interface=interface
334 self.bridge=bridge
335 self.label=label
337 def is_temporary(self):
338 return [self.node,self.interface,self.bridge] in temporary_links
340 def is_broken(self):
341 return ([self.node,self.interface,self.bridge] in broken_links)
343 def graphviz_string(self):
344 if self.is_temporary():
345 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",color=blue,len=10,w=5,weight=5]"
346 if self.is_broken():
347 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",style=dashed]"
349 ip="\\n.%s.%s" % (bridges.index(self.bridge)+1, domains.index(self.node)+1)
350 if domain_types[domains.index(self.node)] == 'xenomips':
351 int_name="fa"+str(self.interface)+"/0"
352 else:
353 int_name="eth"+str(self.interface)
354 if self.label != "":
355 int_name = self.label
356 return self.node+" -- "+self.bridge+" [taillabel=\""+int_name+ip+"\",fontsize=14,fontname=fixed]"
359 #-----------------------------------------------------------------------
360 # DOMAINS
362 def get_domain_id(domain):
363 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
366 #-----------------------------------------------------------------------
367 # BRIDGES and IFACES
369 def bridge_down(bridge):
370 """
371 Turn the bridge <bridge> down
372 """
373 if bridge in real_bridges:
374 print "Bridge %s is a real bridge" % (bridge)
375 return -1
376 if bridge in bridges_turned_down:
377 print "Bridge %s is turned down already" % (bridge)
378 else:
379 bridges_turned_down.append(bridge)
380 run_command("sudo ip link set %s down" % bridge)
381 autoredraw()
383 def bridge_up(bridge):
384 """
385 Turn the bridge <bridge> up
386 """
387 if bridge in real_bridges:
388 print "Bridge %s is a real bridge" % (bridge)
389 return -1
390 if not (bridge in bridges_turned_down):
391 print "Bridge %s is turned up already" % (bridge)
392 else:
393 bridges_turned_down.remove(bridge)
394 run_command("sudo ip link set %s up" % bridge)
395 autoredraw()
397 def show_bridge(bridge):
398 """
399 Show the state of the bridge <bridge>
400 """
401 if bridge in real_bridges:
402 print "Bridge %s is a real bridge" % (bridge)
403 return -1
404 run_command("sudo ip link show %s" % bridge)
407 def int_disconnect(domain, int_number):
408 """
409 Disconnect the interface with the number <int_number>
410 of the domain <domain> from the bridge to which
411 it is connected
412 """
413 dom_id=get_domain_id(domain)
414 bridge=vbridges_table[domain][int_number]
415 if not bridge:
416 print "Interface %s of the %s domain is not connected" % (int_number, domain)
417 return 1
418 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
419 vbridges_table[domain][int_number]=''
420 if [ domain, int_number, bridge ] in temporary_links:
421 temporary_links.remove([ domain, int_number, bridge ])
422 else:
423 broken_links.append([ domain, int_number, bridge ])
424 autoredraw()
426 def int_connect(domain, int_number, bridge):
427 """
428 Connect the interface with the number <int_number>
429 of the domain <domain> to the bridge <bridge>
430 """
431 if bridge in real_bridges:
432 print "Bridge %s is a real bridge" % (bridge)
433 return -1
435 dom_id=get_domain_id(domain)
436 if vbridges_table[domain][int_number]:
437 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
438 return 1
439 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
440 vbridges_table[domain][int_number]=bridge
441 if [ domain, int_number, bridge ] in broken_links:
442 broken_links.remove([ domain, int_number, bridge ])
443 else:
444 temporary_links.append([ domain, int_number, bridge ])
445 autoredraw()
447 def int_reconnect(domain, int_number, bridge):
448 """
449 Reconnect the interface with the number <int_number>
450 of the domain <domain> from the bridge to which
451 it is connected to the bridge <bridge>
452 """
453 if bridge in real_bridges:
454 print "Bridge %s is a real bridge" % (bridge)
455 return -1
457 int_disconnect(domain, int_number)
458 int_connect(domain, int_number, bridge)
460 def show_int(domain, int_number):
461 """
462 Show information about the interface <int_nuber>
463 of the domain <domain>
464 """
465 return vbridges_table[domain][int_number]
468 def dump_start(bridge, filter=""):
469 if bridge in real_bridges:
470 print "Bridge %s is a real bridge" % (bridge)
471 return -1
472 try:
473 print "Writing dump... (press Ctrl-C to stop)"
474 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
475 except:
476 print "Done.\n Dump is written to xentaur.dump"
477 return 0
479 def dump_stop():
480 return 0
483 #-----------------------------------------------------------------------
484 # CONFIGURATION TEMPLATES
487 def configure_ip_addresses(doms=domains):
489 cisco_set_ip_on_int="""
490 \n\n\n
491 int fa%s/0
492 no ip address
493 ip address %s 255.255.255.0
494 no shutdown
495 exit
496 """
498 quagga_set_ip_on_int="""
499 int eth%s
500 no ip address
501 ip address %s/24
502 no shutdown
503 exit
504 """
506 for dom in doms:
507 i=domains.index(dom)+1
508 if domain_types[domains.index(dom)] == 'quagga':
509 command = quagga_set_ip_on_int
510 write_to(i,"\nconf t\n")
511 j=0
512 for br in vbridges_table[dom]:
513 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
514 j+=1
515 write_to(i,"\nend\n")
516 else:
517 command = cisco_set_ip_on_int
518 write_to(i,"\nena\nconf t\n")
519 j=0
520 for br in vbridges_table[dom]:
521 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
522 j+=1
523 write_to(i,"\nend\n")
524 return 0
526 def configure_no_ip_addresses(doms=domains):
528 cisco_set_ip_on_int="""
529 \n\n\n
530 int fa%s/0
531 no ip address %s 255.255.255.0
532 exit
533 """
535 quagga_set_ip_on_int="""
536 int eth%s
537 no ip address %s/24
538 exit
539 """
541 for dom in doms:
542 i=domains.index(dom)+1
543 if domain_types[domains.index(dom)] == 'quagga':
544 command = quagga_set_ip_on_int
545 write_to(i,"\nconf t\n")
546 j=0
547 for br in vbridges_table[dom]:
548 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
549 j+=1
550 write_to(i,"\nend\n")
551 else:
552 command = cisco_set_ip_on_int
553 write_to(i,"\nena\nconf t\n")
554 j=0
555 for br in vbridges_table[dom]:
556 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
557 j+=1
558 write_to(i,"\nend\n")
559 return 0
561 def configure_ospf(doms=domains):
562 for dom in doms:
563 if domain_types[domains.index(dom)] == 'quagga':
564 write_to(dom,"\n\nconf t\nrouter ospf\nnetwork 192.168.0.0/16 area 0\nend\n")
565 else:
566 write_to(dom,"\n\nena\nconf t\nrouter ospf 1\nnetwork 192.168.0.0 0.0.255.255 area 0\nend\n")
567 return 0
569 def configure_hostname(doms=domains):
570 for dom in doms:
571 if domain_types[domains.index(dom)] == 'quagga':
572 write_to(dom,"\n\nconf t\nhostname %s\nend\n" % dom)
573 else:
574 write_to(dom,"\n\nena\nconf t\nhostname %s\nend\n" % dom)
575 return 0
577 def configure_logging_synchronous(doms=domains):
578 for dom in domains:
579 if domain_types[domains.index(dom)] == 'quagga':
580 0
581 else:
582 write_to(dom,"\n\nena\nconf t\nline console 0\nlogging synchronous\nend\n")
583 return 0
585 def configure_exec_timeout_0(doms=domains):
586 for dom in domains:
587 if domain_types[domains.index(dom)] == 'quagga':
588 0
589 else:
590 write_to(dom,"\n\nena\nconf t\nline console 0\nexec-timeout 0\nend\n")
591 return 0
593 def configure_no_cdp_log_mismatch_duplex(doms=domains):
594 for dom in filter_by_type(domains,'xenomips'):
595 write_to(dom,"\n\nena\nconf t\nno cdp log mismatch duplex\nend\n")
597 def configure_save(doms=domains):
598 write_to(doms,"\nwr\n")
600 def configure_root(doms=domains):
601 write_to(doms,"root\n")
603 #-----------------------------------------------------------------------
606 def add_domain(name,type):
607 domains.append(name)
608 domain_types.append(type)
610 def brake_link(domain,bridge):
611 broken_links.append([domain,bridge])
613 wt_timeout=0.5
614 def write_to(screen,string,return_to_screen=""):
615 """
616 write_to(screen,string):
618 Type *string* to the specified screen(s).
619 Screen may be specified with the number *screen*,
620 with array of numbers,
621 with array of names.
623 """
624 screen_numbers=[] # number of the screens to write to
625 if type(screen) == list:
626 screen_numbers=map(lambda x: domains.index(x)+1, screen)
627 elif type(screen) == int:
628 screen_numbers=[screen]
629 else:
630 screen_numbers=[domains.index(screen)+1]
632 for screen_number in screen_numbers:
633 run_command("screen -X select "+str(screen_number))
634 time.sleep(wt_timeout)
635 for line in string.splitlines():
636 f=open('/tmp/xentaurbuf', 'w')
637 f.write(line+"\n")
638 f.close()
639 run_command("screen -X readreg p /tmp/xentaurbuf")
640 time.sleep(wt_timeout)
641 run_command("nohup screen -X paste p >& /dev/null")
642 time.sleep(wt_timeout)
644 if return_to_screen != "":
645 run_command("screen -X select %s" % (return_to_screen))
646 time.sleep(wt_timeout)
648 def filter_by_type(doms,type):
649 """
650 filter_by_type(doms,type)
652 Return only domains of *doms* that have specified *type*
653 """
654 return filter(lambda x: domain_types[domains.index(x)]==type,domains)
656 #-----------------------------------------------------------------------
658 cisco_fa01_up="""
659 ena
660 conf t
661 int fa0/0
662 duplex half
663 no shutdown
664 exit
665 int fa1/0
666 duplex half
667 no shutdown
668 exit
669 exit
670 exit
671 """
673 cisco_set_ip_on_int="""
674 interface fa%s/0
675 no ip address
676 ip address %s 255.255.255.0
677 exit
678 """
680 nodes=domains
682 create_objects()
685 if len(sys.argv) == 2:
686 if sys.argv[1] == 'start-all':
687 start_all()
688 elif sys.argv[1] == 'start-domains':
689 start_domains()
690 elif sys.argv[1] == 'start-bridges':
691 start_bridges()
692 elif sys.argv[1] == 'stop-all':
693 stop_all()
694 elif sys.argv[1] == 'stop-domains':
695 stop_domains()
696 elif sys.argv[1] == 'stop-bridges':
697 stop_bridges()
698 elif sys.argv[1] == 'restart-all':
699 restart_all()
700 elif sys.argv[1] == 'screen':
701 screen()
702 elif sys.argv[1] == 'graph':
703 graph()
704 elif sys.argv[1] == 'shell':
705 shell()
706 else:
707 show_usage()
708 sys.exit(1)
709 elif len(sys.argv) == 3:
710 if sys.argv[1] == 'start':
711 start_domain(sys.argv[2])
712 elif sys.argv[1] == 'stop':
713 stop_domain(sys.argv[2])
714 elif sys.argv[1] == 'restart':
715 stop_domain(sys.argv[2])
716 start_domain(sys.argv[2])
717 else:
718 show_usage()
719 sys.exit(1)
720 else:
721 show_usage()
722 sys.exit(1)
724 sys.exit(0)