ebf84087c461f4baec3d8562be7848c62c078f28
[sfa.git] / sfa / util / nodemanager.py
1 import tempfile
2 import commands
3 import os
4
5 class NodeManager:
6
7     method = None
8
9     def __getattr__(self, method):
10         self.method = method
11         return self.__call__
12     
13     def __call__(self, *args):
14         method = self.method
15         sfa_slice = "pl_genicw"
16         python = "/usr/bin/python"
17         vserver_path = "/vservers/%s" % (sfa_slice)
18         script_path = "/tmp/"
19         path = "%(vserver_path)s/%(script_path)s" % locals()
20         (fd, filename) = tempfile.mkstemp(dir=path)        
21         scriptname = script_path + os.sep + filename.split(os.sep)[-1:][0]
22         # define the script to execute
23         script = """
24 #!%(python)s
25 import xmlrpclib
26 s = xmlrpclib.ServerProxy('http://127.0.0.1:812')
27 print s.%(method)s%(args)s"""  % locals()
28
29         try:    
30             # write the script to a temporary file
31             f = open(filename, 'w')
32             f.write(script % locals())
33             f.close()
34             # make the file executeable
35             chmod_cmd = "/bin/chmod 775 %(filename)s" % locals()
36             print chmod_cmd
37             (status, output) = commands.getstatusoutput(chmod_cmd)
38
39             # execute the commad as a slice with root NM privs    
40             cmd = 'su - %(sfa_slice)s -c "%(python)s %(scriptname)s"' % locals()
41             print cmd
42             (status, output) = commands.getstatusoutput(cmd)
43             return (status, output)  
44         finally: os.unlink(filename)