split plc.d/ and db-config.d between myplc and plcapi modules as a first step
[myplc.git] / 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 # $HeadURL$
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,args) = 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     if options.list_steps:
85         for snippet in snippets:
86             if not options.verbose: 
87                 print snippet
88             else: 
89                 print "Found step %s/%s"%(directory,snippet)
90                 os.system("rpm -qf %s/%s"%(directory,snippet))
91         sys.exit(0)
92     
93     for snippet in snippets:
94         fullpath = os.path.join(directory, snippet)
95         if options.verbose:
96             print "Running step %s"%fullpath
97         execfile(fullpath)
98
99 if __name__ == '__main__':
100     main()
101
102 # Local variables:
103 # tab-width: 4
104 # mode: python
105 # End: