From: Tony Mack Date: Tue, 29 Dec 2009 22:17:53 +0000 (+0000) Subject: initial checkin X-Git-Tag: sfa-0.9-7~73 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=8ee86748108d70c354916ce3d653f9472fbcfea5;p=sfa.git initial checkin --- diff --git a/sfa/util/nodemanager.py b/sfa/util/nodemanager.py new file mode 100644 index 00000000..ebf84087 --- /dev/null +++ b/sfa/util/nodemanager.py @@ -0,0 +1,44 @@ +import tempfile +import commands +import os + +class NodeManager: + + method = None + + def __getattr__(self, method): + self.method = method + return self.__call__ + + def __call__(self, *args): + method = self.method + sfa_slice = "pl_genicw" + python = "/usr/bin/python" + vserver_path = "/vservers/%s" % (sfa_slice) + script_path = "/tmp/" + path = "%(vserver_path)s/%(script_path)s" % locals() + (fd, filename) = tempfile.mkstemp(dir=path) + scriptname = script_path + os.sep + filename.split(os.sep)[-1:][0] + # define the script to execute + script = """ +#!%(python)s +import xmlrpclib +s = xmlrpclib.ServerProxy('http://127.0.0.1:812') +print s.%(method)s%(args)s""" % locals() + + try: + # write the script to a temporary file + f = open(filename, 'w') + f.write(script % locals()) + f.close() + # make the file executeable + chmod_cmd = "/bin/chmod 775 %(filename)s" % locals() + print chmod_cmd + (status, output) = commands.getstatusoutput(chmod_cmd) + + # execute the commad as a slice with root NM privs + cmd = 'su - %(sfa_slice)s -c "%(python)s %(scriptname)s"' % locals() + print cmd + (status, output) = commands.getstatusoutput(cmd) + return (status, output) + finally: os.unlink(filename)