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_dests, g_conf_files))
51 def SetInitScript(initscript):
52 global g_oldinitscripts, g_oldinitscript_names
53 if initscript['name'] not in g_oldinitscript_names:
54 AddInitScript(initscript)
55 g_oldinitscript_names.append(initscript['name'])
57 orig_initscript = g_oldinitscripts[initscript['name']]
58 initscript_id = orig_initscript['initscript_id']
59 UpdateConfFile(initscript_id, initscript)
61 def SetConfFile(conf_file):
62 global g_conf_files, g_dests
63 if conf_file['dest'] not in g_dests:
64 AddConfFile(conf_file)
66 orig_conf_file = g_conf_files[conf_file['dest']]
67 conf_file_id = orig_conf_file['conf_file_id']
68 UpdateConfFile(conf_file_id, conf_file)
70 def SetSlice(slice, tags):
71 # Create or Update slice
72 slices = GetSlices([slice['name']])
74 slice_id = slices[0]['slice_id']
75 UpdateSlice(slice_id, slice)
79 # Get slice structure with all fields
80 slice = GetSlices([slice['name']])[0]
82 # Create/update all tags
84 if slice['slice_tag_ids']:
85 # Delete unknown attributes
86 for slice_tag in GetSliceTags(slice['slice_tag_ids']):
87 if (slice_tag['tagname'], slice_tag['value']) not in tags:
88 DeleteSliceTag(slice_tag['slice_tag_id'])
90 slice_tags[slice_tag['tagname']]=slice_tag['value']
92 # only update slice tags that have changed
93 for (name, value) in tags:
94 if name not in slice_tags:
95 AddSliceTag(slice['name'], name, value)
96 elif value <> slice_tags[name]:
97 UpdateSliceTag(slice['name'],value)
99 def SetMessage(message):
100 messages = GetMessages([message['message_id']])
104 UpdateMessage(message['message_id'],message)
106 # Get all model names
107 g_pcu_models = [type['model'] for type in GetPCUTypes()]
109 def SetPCUType(pcu_type):
111 if 'pcu_protocol_types' in pcu_type:
112 protocol_types = pcu_type['pcu_protocol_types']
113 # Take this value out of the struct.
114 del pcu_type['pcu_protocol_types']
118 if pcu_type['model'] not in g_pcu_models:
119 # Add the name/model info into DB
120 id = AddPCUType(pcu_type)
121 # for each protocol, also add this.
122 for ptype in protocol_types:
123 AddPCUProtocolType(id, ptype)
125 def GetSnippets(directory):
127 if os.path.exists(directory):
129 filenames = os.listdir(directory)
131 raise Exception, "Error when opening %s (%s)" % \
132 (os.path.join(dir, file), e)
134 ignored = (".bak","~",".rpmsave",".rpmnew",".orig")
136 for filename in filenames:
138 for ignore in ignored:
139 if filename.endswith(ignore):
144 parts = filename.split('-')
146 name = '-'.join(parts)
148 number = int(parts[0])
149 entry = numberedfiles.get(number,[])
151 numberedfiles[number]=entry
158 print "db-config: ignoring %s snippet" % filename
161 keys = numberedfiles.keys()
164 for filename in numberedfiles[k]:
165 filenames.append(filename)
169 cfg = PLCConfiguration()
171 variables = cfg.variables()
173 # Load variables into dictionaries
174 for category_id, (category, variablelist) in variables.iteritems():
175 globals()[category_id] = dict(zip(variablelist.keys(),
176 [variable['value'] for variable in variablelist.values()]))
178 directory="/etc/planetlab/db-config.d"
179 snippets = GetSnippets(directory)
180 for snippet in snippets:
181 fullpath = os.path.join(directory, snippet)
184 if __name__ == '__main__':