Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1 | From ffe06c6dd812b604d6482e4353d5564fad78bc90 Mon Sep 17 00:00:00 2001 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 2 | From: Hongxu Jia <hongxu.jia@windriver.com> |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 3 | Date: Mon, 30 Jul 2018 15:52:21 +0800 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 4 | Subject: [PATCH 4/4] load.py: retry to invoke request with timeout |
| 5 | |
| 6 | While networkless, use request to fetch kickstart file from |
| 7 | network, it failed and wait 300s to break, we should retry |
| 8 | to invoke request with timeout explicitly. So if it the |
| 9 | network is up, the fetch works. |
| 10 | |
| 11 | Upstream-Status: inappropriate [oe specific] |
| 12 | |
| 13 | Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com> |
| 14 | --- |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 15 | pykickstart/load.py | 31 +++++++++++++++++++++++++++++++ |
| 16 | 1 file changed, 31 insertions(+) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 17 | |
| 18 | diff --git a/pykickstart/load.py b/pykickstart/load.py |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 19 | index ad3bad1..a5cbbc5 100644 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 20 | --- a/pykickstart/load.py |
| 21 | +++ b/pykickstart/load.py |
| 22 | @@ -21,6 +21,7 @@ import requests |
| 23 | from requests.auth import HTTPDigestAuth |
| 24 | from requests.auth import HTTPBasicAuth |
| 25 | |
| 26 | +import time |
| 27 | import shutil |
| 28 | import six |
| 29 | |
| 30 | @@ -28,6 +29,9 @@ from pykickstart.errors import KickstartError, KickstartAuthError |
| 31 | from pykickstart.i18n import _ |
| 32 | from requests.exceptions import SSLError, RequestException |
| 33 | |
| 34 | +import logging |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 35 | +log = logging.getLogger("anaconda.main") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 36 | + |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 37 | _is_url = lambda location: '://' in location # RFC 3986 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 38 | |
| 39 | SSL_VERIFY = False |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 40 | @@ -73,6 +77,29 @@ def load_to_file(location, destination): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 41 | _copy_file(location, destination) |
| 42 | return destination |
| 43 | |
| 44 | +def _access_url(location): |
| 45 | + status = False |
| 46 | + |
| 47 | + # Retry 45 times, wait 45s~135s |
| 48 | + i = 0 |
| 49 | + while i < 45: |
| 50 | + |
| 51 | + try: |
| 52 | + request = requests.get(location, verify=SSL_VERIFY, timeout=2) |
| 53 | + except RequestException as e: |
| 54 | + log.info("Try '%s' %d times, %s" % (location, i, str(e))) |
| 55 | + status = False |
| 56 | + i += 1 |
| 57 | + time.sleep(1) |
| 58 | + continue |
| 59 | + |
| 60 | + else: |
| 61 | + status = True |
| 62 | + return status |
| 63 | + |
| 64 | + return status |
| 65 | + |
| 66 | + |
| 67 | def _get_auth(location, user=None, passwd=None): |
| 68 | |
| 69 | auth = None |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 70 | @@ -94,6 +121,10 @@ def _get_auth(location, user=None, passwd=None): |
| 71 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 72 | def _load_url(location, user=None, passwd=None): |
| 73 | '''Load a location (URL or filename) and return contents as string''' |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 74 | + |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | + if not _access_url(location): |
| 76 | + raise KickstartError(_("Connection %s failed" % location)) |
| 77 | + |
| 78 | auth = _get_auth(location, user=user, passwd=passwd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 79 | try: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 80 | request = requests.get(location, verify=SSL_VERIFY, auth=auth) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 81 | -- |
| 82 | 2.7.4 |
| 83 | |