expects the 'interfaces' key in GetSlivers - review logs to always mention module
[nodemanager.git] / plugins / vsys_privs.py
1 # $Id$
2 # $URL$
3
4 """
5 vsys sub-configurator.  Maintains configuration parameters associated with vsys scripts.
6 All slice attributes with the prefix vsys_ are written into configuration files on the
7 node for the reference of vsys scripts.
8 """
9
10 import logger
11 import os
12 from sets import Set
13
14 VSYS_PRIV_DIR = "/etc/planetlab/vsys-attributes"
15
16 def start(options, conf):
17     logger.log("vsys_privs: plugin starting")
18     if (not os.path.exists(VSYS_PRIV_DIR)):
19         os.makedirs(VSYS_PRIV_DIR)
20         logger.log("vsys_privs: Created vsys attributes dir")
21
22 def GetSlivers(data, config=None, plc=None):
23
24     if 'slivers' not in data:
25         logger.log_missing_data("vsys_privs.GetSlivers",'slivers')
26         return
27
28
29     privs = {}
30
31     # Parse attributes and update dict of scripts
32     if 'slivers' not in data:
33         logger.log_missing_data("vsys_privs.GetSlivers",'slivers')
34         return
35     for sliver in data['slivers']:
36         slice = sliver['name']
37         for attribute in sliver['attributes']:
38             tag = attribute['tagname']
39             value = attribute['value']
40             if tag.startswith('vsys_'):
41                 if (privs.has_key(slice)):
42                     slice_priv = privs[slice]
43                     if (slice_priv.has_key(tag)):
44                         slice_priv[tag].append(value)
45                     else:
46                         slice_priv[tag]=[value]
47
48                     privs[slice] = slice_priv
49                 else:
50                     privs[slice] = {tag:[value]}
51
52     cur_privs = read_privs()
53     write_privs(cur_privs, privs)
54
55 def read_privs():
56     cur_privs={}
57     priv_finder = os.walk(VSYS_PRIV_DIR)
58     priv_find = [i for i in priv_finder]
59     (rootdir,slices,foo) = priv_find[0]
60
61     for slice in slices:
62         cur_privs[slice]={}
63
64     if (len(priv_find)>1):
65         for (slicedir,bar,tagnames) in priv_find[1:]:
66             if (bar != []):
67                 # The depth of the vsys-privileges directory = 1
68                 pass
69
70             for tagname in tagnames:
71                 tagfile = os.path.join(slicedir,tagname)
72                 values_n = file(tagfile).readlines()
73                 values = map(lambda s:s.rstrip(),values_n)
74                 slice = os.path.basename(slicedir)
75                 cur_privs[slice][tagname]=values
76
77     return cur_privs
78
79 def write_privs(cur_privs,privs):
80     for slice in privs.keys():
81         variables = privs[slice]
82         slice_dir = os.path.join(VSYS_PRIV_DIR,slice)
83         if (not os.path.exists(slice_dir)):
84             os.mkdir(slice_dir)
85
86         # Add values that do not exist
87         for k in variables.keys():
88             v = variables[k]
89             if (cur_privs.has_key(slice) 
90                     and cur_privs[slice].has_key(k)
91                     and cur_privs[slice][k] == v):
92                 # The binding has not changed
93                 pass
94             else:
95                 v_file = os.path.join(slice_dir, k)
96                 f = open(v_file,'w')
97                 data = '\n'.join(v)
98                 f.write(data)
99                 f.close()
100                 logger.log("vsys_privs: added vsys attribute %s for %s"%(k,slice))
101
102     # Remove files and directories 
103     # that are invalid
104     for slice in cur_privs.keys():
105         variables = cur_privs[slice]
106         slice_dir = os.path.join(VSYS_PRIV_DIR,slice)
107
108         # Add values that do not exist
109         for k in variables.keys():
110             if (privs.has_key(slice) 
111                     and cur_privs[slice].has_key(k)):
112                 # ok, spare this tag
113                 print "Sparing  %s, %s "%(slice,k) 
114             else:
115                 v_file = os.path.join(slice_dir, k)
116                 os.remove(v_file)    
117
118         if (not privs.has_key(slice)):
119             os.rmdir(slice_dir)
120
121
122 if __name__ == "__main__":           
123     test_slivers = {'slivers':[
124         {'name':'foo','attributes':[
125             {'tagname':'vsys_m','value':'2'},
126             {'tagname':'vsys_m','value':'3'},
127             {'tagname':'vsys_m','value':'4'}
128             ]},
129         {'name':'bar','attributes':[
130             #{'tagname':'vsys_x','value':'z'}
131             ]}
132         ]}
133     start(None,None)
134     GetSlivers(test_slivers)