Merge branch 'master' of ssh://git.onelab.eu/git/plcapi
[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     if 'roles' in tag_type:
39         for role in tag_type['roles']:
40             AddRoleToTagType(role,tag_type['tagname'])
41
42 # Get list of existing (enabled, global) files
43 g_conf_files = GetConfFiles()
44 g_conf_files = filter(lambda conf_file: conf_file['enabled'] and \
45                     not conf_file['node_ids'] and \
46                     not conf_file['nodegroup_ids'],
47                     g_conf_files)
48 g_dests = [conf_file['dest'] for conf_file in g_conf_files]
49 g_conf_files = dict(zip(g_dests, g_conf_files))
50
51 # Get list of existing initscripts
52 g_oldinitscripts = GetInitScripts()
53 g_oldinitscript_names = [script['name'] for script in g_oldinitscripts]
54 g_oldinitscripts = dict(zip(g_oldinitscript_names, g_oldinitscripts))
55
56 def SetInitScript(initscript):
57     global g_oldinitscripts, g_oldinitscript_names
58     if initscript['name'] not in g_oldinitscript_names:
59         initscript_id = AddInitScript(initscript)
60         g_oldinitscript_names.append(initscript['name'])
61         initscript['initscript_id']=initscript_id
62         g_oldinitscripts[initscript['name']]=initscript
63     else:
64         orig_initscript = g_oldinitscripts[initscript['name']]
65         initscript_id = orig_initscript['initscript_id']
66         UpdateInitScript(initscript_id, initscript)
67         
68 def SetConfFile(conf_file):
69     global g_conf_files, g_dests
70     if conf_file['dest'] not in g_dests:
71         AddConfFile(conf_file)
72     else:
73         orig_conf_file = g_conf_files[conf_file['dest']]
74         conf_file_id = orig_conf_file['conf_file_id']
75         UpdateConfFile(conf_file_id, conf_file)
76
77 def SetSlice(slice, tags):
78     # Create or Update slice
79     slice_name = slice['name']
80     slices = GetSlices([slice_name])
81     if len(slices)==1:
82         slice_id = slices[0]['slice_id']
83         if slice.has_key('name'):
84             del slice['name']
85         UpdateSlice(slice_id, slice)
86         slice['name']=slice_name
87     else:
88         expires = None
89         if slice.has_key('expires'):
90             expires = slice['expires']
91             del slice['expires']
92         slice_id = AddSlice(slice)
93         if expires <> None:
94             UpdateSlice(slice_id, {'expires':expires})
95
96     # Get slice structure with all fields
97     slice = GetSlices([slice_name])[0]
98
99     # Create/delete all tags
100     # NOTE: update is not needed, since unspecified tags are deleted, 
101     #       and new tags are added
102     slice_tags = []
103     if slice['slice_tag_ids']:
104         # Delete unknown attributes
105         for slice_tag in GetSliceTags(slice['slice_tag_ids']):
106             # ignore sliver tags, as those are custom/run-time values
107             if slice_tag['node_id'] <> None: continue
108             if (slice_tag['tagname'], slice_tag['value']) not in tags:
109                 DeleteSliceTag(slice_tag['slice_tag_id'])
110             else:
111                 slice_tags.append((slice_tag['tagname'],slice_tag['value']))
112
113     # only add slice tags that are new
114     for (name, value) in tags:
115         if (name,value) not in slice_tags:
116             AddSliceTag(slice_name, name, value)            
117         else:
118             # NOTE: this confirms that the user-specified tag is 
119             #       returned by GetSliceTags
120             pass
121
122 def SetMessage(message):
123     messages = GetMessages([message['message_id']])
124     if len(messages)==0:
125         AddMessage(message)
126     else:
127         UpdateMessage(message['message_id'],message)
128
129 # Get all model names
130 g_pcu_models = [type['model'] for type in GetPCUTypes()]
131
132 def SetPCUType(pcu_type):
133     global g_pcu_models
134     if 'pcu_protocol_types' in pcu_type:
135         protocol_types = pcu_type['pcu_protocol_types']
136         # Take this value out of the struct.
137         del pcu_type['pcu_protocol_types']
138     else:
139         protocol_types = []
140
141     if pcu_type['model'] not in g_pcu_models:
142         # Add the name/model info into DB
143         id = AddPCUType(pcu_type)
144         # for each protocol, also add this.
145         for ptype in protocol_types:
146             AddPCUProtocolType(id, ptype)
147