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