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