sfaprotocol is renamed into sfaserverproxy, with class SfaServerProxy
[sfa.git] / sfa / plc / plcomponentdriver.py
1 import os
2 import tempfile
3
4 from sfa.client.sfaserverproxy import SfaServerProxy
5 from sfa.plc.nodemanager import NodeManager
6
7 from sfa.trust.credential import Credential
8 from sfa.trust.certificate import Certificate, Keypair
9 from sfa.trust.gid import GID
10
11 ####################
12 class PlComponentDriver:
13     """
14     This class is the type for the toplevel 'api' object 
15     when running the component manager inside a planetlab node.
16     As such it runs an SFA-compliant interface and thus inherits SfaApi
17     However the fact that we run inside a planetlab nodes requires 
18     some tweaks as compared with a service running in the infrastructure.
19     """
20
21     def __init__ (self, config):
22         self.nodemanager = NodeManager(config)
23
24     def sliver_exists(self):
25         sliver_dict = self.nodemanager.GetXIDs()
26         ### xxx slicename is undefined
27         if slicename in sliver_dict.keys():
28             return True
29         else:
30             return False
31
32     def get_registry(self):
33         addr, port = self.config.SFA_REGISTRY_HOST, self.config.SFA_REGISTRY_PORT
34         url = "http://%(addr)s:%(port)s" % locals()
35         ### xxx this would require access to the api...
36         server = SfaServerProxy(url, self.key_file, self.cert_file)
37         return server
38
39     def get_node_key(self):
40         # this call requires no authentication,
41         # so we can generate a random keypair here
42         subject="component"
43         (kfd, keyfile) = tempfile.mkstemp()
44         (cfd, certfile) = tempfile.mkstemp()
45         key = Keypair(create=True)
46         key.save_to_file(keyfile)
47         cert = Certificate(subject=subject)
48         cert.set_issuer(key=key, subject=subject)
49         cert.set_pubkey(key)
50         cert.sign()
51         cert.save_to_file(certfile)
52         registry = self.get_registry()
53         # the registry will scp the key onto the node
54         registry.get_key_from_incoming_ip()        
55
56     # override the method in SfaApi
57     def getCredential(self):
58         """
59         Get our credential from a remote registry
60         """
61         path = self.config.SFA_DATA_DIR
62         config_dir = self.config.config_path
63         cred_filename = path + os.sep + 'node.cred'
64         try:
65             credential = Credential(filename = cred_filename)
66             return credential.save_to_string(save_parents=True)
67         except IOError:
68             node_pkey_file = config_dir + os.sep + "node.key"
69             node_gid_file = config_dir + os.sep + "node.gid"
70             cert_filename = path + os.sep + 'server.cert'
71             if not os.path.exists(node_pkey_file) or \
72                not os.path.exists(node_gid_file):
73                 self.get_node_key()
74
75             # get node's hrn
76             gid = GID(filename=node_gid_file)
77             hrn = gid.get_hrn()
78             # get credential from registry
79             cert_str = Certificate(filename=cert_filename).save_to_string(save_parents=True)
80             registry = self.get_registry()
81             cred = registry.GetSelfCredential(cert_str, hrn, 'node')
82             # xxx credfile is undefined
83             Credential(string=cred).save_to_file(credfile, save_parents=True)            
84
85             return cred
86
87     def clean_key_cred(self):
88         """
89         remove the existing keypair and cred  and generate new ones
90         """
91         files = ["server.key", "server.cert", "node.cred"]
92         for f in files:
93             # xxx KEYDIR is undefined, could be meant to be "/var/lib/sfa/" from sfa_component_setup.py
94             filepath = KEYDIR + os.sep + f
95             if os.path.isfile(filepath):
96                 os.unlink(f)
97
98         # install the new key pair
99         # GetCredential will take care of generating the new keypair
100         # and credential
101         self.get_node_key()
102         self.getCredential()