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
26 class InvirtException(Exception):
31 parser = argparse.ArgumentParser(description='Get values from invirt configuration file')
32 parser.add_argument('-r', '--refresh', action='store_true', help='Force regenerate the cache')
33 parser.add_argument('-l', '--ls', action='store_true', help='List children of node')
34 parser.add_argument('path', nargs='?', default='', help='Path of value to get')
36 args = parser.parse_args()
38 components = args.path.split('.')
43 conf = config.load(opts.refresh)
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))
61 if type(conf) not in (dict, list):
62 raise invirt_exception(
63 '%s: node has no children (atomic datum)'
64 % '.'.join(components))
65 if type(conf) == list:
66 for i in xrange(len(conf)):
69 for k in conf.iterkeys():
72 if type(conf) not in (dict, list):
76 yaml.dump(conf, stdout,
77 Dumper=yaml.CSafeDumper, default_flow_style=False)
78 except invirt_exception, ex:
82 if __name__ == '__main__':