Setting tag myplc-5.3-5
[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     parser = OptionParser(usage=usage )
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: