Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 1 | From 737e9a7c11233183f48ce6c83d38b504c8ffed12 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 |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 4 | Subject: [PATCH] load.py: retry to invoke request with timeout |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 11 | Upstream-Status: Inappropriate [oe specific] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 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 |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 19 | index 58faba6..e856c8d 100644 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 20 | --- a/pykickstart/load.py |
| 21 | +++ b/pykickstart/load.py |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 22 | @@ -20,12 +20,16 @@ |
| 23 | import requests |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 24 | from requests.auth import HTTPDigestAuth |
| 25 | from requests.auth import HTTPBasicAuth |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 26 | +import time |
| 27 | import shutil |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 28 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 29 | from pykickstart.errors import KickstartError, KickstartAuthError |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 30 | from pykickstart.i18n import _ |
| 31 | from requests.exceptions import SSLError, RequestException |
| 32 | |
| 33 | +import logging |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | +log = logging.getLogger("anaconda.main") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 35 | + |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 36 | is_url = lambda location: '://' in location # RFC 3986 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 37 | |
| 38 | SSL_VERIFY = False |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 39 | @@ -71,6 +75,29 @@ def load_to_file(location, destination): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 40 | _copy_file(location, destination) |
| 41 | return destination |
| 42 | |
| 43 | +def _access_url(location): |
| 44 | + status = False |
| 45 | + |
| 46 | + # Retry 45 times, wait 45s~135s |
| 47 | + i = 0 |
| 48 | + while i < 45: |
| 49 | + |
| 50 | + try: |
| 51 | + request = requests.get(location, verify=SSL_VERIFY, timeout=2) |
| 52 | + except RequestException as e: |
| 53 | + log.info("Try '%s' %d times, %s" % (location, i, str(e))) |
| 54 | + status = False |
| 55 | + i += 1 |
| 56 | + time.sleep(1) |
| 57 | + continue |
| 58 | + |
| 59 | + else: |
| 60 | + status = True |
| 61 | + return status |
| 62 | + |
| 63 | + return status |
| 64 | + |
| 65 | + |
| 66 | def _get_auth(location, user=None, passwd=None): |
| 67 | |
| 68 | auth = None |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 69 | @@ -92,6 +119,10 @@ def _get_auth(location, user=None, passwd=None): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 70 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 71 | def _load_url(location, user=None, passwd=None): |
| 72 | '''Load a location (URL or filename) and return contents as string''' |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 73 | + |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 74 | + if not _access_url(location): |
| 75 | + raise KickstartError(_("Connection %s failed" % location)) |
| 76 | + |
| 77 | auth = _get_auth(location, user=user, passwd=passwd) |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 78 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 79 | try: |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 80 | -- |
| 81 | 2.34.1 |
| 82 | |