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

Last change on this file since 562 was 562, checked in by andersk, 16 years ago

Use default version of python (currently 2.4), not 2.5.

  • Property svn:executable set to *
File size: 1.6 KB
Line 
1#!/usr/bin/python
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 PIPE, Popen
10try:
11    from subprocess import CalledProcessError
12except ImportError:
13    # Python 2.4 doesn't implement CalledProcessError
14    class CalledProcessError(Exception):
15        """This exception is raised when a process run by check_call() returns
16        a non-zero exit status. The exit status will be stored in the
17        returncode attribute."""
18        def __init__(self, returncode, cmd):
19            self.returncode = returncode
20            self.cmd = cmd
21        def __str__(self):
22            return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
23from sys import argv, stdout
24from yaml import safe_dump, safe_load
25
26###
27
28def main(argv):
29    # Query each of the server for their VMs.
30    # TODO get `servers` from a real list of all the VM hosts (instead of
31    # hardcoding the list here)
32    servers = ['black-mesa.mit.edu', 'sx-blade-2.mit.edu']
33    # XXX
34    pipes = [Popen(['remctl', server, 'remote', 'web', 'listvms'], stdout=PIPE)
35             for server in servers]
36    outputs = [p.communicate()[0] for p in pipes]
37    for p in pipes:
38        if p.returncode != 0:
39            raise CalledProcessError(p.returncode, cmd)
40    results = [safe_load(o) for o in outputs]
41    results = filter(lambda x: x is not None, results)
42
43    # Merge the results and print.
44    merged = {}
45    for result in results:
46        merged.update(result)
47    print safe_dump(merged, default_flow_style=False)
48
49if __name__ == '__main__':
50    main(argv)
51
52# vim:et:sw=2:ts=4
Note: See TracBrowser for help on using the repository browser.