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.head(url, proxies=proxies, verify=True, timeout=NETWORK_CONNECTION_TIMEOUT) # 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)) from e else: if response.status_code != 200: raise SourceSetupError("http request returned: {}".format( response.status_code)) return size
def _send_request(self, session): """Send a GET request to the image URL.""" proxies = get_proxies_from_option( self._proxy ) response = session.get( url=self._url, proxies=proxies, verify=self._ssl_verify, stream=True, timeout=NETWORK_CONNECTION_TIMEOUT, ) response.raise_for_status() return response
def _send_request(self, session): """Send a HEAD request to the image URL.""" # Send a request. proxies = get_proxies_from_option(self._proxy) response = session.head(url=self._url, proxies=proxies, verify=True, timeout=NETWORK_CONNECTION_TIMEOUT) # Check the response. if response.status_code != 200: msg = "The request has failed: {}".format(response.status_code) raise SourceSetupError(msg) return response
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) from e 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)