4 """vsys configurator. Maintains ACLs and script pipes inside vservers based on slice attributes."""
10 VSYSCONF="/etc/vsys.conf"
13 def start(options, conf):
14 logger.log("vsys plugin starting up...")
16 def GetSlivers(data, config=None, plc=None):
17 """For each sliver with the vsys attribute, set the script ACL, create the vsys directory in the slice, and restart vsys."""
18 # Touch ACLs and create dict of available
20 for script in touchAcls(): scripts[script] = []
21 # slices that need to be written to the conf
24 # Parse attributes and update dict of scripts
25 for sliver in data['slivers']:
26 for attribute in sliver['attributes']:
27 if attribute['tagname'] == 'vsys':
28 if sliver['name'] not in slices:
30 slices.append(sliver['name'])
31 _restart = createVsysDir(sliver['name']) or _restart
32 if attribute['value'] in scripts.keys():
33 scripts[attribute['value']].append(sliver['name'])
36 _restart = writeConf(slices, parseConf()) or _restart
38 if writeAcls(scripts, parseAcls()) or _restart:
39 logger.log("vsys: restarting vsys service")
40 logger.log_call("/etc/init.d/vsys", "restart")
43 def createVsysDir(sliver):
44 '''Create /vsys directory in slice. Update vsys conf file.'''
46 os.mkdir("/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 # ingore scripts that start with local_
61 if file.startswith("local_"): continue
62 if file.endswith(".acl"):
63 acls.append(file.replace(".acl", ""))
66 for new in (Set(scripts) - Set(acls)):
67 logger.log("vsys: Found new script %s. Writing empty acl." % new)
68 f = open("%s/%s.acl" %(VSYSBKEND, new), "w")
75 def writeAcls(currentscripts, oldscripts):
76 '''Creates .acl files for script in the script repo.'''
77 # Check each oldscript entry to see if we need to modify
79 # for iteritems along dict(oldscripts), if length of values
80 # not the same as length of values of new scripts,
81 # and length of non intersection along new scripts is not 0,
82 # then dicts are different.
83 for (acl, oldslivers) in oldscripts.iteritems():
84 if (len(oldslivers) != len(currentscripts[acl])) or \
85 (len(Set(oldslivers) - Set(currentscripts[acl])) != 0):
87 logger.log("vsys: Updating %s.acl w/ slices %s" % (acl, currentscripts[acl]))
88 f = open("%s/%s.acl" % (VSYSBKEND, acl), "w")
89 for slice in currentscripts[acl]: f.write("%s\n" % slice)
96 '''Parse the frontend script acls. Return {script: [slices]} in conf.'''
97 # make a dict of what slices are in what acls.
99 for (root, dirs, files) in os.walk(VSYSBKEND):
101 if file.endswith(".acl") and not file.startswith("local_"):
102 f = open(root+"/"+file,"r+")
103 scriptname = file.replace(".acl", "")
104 scriptacls[scriptname] = []
105 for slice in f.readlines():
106 scriptacls[scriptname].append(slice.rstrip())
108 # return what scripts are configured for which slices.
112 def writeConf(slivers, oldslivers):
113 # Check if this is needed
114 # The assumption here is if lengths are the same,
115 # and the non intersection of both arrays has length 0,
116 # then the arrays are identical.
117 if (len(slivers) != len(oldslivers)) or \
118 (len(Set(oldslivers) - Set(slivers)) != 0):
119 logger.log("vsys: Updating %s" % VSYSCONF)
120 f = open(VSYSCONF,"w")
121 for sliver in slivers:
122 f.write("/vservers/%(name)s/vsys %(name)s\n" % {"name": sliver})
131 '''Parse the vsys conf and return list of slices in conf.'''
136 for line in f.readlines():
137 (path, slice) = line.split()
138 slicesinconf.append(slice)
140 except: logger.log_exc()