1 #!/usr/bin/env python2.5
4 Collates the results of listvms from multiple VM servers. Part of the xvm
8 from itertools import chain
9 from subprocess import CalledProcessError, PIPE, Popen
10 from sys import argv, stdout
11 from yaml import safe_dump, safe_load
17 Run the given command (a list of program and argument strings) and return the
18 stdout as a string, raising a CalledProcessError if the program exited with a
21 p = Popen(cmd, stdout=PIPE)
22 stdout = p.communicate()[0]
23 if p.returncode != 0: raise CalledProcessError(p.returncode, cmd)
27 # Query each of the server for their VMs.
28 # TODO get `servers` from a real list of all the VM hosts (instead of
29 # hardcoding the list here)
30 servers = [ 'black-mesa.mit.edu', 'sx-blade-2.mit.edu' ]
32 results = [ safe_load(run(['remctl', server, 'remote', 'web', 'listvms']))
33 for server in servers ]
34 results = filter( lambda x: x is not None, results )
36 # Merge the results and print.
38 for result in results: merged.update(result)
39 print safe_dump(merged, default_flow_style=False)
41 if __name__ == '__main__':