f22e7f2cc105908bcbc16b8756746da0f8f22bb4
[myplc.git] / bin / db-config
1 #!/usr/bin/env /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 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id$
11 # $URL$
12
13 import os,sys
14 from optparse import OptionParser
15
16 from plc_config import PLCConfiguration
17
18 def GetSnippets(directory):
19     filenames = []
20     if os.path.exists(directory):
21         try:
22             filenames = os.listdir(directory)
23         except OSError, e:
24             raise Exception, "Error when opening %s (%s)" % \
25                   (os.path.join(dir, file), e)
26             
27     ignored = (".bak","~",".rpmsave",".rpmnew",".orig")
28     numberedfiles = {}
29     for filename in filenames:
30         shouldIgnore = False
31         for ignore in ignored:
32             if filename.endswith(ignore):
33                 shouldIgnore = True
34                 break
35
36         if not shouldIgnore:
37             parts = filename.split('-')
38             if len(parts)>=2:
39                 name = '-'.join(parts)
40                 try:
41                     number = int(parts[0])
42                     entry = numberedfiles.get(number,[])
43                     entry.append(name)
44                     numberedfiles[number]=entry
45                 except ValueError:
46                     shouldIgnore = True
47             else:
48                 shouldIgnore = True
49
50         if shouldIgnore:
51             print "db-config: ignoring %s snippet" % filename
52
53     filenames = []
54     keys = numberedfiles.keys()
55     keys.sort()
56     for k in keys:
57         for filename in numberedfiles[k]:
58             filenames.append(filename)
59     return filenames
60
61 def main():
62     cfg = PLCConfiguration()
63     cfg.load()
64     variables = cfg.variables()
65
66     usage="%prog [-- options] [steps]"
67     release_url = "$URL$"
68     parser = OptionParser(usage=usage, version="%prog " + release_url )
69     parser.add_option("-l","--list",dest="list_steps",action="store_true",default=False,
70                       help="Lists available steps")
71     parser.add_option("-v","--verbose",dest="verbose",action="store_true",default=False,
72                       help="Run verbosely")
73
74     (options,steps) = parser.parse_args()
75     
76     # Load variables into dictionaries
77     for category_id, (category, variablelist) in variables.iteritems():
78         globals()[category_id] = dict(zip(variablelist.keys(),
79                                           [variable['value'] for variable in variablelist.values()]))
80
81     directory="/etc/planetlab/db-config.d"
82     snippets = GetSnippets(directory)
83
84     for snippet in snippets:
85         
86         selected=False
87         # no steps provided on the command-line : run them all
88         if not steps:
89             selected=True
90         else:
91             for step in steps:
92                 if snippet.find (step)>=0 : selected=True
93         if not selected: 
94             continue
95         
96         if options.list_steps:
97             if not options.verbose: 
98                 print snippet
99             else: 
100                 print "Found step %s/%s"%(directory,snippet)
101                 os.system("rpm -qf %s/%s"%(directory,snippet))
102             continue
103
104         fullpath = os.path.join(directory, snippet)
105         if options.verbose:
106             print "Running step %s"%fullpath
107         execfile(fullpath)
108
109 if __name__ == '__main__':
110     main()
111
112 # Local variables:
113 # tab-width: 4
114 # mode: python
115 # End: