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