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
RevLine 
[771]1#!/usr/bin/env python
[726]2
[733]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
[726]18from invirt.config import load
[733]19from sys import argv, exit, stderr
20from optparse import OptionParser
[726]21
[771]22class invirt_exception(Exception): pass
[733]23
[771]24def main(argv):
[733]25    try:
[771]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()
[726]38
[733]39        try: [key] = args
[771]40        except: raise invirt_exception(__doc__.strip())
[726]41
[771]42        conf = load(opts.src, opts.cache, opts.refresh)
[733]43        components = key.split('.')
[771]44        for i, component in enumerate(components):
45            progress = '.'.join(components[:i])
46            if type(conf) not in [dict, list]:
[733]47                raise invirt_exception(
[771]48                        '%s: node has no children (atomic datum)' % progress)
49            if type(conf) == list:
50                try: component = int(component)
[733]51                except: raise invirt_exception(
[771]52                        '%s: node a list; integer path component required, '
53                        'but got "%s"' % (progress, component))
54            try: conf = conf[component]
[733]55            except KeyError: raise invirt_exception(
[771]56                    '%s: key "%s" not found' % (progress, component))
57            except IndexError: raise invirt_exception(
58                    '%s: index %s out of range' % (progress, component))
[733]59        print conf
[771]60    except (invirt_exception, OSError), ex:
[733]61        print >> stderr, ex
62        return 1
63
[771]64if __name__ == '__main__':
65    exit(main(argv))
[733]66
[726]67# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.