1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import os,sys |
---|
4 | import logging |
---|
5 | |
---|
6 | from service_error import service_error |
---|
7 | from remote_service import xmlrpc_handler, soap_handler, service_caller |
---|
8 | |
---|
9 | from proxy_segment import proxy_segment |
---|
10 | |
---|
11 | |
---|
12 | class protogeni_proxy(proxy_segment): |
---|
13 | class ProtoGENIError(Exception): |
---|
14 | def __init__(self, op, code, output): |
---|
15 | Exception.__init__(self, output) |
---|
16 | self.op = op |
---|
17 | self.code = code |
---|
18 | self.output = output |
---|
19 | |
---|
20 | def __init__(self, log=None, keyfile=None, debug=False, |
---|
21 | ch_url=None, sa_url=None, cm_url=None): |
---|
22 | proxy_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
23 | |
---|
24 | self.ProtoGENIError = protogeni_proxy.ProtoGENIError |
---|
25 | self.ch_url = ch_url |
---|
26 | self.sa_url = sa_url |
---|
27 | self.cm_url = cm_url |
---|
28 | |
---|
29 | self.call_SetValue = service_caller('SetValue') |
---|
30 | |
---|
31 | self.debug_fail = [] |
---|
32 | self.debug_response = { |
---|
33 | 'RedeemTicket': ("XML blob1", "XML blob2"), |
---|
34 | 'SliverStatus': { 'status': 'ready' }, |
---|
35 | 'Resolve': { |
---|
36 | 'creator_urn': 'debugCreatorURN', |
---|
37 | 'urn': 'debugURN', |
---|
38 | }, |
---|
39 | 'GetKeys': [ { 'key': 'k' } ], |
---|
40 | 'CreateSliver': 'manifest', |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | def pg_call(self, url, method, params, context): |
---|
45 | max_retries = 5 |
---|
46 | retries = 0 |
---|
47 | |
---|
48 | s = service_caller(method, request_body_name="", strict=False) |
---|
49 | if self.log: |
---|
50 | self.log.debug("Calling %s %s" % (url, method)) |
---|
51 | if not self.debug: |
---|
52 | while retries < max_retries: |
---|
53 | r = s.call_xmlrpc_service(url, params, context=context) |
---|
54 | code = r.get('code', -1) |
---|
55 | if code == 0: |
---|
56 | # Success leaves the loop here |
---|
57 | return r.get('value', None) |
---|
58 | elif code == 14 and retries +1 < max_retries: |
---|
59 | # Busy resource |
---|
60 | retries+= 1 |
---|
61 | self.log.info("Resource busy, retrying in 30 secs") |
---|
62 | time.sleep(30) |
---|
63 | else: |
---|
64 | # NB: out of retries falls through to here |
---|
65 | raise self.ProtoGENIError(op=method, |
---|
66 | code=r.get('code', 'unknown'), |
---|
67 | output=r.get('output', 'No output')) |
---|
68 | else: |
---|
69 | if method in self.debug_fail: |
---|
70 | raise self.ProtoGENIError(op=method, code='unknown', |
---|
71 | output='No output') |
---|
72 | elif self.debug_response.has_key(method): |
---|
73 | return self.debug_response[method] |
---|
74 | else: |
---|
75 | return "%s XML blob" % method |
---|
76 | |
---|
77 | def clearinghouse_call(self, method, params, context): |
---|
78 | return self.pg_call(self.ch_url, method, params, context) |
---|
79 | |
---|
80 | def slice_authority_call(self, method, params, context): |
---|
81 | return self.pg_call(self.sa_url, method, params, context) |
---|
82 | |
---|
83 | def component_manager_call(self, method, params, context): |
---|
84 | return self.pg_call(self.cm_url, method, params, context) |
---|
85 | |
---|