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