1 #!/usr/bin/env python2.5
4 Collates the results of listvms from multiple VM servers. Part of the xvm
8 from itertools import chain
9 from subprocess import CalledProcessError, PIPE, Popen
10 from sys import argv, stdout
11 from cPickle import dump, loads
17 #class Unsafe_Source_Error(Exception):
18 # def __init__(self,error,descr = None,node = None):
22 # self.lineno = getattr(node,"lineno",None)
25 # return "Line %d. %s: %s" % (self.lineno, self.error, self.descr)
28 #class SafeEval(object):
30 # def visit(self, node,**kw):
31 # cls = node.__class__
32 # meth = getattr(self,'visit'+cls.__name__,self.default)
33 # return meth(node, **kw)
35 # def default(self, node, **kw):
36 # for child in node.getChildNodes():
37 # return self.visit(child, **kw)
39 # visitExpression = default
41 # def visitConst(self, node, **kw):
44 # def visitDict(self,node,**kw):
45 # return dict([(self.visit(k),self.visit(v)) for k,v in node.items])
47 # def visitTuple(self,node, **kw):
48 # return tuple(self.visit(i) for i in node.nodes)
50 # def visitList(self,node, **kw):
51 # return [self.visit(i) for i in node.nodes]
53 #class SafeEvalWithErrors(SafeEval):
55 # def default(self, node, **kw):
56 # raise Unsafe_Source_Error("Unsupported source construct",
57 # node.__class__,node)
59 # def visitName(self,node, **kw):
60 # if node.name == 'None': return None
61 # raise Unsafe_Source_Error("Strings must be quoted",
64 # # Add more specific errors if desired
66 #def safe_eval(source, fail_on_error = True):
67 # if source.strip() == '': return None
68 # walker = fail_on_error and SafeEvalWithErrors() or SafeEval()
70 # ast = compiler.parse(source,"eval")
71 # except SyntaxError, err:
74 # return walker.visit(ast)
75 # except Unsafe_Source_Error, err:
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
86 p = Popen(cmd, stdout=PIPE)
87 stdout = p.communicate()[0]
88 if p.returncode != 0: raise CalledProcessError(p.returncode, cmd)
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' ]
98 results = [ loads(run(['remctl', server, 'remote', 'web', 'listvms', '--pickle']))
99 for server in servers ]
100 results = filter( lambda x: x is not None, results )
102 # Merge the results and print.
104 for result in results: merged.update(result)
105 if argv[1:] == ['--pickle']:
111 if __name__ == '__main__':