def _rpc_call(self, function_name, params): get_server_obj = self.login() # Try a couple of times fault_count = 0 expired_token = 0 cfg = config.initUp2dateConfig() while fault_count - expired_token < cfg['networkRetries']: try: ret = getattr(get_server_obj, function_name)(*params) except rpclib.xmlrpclib.ProtocolError: e = sys.exc_info()[1] # We have two codes to check: the HTTP error code, and the # combination (failtCode, faultString) encoded in the headers # of the request. http_error_code = e.errcode fault_code, fault_string = rpclib.reportError(e.headers) fault_count += 1 if http_error_code == 401 and fault_code == -34: # Login token expired get_server_obj = self.login(force=1) # allow exactly one respin for expired token expired_token = 1 continue if http_error_code == 404 and fault_code == -17: # File not found self.extinctErrorYN = 1 return None log(-1, 'ERROR: http error code :%s; fault code: %s; %s' % (http_error_code, fault_code, fault_string)) # XXX raise else: return ret raise Exception("Failed after multiple attempts!")
def _rpc_call(self, function_name, params): get_server_obj = self.login() # Try a couple of times cfg = config.initUp2dateConfig() for i in range(cfg["networkRetries"]): try: ret = apply(getattr(get_server_obj, function_name), params) except rpclib.ProtocolError, e: # We have two codes to check: the HTTP error code, and the # combination (failtCode, faultString) encoded in the headers # of the request. http_error_code = e.errcode fault_code, fault_string = rpclib.reportError(e.headers) if http_error_code == 401 and fault_code == -34: # Login token expired get_server_obj = self.login(force=1) continue if http_error_code == 404 and fault_code == -17: # File not found self.extinctErrorYN = 1 return None log(-1, "ERROR: http error code :%s; fault code: %s; %s" % (http_error_code, fault_code, fault_string)) # XXX raise else: return ret
def listPackages(function, channel, version): """ Generates a list of objects by calling the function """ try: return function(channel, version) except rpclib.ProtocolError, e: errcode, errmsg = rpclib.reportError(e.headers) raise rhnFault(1000, "RHN Proxy error (rpclib.ProtocolError): " "errode=%s; errmsg=%s" % (errcode, errmsg))
def listPackages(function, channel, version): """ Generates a list of objects by calling the function """ try: return function(channel, version) except xmlrpclib.ProtocolError, e: errcode, errmsg = rpclib.reportError(e.headers) raise rhnFault( 1000, "SpacewalkProxy error (xmlrpclib.ProtocolError): " "errode=%s; errmsg=%s" % (errcode, errmsg)), None, sys.exc_info()[2]
def __channelPackageMapping(self): """ fetch package list on behalf of the client """ log_debug(6, self.rhnParentXMLRPC, self.httpProxy, self.httpProxyUsername, self.httpProxyPassword) log_debug(6, self.clientInfo) try: packageList = self._listPackages() except xmlrpclib.ProtocolError, e: errcode, errmsg = rpclib.reportError(e.headers) raise rhnFault(1000, "SpacewalkProxy error (xmlrpclib.ProtocolError): " "errode=%s; errmsg=%s" % (errcode, errmsg)), None, sys.exc_info()[2]
def listPackages(server, channel, version): """ Generates a list of objects by calling the function """ try: return server.listAllPackagesChecksum(channel, version) # pylint: disable=W0702 except: try: return server.listAllPackages(channel, version) except xmlrpclib.ProtocolError, e: errcode, errmsg = rpclib.reportError(e.headers) raise rhnFault( 1000, "SpacewalkProxy error (xmlrpclib.ProtocolError): " "errode=%s; errmsg=%s" % (errcode, errmsg)), None, sys.exc_info()[2]
def __channelPackageMapping(self): """ fetch package list on behalf of the client """ log_debug(6, self.rhnParentXMLRPC, self.httpProxy, self.httpProxyUsername, self.httpProxyPassword) log_debug(6, self.clientInfo) try: packageList = self._listPackages() except xmlrpclib.ProtocolError as e: errcode, errmsg = rpclib.reportError(e.headers) raise_with_tb( rhnFault( 1000, "SpacewalkProxy error (xmlrpclib.ProtocolError): " "errode=%s; errmsg=%s" % (errcode, errmsg)), sys.exc_info()[2]) # Hash the list _hash = {} for package in packageList: arch = package[4] extension = "rpm" if isSolarisArch(arch): extension = "pkg" if isDebianArch(arch): extension = "deb" filename = "%s-%s-%s.%s.%s" % (package[0], package[1], package[2], package[4], extension) # if the package contains checksum info if len(package) > 6: filePaths = computePackagePaths(package, source=0, prepend=PREFIX, checksum=package[7]) else: filePaths = computePackagePaths(package, source=0, prepend=PREFIX) _hash[filename] = filePaths if CFG.DEBUG > 4: log_debug( 5, "Mapping: %s[...snip snip...]%s" % (str(_hash)[:40], str(_hash)[-40:])) return _hash
system_id_file = sys.argv[2] except: pass def get_test_server_https(): global SERVER, HANDLER return Server("https://%s%s" % (SERVER, HANDLER)) s = get_test_server_https() sysid = open(system_id_file).read() dict = s.up2date.login(sysid) print dict dict['X-RHN-Auth-Server-Time'] = 1324 channels = dict['X-RHN-Auth-Channels'] channel_name, channel_version = channels[0][:2] sg = GETServer("http://xmlrpc.rhn.redhat.com/XMLRPC", headers=dict) try: l = sg.listPackages(channel_name, channel_version) except ProtocolError, e: print reportError(e.headers) print("OK (error above expected)") sys.exit(0) print("ERROR: Exception didn't occurred!") sys.exit(-1)
print "httplib.IncompleteRead" raise rhnErrors.CommunicationError("httplib.IncompleteRead") except urllib2.HTTPError, e: msg = "\nAn HTTP error occurred:\n" msg = msg + "URL: %s\n" % e.filename msg = msg + "Status Code: %s\n" % e.code msg = msg + "Error Message: %s\n" % e.msg log.log_me(msg) raise rhnErrors.CommunicationError(msg) except xmlrpclib.ProtocolError, e: log.log_me("A protocol error occurred: %s , attempt #%s," % ( e.errmsg, attempt_count)) (errCode, errMsg) = rpclib.reportError(e.headers) reset = 0 if abs(errCode) == 34: log.log_me("Auth token timeout occurred\n errmsg: %s" % errMsg) # this calls login, which in tern calls doCall (ie, # this function) but login should never get a 34, so # should be safe from recursion # PORTME : needs to do login without repoDirector preferbly # rd = repoDirector.initRepoDirector() # rd.updateAuthInfo() # reset = 1 # the servers are being throttle to pay users only, catch the # exceptions and display a nice error message if abs(errCode) == 51:
def doCall(method, *args, **kwargs): log = up2dateLog.initLog() log.log_debug("rpcServer: Calling XMLRPC %s" % method.__dict__['_Method__name']) cfg = config.initUp2dateConfig() ret = None attempt_count = 1 try: attempts = int(cfg["networkRetries"]) except ValueError: attempts = 1 if attempts <= 0: attempts = 1 while 1: failure = 0 ret = None try: ret = method(*args, **kwargs) except KeyboardInterrupt: raise_with_tb(up2dateErrors.CommunicationError(_( "Connection aborted by the user"))) # if we get a socket error, keep tryingx2 except (socket.error, SSL.socket_error): log.log_me("A socket error occurred: %s, attempt #%s" % ( sys.exc_info()[1], attempt_count)) if attempt_count >= attempts: e = sys.exc_info()[1] if len(e.args) > 1: raise_with_tb(up2dateErrors.CommunicationError(e.args[1])) else: raise_with_tb(up2dateErrors.CommunicationError(e.args[0])) else: failure = 1 except httplib.IncompleteRead: print("httplib.IncompleteRead") raise_with_tb(up2dateErrors.CommunicationError("httplib.IncompleteRead")) except urllib2.HTTPError: e = sys.exc_info()[1] msg = "\nAn HTTP error occurred:\n" msg = msg + "URL: %s\n" % e.filename msg = msg + "Status Code: %s\n" % e.code msg = msg + "Error Message: %s\n" % e.msg log.log_me(msg) raise_with_tb(up2dateErrors.CommunicationError(msg)) except xmlrpclib.ProtocolError: e = sys.exc_info()[1] log.log_me("A protocol error occurred: %s , attempt #%s," % ( e.errmsg, attempt_count)) if e.errcode == 404: log.log_me("Could not find URL, %s" % (e.url)) log.log_me("Check server name and/or URL, then retry\n"); (errCode, errMsg) = rpclib.reportError(e.headers) reset = 0 if abs(errCode) == 34: log.log_me("Auth token timeout occurred\n errmsg: %s" % errMsg) # this calls login, which in tern calls doCall (ie, # this function) but login should never get a 34, so # should be safe from recursion from up2date_client import up2dateAuth up2dateAuth.updateLoginInfo() # the servers are being throttle to pay users only, catch the # exceptions and display a nice error message if abs(errCode) == 51: log.log_me(_("Server has refused connection due to high load")) raise_with_tb(up2dateErrors.CommunicationError(e.errmsg)) # if we get a 404 from our server, thats pretty # fatal... no point in retrying over and over. Note that # errCode == 17 is specific to our servers, if the # serverURL is just pointing somewhere random they will # get a 0 for errcode and will raise a CommunicationError if abs(errCode) == 17: #in this case, the args are the package string, so lets try to # build a useful error message if type(args[0]) == type([]): pkg = args[0] else: pkg=args[1] if type(pkg) == type([]): pkgName = "%s-%s-%s.%s" % (pkg[0], pkg[1], pkg[2], pkg[4]) else: pkgName = pkg msg = "File Not Found: %s\n%s" % (pkgName, errMsg) log.log_me(msg) raise_with_tb(up2dateErrors.FileNotFoundError(msg)) if not reset: if attempt_count >= attempts: raise_with_tb(up2dateErrors.CommunicationError(e.errmsg)) else: failure = 1 except xmlrpclib.ResponseError: raise_with_tb(up2dateErrors.CommunicationError( "Broken response from the server.")) if ret != None: break else: failure = 1 if failure: # rest for five seconds before trying again time.sleep(5) attempt_count = attempt_count + 1 if attempt_count > attempts: raise up2dateErrors.CommunicationError("The data returned from the server was incomplete") return ret
def doCall(method, *args, **kwargs): log = up2dateLog.initLog() log.log_debug("rpcServer: Calling XMLRPC %s" % method.__dict__['_Method__name']) cfg = config.initUp2dateConfig() ret = None attempt_count = 1 try: attempts = int(cfg["networkRetries"]) except ValueError: attempts = 1 if attempts <= 0: attempts = 1 while 1: failure = 0 ret = None try: ret = method(*args, **kwargs) except KeyboardInterrupt: raise (up2dateErrors.CommunicationError( _("Connection aborted by the user")), None, sys.exc_info()[2]) # if we get a socket error, keep tryingx2 except (socket.error, socket.sslerror): log.log_me("A socket error occurred: %s, attempt #%s" % (sys.exc_info()[1], attempt_count)) if attempt_count >= attempts: if len(e.args) > 1: raise (up2dateErrors.CommunicationError(e.args[1]), None, sys.exc_info()[2]) else: raise (up2dateErrors.CommunicationError(e.args[0]), None, sys.exc_info()[2]) else: failure = 1 except httplib.IncompleteRead: print "httplib.IncompleteRead" raise (up2dateErrors.CommunicationError("httplib.IncompleteRead"), None, sys.exc_info()[2]) except urllib2.HTTPError: e = sys.exc_info()[1] msg = "\nAn HTTP error occurred:\n" msg = msg + "URL: %s\n" % e.filename msg = msg + "Status Code: %s\n" % e.code msg = msg + "Error Message: %s\n" % e.msg log.log_me(msg) raise (up2dateErrors.CommunicationError(msg), None, sys.exc_info()[2]) except xmlrpclib.ProtocolError: e = sys.exc_info()[1] log.log_me("A protocol error occurred: %s , attempt #%s," % (e.errmsg, attempt_count)) if e.errcode == 404: log.log_me("Could not find URL, %s" % (e.url)) log.log_me("Check server name and/or URL, then retry\n") (errCode, errMsg) = rpclib.reportError(e.headers) reset = 0 if abs(errCode) == 34: log.log_me("Auth token timeout occurred\n errmsg: %s" % errMsg) # this calls login, which in tern calls doCall (ie, # this function) but login should never get a 34, so # should be safe from recursion up2dateAuth.updateLoginInfo() # the servers are being throttle to pay users only, catch the # exceptions and display a nice error message if abs(errCode) == 51: log.log_me(_("Server has refused connection due to high load")) raise (up2dateErrors.CommunicationError(e.errmsg), None, sys.exc_info()[2]) # if we get a 404 from our server, thats pretty # fatal... no point in retrying over and over. Note that # errCode == 17 is specific to our servers, if the # serverURL is just pointing somewhere random they will # get a 0 for errcode and will raise a CommunicationError if abs(errCode) == 17: #in this case, the args are the package string, so lets try to # build a useful error message if type(args[0]) == type([]): pkg = args[0] else: pkg = args[1] if type(pkg) == type([]): pkgName = "%s-%s-%s.%s" % (pkg[0], pkg[1], pkg[2], pkg[4]) else: pkgName = pkg msg = "File Not Found: %s\n%s" % (pkgName, errMsg) log.log_me(msg) raise (up2dateErrors.FileNotFoundError(msg), None, sys.exc_info()[2]) if not reset: if attempt_count >= attempts: raise (up2dateErrors.CommunicationError(e.errmsg), None, sys.exc_info()[2]) else: failure = 1 except xmlrpclib.ResponseError: raise (up2dateErrors.CommunicationError( "Broken response from the server."), None, sys.exc_info()[2]) if ret != None: break else: failure = 1 if failure: # rest for five seconds before trying again time.sleep(5) attempt_count = attempt_count + 1 if attempt_count > attempts: raise up2dateErrors.CommunicationError( "The data returned from the server was incomplete") return ret
SERVER = sys.argv[1] system_id_file = sys.argv[2] except: pass def get_test_server_https(): global SERVER, HANDLER return Server("https://%s%s" % (SERVER, HANDLER)) s = get_test_server_https() sysid = open(system_id_file).read() dict = s.up2date.login(sysid) print dict dict['X-RHN-Auth-Server-Time'] = 1324 channels = dict['X-RHN-Auth-Channels'] channel_name, channel_version = channels[0][:2] sg = GETServer("http://xmlrpc.rhn.redhat.com/XMLRPC", headers=dict) try: l = sg.listPackages(channel_name, channel_version) except ProtocolError, e: print reportError(e.headers) print("OK (error above expected)"); sys.exit(0); print("ERROR: Exception didn't occured!"); sys.exit(-1);