bac659626eb46cbb9ba22f292bb2ea21505e4933
[invirt/packages/invirt-base.git] / files / usr / sbin / invirt-getconf
1 #!/usr/bin/env python
2
3 """
4 invirt-getconf loads an invirt configuration file (either the original YAML
5 source or the faster-to-load JSON cache) and prints the configuration option
6 with the given name (key).  Keys are dot-separated paths into the YAML
7 configuration tree.  List indexes (0-based) are also treated as path
8 components.
9
10 (Due to this path language, certain restrictions are placed on the keys used in
11 the YAML configuration; e.g., they cannot contain dots.)
12
13 Examples:
14
15   invirt-getconf db.uri
16   invirt-getconf authn.0.type
17 """
18
19 from invirt.config import load
20 from sys import argv, exit, stderr
21 from optparse import OptionParser
22
23 class invirt_exception(Exception): pass
24
25 def main(argv):
26     try:
27         parser = OptionParser(usage = '%prog [options] key',
28                 description = __doc__.strip().split('\n\n')[0])
29         parser.add_option('-s', '--src',
30                 default = '/etc/invirt/master.yaml',
31                 help = 'the source YAML configuration file to read from')
32         parser.add_option('-c', '--cache',
33                 default = '/var/lib/invirt/invirt.json',
34                 help = 'path to the JSON cache')
35         parser.add_option('-r', '--refresh',
36                 action = 'store_true',
37                 help = 'force the cache to be regenerated')
38         parser.add_option('-l', '--ls',
39                 action = 'store_true',
40                 help = 'list node\'s children')
41         opts, args = parser.parse_args()
42
43         try: [key] = args
44         except: raise invirt_exception(__doc__.strip())
45
46         conf = load(opts.src, opts.cache, opts.refresh)
47         components = key.split('.')
48         for i, component in enumerate(components):
49             progress = '.'.join(components[:i])
50             if type(conf) not in [dict, list]:
51                 raise invirt_exception(
52                         '%s: node has no children (atomic datum)' % progress)
53             if type(conf) == list:
54                 try: component = int(component)
55                 except: raise invirt_exception(
56                         '%s: node a list; integer path component required, '
57                         'but got "%s"' % (progress, component))
58             try: conf = conf[component]
59             except KeyError: raise invirt_exception(
60                     '%s: key "%s" not found' % (progress, component))
61             except IndexError: raise invirt_exception(
62                     '%s: index %s out of range' % (progress, component))
63         if opts.ls:
64             if type(conf) not in [dict, list]:
65                 raise invirt_exception(
66                         '%s: node has no children (atomic datum)' % progress)
67             if type(conf) == list:
68                 for i in xrange(len(conf)):
69                     print i
70             else:
71                 for k in conf.iterkeys():
72                     print k
73         else:
74             print conf
75     except invirt_exception, ex:
76         print >> stderr, ex
77         return 1
78
79 if __name__ == '__main__':
80     exit(main(argv))
81
82 # vim:et:sw=4:ts=4