f9b3f36502507933c183f25e3e13b6a574e80cd7
[invirt/packages/invirt-remote.git] / files / usr / sbin / sipb-xen-remote-listvms
1 #!/usr/bin/env python2.5
2
3 """
4 Collates the results of listvms from multiple VM servers.  Part of the xvm
5 suite.
6 """
7
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
12
13 ###
14
15 def run(cmd):
16   """
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
19   non-zero status.
20   """
21   p = Popen(cmd, stdout=PIPE)
22   stdout = p.communicate()[0]
23   if p.returncode != 0: raise CalledProcessError(p.returncode, cmd)
24   return stdout
25
26 def main(argv):
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' ]
31   # XXX
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 )
35
36   # Merge the results and print.
37   merged = {}
38   for result in results: merged.update(result)
39   print safe_dump(merged, default_flow_style=False)
40
41 if __name__ == '__main__':
42   main(argv)
43
44 # vim:et:sw=2:ts=2