99575c68f4b8610544bb57ba05ef72b76ec70556
[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 __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         ### WARNING -- hardwired name needs to be computed
19         sfa_slice_prefix = self.config.SFA_CM_SLICE_PREFIX 
20         sfa_slice = sfa_slice_prefix + "_sfacm"
21         python = "/usr/bin/python"
22         vserver_path = "/vservers/%s" % (sfa_slice)
23         script_path = "/tmp/"
24         path = "%(vserver_path)s/%(script_path)s" % locals()
25         (fd, filename) = tempfile.mkstemp(dir=path)        
26         scriptname = script_path + os.sep + filename.split(os.sep)[-1:][0]
27         # define the script to execute
28         script = """
29 #!%(python)s
30 import xmlrpclib
31 s = xmlrpclib.ServerProxy('http://127.0.0.1:812')
32 print s.%(method)s%(args)s"""  % locals()
33
34         try:    
35             # write the script to a temporary file
36             f = open(filename, 'w')
37             f.write(script % locals())
38             f.close()
39             # make the file executeable
40             chmod_cmd = "/bin/chmod 775 %(filename)s" % locals()
41             (status, output) = commands.getstatusoutput(chmod_cmd)
42
43             # execute the commad as a slice with root NM privs    
44             cmd = 'su - %(sfa_slice)s -c "%(python)s %(scriptname)s"' % locals()
45             (status, output) = commands.getstatusoutput(cmd)
46             return (status, output)  
47         finally: os.unlink(filename)