def _download_image(self, url, image_path, session): """Download the image using Requests with progress reporting""" error = None try: log.info("Starting image download") with open(image_path, "wb") as f: ssl_verify = not self._noverifyssl proxies = get_proxies_from_option(self._proxy) response = session.get(url, proxies=proxies, verify=ssl_verify, stream=True, timeout=NETWORK_CONNECTION_TIMEOUT) total_length = response.headers.get('content-length') if total_length is None: # just download the file in one go and fake the progress reporting once done log.warning( "content-length header is missing for the installation image, " "download progress reporting will not be available") f.write(response.content) size = f.tell() progress = DownloadProgress(self._url, size, self.report_progress) progress.end() else: # requests return headers as strings, so convert total_length to int progress = DownloadProgress(self._url, int(total_length), self.report_progress) bytes_read = 0 for buf in response.iter_content(1024 * 1024): if buf: f.write(buf) f.flush() bytes_read += len(buf) progress.update(bytes_read) progress.end() log.info("Image download finished") except RequestException as e: error = "Error downloading liveimg: {}".format(e) log.error(error) raise SourceSetupError(error) else: if not os.path.exists(image_path): error = "Failed to download {}, file doesn't exist".format( self._url) log.error(error) raise SourceSetupError(error)
def _check_remote_image(self, url, proxy): """Check that the url is available and return required space.""" size = 0 # FIXME: validate earlier when setting? proxies = get_proxies_from_option(self._proxy) try: response = self._session.get(url, proxies=proxies, verify=True) # At this point we know we can get the image and what its size is # Make a guess as to minimum size needed: # Enough space for image and image * 3 if response.headers.get('content-length'): size = int(response.headers.get('content-length')) * 4 except IOError as e: raise SourceSetupError("Error opening liveimg: {}".format(e)) else: if response.status_code != 200: raise SourceSetupError("http request returned: {}".format( response.status_code)) return size