source: trunk/packages/sipb-xen-base/files/usr/sbin/invirt-getconf @ 772

Last change on this file since 772 was 771, checked in by y_z, 16 years ago
  • added timestamp-based JSON caching of configuration for faster loading
  • exposed (more) options to command-line frontend
  • improved error messages/handling/help
  • removed all python 2.5-isms
  • reformatted to fit project style conventions
  • Property svn:executable set to *
File size: 2.4 KB
Line 
1#!/usr/bin/env python
2
3"""
4invirt-getconf [-f FILE] KEY prints the configuration the option named KEY from
5the invirt configuration file FILE.  Keys are dot-separated paths into the YAML
6configuration tree.  List indexes (0-based) are also treated as path
7components.
8
9(Due to this path language, certain restrictions are placed on the keys used in
10the YAML configuration, e.g. they cannot contain dots.)
11
12Examples:
13
14  invirt-getconf db.uri
15  invirt-getconf authn.0.type
16"""
17
18from invirt.config import load
19from sys import argv, exit, stderr
20from optparse import OptionParser
21
22class invirt_exception(Exception): pass
23
24def main(argv):
25    try:
26        parser = OptionParser(usage = '%prog [options] key',
27                description = __doc__.strip().split('\n\n')[0])
28        parser.add_option('-s', '--src',
29                default = '/etc/invirt/master.yaml',
30                help = 'the source YAML configuration file to read from')
31        parser.add_option('-c', '--cache',
32                default = '/var/lib/invirt/invirt.json',
33                help = 'path to the JSON cache')
34        parser.add_option('-r', '--refresh',
35                action = 'store_true',
36                help = 'force the cache to be regenerated')
37        opts, args = parser.parse_args()
38
39        try: [key] = args
40        except: raise invirt_exception(__doc__.strip())
41
42        conf = load(opts.src, opts.cache, opts.refresh)
43        components = key.split('.')
44        for i, component in enumerate(components):
45            progress = '.'.join(components[:i])
46            if type(conf) not in [dict, list]:
47                raise invirt_exception(
48                        '%s: node has no children (atomic datum)' % progress)
49            if type(conf) == list:
50                try: component = int(component)
51                except: raise invirt_exception(
52                        '%s: node a list; integer path component required, '
53                        'but got "%s"' % (progress, component))
54            try: conf = conf[component]
55            except KeyError: raise invirt_exception(
56                    '%s: key "%s" not found' % (progress, component))
57            except IndexError: raise invirt_exception(
58                    '%s: index %s out of range' % (progress, component))
59        print conf
60    except (invirt_exception, OSError), ex:
61        print >> stderr, ex
62        return 1
63
64if __name__ == '__main__':
65    exit(main(argv))
66
67# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.