db-config scripts also need to go python3 - plus autopep8 in the mix
[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 AllPersonRoles (): return [ 'pi','user','tech' ]
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             AddRoleToTagType(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         import traceback
60         traceback.print_exc()
61         print("Could not enforce tagtype %s --- end"%tagname)
62
63 # Get list of existing (enabled, global) files
64 g_conf_files = GetConfFiles()
65 g_conf_files = [conf_file for conf_file in g_conf_files if conf_file['enabled'] and \
66                     not conf_file['node_ids'] and \
67                     not conf_file['nodegroup_ids']]
68 g_dests = [conf_file['dest'] for conf_file in g_conf_files]
69 g_conf_files = dict(list(zip(g_dests, g_conf_files)))
70
71 # Get list of existing initscripts
72 g_oldinitscripts = GetInitScripts()
73 g_oldinitscript_names = [script['name'] for script in g_oldinitscripts]
74 g_oldinitscripts = dict(list(zip(g_oldinitscript_names, g_oldinitscripts)))
75
76 def SetInitScript(initscript):
77     global g_oldinitscripts, g_oldinitscript_names
78     if initscript['name'] not in g_oldinitscript_names:
79         initscript_id = AddInitScript(initscript)
80         g_oldinitscript_names.append(initscript['name'])
81         initscript['initscript_id']=initscript_id
82         g_oldinitscripts[initscript['name']]=initscript
83     else:
84         orig_initscript = g_oldinitscripts[initscript['name']]
85         initscript_id = orig_initscript['initscript_id']
86         UpdateInitScript(initscript_id, initscript)
87
88 def SetConfFile(conf_file):
89     global g_conf_files, g_dests
90     if conf_file['dest'] not in g_dests:
91         AddConfFile(conf_file)
92     else:
93         orig_conf_file = g_conf_files[conf_file['dest']]
94         conf_file_id = orig_conf_file['conf_file_id']
95         UpdateConfFile(conf_file_id, conf_file)
96
97 def SetSlice(slice, tags):
98     try:
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 'name' in slice:
105                 del slice['name']
106             UpdateSlice(slice_id, slice)
107             slice['name']=slice_name
108         else:
109             expires = None
110             if 'expires' in slice:
111                 expires = slice['expires']
112                 del slice['expires']
113             slice_id = AddSlice(slice)
114             if expires is not 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'] is not None:
129                     continue
130                 if (slice_tag['tagname'], slice_tag['value']) not in tags:
131                     DeleteSliceTag(slice_tag['slice_tag_id'])
132                 else:
133                     slice_tags.append((slice_tag['tagname'],slice_tag['value']))
134
135         # only add slice tags that are new
136         for (name, value) in tags:
137             if (name,value) not in slice_tags:
138                 AddSliceTag(slice_name, name, value)
139             else:
140                 # NOTE: this confirms that the user-specified tag is
141                 #       returned by GetSliceTags
142                 pass
143     except:
144         # something went wrong for that tagname,
145         print("Could not create init slice %s --- beg"%slice['name'])
146         import traceback
147         traceback.print_exc()
148         print("Could not create init slice %s --- end"%slice['name'])
149
150 def SetMessage(message):
151     messages = GetMessages([message['message_id']])
152     if len(messages)==0:
153         AddMessage(message)
154     ### Thierry 2012-03
155     # let people customize their messages if they want to
156     #else:
157     #    UpdateMessage(message['message_id'],message)
158
159 # Get all model names
160 g_pcu_models = [type['model'] for type in GetPCUTypes()]
161
162 def SetPCUType(pcu_type):
163     global g_pcu_models
164     if 'pcu_protocol_types' in pcu_type:
165         protocol_types = pcu_type['pcu_protocol_types']
166         # Take this value out of the struct.
167         del pcu_type['pcu_protocol_types']
168     else:
169         protocol_types = []
170
171     if pcu_type['model'] not in g_pcu_models:
172         # Add the name/model info into DB
173         id = AddPCUType(pcu_type)
174         # for each protocol, also add this.
175         for ptype in protocol_types:
176             AddPCUProtocolType(id, ptype)