xentaur
view xentaur.py @ 68:f652fab38c7a
ec2 parameters small fixes
author | Igor Chubin <igor@chub.in> |
---|---|
date | Mon Jan 11 19:36:56 2010 +0200 (2010-01-11) |
parents | 6c145935ece5 |
children |
line source
1 #!/usr/bin/python
2 # vim: set fileencoding=utf-8 :
4 import sys,os,time
5 from IPython.Shell import IPShellEmbed
7 path_xentaur="%s/hg/xentaur" % os.environ['HOME']
8 path_shapes=path_xentaur+'/shapes'
9 path_scripts=path_xentaur+'/files'
11 network='net1'
13 path_network=os.environ['HOME']+"/.xentaur/"+network
14 if not os.path.exists(path_network):
15 os.makedirs(path_network)
16 screenrc=path_network+"/.screenrc_xentaur"
18 sys.path.append('/etc/xen')
19 sys.path.append(path_xentaur)
20 sys.path.append(path_network)
21 sys.path.append('.')
24 node_object={}
25 link_object={}
26 bridge_object={}
27 ec2_node={}
29 # ec2 settings
30 ec2_dns_domain='ec2.xgu.ru'
31 ec2_zone="eu-west-1b"
32 ec2_volume_name="vol-86d234ef"
33 ec2_ssh_secret_key="~/.ec2/id_rsa-eu-keypair"
34 ec2_ssh_keypair="eu-keypair"
35 ec2_ami="ami-a62a01d2"
37 domain='dyn1'
38 from xendomain import *
40 execfile(path_xentaur+"/configuration_templates.py")
42 bridges_turned_down=[]
44 def run(program, *args):
45 pid = os.fork()
46 if not pid:
47 os.execvp(program, (program,) + args)
48 return os.wait()[0]
50 def run_command(line):
51 #cmds=line.split()
52 #run(cmds[0],*cmds[1:])
53 run("/bin/sh", "-c", line)
55 def run_command_return_stdout(line):
56 p = os.popen(line)
57 output = p.read()
58 p.close()
59 return output
61 ################################################################################
62 #Xentaur command-line commands
64 ## Start
66 def start_bridges():
67 unbound_bridges=set(bridges)-set(real_bridges)
68 script=""
69 script="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges))
70 script+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges))
71 script+="\n"+"\n".join(map(lambda x: "sudo ip link set "+x+" up", unbound_bridges))
73 print """#!/bin/sh
74 # create unbound bridges
75 %s
76 """ % (script)
78 def start_domain(domain):
79 print "sudo xm create "+path_xentaur+"/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"
81 def start_domains(doms=domains):
82 for domain in doms:
83 if not domain in real_nodes:
84 start_domain(domain)
86 def start_all():
87 graph()
88 screen()
89 html()
90 start_bridges()
91 start_domains()
93 def ec2_assign_nodes_to_instances():
94 domain_number=0
95 for dom in domains:
96 node_name=network+"-node%s"%(domain_number/3)
97 ec2_node[dom]=node_name+"."+ec2_dns_domain
98 domain_number+=1
100 def make_start_emulators():
101 s=""
102 ios="/mnt/ios/C7200-AD.BIN"
103 ec2_assign_nodes_to_instances()
104 for dom in domains:
105 node_name=ec2_node[dom]
106 i=0
107 line='dynamips '+ios
108 line2=''
109 for iface in vbridges_table[dom]:
110 bridge=vbridges_table[dom][i]
111 if i>0:
112 line += " -p %s:PA-FE-TX "%i
113 line += " -s %s:0:tap:%s_%s "% (i,dom,iface)
114 line2 += "sleep 2; brctl addif %s %s; "%(bridge, dom+"_"+iface)
115 line2 +='ifconfig %s up; ifconfig %s promisc ;' %(bridge, bridge)
116 line2 += "ifconfig %s_%s up; "%(dom, iface)
117 i+=1
118 s += "ssh %s \"mkdir -p /mnt/%s; cd /mnt/%s; screen -S %s- -d -m %s\"\n"%(node_name,dom,dom,dom,line)
119 s += "ssh %s \"%s\"\n" % (node_name, line2)
120 return s
122 def make_start_instances_config(net=network):
123 f = open(path_network+"/start-instances-"+network, "w");
124 f.write("""
125 NETWORK=%(network)s
126 INSTANCES_NUMBER=%(instances_number)s
127 INSTANCE_AMI=%(ec2_ami)s
128 VOLUME_NAME=%(ec2_volume_name)s
129 SSH_SECRET_KEY=%(ec2_ssh_secret_key)s
130 SSH_KEYPAIR=%(ec2_ssh_keypair)s
131 EC2_ZONE=%(ec2_zone)s
132 DOMAIN=%(ec2_dns_domain)s
133 SCRIPTS_PATH=%(path_scripts)s
134 """ % {
135 "network" : network,
136 "instances_number" : len(set(ec2_node.values())),
137 "path_scripts" : path_scripts,
139 "ec2_dns_domain" : ec2_dns_domain,
140 "ec2_zone" : ec2_zone,
141 "ec2_ssh_secret_key": ec2_ssh_secret_key,
142 "ec2_ssh_keypair" : ec2_ssh_keypair,
143 "ec2_volume_name" : ec2_volume_name,
144 "ec2_ami" : ec2_ami,
145 })
147 f.write("start_emulators()\n{\n%s\n}\n"%make_start_emulators())
148 f.close()
150 def start_network(net=network):
151 make_start_instances_config()
152 run_command("cd %s; env NETWORK=%s sh %s start" % (path_network, network, path_scripts+"/ec2-instances"))
154 def stop_network(net=network):
155 run_command("cd %s; env NETWORK=%s sh %s stop" % (path_network, network, path_scripts+"/ec2-instances"))
157 ## Stop
159 def stop_domain(domain,wait=0):
160 if wait:
161 print "sudo xm shutdown -w "+domain
162 else:
163 print "sudo xm shutdown "+domain
165 def stop_domains(doms=domains, wait=0):
166 for domain in doms:
167 if not domain in real_nodes:
168 stop_domain(domain,wait)
170 def stop_bridges():
171 ###FIXME###
172 return 0
174 def stop_all(wait=0):
175 stop_domains(domains, wait)
176 stop_bridges()
178 def restart_all():
179 stop_all(1)
180 start_all()
182 ####################################################
184 def create_objects():
185 create_node_objects()
186 create_bridge_objects()
187 create_link_objects()
189 def create_node_objects():
190 for dom in domains:
191 node_object[dom]=Node(dom)
193 def create_bridge_objects():
194 for bridge in bridges:
195 bridge_object[bridge]=Bridge(bridge)
197 def create_link_objects():
199 for node, bridges_raw in vbridges_table.iteritems():
200 interface=0
201 j=0
202 for this_bridge in bridges_raw:
203 int_label=""
204 if this_bridge.find(':') != -1:
205 res = this_bridge.split(':')
206 this_bridge= res[0]
207 bridges_raw[j] = this_bridge
208 int_label = res[1]
209 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
210 name="%s %s %s" % (node,interface,this_bridge)
211 link_object[name]=Link(name,node,interface,this_bridge,int_label)
212 interface+=1
213 vbridges_table[node]=bridges_raw
215 for node, bridges_raw in bridge_bridge_table.iteritems():
216 interface=0
217 j=0
218 for this_bridge in bridges_raw:
219 int_label=""
220 if this_bridge.find(':') != -1:
221 res = this_bridge.split(':')
222 this_bridge= res[2]
223 bridges_raw[j] = this_bridge
224 int_label = res[1]
225 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
226 name="%s %s %s" % (node,interface,this_bridge)
227 link_object[name]=Link(name,node,interface,this_bridge,int_label)
228 interface+=1
229 bridge_bridge_table[node]=bridges_raw
231 for node,interface,bridge in temporary_links:
232 name="%s %s %s" % (node,interface,bridge)
233 link_object[name]=Link(name,node,interface,this_bridge)
235 for node,interface,bridge in broken_links:
236 name="%s %s %s" % (node,interface,bridge)
237 link_object[name]=Link(name,node,interface,this_bridge)
240 ####################################################
242 def screen():
243 wait_seconds=1
244 screens=[]
245 for domain in domains:
246 screens.append("screen -t %(domain)s %(domain_number)s %(console_string)s" % {
247 'domain' : domain,
248 'domain_number' : domains.index(domain)+1,
249 'console_string': node_object[domain].console_string(),
250 'wait_interval' : wait_seconds } )
252 screenlist="\n".join(screens)
254 hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
255 f=open(screenrc, "w");
256 f.write("""
257 hardstatus on
258 hardstatus alwayslastline
259 %s
261 screen -t console 0 sh -c 'while true; do %s/xentaur.py shell ; echo Retrying in %s secods...; sleep %s ; clear; done'
262 %s
263 """ % (hardstatus,path_xentaur,wait_seconds,wait_seconds,screenlist))
264 f.close()
265 print "# GNU Screen config file is written to: %s" % screenrc
267 def graph():
268 nodelist=""
269 bridgelist=""
270 linklist=""
271 physicallist=""
272 networklist=""
274 nodelist=";\n ".join(map(lambda node: node_object[node].graphviz_string(),nodes))
275 if nodelist: nodelist += ";"
276 bridgelist=";\n ".join(map(lambda bridge: bridge_object[bridge].graphviz_string(),bridges))
277 if bridgelist: bridgelist += ";"
278 linklist=";\n ".join(map(lambda link: link_object[link].graphviz_string(),link_object.keys()))
279 if linklist: linklist += ";"
281 f = open(path_network+"/"+network+".dot", "w");
282 f.write ("""
283 graph G {
284 edge [len=1.25];
285 splines=true;
286 // nodes
287 %s
289 // bridges
290 %s
292 // physical
293 node [shape=rectangle,color=blue];
294 %s
296 // networks (not bridges, not physical)
297 node [shape=rectangle,color=green];
298 %s
300 // links (between nodes and bridges)
301 %s
303 };
304 """ % (nodelist, bridgelist, physicallist, networklist, linklist))
305 f.close()
306 run_command("neato -Tpng -o %s.png %s.dot "%(path_network+"/"+network,path_network+"/"+network))
307 run_command("neato -Tjpg -o %s.jpg %s.dot "%(path_network+"/"+network,path_network+"/"+network))
308 run_command("neato -Tsvg -o %s.svg %s.dot "%(path_network+"/"+network,path_network+"/"+network))
309 #run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(path_network+"/"+network,path_network+"/"+network))
310 print "# Network map is written to files: %s.{png,svg,jpg,dot}" % (path_network+"/"+network)
312 def html():
313 f = open(path_network+"/index.html", "w");
314 f.write ("""
315 <html>
316 <head>
317 <title>Network %s map</network>
318 <body>
319 <img src="%s.png" /><br/>
320 <pre>
321 nodes=%s
322 bridges=%s
323 vbridges_table=%s
324 </pre>
325 </body>
326 </head>
327 </html>
328 """ % (network, network, domains, bridges, vbridges_table))
329 f.close()
330 #run_command("cp %s.html /var/www/ec2/network/%s/index.html"%(network,network))
332 def autoredraw():
333 graph()
334 html()
335 screen()
337 def shell():
338 autoredraw()
339 ipshell = IPShellEmbed(['-noconfirm_exit'])
340 ipshell()
342 def version():
343 print """
344 Xentaur 0.1-PRE
346 ,--,
347 _ ___/ /\\|
348 ,;`( )__, ) ~
349 // .// '--;
350 ' / \ |
352 """
354 def info():
355 version()
357 print "Network name: ", network
358 print "-----------------------------------------------"
359 print
360 print "Nodes: ", len(domains)
361 print " * virtual nodes: ", len(domains)-len(real_nodes)
362 print " * real nodes:", len(real_nodes)
363 print
364 print "Bridges:", len(bridges)
365 print " * virtual bridges:", len(bridges)-len(real_bridges)-len(cross_bridges)
366 print " * real switches:", len(real_bridges)
367 print " * direct links:", len(cross_bridges)
369 def show_usage():
370 print """Usage:
371 xentaur <command> [<argument>]
373 Commands:
374 start-all -- start bridges and domains
375 start-domains -- start domains only
376 start-bridges -- start bridges only
377 stop-all -- stop bridges and domains
378 stop-domains -- stop domains only
379 stop-bridges -- stop bridges only (domains have to be stopped already)
380 restart-all -- restart bridges and domains
382 start <domain> -- start the <domain>
383 stop <domain> -- stop the <domain>
385 graph -- generate network scheme (result is in <network>.{png,jpg,svg})
386 screen -- generate GNU Screen config file (~/.screenrc_xentaur)
387 shell -- run Xentaur shell
389 """
391 def save():
392 print "network =", xen_config_name
393 print "domains =", domains
394 print "domain_types =", domain_types
395 print "bridges =", bridges
396 print "vbridges_table =", vbridges_table
397 print "hidden_bridges =", hidden_bridges
398 print "broken_links =", broken_links
399 print "temporary_links =", temporary_links
400 print "bridges_turned_down =", bridges_turned_down
402 #-----------------------------------------------------------------------
403 # CLASSES
405 class Bridge:
406 def __init__ (self,name):
407 self.name=name
408 def up(self):
409 bridge_up(self.name)
410 def down(self):
411 bridge_down(self.name)
412 def show(self):
413 show_bridge(self.name)
414 def dump_start(self,filter=""):
415 dump_start(self.name,filter)
417 def is_hidden(self):
418 return self.name in hidden_bridges
419 def is_real(self):
420 return self.name in real_bridges
421 def is_turned_down(self):
422 return self.name in bridges_turned_down
423 def is_cross(self):
424 return self.name in cross_bridges
426 def graphviz_string(self):
427 if self.is_hidden():
428 return "//"
429 elif self.is_cross():
430 return "%s [shape=circle,height=0.03,color=black,fillcolor=black,style=filled,label=\"\"]" % (self.name)
431 elif self.is_real():
432 return "%s [color=white,shape=none,shapefile=\"%s/all/real_switch.png\"]" % (self.name, path_shapes)
433 elif self.is_turned_down():
434 return "%s [color=white,shape=none,shapefile=\"%s/all/switch_turned_down.png\"]" % (self.name, path_shapes)
435 else:
436 return "%s [color=white,shape=none,shapefile=\"%s/all/switch.png\"]" % (self.name, path_shapes)
439 class Node:
440 def __init__ (self,name):
441 self.name=name
442 self.type=domain_types[domains.index(name)]
443 def start(self):
444 return ""
445 def stop(self):
446 return ""
447 def start_commandline(self):
448 return ""
449 def get_domain_id(self):
450 return get_domain_id(self.name)
451 def graphviz_string(self):
452 return self.name+" [color=white,shape=plaintext,label=\" "+self.name+"\",shapefile=\""+path_shapes+"/all/"+\
453 domain_types[domains.index(self.name)]+".png\",fontcolor=black,fontsize=16,target=\"http://google.com\"]"
454 def console_string(self):
455 ec2=True # FIXME
456 ec2_assign_nodes_to_instances()
457 if ec2:
458 return path_scripts+"/node-terminal-session INSTANCE EC2_NODE %s %s %s"%(ec2_node[self.name], network, self.name)
460 if self.type in [ 'quagga', 'dynamips', 'freebsd', 'linux' ]:
461 return "sudo xm console "+self.name
462 elif self.name in real_bridges or self.name in real_nodes:
463 return "echo Press enter to connect; read line; "+connection_table[self.name]
466 class Link:
467 def __init__ (self,name,node,interface,bridge,label=""):
468 self.name=name
469 self.node=node
470 self.interface=interface
471 self.bridge=bridge
472 self.label=label
474 def is_temporary(self):
475 return [self.node,self.interface,self.bridge] in temporary_links
477 def is_broken(self):
478 return ([self.node,self.interface,self.bridge] in broken_links)
480 def is_hidden(self):
481 return self.bridge in hidden_bridges
483 def graphviz_string(self):
484 if self.is_hidden():
485 return "//"
486 if self.is_temporary():
487 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",color=blue,len=10,w=5,weight=5]"
488 if self.is_broken():
489 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",style=dashed]"
491 ip="\\n.%s.%s" % (bridges.index(self.bridge)+1, domains.index(self.node)+1)
492 if domain_types[domains.index(self.node)] == 'dynamips':
493 int_name="fa"+str(self.interface)+"/0"
494 else:
495 int_name="eth"+str(self.interface)
496 if self.label != "":
497 int_name = self.label
498 return self.node+" -- "+self.bridge+" [taillabel=\""+int_name+ip+"\",fontsize=14,fontname=fixed]"
501 #-----------------------------------------------------------------------
502 # DOMAINS
504 def get_domain_id(domain):
505 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
508 #-----------------------------------------------------------------------
509 # BRIDGES and IFACES
511 def bridge_down(bridge):
512 """
513 Turn the bridge <bridge> down
514 """
515 if bridge in real_bridges:
516 print "Bridge %s is a real bridge" % (bridge)
517 return -1
518 if bridge in bridges_turned_down:
519 print "Bridge %s is turned down already" % (bridge)
520 else:
521 bridges_turned_down.append(bridge)
522 run_command("sudo ip link set %s down" % bridge)
523 autoredraw()
525 def bridge_up(bridge):
526 """
527 Turn the bridge <bridge> up
528 """
529 if bridge in real_bridges:
530 print "Bridge %s is a real bridge" % (bridge)
531 return -1
532 if not (bridge in bridges_turned_down):
533 print "Bridge %s is turned up already" % (bridge)
534 else:
535 bridges_turned_down.remove(bridge)
536 run_command("sudo ip link set %s up" % bridge)
537 autoredraw()
539 def show_bridge(bridge):
540 """
541 Show the state of the bridge <bridge>
542 """
543 if bridge in real_bridges:
544 print "Bridge %s is a real bridge" % (bridge)
545 return -1
546 run_command("sudo ip link show %s" % bridge)
549 def int_disconnect(domain, int_number):
550 """
551 Disconnect the interface with the number <int_number>
552 of the domain <domain> from the bridge to which
553 it is connected
554 """
555 dom_id=get_domain_id(domain)
556 bridge=vbridges_table[domain][int_number]
557 if not bridge:
558 print "Interface %s of the %s domain is not connected" % (int_number, domain)
559 return 1
560 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
561 vbridges_table[domain][int_number]=''
562 if [ domain, int_number, bridge ] in temporary_links:
563 temporary_links.remove([ domain, int_number, bridge ])
564 else:
565 broken_links.append([ domain, int_number, bridge ])
566 autoredraw()
568 def int_connect(domain, int_number, bridge):
569 """
570 Connect the interface with the number <int_number>
571 of the domain <domain> to the bridge <bridge>
572 """
573 if bridge in real_bridges:
574 print "Bridge %s is a real bridge" % (bridge)
575 return -1
577 dom_id=get_domain_id(domain)
578 if vbridges_table[domain][int_number]:
579 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
580 return 1
581 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
582 vbridges_table[domain][int_number]=bridge
583 if [ domain, int_number, bridge ] in broken_links:
584 broken_links.remove([ domain, int_number, bridge ])
585 else:
586 temporary_links.append([ domain, int_number, bridge ])
587 autoredraw()
589 def int_reconnect(domain, int_number, bridge):
590 """
591 Reconnect the interface with the number <int_number>
592 of the domain <domain> from the bridge to which
593 it is connected to the bridge <bridge>
594 """
595 if bridge in real_bridges:
596 print "Bridge %s is a real bridge" % (bridge)
597 return -1
599 int_disconnect(domain, int_number)
600 int_connect(domain, int_number, bridge)
602 def show_int(domain, int_number):
603 """
604 Show information about the interface <int_nuber>
605 of the domain <domain>
606 """
607 return vbridges_table[domain][int_number]
609 def dump_start(bridge, filter=""):
610 if bridge in real_bridges:
611 print "Bridge %s is a real bridge" % (bridge)
612 return -1
613 try:
614 print "Writing dump... (press Ctrl-C to stop)"
615 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
616 except:
617 print "Done.\n Dump is written to xentaur.dump"
618 return 0
620 def dump_stop():
621 return 0
624 #-----------------------------------------------------------------------
626 def add_domain(name,type):
627 domains.append(name)
628 domain_types.append(type)
630 def break_link(domain,bridge):
631 broken_links.append([domain,bridge])
633 #-----------------------------------------------------------------------
635 wt_timeout=1
636 def write_to(screen,string,return_to_screen=""):
637 """
638 write_to(screen,string):
640 Type *string* to the specified screen(s).
641 Screen may be specified with the number *screen*,
642 with array of numbers,
643 with array of names.
645 """
646 screen_numbers=[] # number of the screens to write to
647 if type(screen) == list:
648 screen_numbers=map(lambda x: domains.index(x)+1, screen)
649 elif type(screen) == int:
650 screen_numbers=[screen]
651 else:
652 screen_numbers=[domains.index(screen)+1]
654 for screen_number in screen_numbers:
655 run_command("screen -X select "+str(screen_number))
656 time.sleep(wt_timeout)
657 for line in string.splitlines():
658 f=open('/tmp/xentaurbuf', 'w')
659 f.write(line+"\n")
660 f.close()
661 run_command("screen -X readreg p /tmp/xentaurbuf")
662 time.sleep(wt_timeout)
663 run_command("nohup screen -X paste p >& /dev/null")
664 time.sleep(wt_timeout)
666 if return_to_screen != "":
667 run_command("screen -X select %s" % (return_to_screen))
668 time.sleep(wt_timeout)
670 def filter_by_type(doms,type):
671 """
672 filter_by_type(doms,type)
674 Return only domains of *doms* that have specified *type*
675 """
676 return filter(lambda x: domain_types[domains.index(x)]==type,domains)
678 #-----------------------------------------------------------------------
680 nodes=domains
681 create_objects()
683 if len(sys.argv) == 2:
684 if sys.argv[1] == 'start-all':
685 start_all()
686 elif sys.argv[1] == 'start-domains':
687 start_domains()
688 elif sys.argv[1] == 'start-bridges':
689 start_bridges()
690 elif sys.argv[1] == 'stop-all':
691 stop_all()
692 elif sys.argv[1] == 'stop-domains':
693 stop_domains()
694 elif sys.argv[1] == 'stop-bridges':
695 stop_bridges()
696 elif sys.argv[1] == 'restart-all':
697 restart_all()
698 elif sys.argv[1] == 'screen':
699 screen()
700 elif sys.argv[1] == 'graph':
701 graph()
702 elif sys.argv[1] == 'shell':
703 shell()
704 elif sys.argv[1] == 'info':
705 info()
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)