1From 737e9a7c11233183f48ce6c83d38b504c8ffed12 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] 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---
16 pykickstart/load.py | 31 +++++++++++++++++++++++++++++++
17 1 file changed, 31 insertions(+)
18
19diff --git a/pykickstart/load.py b/pykickstart/load.py
20index f75fe5d3..a8f3ed1d 100644
21--- a/pykickstart/load.py
22+++ b/pykickstart/load.py
23@@ -21,12 +21,16 @@ import requests
24 from requests.auth import HTTPDigestAuth
25 from requests.auth import HTTPBasicAuth
26
27+import time
28 import shutil
29
30 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@@ -72,6 +76,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@@ -93,6 +120,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