[538] | 1 | #!/usr/bin/env python2.5 |
---|
| 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 |
---|
| 9 | from subprocess import CalledProcessError, PIPE, Popen |
---|
[549] | 10 | from sys import argv, stdout |
---|
| 11 | from cPickle import dump, loads |
---|
[538] | 12 | |
---|
| 13 | ### |
---|
| 14 | |
---|
[549] | 15 | #import compiler |
---|
| 16 | # |
---|
| 17 | #class Unsafe_Source_Error(Exception): |
---|
| 18 | # def __init__(self,error,descr = None,node = None): |
---|
| 19 | # self.error = error |
---|
| 20 | # self.descr = descr |
---|
| 21 | # self.node = node |
---|
| 22 | # self.lineno = getattr(node,"lineno",None) |
---|
| 23 | # |
---|
| 24 | # def __repr__(self): |
---|
| 25 | # return "Line %d. %s: %s" % (self.lineno, self.error, self.descr) |
---|
| 26 | # __str__ = __repr__ |
---|
| 27 | # |
---|
| 28 | #class SafeEval(object): |
---|
| 29 | # |
---|
| 30 | # def visit(self, node,**kw): |
---|
| 31 | # cls = node.__class__ |
---|
| 32 | # meth = getattr(self,'visit'+cls.__name__,self.default) |
---|
| 33 | # return meth(node, **kw) |
---|
| 34 | # |
---|
| 35 | # def default(self, node, **kw): |
---|
| 36 | # for child in node.getChildNodes(): |
---|
| 37 | # return self.visit(child, **kw) |
---|
| 38 | # |
---|
| 39 | # visitExpression = default |
---|
| 40 | # |
---|
| 41 | # def visitConst(self, node, **kw): |
---|
| 42 | # return node.value |
---|
| 43 | # |
---|
| 44 | # def visitDict(self,node,**kw): |
---|
| 45 | # return dict([(self.visit(k),self.visit(v)) for k,v in node.items]) |
---|
| 46 | # |
---|
| 47 | # def visitTuple(self,node, **kw): |
---|
| 48 | # return tuple(self.visit(i) for i in node.nodes) |
---|
| 49 | # |
---|
| 50 | # def visitList(self,node, **kw): |
---|
| 51 | # return [self.visit(i) for i in node.nodes] |
---|
| 52 | # |
---|
| 53 | #class SafeEvalWithErrors(SafeEval): |
---|
| 54 | # |
---|
| 55 | # def default(self, node, **kw): |
---|
| 56 | # raise Unsafe_Source_Error("Unsupported source construct", |
---|
| 57 | # node.__class__,node) |
---|
| 58 | # |
---|
| 59 | # def visitName(self,node, **kw): |
---|
| 60 | # if node.name == 'None': return None |
---|
| 61 | # raise Unsafe_Source_Error("Strings must be quoted", |
---|
| 62 | # node.name, node) |
---|
| 63 | # |
---|
| 64 | # # Add more specific errors if desired |
---|
| 65 | # |
---|
| 66 | #def safe_eval(source, fail_on_error = True): |
---|
| 67 | # if source.strip() == '': return None |
---|
| 68 | # walker = fail_on_error and SafeEvalWithErrors() or SafeEval() |
---|
| 69 | # try: |
---|
| 70 | # ast = compiler.parse(source,"eval") |
---|
| 71 | # except SyntaxError, err: |
---|
| 72 | # raise |
---|
| 73 | # try: |
---|
| 74 | # return walker.visit(ast) |
---|
| 75 | # except Unsafe_Source_Error, err: |
---|
| 76 | # raise |
---|
[538] | 77 | |
---|
| 78 | ### |
---|
| 79 | |
---|
| 80 | def run(cmd): |
---|
| 81 | """ |
---|
| 82 | Run the given command (a list of program and argument strings) and return the |
---|
| 83 | stdout as a string, raising a CalledProcessError if the program exited with a |
---|
| 84 | non-zero status. |
---|
| 85 | """ |
---|
| 86 | p = Popen(cmd, stdout=PIPE) |
---|
| 87 | stdout = p.communicate()[0] |
---|
| 88 | if p.returncode != 0: raise CalledProcessError(p.returncode, cmd) |
---|
| 89 | return stdout |
---|
| 90 | |
---|
| 91 | def main(argv): |
---|
| 92 | # Query each of the server for their VMs. |
---|
| 93 | # run('kinit -k host/sipb-vm-58.mit.edu'.split()) |
---|
| 94 | # TODO get `servers` from a real list of all the VM hosts (instead of |
---|
| 95 | # hardcoding the list here) |
---|
| 96 | servers = [ 'black-mesa.mit.edu', 'sx-blade-2.mit.edu' ] |
---|
| 97 | # XXX |
---|
[549] | 98 | results = [ loads(run(['remctl', server, 'remote', 'web', 'listvms', '--pickle'])) |
---|
[538] | 99 | for server in servers ] |
---|
| 100 | results = filter( lambda x: x is not None, results ) |
---|
| 101 | |
---|
| 102 | # Merge the results and print. |
---|
| 103 | merged = {} |
---|
| 104 | for result in results: merged.update(result) |
---|
[549] | 105 | if argv[1:] == ['--pickle']: |
---|
| 106 | dump(merged, stdout) |
---|
| 107 | else: |
---|
| 108 | print merged |
---|
| 109 | print '.' |
---|
[538] | 110 | |
---|
| 111 | if __name__ == '__main__': |
---|
| 112 | main(argv) |
---|
| 113 | |
---|
| 114 | # vim:et:sw=2:ts=2 |
---|