worked on splitting regions and implemented set_allocator / set_consumer
[sfa.git] / gacks / gacksserver.py
1 ##
2 # Gacks Server
3 ##
4
5 import tempfile
6 import os
7
8 import sys
9
10 from cert import *
11 from gid import *
12 from geniserver import *
13 from excep import *
14 from trustedroot import *
15 from misc import *
16 from record import *
17 from geniticket import *
18
19 from gacksexcep import *
20 from gackscalendar import *
21
22 ##
23 # GacksServer is a GeniServer that serves component interface requests.
24 #
25
26 class GacksServer(GeniServer):
27
28     ##
29     # Create a new GacksServer object.
30     #
31     # @param ip the ip address to listen on
32     # @param port the port to listen on
33     # @param key_file private key filename of registry
34     # @param cert_file certificate filename containing public key (could be a GID file)
35
36     def __init__(self, ip, port, key_file, cert_file):
37         GeniServer.__init__(self, ip, port, key_file, cert_file)
38
39     ##
40     # Register the server RPCs for Gacks
41
42     def register_functions(self):
43         GeniServer.register_functions(self)
44         self.server.register_function(self.get_handle)
45
46     def get_handle(self, rspec):
47         handles = rspec_to_handles(rspec)
48         return handles_to_strings(handles)
49
50     def set_allocator(self, callerGID_str, handle_strs, allocatorGID_str, which, where, reqsig)
51         callerGID = GID(callerGID_str)
52         allocatorGID = GID(allocatorGID_str)
53
54         # TODO: verify callerGID ssl key
55
56         callerGID.verify_chain(self.trusted_cert_list)
57         allocatorGID.verify_chain(self.trusted_cert_list)
58
59         handles = strings_to_handles(handle_strs)
60         for handle in handles:
61             # find the existing records that overlap the handle
62             existing_recs = self.calendar.query_handles([handle])
63
64             if not existing_recs:
65                 raise GacksResourceNotFound(hand.as_string())
66
67             # TODO: Merge existing_recs
68
69             for item in existing_recs:
70                 if not item.contains_allocator(callerGID->get_name()):
71                     raise CallerNotAllocator(item.as_string())
72                 if not item.is_superset(handle):
73                     raise RequestSpansReservations(handle.as_string() + " on " + item.as_string())
74
75             leftovers = []
76             results = []
77             for item in existing_recs:
78                 if item.is_proper_supserset(handle):
79                     parts = item.clone().split_subset(handle.unitStart, handle.unitStop, handle.timeStart, handle.timeStop)
80                     results.extend(parts[0])
81                     leftovers.extend(parts[1:])
82                 else:
83                     results.extend(item)
84
85             for item in existing_recs:
86                 calendar.remove_record(item)
87
88             for item in leftovers:
89                 calendar.insert_record(item)
90
91             for item in results:
92                 item.set_allocator(callerGID->get_name(), allocatorGID->get_name(), which, where)
93                 calendar.insert_record(item)
94
95     def set_consumer(self, callerGID_str, handle_strs, cred_str, reqsig):
96         callerGID = GID(string = callerGID_str)
97         cred = Credential(string = cred_str)
98
99         # TODO: verify callerGID ssl key
100
101         callerGID.verify_chain(self.trusted_cert_list)
102         cred.verify_chain(self.trusted_cert_list)
103
104         handles = strings_to_handles(handle_strs)
105         for handle in handles:
106             existing_recs = self.calendar.query_handles([handle])
107
108             if not existing_recs:
109                 raise GacksResourceNotFound(hand.as_string())
110
111             for rec in existing_recs:
112                 rec.set_consumer(cred.objectGID.get_name())
113                 calendar.update_record(rec)
114
115 if __name__ == "__main__":
116     global TrustedRoots
117
118     key_file = "gacksserver.key"
119     cert_file = "gacksserver.cert"
120
121     # if no key is specified, then make one up
122     if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
123         key = Keypair(create=True)
124         key.save_to_file(key_file)
125
126         cert = Certificate(subject="component")
127         cert.set_issuer(key=key, subject="component")
128         cert.set_pubkey(key)
129         cert.sign()
130         cert.save_to_file(cert_file)
131
132     TrustedRoots = TrustedRootList()
133
134     s = ComponentManager("", 12346, key_file, cert_file)
135     s.trusted_cert_list = TrustedRoots.get_list()
136     s.run()
137