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

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

s/CDumper/CSafeDumper/, because it's "Safe"?

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