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 PathResolutionException(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('.')
40 conf = invirt.config.load(args.refresh)
41 for i, component in enumerate(components):
42 progress = '.'.join(components[:i])
44 if isinstance(conf, list):
46 component = int(component)
48 raise PathResolutionException(f'{progress}: node is a list; integer path component required, '
49 'but got "{component}"')
52 conf = conf[component]
54 raise PathResolutionException(f'{progress}: index {component} out of range')
55 elif isinstance(conf, dict):
57 conf = conf[component]
59 raise PathResolutionException(f'{progress}: key "{component}" not found')
61 raise PathResolutionException(f'{progress}: node has no children (atomic datum)')
64 if isinstance(conf, list):
65 for i in range(len(conf)):
67 elif isinstance(conf, dict):
71 raise PathResolutionException(f'{".".join(components)}: node has no children (atomic datum)')
73 if isinstance(conf, (dict, list)):
76 yaml.dump(conf, sys.stdout, Dumper=yaml.CSafeDumper, default_flow_style=False)
79 if __name__ == '__main__':