replace urllib with requests

This commit is contained in:
GeorgianaElena
2019-05-24 14:37:42 +03:00
parent a54f104d1d
commit cfb3ec43cd
4 changed files with 23 additions and 14 deletions

View File

@@ -16,6 +16,7 @@ setup(
'pluggy>0.7<1.0', 'pluggy>0.7<1.0',
'passlib', 'passlib',
'backoff', 'backoff',
'requests',
'jupyterhub-traefik-proxy==0.1.*' 'jupyterhub-traefik-proxy==0.1.*'
], ],
entry_points={ entry_points={

View File

@@ -7,7 +7,7 @@ import json
import hashlib import hashlib
import contextlib import contextlib
import tempfile import tempfile
import urllib.request import requests
from distutils.version import LooseVersion as V from distutils.version import LooseVersion as V
@@ -50,7 +50,8 @@ def download_miniconda_installer(version, md5sum):
""" """
with tempfile.NamedTemporaryFile() as f: with tempfile.NamedTemporaryFile() as f:
installer_url = "https://repo.continuum.io/miniconda/Miniconda3-{}-Linux-x86_64.sh".format(version) installer_url = "https://repo.continuum.io/miniconda/Miniconda3-{}-Linux-x86_64.sh".format(version)
urllib.request.urlretrieve(installer_url, f.name) with open(f.name, 'wb') as f:
f.write(requests.get(installer_url).content)
if md5_file(f.name) != md5sum: if md5_file(f.name) != md5sum:
raise Exception('md5 hash mismatch! Downloaded file corrupted') raise Exception('md5 hash mismatch! Downloaded file corrupted')

View File

@@ -8,10 +8,9 @@ import secrets
import subprocess import subprocess
import sys import sys
import time import time
from urllib.error import HTTPError
from urllib.request import urlopen, URLError
import pluggy import pluggy
import requests
from tljh import ( from tljh import (
apt, apt,
@@ -293,20 +292,20 @@ def ensure_jupyterhub_running(times=20):
for i in range(times): for i in range(times):
try: try:
logger.info('Waiting for JupyterHub to come up ({}/{} tries)'.format(i + 1, times)) logger.info('Waiting for JupyterHub to come up ({}/{} tries)'.format(i + 1, times))
urlopen('http://127.0.0.1') requests.get('http://127.0.0.1')
return return
except HTTPError as h: except requests.HTTPError as h:
if h.code in [404, 502, 503]: if h.response.status_code in [404, 502, 503]:
# May be transient # May be transient
time.sleep(1) time.sleep(1)
continue continue
# Everything else should immediately abort # Everything else should immediately abort
raise raise
except URLError as e: except requests.ConnectionError:
if isinstance(e.reason, ConnectionRefusedError):
# Hub isn't up yet, sleep & loop # Hub isn't up yet, sleep & loop
time.sleep(1) time.sleep(1)
continue continue
except Exception:
# Everything else should immediately abort # Everything else should immediately abort
raise raise

View File

@@ -1,11 +1,11 @@
"""Traefik installation and setup""" """Traefik installation and setup"""
import hashlib import hashlib
import os import os
from urllib.request import urlretrieve, ContentTooShortError
from jinja2 import Template from jinja2 import Template
from passlib.apache import HtpasswdFile from passlib.apache import HtpasswdFile
import backoff import backoff
import requests
from tljh.configurer import load_config from tljh.configurer import load_config
@@ -27,11 +27,15 @@ def checksum_file(path):
hasher.update(chunk) hasher.update(chunk)
return hasher.hexdigest() return hasher.hexdigest()
def fatal_error(e):
# Retry only when connection is reset or we think we didn't download entire file
return str(e) != "ContentTooShort" and not isinstance(e, ConnectionResetError)
@backoff.on_exception( @backoff.on_exception(
backoff.expo, backoff.expo,
# Retry when connection is reset or we think we didn't download entire file Exception,
(ConnectionResetError, ContentTooShortError), max_tries=2,
max_tries=2 giveup=fatal_error
) )
def ensure_traefik_binary(prefix): def ensure_traefik_binary(prefix):
"""Download and install the traefik binary""" """Download and install the traefik binary"""
@@ -53,7 +57,11 @@ def ensure_traefik_binary(prefix):
) )
print(f"Downloading traefik {traefik_version}...") print(f"Downloading traefik {traefik_version}...")
# download the file # download the file
urlretrieve(traefik_url, traefik_bin) response = requests.get(traefik_url)
if response.status_code == 206:
raise Exception("ContentTooShort")
with open(traefik_bin, 'wb') as f:
f.write(response.content)
os.chmod(traefik_bin, 0o755) os.chmod(traefik_bin, 0o755)
# verify that we got what we expected # verify that we got what we expected