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