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