e2e2c12f9e6868a708c53614832f23722d05b66e
[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 # 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
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     # ignore files that contain either ~ or .
28     ignore_tokens = ("~",".")
29     numberedfiles = {}
30     for filename in filenames:
31         ignore = False
32         for token in ignore_tokens:
33             if filename.find(token)>=0:
34                 ignore = True
35                 break
36
37         if not ignore:
38             parts = filename.split('-')
39             if len(parts)>=2:
40                 name = '-'.join(parts)
41                 try:
42                     number = int(parts[0])
43                     entry = numberedfiles.get(number,[])
44                     entry.append(name)
45                     numberedfiles[number]=entry
46                 except ValueError:
47                     ignore = True
48             else:
49                 ignore = True
50
51         if ignore:
52             print "db-config: ignored %s snippet" % filename
53
54     filenames = []
55     keys = numberedfiles.keys()
56     keys.sort()
57     for k in keys:
58         for filename in numberedfiles[k]:
59             filenames.append(filename)
60     return filenames
61
62 def main():
63     cfg = PLCConfiguration()
64     cfg.load()
65     variables = cfg.variables()
66
67     usage="%prog [-- options] [steps]"
68     release_url = "$URL$"
69     parser = OptionParser(usage=usage, version="%prog " + release_url )
70     parser.add_option("-l","--list",dest="list_steps",action="store_true",default=False,
71                       help="Lists available steps")
72     parser.add_option("-v","--verbose",dest="verbose",action="store_true",default=False,
73                       help="Run verbosely")
74
75     (options,steps) = parser.parse_args()
76     
77     # Load variables into dictionaries
78     for category_id, (category, variablelist) in variables.iteritems():
79         globals()[category_id] = dict(zip(variablelist.keys(),
80                                           [variable['value'] for variable in variablelist.values()]))
81
82     directory="/etc/planetlab/db-config.d"
83     snippets = GetSnippets(directory)
84
85     for snippet in snippets:
86         
87         selected=False
88         # no steps provided on the command-line : run them all
89         if not steps:
90             selected=True
91         else:
92             for step in steps:
93                 if snippet.find (step)>=0 : selected=True
94         if not selected: 
95             continue
96         
97         if options.list_steps:
98             if not options.verbose: 
99                 print snippet
100             else: 
101                 print "Found step %s/%s"%(directory,snippet)
102                 os.system("rpm -qf %s/%s"%(directory,snippet))
103             continue
104
105         fullpath = os.path.join(directory, snippet)
106         if options.verbose:
107             print "Running step %s"%fullpath
108         execfile(fullpath)
109
110 if __name__ == '__main__':
111     main()
112
113 # Local variables:
114 # tab-width: 4
115 # mode: python
116 # End: