1 """vsys configurator. Maintains ACLs and script pipes inside vservers based on slice attributes."""
6 VSYSCONF="/etc/vsys.conf"
10 logger.log("vsys: plugin starting up...")
12 def GetSlivers(data, config=None, plc=None):
13 """For each sliver with the vsys attribute, set the script ACL, create the vsys directory in the slice, and restart vsys."""
15 if 'slivers' not in data:
16 logger.log_missing_data("vsys.GetSlivers",'slivers')
19 # Touch ACLs and create dict of available
21 for script in touchAcls(): scripts[script] = []
22 # slices that need to be written to the conf
25 # Parse attributes and update dict of scripts
26 if 'slivers' not in data:
27 logger.log_missing_data("vsys.GetSlivers",'slivers')
29 for sliver in data['slivers']:
30 for attribute in sliver['attributes']:
31 if attribute['tagname'] == 'vsys':
32 if sliver['name'] not in slices:
34 slices.append(sliver['name'])
35 _restart = createVsysDir(sliver['name']) or _restart
36 if attribute['value'] in scripts.keys():
37 scripts[attribute['value']].append(sliver['name'])
40 _restart = writeConf(slices, parseConf()) or _restart
42 if writeAcls(scripts, parseAcls()) or _restart:
43 logger.log("vsys: restarting vsys service")
44 logger.log_call(["/etc/init.d/vsys", "restart", ])
47 def createVsysDir(sliver):
48 '''Create /vsys directory in slice. Update vsys conf file.'''
50 os.mkdir("/vservers/%s/vsys" % sliver)
57 '''Creates empty acl files for scripts.
58 To be ran in case of new scripts that appear in the backend.
59 Returns list of available scripts.'''
62 for (root, dirs, files) in os.walk(VSYSBKEND):
64 # ingore scripts that start with local_
65 if file.startswith("local_"): continue
66 if file.endswith(".acl"):
67 acls.append(file.replace(".acl", ""))
70 for new in (set(scripts) - set(acls)):
71 logger.log("vsys: Found new script %s. Writing empty acl." % new)
72 f = open("%s/%s.acl" %(VSYSBKEND, new), "w")
79 def writeAcls(currentscripts, oldscripts):
80 '''Creates .acl files for script in the script repo.'''
81 # Check each oldscript entry to see if we need to modify
83 # for iteritems along dict(oldscripts), if length of values
84 # not the same as length of values of new scripts,
85 # and length of non intersection along new scripts is not 0,
86 # then dicts are different.
87 for (acl, oldslivers) in oldscripts.iteritems():
89 if (len(oldslivers) != len(currentscripts[acl])) or \
90 (len(set(oldslivers) - set(currentscripts[acl])) != 0):
92 logger.log("vsys: Updating %s.acl w/ slices %s" % (acl, currentscripts[acl]))
93 f = open("%s/%s.acl" % (VSYSBKEND, acl), "w")
94 for slice in currentscripts[acl]: f.write("%s\n" % slice)
97 logger.log("vsys: #:)# Warning,Not a valid Vsys script,%s"%acl)
103 '''Parse the frontend script acls. Return {script: [slices]} in conf.'''
104 # make a dict of what slices are in what acls.
106 for (root, dirs, files) in os.walk(VSYSBKEND):
108 if file.endswith(".acl") and not file.startswith("local_"):
109 f = open(root+"/"+file,"r+")
110 scriptname = file.replace(".acl", "")
111 scriptacls[scriptname] = []
112 for slice in f.readlines():
113 scriptacls[scriptname].append(slice.rstrip())
115 # return what scripts are configured for which slices.
119 def writeConf(slivers, oldslivers):
120 # Check if this is needed
121 # The assumption here is if lengths are the same,
122 # and the non intersection of both arrays has length 0,
123 # then the arrays are identical.
124 if (len(slivers) != len(oldslivers)) or \
125 (len(set(oldslivers) - set(slivers)) != 0):
126 logger.log("vsys: Updating %s" % VSYSCONF)
127 f = open(VSYSCONF,"w")
128 for sliver in slivers:
129 f.write("/vservers/%(name)s/vsys %(name)s\n" % {"name": sliver})
138 '''Parse the vsys conf and return list of slices in conf.'''
143 for line in f.readlines():
144 (path, slice) = line.split()
145 slicesinconf.append(slice)
147 except: logger.log_exc("vsys: failed parseConf")