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

Last change on this file since 575 was 564, checked in by andersk, 17 years ago

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

  • 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 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)
22import sys
23import yaml
24
25###
26
27def main(argv):
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)
39    results = [yaml.load(o, yaml.CSafeLoader) for o in outputs]
40    results = filter(lambda x: x is not None, results)
41
42    # Merge the results and print.
43    merged = {}
44    for result in results:
45        merged.update(result)
46    print yaml.dump(merged, Dumper=yaml.CSafeDumper, default_flow_style=False)
47
48if __name__ == '__main__':
49    main(sys.argv)
50
51# vim:et:sw=2:ts=4
Note: See TracBrowser for help on using the repository browser.