import repository from arizona
[raven.git] / apps / gacks / gacksclient.py
1 import base64
2 import xmlrpclib
3
4 from gacksreceipt import *
5 from gackshandle import *
6 from gacksjob import *
7 from gacksaccount import GacksAccount
8
9 from ravenlib.modpythonapi.BaseClient import EnableVerboseExceptions, EnableVerboseExceptionsTempFile
10 from ravenlib.modpythonapi.AuthenticatedClient import AuthenticatedClient
11
12 EnableVerboseExceptions(True)
13 EnableVerboseExceptionsTempFile(True)
14
15 class GacksClient(AuthenticatedClient):
16     ##
17     # Create a new GeniClient object.
18
19     def __init__(self, url, key_file):
20        AuthenticatedClient.__init__(self, url, key_file)
21
22        self.key_file = key_file
23
24        # load the private key into a keypair object that we can use for
25        # creating request signatures
26        self.keypair = Keypair(filename = self.key_file)
27
28     ##
29     # Get an updated bundle of records. 'hash' is the hash of the last bundle
30     # that the client received. If the hash is the same as the current bundle,
31     # then ("notmodified",,) is returned. Otherwise, ("update", hash, itemList)
32     # is returned.
33
34     def get_update(self, hash):
35         (result, updateHash, recDicts) = self.server.get_update(hash)
36
37         recList = []
38         for dict in recDicts:
39             recList.append(GacksRecord(dict=dict))
40
41         return (result, updateHash, recList)
42
43     def query_overlap(self, id=None, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY, hasAllocator=None, isLastAllocator=None):
44         recDicts = self.server.query_overlap(id, unitStart, unitStop, timeStart, timeStop, hasAllocator, isLastAllocator)
45
46         recList = []
47         for dict in recDicts:
48             recList.append(GacksRecord(dict=dict))
49
50         return recList
51
52     def query_exact(self, id=None, unitStart=None, unitStop=None, timeStart=None, timeStop=None, hasAllocator=None, isLastAllocator=None):
53         recDicts = self.server.query_exact(id, unitStart, unitStop, timeStart, timeStop, hasAllocator, isLastAllocator)
54
55         recList = []
56         for dict in recDicts:
57             recList.append(GacksRecord(dict=dict))
58
59         return recList
60
61     def computeRequestHash(self, argList):
62         return self.keypair.sign_string(str(argList))
63
64     def get_handle(self, rspec):
65         handleStrs = self.server.get_handle(rspec)
66
67         return strings_to_handles(handleStrs)
68
69     def set_allocator(self, authToken, handles, allocatorGID, which, where):
70         if allocatorGID:
71             allocatorGidStr = allocatorGID.save_to_string(True)
72         else:
73             allocatorGidStr = None
74
75         handleStrs = []
76         for handle in handles:
77             handleStrs.append(handle.as_string())
78
79         authTokenStr = authToken.xmlrpc_arg(argList=[handleStrs, allocatorGidStr, which, where])
80         receiptStrs = self.server.set_allocator(authTokenStr, handleStrs, allocatorGidStr, which, where)
81
82         receiptList = []
83         for receiptStr in receiptStrs:
84             receiptList.append(GacksReceipt(string=receiptStr))
85
86         return receiptList
87
88     def set_consumer(self, authToken, handles, cred):
89         handleStrs = []
90         for handle in handles:
91             handleStrs.append(handle.as_string())
92
93         if cred:
94             credStr = cred.save_to_string(save_parents = True)
95         else:
96             credStr = None
97
98         authTokenStr = authToken.xmlrpc_arg(argList=[handleStrs, credStr])
99         receiptStrs = self.server.set_consumer(authTokenStr, handleStrs, credStr)
100
101         receiptList = []
102         for receiptStr in receiptStrs:
103             receiptList.append(GacksReceipt(string=receiptStr))
104
105         return receiptList
106
107     def set_consumer_hrn(self, authToken, handles, consumerhrn):
108
109         handleStrs = []
110         for handle in handles:
111             handleStrs.append(handle.as_string())
112
113         authTokenStr = authToken.xmlrpc_arg(argList=[handleStrs, consumerhrn])
114         receiptStrs = self.server.set_consumer_hrn(authTokenStr, handleStrs, consumerhrn)
115
116         receiptList = []
117         for receiptStr in receiptStrs:
118             receiptList.append(GacksReceipt(string=receiptStr))
119
120         return receiptList
121
122     def submit_receipt(self, receipt):
123         receiptDict = receipt.as_dict()
124         result = self.server.submit_receipt(receiptDict)
125         return result
126
127     def pick(self, authToken, resourceName, resourceGroup, amount, expand):
128         authTokenStr = authToken.xmlrpc_arg(argList=[resourceName, resourceGroup, amount, expand])
129         result = self.server.pick(authTokenStr, resourceName, resourceGroup, amount, expand)
130         return (result[0], result[1], result[2])  # True/False, delSet, addSet
131
132     def query_asap(self, id=None, allocatorHRN=None, consumerHRN=None):
133         jobDicts = self.server.query_asap(id, allocatorHRN, consumerHRN)
134         recList = []
135         for dict in jobDicts:
136             recList.append(AsapJob(dict=dict))
137         return recList
138
139     def add_asap(self, authToken, job):
140         allocatorHRN = job.get_allocator()
141         consumerHRN = job.get_consumer()
142         duration = job.get_duration()
143         resourceStr = job.get_resources_string()
144         authTokenStr = authToken.xmlrpc_arg(argList=[allocatorHRN, consumerHRN, duration, resourceStr])
145         self.server.add_asap(authTokenStr, allocatorHRN, consumerHRN, duration, resourceStr)
146
147     def get_resources(self, hash):
148         return self.server.get_resources(hash)
149
150     def grm_delete(self, authToken, handles):
151         handleStrs = []
152         for handle in handles:
153             handleStrs.append(handle.as_string())
154
155         authTokenStr = authToken.xmlrpc_arg(argList=[handleStrs])
156         self.server.grm_delete(authTokenStr, handleStrs)
157
158     def grm_reserve(self, authToken, rspec, consumerHRN=None, noreserve=False, ignorepolicy=False):
159         rspec = str(rspec)
160         consumerHRN = str(consumerHRN)
161         noreserve = str(noreserve)
162         ignorepolicy = str(ignorepolicy)
163         authTokenStr = authToken.xmlrpc_arg(argList=[rspec, consumerHRN, noreserve, ignorepolicy])
164         self.server.grm_reserve(authTokenStr, rspec, consumerHRN, noreserve, ignorepolicy)
165
166     def delete_asap(self, authToken, id=None, allocatorHRN=None, consumerHRN=None):
167         authTokenStr = authToken.xmlrpc_arg(argList=[id, allocatorHRN, consumerHRN])
168         self.server.delete_asap(authTokenStr, id, allocatorHRN, consumerHRN)
169
170     def get_account(self, authToken, name, kind):
171         authTokenStr = authToken.xmlrpc_arg(argList=[name, kind])
172         acct_dict = self.server.get_account(authTokenStr, name, kind)
173         if acct_dict:
174             return GacksAccount(dict=acct_dict)
175         else:
176             return None
177
178     def admin_asap_run(self, authToken):
179         authTokenStr = authToken.xmlrpc_arg(argList=[])
180         job_ids_converted = self.server.admin_asap_run(authTokenStr)
181         return job_ids_converted
182
183     def admin_reset(self, authToken):
184         authTokenStr = authToken.xmlrpc_arg(argList=[])
185         return self.server.admin_reset(authTokenStr)
186
187     def admin_garbage_collect(self, authToken, timeStop=None):
188         authTokenStr = authToken.xmlrpc_arg(argList=[timeStop])
189         return self.server.admin_garbage_collect(authTokenStr, timeStop)
190
191     def admin_defragment(self, authToken):
192         authTokenStr = authToken.xmlrpc_arg(argList=[])
193         return self.server.admin_defragment(authTokenStr)
194
195