review management of relationships - allow to update them with sfaadmin
[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         script = """
28 #!%(python)s
29 import xmlrpclib
30 s = xmlrpclib.ServerProxy('http://127.0.0.1:812')
31 print s.%(method)s%(args)s"""  % locals()
32
33         try:    
34             # write the script to a temporary file
35             f = open(filename, 'w')
36             f.write(script % locals())
37             f.close()
38             # make the file executeable
39             chmod_cmd = "/bin/chmod 775 %(filename)s" % locals()
40             (status, output) = commands.getstatusoutput(chmod_cmd)
41
42             # execute the commad as a slice with root NM privs    
43             cmd = 'su - %(sfa_slice)s -c "%(python)s %(scriptname)s"' % locals()
44             (status, output) = commands.getstatusoutput(cmd)
45             return (status, output)  
46         finally: os.unlink(filename)