#!/usr/local/bin/python import os,sys import logging from service_error import service_error from remote_service import xmlrpc_handler, soap_handler, service_caller from proxy_segment import proxy_segment class protogeni_proxy(proxy_segment): class ProtoGENIError(Exception): def __init__(self, op, code, output): Exception.__init__(self, output) self.op = op self.code = code self.output = output def __init__(self, log=None, keyfile=None, debug=False, ch_url=None, sa_url=None, cm_url=None): proxy_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) self.ProtoGENIError = protogeni_proxy.ProtoGENIError self.ch_url = ch_url self.sa_url = sa_url self.cm_url = cm_url self.call_SetValue = service_caller('SetValue') self.debug_fail = [] self.debug_response = { 'RedeemTicket': ("XML blob1", "XML blob2"), 'SliverStatus': { 'status': 'ready' }, 'Resolve': { 'creator_urn': 'debugCreatorURN', 'urn': 'debugURN', }, 'GetKeys': [ { 'key': 'k' } ], 'CreateSliver': 'manifest', } def pg_call(self, url, method, params, context): max_retries = 5 retries = 0 s = service_caller(method, request_body_name="", strict=False) if self.log: self.log.debug("Calling %s %s" % (url, method)) if not self.debug: while retries < max_retries: r = s.call_xmlrpc_service(url, params, context=context) code = r.get('code', -1) if code == 0: # Success leaves the loop here return r.get('value', None) elif code == 14 and retries +1 < max_retries: # Busy resource retries+= 1 self.log.info("Resource busy, retrying in 30 secs") time.sleep(30) else: # NB: out of retries falls through to here raise self.ProtoGENIError(op=method, code=r.get('code', 'unknown'), output=r.get('output', 'No output')) else: if method in self.debug_fail: raise self.ProtoGENIError(op=method, code='unknown', output='No output') elif self.debug_response.has_key(method): return self.debug_response[method] else: return "%s XML blob" % method def clearinghouse_call(self, method, params, context): return self.pg_call(self.ch_url, method, params, context) def slice_authority_call(self, method, params, context): return self.pg_call(self.sa_url, method, params, context) def component_manager_call(self, method, params, context): return self.pg_call(self.cm_url, method, params, context)