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
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.)
16 invirt-getconf hosts.0.ip
21 parser = OptionParser(usage = '%prog [options] key',
22 description = __doc__.strip().split('\n\n')[0])
23 parser.add_option('-r', '--refresh',
24 action = 'store_true',
25 help = 'force the cache to be regenerated')
26 parser.add_option('-l', '--ls',
27 action = 'store_true',
28 help = 'list node\'s children')
29 opts, args = parser.parse_args()
32 raise invirt_exception(__doc__.strip())
33 elif args and args[0]:
34 components = args[0].split('.')
42 class InvirtException(Exception):
48 conf = config.load(opts.refresh)
49 for i, component in enumerate(components):
50 progress = '.'.join(components[:i])
51 if type(conf) not in (dict, list):
52 raise invirt_exception(
53 '%s: node has no children (atomic datum)' % progress)
54 if type(conf) == list:
55 try: component = int(component)
56 except: raise invirt_exception(
57 '%s: node a list; integer path component required, '
58 'but got "%s"' % (progress, component))
59 try: conf = conf[component]
60 except KeyError: raise invirt_exception(
61 '%s: key "%s" not found' % (progress, component))
62 except IndexError: raise invirt_exception(
63 '%s: index %s out of range' % (progress, component))
66 if type(conf) not in (dict, list):
67 raise invirt_exception(
68 '%s: node has no children (atomic datum)'
69 % '.'.join(components))
70 if type(conf) == list:
71 for i in xrange(len(conf)):
74 for k in conf.iterkeys():
77 if type(conf) not in (dict, list):
81 yaml.dump(conf, stdout,
82 Dumper=yaml.CSafeDumper, default_flow_style=False)
83 except invirt_exception, ex:
87 if __name__ == '__main__':