a little nicer db-config
[myplc.git] / bin / db-config
1 #!/usr/bin/plcsh
2 #
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
7 #
8 # Mark Huang <mlhuang@cs.princeton.edu>
9 # Copyright (C) 2006 The Trustees of Princeton University
10 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
11 #
12 # to run with options, do e.g.
13 # db-config -- -l
14
15 import os
16 import sys
17 from argparse import ArgumentParser
18
19 from plc_config import PLCConfiguration
20
21
22 def GetSnippets(directory):
23     filenames = []
24     if os.path.exists(directory):
25         try:
26             filenames = os.listdir(directory)
27         except OSError as e:
28             raise Exception("Error when opening %s (%s)" %
29                             (os.path.join(dir, file), e))
30
31     # ignore files that contain either ~ or .
32     ignore_tokens = ("~", ".")
33     numberedfiles = {}
34     for filename in filenames:
35         ignore = False
36         for token in ignore_tokens:
37             if filename.find(token) >= 0:
38                 ignore = True
39                 break
40
41         if not ignore:
42             parts = filename.split('-')
43             if len(parts) >= 2:
44                 name = '-'.join(parts)
45                 try:
46                     number = int(parts[0])
47                     entry = numberedfiles.get(number, [])
48                     entry.append(name)
49                     numberedfiles[number] = entry
50                 except ValueError:
51                     ignore = True
52             else:
53                 ignore = True
54
55         if ignore:
56             print("db-config: ignored %s snippet" % filename)
57
58     filenames = []
59     keys = list(numberedfiles.keys())
60     keys.sort()
61     for k in keys:
62         for filename in numberedfiles[k]:
63             filenames.append(filename)
64     return filenames
65
66
67 def main():
68     cfg = PLCConfiguration()
69     cfg.load()
70     variables = cfg.variables()
71
72     usage = "%prog [-- options] [steps]"
73
74     parser = ArgumentParser(usage=usage)
75     parser.add_argument(
76         "-l", "--list",
77         dest="list_steps", action="store_true", default=False,
78         help="Lists available steps")
79     parser.add_argument(
80         "-v", "--verbose",
81         dest="verbose", action="store_true", default=False,
82         help="Run verbosely")
83     parser.add_argument('steps', nargs='*')
84
85     args = parser.parse_args()
86
87     globals_exec = globals().copy()
88     locals_exec = {}
89
90     # Load variables into dictionaries
91     for category_id, (category, variablelist) in variables.items():
92         globals_exec[category_id] = variablelist.copy()
93
94     directory = "/etc/planetlab/db-config.d"
95     snippets = GetSnippets(directory)
96
97     steps = args.steps
98
99     for snippet in snippets:
100
101         selected = False
102         # no steps provided on the command-line : run them all
103         if not steps:
104             selected = True
105         else:
106             for step in steps:
107                 if snippet.find(step) >= 0:
108                     selected = True
109         if not selected:
110             continue
111
112         if args.list_steps:
113             if not args.verbose:
114                 print(snippet)
115             else:
116                 print("Found step %s/%s" % (directory, snippet))
117                 os.system("rpm -qf %s/%s" % (directory, snippet))
118             continue
119
120         fullpath = os.path.join(directory, snippet)
121         if args.verbose:
122             print("Running step %s" % fullpath)
123         with open(fullpath) as feed:
124             exec(feed.read(), globals_exec, locals_exec)
125
126
127 if __name__ == '__main__':
128     main()