Merge branch 'master' of ssh://git.onelab.eu/git/plcapi
[plcapi.git] / db-config.d / 000-functions
1 # -*-python-*-
2 #################### 
3 import sys, os
4 import resource
5
6 g_url = ""
7 def GetMyPLCURL(): return g_url
8 def SetMyPLCURL(url):
9     global g_url
10     g_url = url
11
12 # Get all currently registered roles
13 g_role_names = [ role['name'] for role in GetRoles()]
14 g_role_names.sort()
15
16 def SetRole(level, role):
17     global g_role_names
18     if role not in g_role_names:
19         AddRole(level, role)
20         g_role_names.append(role)
21         g_role_names.sort()
22
23 # Get list of existing tag types
24 g_known_tag_types = [tag_type['tagname'] for tag_type in GetTagTypes()]
25 g_known_tag_types.sort()
26
27 def SetTagType(tag_type):
28     tagname=tag_type['tagname']
29     global g_known_tag_types
30     # handle 'roles' field differently
31     if 'roles' in tag_type:
32         roles=tag_type['roles']
33         del tag_type['roles']
34     else:
35         roles=['admin']
36     # just in case
37     if 'min_role_id' in tag_type:
38         print "WARNING: ignoring deprecated field min_role_id for tagtype %s"%tagname
39         del tag_type['min_role_id']
40     # Create/update default slice tag types
41     if tagname not in g_known_tag_types:
42         AddTagType(tag_type)
43         g_known_tag_types.append(tagname)
44         g_known_tag_types.sort()
45     else:
46         UpdateTagType(tagname, tag_type)
47     # enforce provided roles if present
48     try:
49         old_roles=GetTagTypes(tagname)[0]['roles']
50         for minus_role in set(old_roles).difference(set(roles)):
51             DeleteRoleFromTagType(minus_role,tagname)
52         for plus_role in set(roles).difference(set(old_roles)):
53             AddRoleToType(plus_role,tagname)
54     except:
55         # something went wrong for that tagname, 
56         # but don't want to break the whole startup sequence
57         print "Could not enforce roles on tagtype %s"%tagname
58         pass
59
60 # Get list of existing (enabled, global) files
61 g_conf_files = GetConfFiles()
62 g_conf_files = filter(lambda conf_file: conf_file['enabled'] and \
63                     not conf_file['node_ids'] and \
64                     not conf_file['nodegroup_ids'],
65                     g_conf_files)
66 g_dests = [conf_file['dest'] for conf_file in g_conf_files]
67 g_conf_files = dict(zip(g_dests, g_conf_files))
68
69 # Get list of existing initscripts
70 g_oldinitscripts = GetInitScripts()
71 g_oldinitscript_names = [script['name'] for script in g_oldinitscripts]
72 g_oldinitscripts = dict(zip(g_oldinitscript_names, g_oldinitscripts))
73
74 def SetInitScript(initscript):
75     global g_oldinitscripts, g_oldinitscript_names
76     if initscript['name'] not in g_oldinitscript_names:
77         initscript_id = AddInitScript(initscript)
78         g_oldinitscript_names.append(initscript['name'])
79         initscript['initscript_id']=initscript_id
80         g_oldinitscripts[initscript['name']]=initscript
81     else:
82         orig_initscript = g_oldinitscripts[initscript['name']]
83         initscript_id = orig_initscript['initscript_id']
84         UpdateInitScript(initscript_id, initscript)
85         
86 def SetConfFile(conf_file):
87     global g_conf_files, g_dests
88     if conf_file['dest'] not in g_dests:
89         AddConfFile(conf_file)
90     else:
91         orig_conf_file = g_conf_files[conf_file['dest']]
92         conf_file_id = orig_conf_file['conf_file_id']
93         UpdateConfFile(conf_file_id, conf_file)
94
95 def SetSlice(slice, tags):
96     # Create or Update slice
97     slice_name = slice['name']
98     slices = GetSlices([slice_name])
99     if len(slices)==1:
100         slice_id = slices[0]['slice_id']
101         if slice.has_key('name'):
102             del slice['name']
103         UpdateSlice(slice_id, slice)
104         slice['name']=slice_name
105     else:
106         expires = None
107         if slice.has_key('expires'):
108             expires = slice['expires']
109             del slice['expires']
110         slice_id = AddSlice(slice)
111         if expires <> None:
112             UpdateSlice(slice_id, {'expires':expires})
113
114     # Get slice structure with all fields
115     slice = GetSlices([slice_name])[0]
116
117     # Create/delete all tags
118     # NOTE: update is not needed, since unspecified tags are deleted, 
119     #       and new tags are added
120     slice_tags = []
121     if slice['slice_tag_ids']:
122         # Delete unknown attributes
123         for slice_tag in GetSliceTags(slice['slice_tag_ids']):
124             # ignore sliver tags, as those are custom/run-time values
125             if slice_tag['node_id'] <> None: continue
126             if (slice_tag['tagname'], slice_tag['value']) not in tags:
127                 DeleteSliceTag(slice_tag['slice_tag_id'])
128             else:
129                 slice_tags.append((slice_tag['tagname'],slice_tag['value']))
130
131     # only add slice tags that are new
132     for (name, value) in tags:
133         if (name,value) not in slice_tags:
134             AddSliceTag(slice_name, name, value)            
135         else:
136             # NOTE: this confirms that the user-specified tag is 
137             #       returned by GetSliceTags
138             pass
139
140 def SetMessage(message):
141     messages = GetMessages([message['message_id']])
142     if len(messages)==0:
143         AddMessage(message)
144     else:
145         UpdateMessage(message['message_id'],message)
146
147 # Get all model names
148 g_pcu_models = [type['model'] for type in GetPCUTypes()]
149
150 def SetPCUType(pcu_type):
151     global g_pcu_models
152     if 'pcu_protocol_types' in pcu_type:
153         protocol_types = pcu_type['pcu_protocol_types']
154         # Take this value out of the struct.
155         del pcu_type['pcu_protocol_types']
156     else:
157         protocol_types = []
158
159     if pcu_type['model'] not in g_pcu_models:
160         # Add the name/model info into DB
161         id = AddPCUType(pcu_type)
162         # for each protocol, also add this.
163         for ptype in protocol_types:
164             AddPCUProtocolType(id, ptype)
165