more consistency between API method names and corresponding manager implementation
[sfa.git] / sfa / methods / get_key.py
1 import os
2 import tempfile
3 import commands
4 from sfa.util.faults import NonExistingRecord, RecordNotFound
5 from sfa.util.xrn import hrn_to_urn
6 from sfa.util.method import Method
7 from sfa.util.parameter import Parameter
8 from sfa.util.table import SfaTable
9 from sfa.trust.certificate import Keypair
10 from sfa.trust.gid import create_uuid
11
12 class get_key(Method):
13     """
14     Generate a new keypair and gid for requesting caller (component).     
15     @return 1 If successful  
16     """
17
18     interfaces = ['registry']
19     
20     accepts = []
21
22     returns = Parameter(int, "1 if successful, faults otherwise")
23     
24     def call(self):
25         # verify that the callers's ip address exist in the db and is an interface
26         # for a node in the db
27         (ip, port) = self.api.remote_addr
28         interfaces = self.api.driver.GetInterfaces({'ip': ip}, ['node_id'])
29         if not interfaces:
30             raise NonExistingRecord("no such ip %(ip)s" % locals())
31         nodes = self.api.driver.GetNodes([interfaces[0]['node_id']], ['node_id', 'hostname'])
32         if not nodes:
33             raise NonExistingRecord("no such node using ip %(ip)s" % locals())
34         node = nodes[0]
35        
36         # look up the sfa record
37         table = SfaTable()
38         records = table.findObjects({'type': 'node', 'pointer': node['node_id']})
39         if not records:
40             raise RecordNotFound("pointer:" + str(node['node_id']))  
41         record = records[0]
42         
43         # generate a new keypair and gid
44         uuid = create_uuid()
45         pkey = Keypair(create=True)
46         urn = hrn_to_urn(record['hrn'], record['type'])
47         gid_object = self.api.auth.hierarchy.create_gid(urn, uuid, pkey)
48         gid = gid_object.save_to_string(save_parents=True)
49         record['gid'] = gid
50         record.set_gid(gid)
51
52         # update the record
53         table.update(record)
54   
55         # attempt the scp the key
56         # and gid onto the node
57         # this will only work for planetlab based components
58         (kfd, key_filename) = tempfile.mkstemp() 
59         (gfd, gid_filename) = tempfile.mkstemp() 
60         pkey.save_to_file(key_filename)
61         gid_object.save_to_file(gid_filename, save_parents=True)
62         host = node['hostname']
63         key_dest="/etc/sfa/node.key"
64         gid_dest="/etc/sfa/node.gid" 
65         scp = "/usr/bin/scp" 
66         #identity = "/etc/planetlab/root_ssh_key.rsa"
67         identity = "/etc/sfa/root_ssh_key"
68         scp_options=" -i %(identity)s " % locals()
69         scp_options+="-o StrictHostKeyChecking=no " % locals()
70         scp_key_command="%(scp)s %(scp_options)s %(key_filename)s root@%(host)s:%(key_dest)s" %\
71                          locals()
72         scp_gid_command="%(scp)s %(scp_options)s %(gid_filename)s root@%(host)s:%(gid_dest)s" %\
73                          locals()    
74
75         all_commands = [scp_key_command, scp_gid_command]
76         
77         for command in all_commands:
78             (status, output) = commands.getstatusoutput(command)
79             if status:
80                 raise Exception, output
81
82         for filename in [key_filename, gid_filename]:
83             os.unlink(filename)
84
85         return 1