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
16 class Unsafe_Source_Error(Exception):
17 def __init__(self,error,descr = None,node = None):
21 self.lineno = getattr(node,"lineno",None)
24 return "Line %d. %s: %s" % (self.lineno, self.error, self.descr)
27 class SafeEval(object):
29 def visit(self, node,**kw):
31 meth = getattr(self,'visit'+cls.__name__,self.default)
32 return meth(node, **kw)
34 def default(self, node, **kw):
35 for child in node.getChildNodes():
36 return self.visit(child, **kw)
38 visitExpression = default
40 def visitConst(self, node, **kw):
43 def visitDict(self,node,**kw):
44 return dict([(self.visit(k),self.visit(v)) for k,v in node.items])
46 def visitTuple(self,node, **kw):
47 return tuple(self.visit(i) for i in node.nodes)
49 def visitList(self,node, **kw):
50 return [self.visit(i) for i in node.nodes]
52 class SafeEvalWithErrors(SafeEval):
54 def default(self, node, **kw):
55 raise Unsafe_Source_Error("Unsupported source construct",
58 def visitName(self,node, **kw):
59 if node.name == 'None': return None
60 raise Unsafe_Source_Error("Strings must be quoted",
63 # Add more specific errors if desired
65 def safe_eval(source, fail_on_error = True):
66 if source.strip() == '': return None
67 walker = fail_on_error and SafeEvalWithErrors() or SafeEval()
69 ast = compiler.parse(source,"eval")
70 except SyntaxError, err:
73 return walker.visit(ast)
74 except Unsafe_Source_Error, err:
81 Run the given command (a list of program and argument strings) and return the
82 stdout as a string, raising a CalledProcessError if the program exited with a
85 p = Popen(cmd, stdout=PIPE)
86 stdout = p.communicate()[0]
87 if p.returncode != 0: raise CalledProcessError(p.returncode, cmd)
91 # Query each of the server for their VMs.
92 # run('kinit -k host/sipb-vm-58.mit.edu'.split())
93 # TODO get `servers` from a real list of all the VM hosts (instead of
94 # hardcoding the list here)
95 servers = [ 'black-mesa.mit.edu', 'sx-blade-2.mit.edu' ]
97 results = [ safe_eval(run(['remctl', server, 'remote', 'web', 'listvms'] + argv[1:]))
98 for server in servers ]
99 results = filter( lambda x: x is not None, results )
101 # Merge the results and print.
103 for result in results: merged.update(result)
107 if __name__ == '__main__':