4 """vsys configurator. Maintains ACLs and script pipes inside vservers based on slice attributes."""
10 VSYSCONF="/etc/vsys.conf"
13 def start(options, config):
18 """For each sliver with the vsys attribute, set the script ACL, create the vsys directory in the slice, and restart vsys."""
19 # Touch ACLs and create dict of available
20 # XXX ...Sigh... fromkeys will use an immutable
21 #scripts = dict.fromkeys(touchAcls(),[])A
23 for script in touchAcls(): scripts[script] = []
24 # slices that need to be written to the conf
26 # Parse attributes and update dict of scripts
27 for sliver in data['slivers']:
28 for attribute in sliver['attributes']:
29 if attribute['name'] == 'vsys':
31 slices.append(sliver['name'])
32 # As the name implies, when we find an attribute, we
33 createVsysDir(sliver['name'])
34 # add it to our list of slivers that need vsys
35 if attribute['value'] in scripts.keys():
36 scripts[attribute['value']].append(sliver['name'])
39 writeConf(slices, parseConf())
41 if writeAcls(scripts, parseAcls()):
42 logger.log("vsys: restarting vsys service")
43 os.system("/etc/init.d/vsys restart")
46 def createVsysDir(sliver):
47 '''Create /vsys directory in slice. Update vsys conf file.'''
48 try: os.makedirs("/vservers/%s/vsys" % sliver)
53 '''Creates empty acl files for scripts.
54 To be ran in case of new scripts that appear in the backend.
55 Returns list of available scripts.'''
58 for (root, dirs, files) in os.walk(VSYSBKEND):
60 if file.endswith(".acl"):
61 acls.append(file.rstrip(".acl"))
64 for new in (Set(scripts) - Set(acls)):
65 logger.log("vsys: Found new script %s. Writing empty acl." % new)
66 f = open("%s/%s.acl" %(VSYSBKEND, new), "w")
73 def writeAcls(currentscripts, oldscripts):
74 '''Creates .acl files for script in the script repo.'''
75 # Check each oldscript entry to see if we need to modify
77 # for iteritems along dict(oldscripts), if length of values
78 # not the same as length of values of new scripts,
79 # and length of non intersection along new scripts is not 0,
80 # then dicts are different.
81 for (acl, oldslivers) in oldscripts.iteritems():
82 if (len(oldslivers) != len(currentscripts[acl])) or \
83 (len(Set(oldslivers) - Set(currentscripts[acl])) != 0):
85 logger.log("vsys: Updating %s.acl w/ slices %s" % (acl, currentscripts[acl]))
86 f = open("%s/%s.acl" % (VSYSBKEND, acl), "w")
87 for slice in currentscripts[acl]: f.write("%s\n" % slice)
94 '''Parse the frontend script acls. Return {script: [slices]} in conf.'''
95 # make a dict of what slices are in what acls.
97 for (root, dirs, files) in os.walk(VSYSBKEND):
99 if file.endswith(".acl"):
100 f = open(root+"/"+file,"r+")
101 scriptname = file.rstrip(".acl")
102 scriptacls[scriptname] = []
103 for slice in f.readlines():
104 scriptacls[scriptname].append(slice.rstrip())
106 # return what scripts are configured for which slices.
110 def writeConf(slivers, oldslivers):
111 # Check if this is needed
112 # The assumption here is if lengths are the same,
113 # and the non intersection of both arrays has length 0,
114 # then the arrays are identical.
115 if (len(slivers) != len(oldslivers)) or \
116 (len(Set(oldslivers) - Set(slivers)) != 0):
117 logger.log("vsys: Updating %s" % VSYSCONF)
118 f = open(VSYSCONF,"w")
119 for sliver in slivers:
120 f.write("/vservers/%(name)s/vsys %(name)s\n" % {"name": sliver})
126 '''Parse the vsys conf and return list of slices in conf.'''
131 for line in f.readlines():
132 (path, slice) = line.split()
133 slicesinconf.append(slice)
135 except: logger.log_exc()