+
+"""
+Write /etc/vservers/<slicename>/spaces/net. If the vserver is running and the spaces/net file is modified, we need to restart it.
+"""
+def write_conf_and_restart(slicename, value):
+ SLICEDIR="/etc/vservers/%s/" % slicename
+ SPACESDIR="%s/spaces/" % SLICEDIR
+ FILENAME="%s/net" % SPACESDIR
+ if os.path.exists(SLICEDIR):
+ if not os.path.exists(SPACESDIR):
+ try:
+ os.mkdir(SPACESDIR)
+ except os.error:
+ logger.log("topo: could not create %s\n" % SPACESDIR)
+ return
+
+ if os.path.exists(FILENAME) != value:
+ sliver = vserver.VServer(slicename)
+ restart = sliver.is_running()
+
+ if (restart):
+ sliver.stop()
+
+ if value:
+ STATUS="ON"
+ f = open(FILENAME, "w")
+ f.close()
+ else:
+ STATUS="OFF"
+ os.remove(FILENAME)
+
+ logger.log("%s: network namespace %s\n" % (slicename, STATUS))
+
+ if (restart):
+ logger.log("topo: restarting sliver %s\n" % slicename)
+ sliver.start()
+
+
+"""
+Generate information for each interface in the sliver, in order to configure
+Quagga.
+"""
+def get_ifaces(hostname, myid, topospec, key):
+ ifaces = {}
+ topolist = convert_topospec_to_list(topospec)
+ for (nodeid, ipaddr, rate, myvirtip, remvirtip, virtnet) in topolist:
+ name = "a%sx%s" % (key, nodeid)
+ ifaces[name] = {}
+ ifaces[name]['remote-ip'] = remvirtip
+ ifaces[name]['local-ip'] = myvirtip
+ ifaces[name]['network'] = virtnet
+ ifaces[name]['short-name'] = hostname.replace('.vini-veritas.net', '')
+ return ifaces
+
+
+def write_header(f, myname, password):
+ f.write ("""! Configuration for %s
+! Generated at %s
+
+hostname %s
+password %s
+
+""" % (myname, strftime("%Y-%m-%d %H:%M:%S"), myname, password))
+ return
+
+
+"""
+IP address of NAT gateway to outside world
+"""
+def nat_gw(key):
+ return "10.0.%s.1" % key
+
+"""
+IP address of the NAT interface inside the slice
+"""
+def nat_inner(key):
+ return "10.0.%s.2" % key
+
+
+"""
+Write zebra.conf file for Quagga
+"""
+def write_zebra(filename, myname, ifaces, myid, key):
+ f = open(filename, 'w')
+ password = "zebra"
+ write_header(f, myname, password)
+
+ f.write ("enable password %s\n" % password)
+
+ for name in ifaces:
+ f.write ("""!
+interface %s
+link-detect
+""" % name)
+
+ f.write ("""!
+access-list vty permit 127.0.0.1/32
+!
+line vty
+!
+""")
+ f.close()
+ return
+
+
+"""
+Write ospfd.conf file for Quagga.
+"""
+def write_ospf(filename, myname, ifaces):
+ f = open(filename, 'w')
+ password = "zebra"
+ write_header(f, myname, password)
+
+ for name in ifaces:
+ f.write ("""!
+ interface %s
+ ip ospf cost 10
+ ip ospf hello-interval 5
+ ip ospf dead-interval 10
+ ip ospf network non-broadcast
+""" % name)
+
+ f.write ("""!
+ router ospf
+ ospf router-id %s
+""" % ifaces[name]['local-ip'])
+
+ for name in ifaces:
+ f.write (" neighbor %s\n" % ifaces[name]['remote-ip'])
+
+ for name in ifaces:
+ net = ifaces[name]['network']
+ f.write (" network %s area 0\n" % net)
+
+ f.write(""" redistribute kernel
+!
+access-list vty permit 127.0.0.1/32
+!
+line vty
+""")
+ return
+
+
+"""
+Write config files directly into the slice's file system.
+"""
+def update_quagga_configs(slicename, hostname, myid, topo, key, netns):
+ ifaces = get_ifaces(hostname, myid, topo, key)
+
+ quagga_dir = "/vservers/%s/etc/quagga/" % slicename
+ if not os.path.exists(quagga_dir):
+ try:
+ # Quagga not installed. Install it here? Chkconfig, sym links.
+ os.mkdir(quagga_dir)
+ except os.error:
+ logger.log("topo: could not create %s\n" % quagga_dir)
+ return
+
+ write_zebra(quagga_dir + "zebra.conf.generated", hostname, ifaces,
+ myid, key)
+ write_ospf(quagga_dir + "ospfd.conf.generated", hostname, ifaces)
+
+ # Start up Quagga if we installed it earlier and netns = 1.
+
+ return
+
+
+"""
+Write /etc/hosts in the sliver
+"""
+def update_hosts(slicename, hosts):
+ hosts_file = "/vservers/%s/etc/hosts" % slicename
+ f = open(hosts_file, 'w')
+ f.write(hosts)
+ f.close()
+ return