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