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.
6 # scan ordered scripts in /etc/planetlab/db-config.d
8 # Mark Huang <mlhuang@cs.princeton.edu>
9 # Copyright (C) 2006 The Trustees of Princeton University
10 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
12 # to run with options, do e.g.
17 from argparse import ArgumentParser
19 from plc_config import PLCConfiguration
22 def GetSnippets(directory):
24 if os.path.exists(directory):
26 filenames = os.listdir(directory)
28 raise Exception("Error when opening %s (%s)" %
29 (os.path.join(dir, file), e))
31 # ignore files that contain either ~ or .
32 ignore_tokens = ("~", ".")
34 for filename in filenames:
36 for token in ignore_tokens:
37 if filename.find(token) >= 0:
42 parts = filename.split('-')
44 name = '-'.join(parts)
46 number = int(parts[0])
47 entry = numberedfiles.get(number, [])
49 numberedfiles[number] = entry
56 print("db-config: ignored %s snippet" % filename)
59 keys = list(numberedfiles.keys())
62 for filename in numberedfiles[k]:
63 filenames.append(filename)
68 cfg = PLCConfiguration()
70 variables = cfg.variables()
72 usage = "%prog [-- options] [steps]"
74 parser = ArgumentParser(usage=usage)
77 dest="list_steps", action="store_true", default=False,
78 help="Lists available steps")
81 dest="verbose", action="store_true", default=False,
83 parser.add_argument('steps', nargs='*')
85 args = parser.parse_args()
87 globals_exec = globals().copy()
90 # Load variables into dictionaries
91 for category_id, (category, variablelist) in variables.items():
92 globals_exec[category_id] = dict(
93 zip(variablelist.keys(),
94 [variable['value'] for variable in variablelist.values()]))
96 directory = "/etc/planetlab/db-config.d"
97 snippets = GetSnippets(directory)
101 for snippet in snippets:
104 # no steps provided on the command-line : run them all
109 if snippet.find(step) >= 0:
118 print("Found step %s/%s" % (directory, snippet))
119 os.system("rpm -qf %s/%s" % (directory, snippet))
122 fullpath = os.path.join(directory, snippet)
124 print("Running step %s" % fullpath)
125 with open(fullpath) as feed:
126 exec(feed.read(), globals_exec, locals_exec)
129 if __name__ == '__main__':