1 #!/usr/bin/env /usr/bin/plcsh
3 # Bootstraps the PLC database with a default administrator account and
4 # a default site, defines default slice attribute types, and
5 # creates/updates default system slices.
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
13 from plc_config import PLCConfiguration
18 def GetMyPLCURL(): return g_url
23 # Get list of existing tag types
24 g_known_tag_types = [tag_type['tagname'] for tag_type in GetTagTypes()]
25 g_known_tag_types.sort()
27 def SetTagType(tag_type):
28 global g_known_tag_types
29 # Create/update default slice tag types
30 if tag_type['tagname'] not in g_known_tag_types:
32 g_known_tag_types.append(tag_type['tagname'])
33 g_known_tag_types.sort()
35 UpdateTagType(tag_type['tagname'], tag_type)
37 # Get list of existing (enabled, global) files
38 g_conf_files = GetConfFiles()
39 g_conf_files = filter(lambda conf_file: conf_file['enabled'] and \
40 not conf_file['node_ids'] and \
41 not conf_file['nodegroup_ids'],
43 g_dests = [conf_file['dest'] for conf_file in g_conf_files]
44 g_conf_files = dict(zip(g_dests, g_conf_files))
46 # Get list of existing initscripts
47 g_oldinitscripts = GetInitScripts()
48 g_oldinitscript_names = [script['name'] for script in g_oldinitscripts]
49 g_oldinitscripts = dict(zip(g_oldinitscript_names, g_oldinitscripts))
51 def SetInitScript(initscript):
52 global g_oldinitscripts, g_oldinitscript_names
53 if initscript['name'] not in g_oldinitscript_names:
54 initscript_id = AddInitScript(initscript)
55 g_oldinitscript_names.append(initscript['name'])
56 initscript['initscript_id']=initscript_id
57 g_oldinitscripts[initscript['name']]=initscript
59 orig_initscript = g_oldinitscripts[initscript['name']]
60 initscript_id = orig_initscript['initscript_id']
61 UpdateInitScript(initscript_id, initscript)
63 def SetConfFile(conf_file):
64 global g_conf_files, g_dests
65 if conf_file['dest'] not in g_dests:
66 AddConfFile(conf_file)
68 orig_conf_file = g_conf_files[conf_file['dest']]
69 conf_file_id = orig_conf_file['conf_file_id']
70 UpdateConfFile(conf_file_id, conf_file)
72 def SetSlice(slice, tags):
73 # Create or Update slice
74 slice_name = slice['name']
75 slices = GetSlices([slice_name])
77 slice_id = slices[0]['slice_id']
78 if slice.has_key('name'):
80 UpdateSlice(slice_id, slice)
81 slice['name']=slice_name
84 if slice.has_key('expires'):
85 expires = slice['expires']
87 slice_id = AddSlice(slice)
89 UpdateSlice(slice_id, {'expires':expires})
91 # Get slice structure with all fields
92 slice = GetSlices([slice_name])[0]
94 # Create/update all tags
96 if slice['slice_tag_ids']:
97 # Delete unknown attributes
98 for slice_tag in GetSliceTags(slice['slice_tag_ids']):
99 if (slice_tag['tagname'], slice_tag['value']) not in tags:
100 DeleteSliceTag(slice_tag['slice_tag_id'])
102 slice_tags[slice_tag['tagname']]=slice_tag['value']
104 # only update slice tags that have changed
105 for (name, value) in tags:
106 if name not in slice_tags:
107 AddSliceTag(slice_name, name, value)
108 elif value <> slice_tags[name]:
109 UpdateSliceTag(slice_name,value)
111 def SetMessage(message):
112 messages = GetMessages([message['message_id']])
116 UpdateMessage(message['message_id'],message)
118 # Get all model names
119 g_pcu_models = [type['model'] for type in GetPCUTypes()]
121 def SetPCUType(pcu_type):
123 if 'pcu_protocol_types' in pcu_type:
124 protocol_types = pcu_type['pcu_protocol_types']
125 # Take this value out of the struct.
126 del pcu_type['pcu_protocol_types']
130 if pcu_type['model'] not in g_pcu_models:
131 # Add the name/model info into DB
132 id = AddPCUType(pcu_type)
133 # for each protocol, also add this.
134 for ptype in protocol_types:
135 AddPCUProtocolType(id, ptype)
137 def GetSnippets(directory):
139 if os.path.exists(directory):
141 filenames = os.listdir(directory)
143 raise Exception, "Error when opening %s (%s)" % \
144 (os.path.join(dir, file), e)
146 ignored = (".bak","~",".rpmsave",".rpmnew",".orig")
148 for filename in filenames:
150 for ignore in ignored:
151 if filename.endswith(ignore):
156 parts = filename.split('-')
158 name = '-'.join(parts)
160 number = int(parts[0])
161 entry = numberedfiles.get(number,[])
163 numberedfiles[number]=entry
170 print "db-config: ignoring %s snippet" % filename
173 keys = numberedfiles.keys()
176 for filename in numberedfiles[k]:
177 filenames.append(filename)
181 cfg = PLCConfiguration()
183 variables = cfg.variables()
185 # Load variables into dictionaries
186 for category_id, (category, variablelist) in variables.iteritems():
187 globals()[category_id] = dict(zip(variablelist.keys(),
188 [variable['value'] for variable in variablelist.values()]))
190 directory="/etc/planetlab/db-config.d"
191 snippets = GetSnippets(directory)
192 for snippet in snippets:
193 fullpath = os.path.join(directory, snippet)
196 if __name__ == '__main__':