2 # Geni Component Wrapper
4 # This wrapper implements the Geni Slice and Mgmt Interfaces on a node.
15 from geniserver import *
17 from trustedroot import *
20 from geniticket import *
28 # ComponentManager is a GeniServer that serves slice and
29 # management operations at a node.
31 class ComponentManager(GeniServer):
32 def __init__(self, ip, port, key_file, cert_file):
33 GeniServer.__init__(self, ip, port, key_file, cert_file)
35 def register_functions(self):
36 GeniServer.register_functions(self)
37 self.server.register_function(self.stop_slice)
38 self.server.register_function(self.start_slice)
39 self.server.register_function(self.reset_slice)
40 self.server.register_function(self.delete_slice)
41 self.server.register_function(self.list_slices)
42 self.server.register_function(self.redeem_ticket)
43 self.server.register_function(self.reboot)
47 def stop_slice(self, cred_str):
48 self.decode_authentication(cred_str, "stopslice")
49 slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
50 print "stopslice:", slicename
51 accounts.get(slicename).start()
53 def start_slice(self, cred_str):
54 self.decode_authentication(cred_str, "startslice")
55 slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
56 print "startslice:", slicename
57 accounts.get(slicename).start()
59 def reset_slice(self, cred_str):
60 self.decode_authentication(cred_str, "resetslice")
61 slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
62 print "resetslice:", slicename
64 # find the existing record for the slice
66 rec = database.db[slicename]
68 raise SliverDoesNotExist(slicename)
70 accounts.get(slicename).stop()
71 accounts.get(slicename).ensure_destroyed()
72 accounts.get(slicename).ensure_created(rec)
74 def delete_slice(self, cred_str):
75 self.decode_authentication(cred_str, "deleteslice")
76 slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
77 print "deleteslice:", slicename
78 accounts.get(slicename).ensure_destroyed()
80 # this is similar to geniserver.decode_authentication
81 def decode_ticket(self, ticket_string):
82 self.client_ticket = Ticket(string = ticket_string)
83 self.client_gid = self.client_ticket.get_gid_caller()
84 self.object_gid = self.client_ticket.get_gid_object()
86 # make sure the client_gid is not blank
87 if not self.client_gid:
88 raise MissingCallerGID(self.client_ticket.get_subject())
90 # make sure the client_gid matches the certificate that the client is using
91 peer_cert = self.server.peer_cert
92 if not peer_cert.is_pubkey(self.client_gid.get_pubkey()):
93 raise ConnectionKeyGIDMismatch(self.client_gid.get_subject())
95 if self.trusted_cert_list:
96 self.client_ticket.verify_chain(self.trusted_cert_list)
98 self.client_gid.verify_chain(self.trusted_cert_list)
100 self.object_gid.verify_chain(self.trusted_cert_list)
102 def geni_ticket_to_plc_rec(self, ticket):
103 ticket_attrs = ticket.get_attributes()
104 ticket_rspec = ticket.get_rspec()
107 rec["name"] = ticket_attrs.get("name")
108 rec["keys"] = '\n'.join(ticket_attrs.get("keys",[]))
109 rec["initscript"] = ticket_attrs.get("initscript", "")
110 rec["vref"] = ticket_attrs.get("vref", "default")
111 rec["timestamp"] = ticket_attrs.get("timestamp") # should there be a default timestamp?
115 for resname, default_amt in sm.DEFAULT_ALLOCATION.iteritems():
117 t = type(default_amt)
118 amt = t.__new__(t, ticket_attrs[resname])
119 except (KeyError, ValueError):
125 def redeem_ticket(self, ticket_str):
126 self.decode_ticket(ticket_str)
127 ticket = self.client_ticket
129 print "ticket received for", self.object_gid.get_hrn()
131 rec = self.geni_ticket_to_plc_rec(ticket)
135 database.db.deliver_record(rec)
139 def list_slices(self, cred_str):
140 self.decode_authentication(cred_str, "listslices")
141 slice_names = database.db.keys()
144 # Management Interface
146 def reboot(self, cred_str):
147 self.decode_authentication(cred_str, "reboot")
148 system("/sbin/reboot")
151 if __name__ == "__main__":
154 key_file = "component.key"
155 cert_file = "component.cert"
157 # if no key is specified, then make one up
158 if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
159 key = Keypair(create=True)
160 key.save_to_file(key_file)
162 cert = Certificate(subject="component")
163 cert.set_issuer(key=key, subject="component")
166 cert.save_to_file(cert_file)
168 TrustedRoots = TrustedRootList()
170 # XXX: does this conflict with the nodemanager's database? I don't think
171 # so because there are locks, but double check...
174 s = ComponentManager("", 12345, key_file, cert_file)
175 s.trusted_cert_list = TrustedRoots.get_list()