fix syntax error
[sfa.git] / sfa / methods / get_key.py
1 ### $Id:  $
2 ### $URL:  $
3 import os
4 import tempfile
5 from sfa.util.faults import *
6 from sfa.util.misc import *
7 from sfa.util.method import Method
8 from sfa.util.parameter import Parameter, Mixed
9 from sfa.trust.auth import Auth
10
11 class request_key(Method):
12     """
13     Generate a new keypair and gid for requesting caller (component).     
14     @return 1 If successful  
15     """
16
17     interfaces = ['registry']
18     
19     accepts = []
20
21     returns = Parameter(int, "1 if successful, faults otherwise")
22     
23     def call(self):
24         # verify that the callers's ip address exist in the db and is an inteface
25         # for a node in the db
26         (ip, port) = self.api.remote_addr
27         interfaces = self.api.plshell(self.api.plauth, {'ip': ip}, ['node_id'])
28         if not interfaces:
29             raise NonExistingRecord("no such ip %(ip)s" % locals())
30         nodes = self.api.plshell(self.api.plauth, [interfaces[0]['node_id']], ['node_id', 'hostname'])
31         if not nodes:
32             raise NonExistingRecord("no such node using ip %(ip)s" % locals())
33         node = nodes[0]
34        
35         # look up the sfa record
36         table = GeniTable()
37         records = table.find({'type': 'node', 'pointer': node['node_id']})
38         if not records:
39             raise RecordNotFound("pointer:" + str(node['node_id']))  
40         record = records[0]
41         
42         # generate a new keypair and gid
43         uuid = create_uuid()
44         pkey = Keypair(create=True)
45         gid_object = self.api.auth.hierarchy.create_gid(record['hrn'], uuid, pkey)
46         gid = gid_object.save_to_string(save_parents=True)
47         record['gid'] = gid
48         record.set_gid(gid)
49
50         # update the record
51         table.update(record)
52   
53         # attempt the scp the key
54         # this will only work for planetlab based compoenents
55         (fd, filename) = tempfile.mkstemp() 
56         pkey.save_to_file(filename)
57         host = node['hostname']
58         dest="/etc/sfa/nodekey.key" 
59         identity = "/etc/planetlab/root_ssh_key.pub"
60         os.system("scp -i %(identity)s %(filename)s root@%(host)s:%(dest)s" % locals()
61         os.remove(filename)
62
63         return 1