| [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 |  | 
|---|
 | 8 | from itertools import chain | 
|---|
| [562] | 9 | from subprocess import PIPE, Popen | 
|---|
 | 10 | try: | 
|---|
 | 11 |     from subprocess import CalledProcessError | 
|---|
 | 12 | except 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) | 
|---|
| [549] | 23 | from sys import argv, stdout | 
|---|
| [552] | 24 | from yaml import safe_dump, safe_load | 
|---|
| [538] | 25 |  | 
|---|
 | 26 | ### | 
|---|
 | 27 |  | 
|---|
 | 28 | def main(argv): | 
|---|
| [561] | 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) | 
|---|
| [538] | 42 |  | 
|---|
| [561] | 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) | 
|---|
| [538] | 48 |  | 
|---|
 | 49 | if __name__ == '__main__': | 
|---|
| [561] | 50 |     main(argv) | 
|---|
| [538] | 51 |  | 
|---|
| [561] | 52 | # vim:et:sw=2:ts=4 | 
|---|