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