|
4 | 4 | from __future__ import print_function
|
5 | 5 | from __future__ import absolute_import
|
6 | 6 | import requests
|
| 7 | +import socket |
7 | 8 | import os
|
8 | 9 | import six
|
9 | 10 | import json
|
|
18 | 19 | try:
|
19 | 20 | # python 2
|
20 | 21 | from urlparse import urlparse
|
| 22 | + from urlparse import urlunparse |
21 | 23 | from urllib import quote
|
22 | 24 | except ImportError:
|
23 | 25 | # python 3
|
24 | 26 | from urllib.parse import urlparse
|
| 27 | + from urllib.parse import urlunparse |
25 | 28 | from urllib.parse import quote
|
26 | 29 | from .utilities import (determine_hostname,
|
27 | 30 | generate_machine_id,
|
@@ -74,6 +77,26 @@ def _api_request_failed(exception, message='The Insights API could not be reache
|
74 | 77 | logger.error(message)
|
75 | 78 |
|
76 | 79 |
|
| 80 | +def _is_dns_error(exception): |
| 81 | + while exception: |
| 82 | + if isinstance(exception, socket.gaierror): |
| 83 | + return True |
| 84 | + |
| 85 | + exception = exception.__context__ |
| 86 | + |
| 87 | + return False |
| 88 | + |
| 89 | + |
| 90 | +def _fallback_ip(hostname): |
| 91 | + if hostname.endswith("redhat.com"): |
| 92 | + if hostname.endswith("stage.redhat.com"): |
| 93 | + return constants.insights_ip_stage |
| 94 | + else: |
| 95 | + return constants.insights_ip_prod |
| 96 | + else: |
| 97 | + return None |
| 98 | + |
| 99 | + |
77 | 100 | class InsightsConnection(object):
|
78 | 101 |
|
79 | 102 | """
|
@@ -369,7 +392,6 @@ def _legacy_test_urls(self, url, method):
|
369 | 392 | if test_req.status_code in (200, 201):
|
370 | 393 | return True
|
371 | 394 | else:
|
372 |
| - logger.error(" Failed.") |
373 | 395 | return False
|
374 | 396 | except REQUEST_FAILED_EXCEPTIONS as exc:
|
375 | 397 | last_ex = exc
|
@@ -492,6 +514,22 @@ def test_connection(self, rc=0):
|
492 | 514 | logger.info("Running Connection Tests...")
|
493 | 515 | logger.info("")
|
494 | 516 |
|
| 517 | + # base_url_hostname = urlparse(self.base_url).hostname |
| 518 | + # if base_url_hostname.endswith("redhat.com"): |
| 519 | + # if base_url_hostname.endswith("stage.redhat.com"): |
| 520 | + # base_url_ip = constants.insights_ip_stage |
| 521 | + # else: |
| 522 | + # base_url_ip = constants.insights_ip_prod |
| 523 | + # else: |
| 524 | + # base_url_ip = None |
| 525 | + # |
| 526 | + # resolved_base_url_ip, success_base_url_ip = self._test_connection(base_url_hostname, base_url_ip) |
| 527 | + # if not resolved_base_url_ip or not success_base_url_ip: |
| 528 | + # resolved_fallback_ip, success_fallback_ip = self._test_connection("www.redhat.com", constants.stable_public_ip) |
| 529 | + # logger.debug("dns %s, conn %s", resolved_fallback_ip, success_fallback_ip) |
| 530 | + # return False |
| 531 | + |
| 532 | + last_ex = None |
495 | 533 | for description, url, method in [
|
496 | 534 | ("Uploading a file to Ingress", self.upload_url, "POST"),
|
497 | 535 | ("Getting hosts from Inventory", self.inventory_url + "/hosts", "GET"),
|
@@ -520,6 +558,30 @@ def test_connection(self, rc=0):
|
520 | 558 | logger.error(" Additional information may be in %s" % self.config.logging_file)
|
521 | 559 | logger.error("")
|
522 | 560 |
|
| 561 | + if _is_dns_error(last_ex): |
| 562 | + parsed_url = urlparse(url) |
| 563 | + logger.error(" Could not resolve %s.", parsed_url.hostname) |
| 564 | + fallback = [constants.stable_public_url, constants.stable_public_ip] |
| 565 | + ip = _fallback_ip(parsed_url.hostname) |
| 566 | + if ip: |
| 567 | + fallback = [ip] + fallback |
| 568 | + |
| 569 | + for fallback_url in fallback: |
| 570 | + parsed_ip_url = urlunparse((parsed_url.scheme, fallback_url, "/", "", "", "")) |
| 571 | + try: |
| 572 | + logger.info(' Testing %s', parsed_ip_url) |
| 573 | + log_prefix = " " |
| 574 | + log_level = NETWORK if self.config.verbose else logging.DEBUG |
| 575 | + self.get(parsed_ip_url, log_prefix=log_prefix, log_level=log_level, verify=False) |
| 576 | + except REQUEST_FAILED_EXCEPTIONS as exc: |
| 577 | + logger.debug(" Caught %s: %s", type(exc).__name__, exc) |
| 578 | + logger.error(" Failed.") |
| 579 | + else: |
| 580 | + logger.info(" SUCCESS.") |
| 581 | + break |
| 582 | + else: |
| 583 | + logger.info(" FAILED.") |
| 584 | + |
523 | 585 | return 1
|
524 | 586 |
|
525 | 587 | def handle_fail_rcs(self, req):
|
|
0 commit comments