[562] | 1 | #!/usr/bin/python |
---|
[538] | 2 | |
---|
| 3 | """ |
---|
| 4 | Collates the results of listvms from multiple VM servers. Part of the xvm |
---|
| 5 | suite. |
---|
| 6 | """ |
---|
| 7 | |
---|
[562] | 8 | from subprocess import PIPE, Popen |
---|
| 9 | try: |
---|
| 10 | from subprocess import CalledProcessError |
---|
| 11 | except 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] | 22 | import sys |
---|
| 23 | import yaml |
---|
[538] | 24 | |
---|
| 25 | ### |
---|
| 26 | |
---|
| 27 | def 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 | |
---|
| 48 | if __name__ == '__main__': |
---|
[563] | 49 | main(sys.argv) |
---|
[538] | 50 | |
---|
[561] | 51 | # vim:et:sw=2:ts=4 |
---|