source: trunk/packages/sipb-xen-remote-server/files/usr/sbin/sipb-xen-remote-listvms @ 556

Last change on this file since 556 was 552, checked in by broder, 17 years ago

And round 3 of using YAML

  • Property svn:executable set to *
File size: 1.2 KB
Line 
1#!/usr/bin/env python2.5
2
3"""
4Collates the results of listvms from multiple VM servers.  Part of the xvm
5suite.
6"""
7
8from itertools import chain
9from subprocess import CalledProcessError, PIPE, Popen
10from sys import argv, stdout
11from yaml import safe_dump, safe_load
12
13###
14
15def 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
26def 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
41if __name__ == '__main__':
42  main(argv)
43
44# vim:et:sw=2:ts=2
Note: See TracBrowser for help on using the repository browser.