xentaur

diff xentaur.py @ 49:65e80fabc89c

many fixes
author igor
date Thu Oct 25 17:59:28 2007 +0300 (2007-10-25)
parents 85794a582ef5
children ca80f73e403b
line diff
     1.1 --- a/xentaur.py	Wed Oct 24 15:30:43 2007 +0300
     1.2 +++ b/xentaur.py	Thu Oct 25 17:59:28 2007 +0300
     1.3 @@ -36,7 +36,12 @@
     1.4      p.close()
     1.5      return output
     1.6  
     1.7 -def create_bridges_script():
     1.8 +################################################################################
     1.9 +#Xentaur command-line commands
    1.10 +
    1.11 +## Start
    1.12 +
    1.13 +def start_bridges():
    1.14      unbound_bridges=set(bridges)-set(real_bridges)
    1.15      create_unbound_bridges="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges))
    1.16      create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges))
    1.17 @@ -47,18 +52,46 @@
    1.18  %(create_unbound_bridges)s
    1.19  """ % {'create_unbound_bridges' : create_unbound_bridges}
    1.20  
    1.21 +def start_domain(domain):
    1.22 +    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"
    1.23  
    1.24 -def create_domains_script():
    1.25 -    for domain in domains:
    1.26 +def start_domains(doms=domains):
    1.27 +    for domain in doms:
    1.28          if not domain in real_nodes:
    1.29 -            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"
    1.30 +            start_domain(domain)
    1.31  
    1.32 -def destroy_domains_script():
    1.33 -    for domain in domains:
    1.34 +def start_all():
    1.35 +    graph()
    1.36 +    screen()
    1.37 +    start_bridges()
    1.38 +    start_domains()
    1.39 +
    1.40 +## Stop
    1.41 +
    1.42 +def stop_domain(domain,wait=0):
    1.43 +    if wait:
    1.44 +        print "sudo xm shutdown -w "+domain
    1.45 +    else:
    1.46 +        print "sudo xm shutdown "+domain
    1.47 +
    1.48 +def stop_domains(doms=domains, wait=0):
    1.49 +    for domain in doms:
    1.50          if not domain in real_nodes:
    1.51 -            print "sudo xm shutdown "+domain
    1.52 +            stop_domain(domain,wait)
    1.53  
    1.54 -def create_screens_script():
    1.55 +def stop_bridges():
    1.56 +    ###FIXME###
    1.57 +    return 0
    1.58 +
    1.59 +def stop_all(wait=0):
    1.60 +    stop_domains(domains, wait)
    1.61 +    stop_bridges()
    1.62 +
    1.63 +def restart_all():
    1.64 +    stop_all(1)
    1.65 +    start_all()
    1.66 +
    1.67 +def screen():
    1.68      N=1
    1.69      screens=[]
    1.70      for domain in domains:
    1.71 @@ -72,17 +105,19 @@
    1.72  #        screens.append("screen -t "+domain+"   "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
    1.73  #
    1.74  
    1.75 -#hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
    1.76 +    hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
    1.77     
    1.78      f=open(screenrc, "w");
    1.79      f.write("""
    1.80  hardstatus on
    1.81  hardstatus alwayslastline
    1.82 +%s
    1.83  
    1.84  screen -t console 0 bash
    1.85  %s
    1.86 -""" % (screenlist))
    1.87 +""" % (hardstatus,screenlist))
    1.88      f.close()
    1.89 +    print "# GNU Screen config file is written to: %s" % screenrc
    1.90  
    1.91  def graph_node(node):
    1.92      i=0
    1.93 @@ -188,35 +223,37 @@
    1.94      run_command("neato -Tpng -o %s.png %s.dot "%(network,network))
    1.95      run_command("neato -Tjpg -o %s.jpg %s.dot "%(network,network))
    1.96      run_command("neato -Tsvg -o %s.svg %s.dot "%(network,network))
    1.97 +    print "# Network map is written to files: %s.{png,svg,jpg,dot}" % network
    1.98  
    1.99  def autoredraw():
   1.100      graph()
   1.101  
   1.102 -def start_all():
   1.103 -        create_bridges_script()
   1.104 -        create_screens_script()
   1.105 -        create_domains_script()
   1.106 -        graph()
   1.107 -        print """
   1.108 -        cat <<NOTE_FOR_USER
   1.109 -# To view virtual network map, run:
   1.110 -    gqview xenomips.png
   1.111 -# To attach to VM consoles, run:
   1.112 -    screen -c screenrc
   1.113 -NOTE_FOR_USER
   1.114 -"""
   1.115 -
   1.116  def shell():
   1.117      autoredraw()
   1.118      ipshell = IPShellEmbed()
   1.119      ipshell()
   1.120  
   1.121 -def stop_all():
   1.122 -        destroy_domains_script()
   1.123  
   1.124  def show_usage():
   1.125      print """Usage:
   1.126 -    xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
   1.127 +    xentaur <command> [<argument>]
   1.128 +
   1.129 +Commands:
   1.130 +    start-all       --  start bridges and domains
   1.131 +    start-domains   --  start domains only
   1.132 +    start-bridges   --  start bridges only
   1.133 +    stop-all        --  stop bridges and domains
   1.134 +    stop-domains    --  stop domains only
   1.135 +    stop-bridges    --  stop bridges only (domains have to be stopped already)
   1.136 +    restart-all     --  restart bridges and domains
   1.137 +
   1.138 +    start <domain>  --  start the <domain>
   1.139 +    stop <domain>   --  stop the <domain>
   1.140 +
   1.141 +    graph           --  generate network scheme (result is in <network>.{png,jpg,svg})
   1.142 +    screen          --  generate GNU Screen config file (~/.screenrc_xentaur)
   1.143 +    shell           --  run Xentaur shell
   1.144 +
   1.145  """
   1.146  
   1.147  def save():
   1.148 @@ -470,9 +507,28 @@
   1.149              write_to(dom,"\n\nena\nconf t\nrouter ospf 1\nnetwork 192.168.0.0 0.0.255.255 area 0\nend\n")
   1.150      return 0
   1.151  
   1.152 +def configure_hostname(doms=domains):
   1.153 +    for dom in doms:
   1.154 +        if domain_types[domains.index(dom)] == 'quagga':
   1.155 +            write_to(dom,"\n\nconf t\nhostname %s\nend\n" % dom)
   1.156 +        else:
   1.157 +            write_to(dom,"\n\nena\nconf t\nhostname %s\nend\n" % dom)
   1.158 +    return 0
   1.159 +
   1.160 +def configure_logging_synchronous(doms=domains):
   1.161 +    for dom in domains:
   1.162 +        if domain_types[domains.index(dom)] == 'quagga':
   1.163 +            0
   1.164 +        else:
   1.165 +            write_to(dom,"\n\nena\nconf t\nline console 0\nlogging synchronous\nend\n")
   1.166 +    return 0
   1.167 +
   1.168  def configure_save(doms=domains):
   1.169      write_to(doms,"\nwr\n")
   1.170  
   1.171 +def configure_root(doms=domains):
   1.172 +    write_to(doms,"root\n")
   1.173 +
   1.174  #-----------------------------------------------------------------------
   1.175  
   1.176  
   1.177 @@ -518,15 +574,25 @@
   1.178              run_command("screen -X select %s" % (return_to_screen))
   1.179              time.sleep(wt_timeout)
   1.180  
   1.181 +def filter_by_type(doms,type):
   1.182 +    """
   1.183 +    filter_by_type(doms,type)
   1.184 +
   1.185 +        Return only domains of *doms* that have specified *type*
   1.186 +    """
   1.187 +    return filter(lambda x: domain_types[domains.index(x)]==type,domains)
   1.188 +
   1.189  #-----------------------------------------------------------------------
   1.190  
   1.191  cisco_fa01_up="""
   1.192  ena
   1.193  conf t
   1.194  int fa0/0
   1.195 +duplex half
   1.196  no shutdown
   1.197  exit
   1.198  int fa1/0
   1.199 +duplex half
   1.200  no shutdown
   1.201  exit
   1.202  exit
   1.203 @@ -540,28 +606,43 @@
   1.204  exit
   1.205  """
   1.206  
   1.207 -
   1.208 -
   1.209  nodes=domains
   1.210  
   1.211 -if len(sys.argv) > 1:
   1.212 -    if sys.argv[1] == 'start':
   1.213 +if len(sys.argv) == 2:
   1.214 +    if sys.argv[1] == 'start-all':
   1.215          start_all()
   1.216 -    if sys.argv[1] == 'stop':
   1.217 +    elif sys.argv[1] == 'start-domains':
   1.218 +        start_domains()
   1.219 +    elif sys.argv[1] == 'start-bridges':
   1.220 +        start_bridges()
   1.221 +    elif sys.argv[1] == 'stop-all':
   1.222          stop_all()
   1.223 -    if sys.argv[1] == 'start-bridges':
   1.224 -        create_bridges_script()
   1.225 -    if sys.argv[1] == 'start-domains':
   1.226 -        create_domains_script()
   1.227 -    if sys.argv[1] == 'stop-domains':
   1.228 -        destroy_domains_script()
   1.229 +    elif sys.argv[1] == 'stop-domains':
   1.230 +        stop_domains()
   1.231 +    elif sys.argv[1] == 'stop-bridges':
   1.232 +        stop_bridges()
   1.233 +    elif sys.argv[1] == 'restart-all':
   1.234 +        restart_all()
   1.235      elif sys.argv[1] == 'screen':
   1.236          create_screens_script()
   1.237      elif sys.argv[1] == 'graph':
   1.238          graph()
   1.239      elif sys.argv[1] == 'shell':
   1.240          shell()
   1.241 -
   1.242 +    else:
   1.243 +        show_usage()
   1.244 +        sys.exit(1)
   1.245 +elif len(sys.argv) == 3:
   1.246 +    if sys.argv[1] == 'start':
   1.247 +        start_domain(sys.argv[2])
   1.248 +    elif sys.argv[1] == 'stop':
   1.249 +        stop_domain(sys.argv[2])
   1.250 +    elif sys.argv[1] == 'restart':
   1.251 +        stop_domain(sys.argv[2])
   1.252 +        start_domain(sys.argv[2])
   1.253 +    else:
   1.254 +        show_usage()
   1.255 +        sys.exit(1)
   1.256  else:
   1.257      show_usage()
   1.258      sys.exit(1)