6 from sfa.util.faults import *
7 from sfa.util.misc import *
8 from sfa.util.method import Method
9 from sfa.util.parameter import Parameter, Mixed
10 from sfa.trust.auth import Auth
11 from sfa.util.genitable import *
13 class get_key(Method):
15 Generate a new keypair and gid for requesting caller (component).
16 @return 1 If successful
19 interfaces = ['registry']
23 returns = Parameter(int, "1 if successful, faults otherwise")
26 # verify that the callers's ip address exist in the db and is an inteface
27 # for a node in the db
28 (ip, port) = self.api.remote_addr
29 interfaces = self.api.plshell.GetInterfaces(self.api.plauth, {'ip': ip}, ['node_id'])
31 raise NonExistingRecord("no such ip %(ip)s" % locals())
32 nodes = self.api.plshell.GetNodes(self.api.plauth, [interfaces[0]['node_id']], ['node_id', 'hostname'])
34 raise NonExistingRecord("no such node using ip %(ip)s" % locals())
37 # look up the sfa record
39 records = table.find({'type': 'node', 'pointer': node['node_id']})
41 raise RecordNotFound("pointer:" + str(node['node_id']))
44 # generate a new keypair and gid
46 pkey = Keypair(create=True)
47 gid_object = self.api.auth.hierarchy.create_gid(record['hrn'], uuid, pkey)
48 gid = gid_object.save_to_string(save_parents=True)
55 # attempt the scp the key
56 # this will only work for planetlab based compoenents
57 (fd, filename) = tempfile.mkstemp()
58 pkey.save_to_file(filename)
59 host = node['hostname']
60 dest="/etc/sfa/nodekey.key"
61 identity = "/etc/planetlab/root_ssh_key.pub"
62 scp_command = "scp -i %(identity)s %(filename)s root@%(host)s:%(dest)s" % locals()
63 (status, output) = commands(scp_command)
65 raise Exception, output