Simple module for toggling network namespaces based on slice attributes
[nodemanager.git] / trellis.py
1 # $Id$
2 # $URL$
3
4 """Trellis configurator.  Toggles network namespace based on slice attribute"""
5
6 import logger
7 import os
8 from sets import Set
9
10 def start(options, config):
11     pass
12
13 def GetSlivers(data):
14     """For each sliver with the netns attribute, write the value to /etc/vservers/<slice>/spaces/net"""
15     # Parse attributes and update dict of scripts
16     for sliver in data['slivers']:
17         for attribute in sliver['attributes']:
18             if attribute['name'] == 'netns':
19                 writeConf(sliver['name'], attribute['value'])
20
21 def writeConf(slicename, value):
22     SLICEDIR="/etc/vservers/%s/" % slicename
23     SPACESDIR="%s/spaces/" % SLICEDIR
24     if os.path.exists(SLICEDIR):
25         if not os.path.exists(SPACESDIR):
26             try:
27                 os.mkdir(SPACESDIR)
28             except os.error:
29                 logger.log("trellis: could not create %s\n" % SPACESDIR)
30                 return
31         f = open("%s/net" % SPACESDIR, "w")
32         f.write("%s\n" % value)
33         f.close()
34
35