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