def initDB(backend=None, host=None, port=None, username=None, password=None, database=None, sslmode=None, sslrootcert=None): """ Initialize the database. Either we get backend and all parameter which means the caller knows what they are doing, or we populate everything from the config files. """ if backend is None: if CFG is None or not CFG.is_initialized(): initCFG('server') backend = CFG.DB_BACKEND host = CFG.DB_HOST port = CFG.DB_PORT database = CFG.DB_NAME username = CFG.DB_USER password = CFG.DB_PASSWORD sslmode = None sslrootcert = None if CFG.DB_SSL_ENABLED: sslmode = 'verify-full' sslrootcert = CFG.DB_SSLROOTCERT if backend not in SUPPORTED_BACKENDS: raise rhnException("Unsupported database backend", backend) if port: port = int(port) # Hide the password add_to_seclist(password) try: __init__DB(backend, host, port, username, password, database, sslmode, sslrootcert) __init__DB2(backend, host, port, username, password, database, sslmode, sslrootcert) # except (rhnException, SQLError): # raise # pass on, we know those ones # except (KeyboardInterrupt, SystemExit): # raise except SQLConnectError, e: try: global __DB global __DB2 del __DB del __DB2 except NameError: pass raise e
def initDB(backend=None, host=None, port=None, username=None, password=None, database=None, sslmode=None, sslrootcert=None, initsecond=False): """ Initialize the database. Either we get backend and all parameter which means the caller knows what they are doing, or we populate everything from the config files. initsecond: If set to True it initialize a second DB connection. By default only one DB connection is needed. """ if backend is None: if CFG is None or not CFG.is_initialized(): initCFG('server') backend = CFG.DB_BACKEND host = CFG.DB_HOST port = CFG.DB_PORT database = CFG.DB_NAME username = CFG.DB_USER password = CFG.DB_PASSWORD sslmode = None sslrootcert = None if CFG.DB_SSL_ENABLED: sslmode = 'verify-full' sslrootcert = CFG.DB_SSLROOTCERT if backend not in SUPPORTED_BACKENDS: raise rhnException("Unsupported database backend", backend) if port: port = int(port) # Hide the password add_to_seclist(password) try: if initsecond == False: __init__DB(backend, host, port, username, password, database, sslmode, sslrootcert) else: __init__DB2(backend, host, port, username, password, database, sslmode, sslrootcert) # except (rhnException, SQLError): # raise # pass on, we know those ones # except (KeyboardInterrupt, SystemExit): # raise except SQLConnectError: e = sys.exc_info()[1] try: closeDB() except NameError: pass raise e except: raise #e_type, e_value = sys.exc_info()[:2] # raise rhnException("Could not initialize Oracle database connection", # str(e_type), str(e_value)) return 0
def initDB(backend=None, host=None, port=None, username=None, password=None, database=None, sslmode=None, sslrootcert=None, initsecond=False): """ Initialize the database. Either we get backend and all parameter which means the caller knows what they are doing, or we populate everything from the config files. initsecond: If set to True it initialize a second DB connection. By default only one DB connection is needed. """ if backend is None: if CFG is None or not CFG.is_initialized(): initCFG('server') backend = CFG.DB_BACKEND host = CFG.DB_HOST port = CFG.DB_PORT database = CFG.DB_NAME username = CFG.DB_USER password = CFG.DB_PASSWORD sslmode = None sslrootcert = None if CFG.DB_SSL_ENABLED: sslmode = 'verify-full' sslrootcert = CFG.DB_SSLROOTCERT if backend not in SUPPORTED_BACKENDS: raise rhnException("Unsupported database backend", backend) if port: port = int(port) # Hide the password add_to_seclist(password) try: if initsecond == False: __init__DB(backend, host, port, username, password, database, sslmode, sslrootcert) else: __init__DB2(backend, host, port, username, password, database, sslmode, sslrootcert) # except (rhnException, SQLError): # raise # pass on, we know those ones # except (KeyboardInterrupt, SystemExit): # raise except SQLConnectError: e = sys.exc_info()[1] try: closeDB() except NameError: pass raise_with_tb(e, sys.exc_info()[2]) except: raise #e_type, e_value = sys.exc_info()[:2] # raise rhnException("Could not initialize Oracle database connection", # str(e_type), str(e_value)) return 0
def accessible(url): """Try if url is accessible :arg url: the url which is tried to access Returns True if url is accessible, otherwise False. """ timeout = 120 if CFG.is_initialized() and CFG.has_key('TIMEOUT'): timeout = CFG.TIMEOUT curl = pycurl.Curl() curl.setopt(pycurl.CONNECTTIMEOUT, timeout) curl.setopt(pycurl.URL, url) curl.setopt(pycurl.DEBUGFUNCTION, _curl_debug) curl.setopt(pycurl.VERBOSE, True) proxy_url, proxy_user, proxy_pass = get_proxy(url) if proxy_url: curl.setopt(pycurl.PROXY, proxy_url) log_debug(2, "Connect to %s" % url) # We implement our own redirection-following, because pycurl # 7.19 doesn't POST after it gets redirected. Ideally we'd be # using pycurl.POSTREDIR here, but that's in 7.21. curl.setopt(pycurl.FOLLOWLOCATION, False) curl.setopt(pycurl.NOBODY, True) try_counter = 5 while try_counter: try_counter -= 1 try: curl.perform() except pycurl.error as e: if e[0] == 56: # Proxy requires authentication log_debug(2, e[1]) if not (proxy_user and proxy_pass): raise TransferException("Proxy requires authentication, " "but reading credentials from " "%s failed." % YAST_PROXY) curl.setopt(pycurl.PROXYUSERPWD, "%s:%s" % (proxy_user, proxy_pass)) else: break status = curl.getinfo(pycurl.HTTP_CODE) # OK or file if status == 200 or (URL(url).scheme == "file" and status == 0): return True elif status in (301, 302): # redirects url = curl.getinfo(pycurl.REDIRECT_URL) log_debug(2, "Got redirect to %s" % url) curl.setopt(pycurl.URL, url) elif status >= 400: break return False
def accessible(url): """Try if url is accessible :arg url: the url which is tried to access Returns True if url is accessible, otherwise False. """ timeout = 120 if CFG.is_initialized() and CFG.has_key('TIMEOUT'): timeout = CFG.TIMEOUT curl = pycurl.Curl() curl.setopt(pycurl.CONNECTTIMEOUT, timeout) curl.setopt(pycurl.URL, url) curl.setopt(pycurl.DEBUGFUNCTION, _curl_debug) curl.setopt(pycurl.VERBOSE, True) proxy_url, proxy_user, proxy_pass = get_proxy(url) if proxy_url: curl.setopt(pycurl.PROXY, proxy_url) log_debug(2, "Connect to %s" % url) # We implement our own redirection-following, because pycurl # 7.19 doesn't POST after it gets redirected. Ideally we'd be # using pycurl.POSTREDIR here, but that's in 7.21. curl.setopt(pycurl.FOLLOWLOCATION, False) curl.setopt(pycurl.NOBODY, True) try_counter = 5 while try_counter: try_counter -= 1 try: curl.perform() except pycurl.error, e: if e[0] == 56: # Proxy requires authentication log_debug(2, e[1]) if not (proxy_user and proxy_pass): raise TransferException("Proxy requires authentication, " "but reading credentials from " "%s failed." % YAST_PROXY) curl.setopt(pycurl.PROXYUSERPWD, "%s:%s" % (proxy_user, proxy_pass)) else: break status = curl.getinfo(pycurl.HTTP_CODE) # OK or file if status == 200 or (URL(url).scheme == "file" and status == 0): return True elif status in (301, 302): # redirects url = curl.getinfo(pycurl.REDIRECT_URL) log_debug(2, "Got redirect to %s" % url) curl.setopt(pycurl.URL, url) elif status >= 400: break
def initDB(backend=None, host=None, port=None, username=None, password=None, database=None, sslmode=None, sslrootcert=None): """ Initialize the database. Either we get backend and all parameter which means the caller knows what they are doing, or we populate everything from the config files. """ if backend is None: if CFG is None or not CFG.is_initialized(): initCFG('server') backend = CFG.DB_BACKEND host = CFG.DB_HOST port = CFG.DB_PORT database = CFG.DB_NAME username = CFG.DB_USER password = CFG.DB_PASSWORD sslmode = None sslrootcert = None if CFG.DB_SSL_ENABLED: sslmode = 'verify-full' sslrootcert = CFG.DB_SSLROOTCERT if backend not in SUPPORTED_BACKENDS: raise rhnException("Unsupported database backend", backend) if port: port = int(port) # Hide the password add_to_seclist(password) try: __init__DB(backend, host, port, username, password, database, sslmode, sslrootcert) __init__DB2(backend, host, port, username, password, database, sslmode, sslrootcert) # except (rhnException, SQLError): # raise # pass on, we know those ones # except (KeyboardInterrupt, SystemExit): # raise except SQLConnectError, e: try: global __DB global __DB2 del __DB del __DB2 except NameError: pass raise e
def send(url, sendData=None): """Connect to url and return the result as stringIO :arg url: the url where the request will be sent :kwarg sendData: do a post-request when "sendData" is given. Returns the result as stringIO object. """ connect_retries = 10 try_counter = connect_retries timeout = 120 if CFG.is_initialized() and CFG.has_key('TIMEOUT'): timeout = CFG.TIMEOUT curl = pycurl.Curl() curl.setopt(pycurl.CONNECTTIMEOUT, timeout) curl.setopt(pycurl.URL, url) curl.setopt(pycurl.DEBUGFUNCTION, _curl_debug) curl.setopt(pycurl.VERBOSE, True) proxy_url, proxy_user, proxy_pass = get_proxy(url) if proxy_url: curl.setopt(pycurl.PROXY, proxy_url) log_debug(2, "Connect to %s" % url) if sendData is not None: curl.setopt(pycurl.POSTFIELDS, sendData) if (CFG.is_initialized() and CFG.has_key('DISABLE_EXPECT') and CFG.DISABLE_EXPECT): # disable Expect header curl.setopt(pycurl.HTTPHEADER, ['Expect:']) # We implement our own redirection-following, because pycurl # 7.19 doesn't POST after it gets redirected. Ideally we'd be # using pycurl.POSTREDIR here, but that's in 7.21. curl.setopt(pycurl.FOLLOWLOCATION, False) response = StringIO() curl.setopt(pycurl.WRITEFUNCTION, response.write) try_counter = connect_retries while try_counter: try_counter -= 1 try: curl.perform() except pycurl.error, e: if e[0] == 56: # Proxy requires authentication log_debug(2, e[1]) if not (proxy_user and proxy_pass): raise TransferException("Proxy requires authentication, " "but reading credentials from " "%s failed." % YAST_PROXY) curl.setopt(pycurl.PROXYUSERPWD, "%s:%s" % (proxy_user, proxy_pass)) elif e[0] == 60: log_error("Peer certificate could not be authenticated " "with known CA certificates.") raise TransferException("Peer certificate could not be " "authenticated with known CA " "certificates.") else: log_error(e[1]) raise status = curl.getinfo(pycurl.HTTP_CODE) if status == 200 or (URL(url).scheme == "file" and status == 0): # OK or file break elif status in (301, 302): # redirects url = curl.getinfo(pycurl.REDIRECT_URL) log_debug(2, "Got redirect to %s" % url) curl.setopt(pycurl.URL, url)
def send(url, sendData=None): """Connect to url and return the result as stringIO :arg url: the url where the request will be sent :kwarg sendData: do a post-request when "sendData" is given. Returns the result as stringIO object. """ connect_retries = 10 try_counter = connect_retries timeout = 120 if CFG.is_initialized() and CFG.has_key('TIMEOUT'): timeout = CFG.TIMEOUT curl = pycurl.Curl() curl.setopt(pycurl.CONNECTTIMEOUT, timeout) curl.setopt(pycurl.URL, url) curl.setopt(pycurl.DEBUGFUNCTION, _curl_debug) curl.setopt(pycurl.VERBOSE, True) proxy_url, proxy_user, proxy_pass = get_proxy(url) if proxy_url: curl.setopt(pycurl.PROXY, proxy_url) log_debug(2, "Connect to %s" % url) if sendData is not None: curl.setopt(pycurl.POSTFIELDS, sendData) if (CFG.is_initialized() and CFG.has_key('DISABLE_EXPECT') and CFG.DISABLE_EXPECT): # disable Expect header curl.setopt(pycurl.HTTPHEADER, ['Expect:']) # We implement our own redirection-following, because pycurl # 7.19 doesn't POST after it gets redirected. Ideally we'd be # using pycurl.POSTREDIR here, but that's in 7.21. curl.setopt(pycurl.FOLLOWLOCATION, False) response = StringIO() curl.setopt(pycurl.WRITEFUNCTION, response.write) try_counter = connect_retries while try_counter: try_counter -= 1 try: curl.perform() except pycurl.error, e: if e[0] == 56: # Proxy requires authentication log_debug(2, e[1]) if not (proxy_user and proxy_pass): raise TransferException("Proxy requires authentication, " "but reading credentials from " "%s failed." % YAST_PROXY) curl.setopt(pycurl.PROXYUSERPWD, "%s:%s" % (proxy_user, proxy_pass)) elif e[0] == 60: log_error("Peer certificate could not be authenticated " "with known CA certificates.") raise TransferException("Peer certificate could not be " "authenticated with known CA " "certificates.") else: log_error(e[1]) raise status = curl.getinfo(pycurl.HTTP_CODE) if status == 200 or (URL(url).scheme == "file" and status == 0): # OK or file break elif status in (301, 302): # redirects url = curl.getinfo(pycurl.REDIRECT_URL) log_debug(2, "Got redirect to %s" % url) curl.setopt(pycurl.URL, url)