NetUtil.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from urlparse import urlparse
  16. import time
  17. import logging
  18. import httplib
  19. from ssl import SSLError
  20. logger = logging.getLogger()
  21. class NetUtil:
  22. CONNECT_SERVER_RETRY_INTERVAL_SEC = 10
  23. HEARTBEAT_IDDLE_INTERVAL_SEC = 10
  24. MINIMUM_INTERVAL_BETWEEN_HEARTBEATS = 0.1
  25. # Url within server to request during status check. This url
  26. # should return HTTP code 200
  27. SERVER_STATUS_REQUEST = "{0}/cert/ca"
  28. # For testing purposes
  29. DEBUG_STOP_RETRIES_FLAG = False
  30. def checkURL(self, url):
  31. """Try to connect to a given url. Result is True if url returns HTTP code 200, in any other case
  32. (like unreachable server or wrong HTTP code) result will be False
  33. """
  34. logger.info("Connecting to " + url);
  35. try:
  36. parsedurl = urlparse(url)
  37. ca_connection = httplib.HTTPSConnection(parsedurl[1])
  38. ca_connection.request("HEAD", parsedurl[2])
  39. response = ca_connection.getresponse()
  40. status = response.status
  41. requestLogMessage = "HEAD %s -> %s"
  42. if status == 200:
  43. logger.debug(requestLogMessage, url, str(status) )
  44. return True
  45. else:
  46. logger.warning(requestLogMessage, url, str(status) )
  47. return False
  48. except SSLError as slerror:
  49. logger.error(str(slerror))
  50. logger.error("SSLError: Failed to connect. Please check openssl library versions. \n" +
  51. "Refer to: https://bugzilla.redhat.com/show_bug.cgi?id=1022468 for more details.")
  52. return False
  53. except Exception, e:
  54. logger.warning("Failed to connect to " + str(url) + " due to " + str(e) + " ")
  55. return False
  56. def try_to_connect(self, server_url, max_retries, logger = None):
  57. """Try to connect to a given url, sleeping for CONNECT_SERVER_RETRY_INTERVAL_SEC seconds
  58. between retries. No more than max_retries is performed. If max_retries is -1, connection
  59. attempts will be repeated forever until server is not reachable
  60. Returns count of retries
  61. """
  62. if logger is not None:
  63. logger.debug("Trying to connect to %s", server_url)
  64. retries = 0
  65. while (max_retries == -1 or retries < max_retries) and not self.DEBUG_STOP_RETRIES_FLAG:
  66. server_is_up = self.checkURL(self.SERVER_STATUS_REQUEST.format(server_url))
  67. if server_is_up:
  68. break
  69. else:
  70. if logger is not None:
  71. logger.warn('Server at {0} is not reachable, sleeping for {1} seconds...'.format(server_url,
  72. self.CONNECT_SERVER_RETRY_INTERVAL_SEC))
  73. retries += 1
  74. time.sleep(self.CONNECT_SERVER_RETRY_INTERVAL_SEC)
  75. return retries