blob: 6ed15ab09c5a713693164d45fb5292bc4a220793 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001From ffe06c6dd812b604d6482e4353d5564fad78bc90 Mon Sep 17 00:00:00 2001
2From: Hongxu Jia <hongxu.jia@windriver.com>
3Date: Mon, 30 Jul 2018 15:52:21 +0800
4Subject: [PATCH 4/4] load.py: retry to invoke request with timeout
5
6While networkless, use request to fetch kickstart file from
7network, it failed and wait 300s to break, we should retry
8to invoke request with timeout explicitly. So if it the
9network is up, the fetch works.
10
11Upstream-Status: inappropriate [oe specific]
12
13Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
14---
15 pykickstart/load.py | 31 +++++++++++++++++++++++++++++++
16 1 file changed, 31 insertions(+)
17
18diff --git a/pykickstart/load.py b/pykickstart/load.py
19index ad3bad1..a5cbbc5 100644
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
35+log = logging.getLogger("anaconda.main")
36+
37 _is_url = lambda location: '://' in location # RFC 3986
38
39 SSL_VERIFY = False
40@@ -73,6 +77,29 @@ def load_to_file(location, destination):
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
70@@ -94,6 +121,10 @@ def _get_auth(location, user=None, passwd=None):
71
72 def _load_url(location, user=None, passwd=None):
73 '''Load a location (URL or filename) and return contents as string'''
74+
75+ if not _access_url(location):
76+ raise KickstartError(_("Connection %s failed" % location))
77+
78 auth = _get_auth(location, user=user, passwd=passwd)
79 try:
80 request = requests.get(location, verify=SSL_VERIFY, auth=auth)
81--
822.7.4
83