Pull image from docker repository

        :params image: Image name
        :params progress_callback: A function that receive a log message about image download progress
491301
def pull_image(self, image, progress_callback=None):
        

        try:
            yield from self.query("GET", "images/{}/json".format(image))
            return  # We already have the image skip the download
        except DockerHttp404Error:
            pass

        if progress_callback:
            progress_callback("Pull {} from docker hub".format(image))
        response = yield from self.http_query("POST", "images/create", params={"fromImage": image}, timeout=None)
        # The pull api will stream status via an HTTP JSON stream
        content = ""
        while True:
            try:
                chunk = yield from response.content.read(1024)
            except aiohttp.ServerDisconnectedError:
                break
            if not chunk:
                break
            content += chunk.decode("utf-8")

            try:
                while True:
                    content = content.lstrip(" \r\n\t")
                    answer, index = json.JSONDecoder().raw_decode(content)
                    if "progress" in answer and progress_callback:
                        progress_callback("Pulling image {}:{}: {}".format(image, answer["id"], answer["progress"]))
                    content = content[index:]
            except ValueError:  # Partial JSON
                pass
        response.close()
        if progress_callback:
            progress_callback("Success pulling image {}".format(image))
776544
def pull(self, platform=None):
        
        repository, _ = parse_repository_tag(self.image_name)
        return self.collection.pull(repository, tag=self.id, platform=platform)
1201607
def backup_progress(self):
        
        epoch_time = int(time.time() * 1000)
        if self.deploymentType == 'Cloud':
            url = self._options['server'] + '/rest/obm/1.0/getprogress?_=%i' % epoch_time
        else:
            logging.warning(
                'This functionality is not available in Server version')
            return None
        r = self._session.get(
            url, headers=self._options['headers'])
        # This is weird.  I used to get xml, but now I'm getting json
        try:
            return json.loads(r.text)
        except Exception:
            import defusedxml.ElementTree as etree

            progress = {}
            try:
                root = etree.fromstring(r.text)
            except etree.ParseError as pe:
                logging.warning('Unable to find backup info.  You probably need to initiate a new backup. %s' % pe)
                return None
            for k in root.keys():
                progress[k] = root.get(k)
            return progress
1215026
def progress_callback(self, action, node, elapsed_time=None):
        
        if action == 'load_start':
            self.stdout.write('Loading fixture {}.{}...'.format(*node),
                              ending='')
            self.stdout.flush()
        elif action == 'load_success':
            message = 'SUCCESS'
            if elapsed_time:
                message += ' ({:.03} seconds) '.format(elapsed_time)

            self.stdout.write(message)
145591
def fetch_image(self, path, dest, user='root'):
        
        self.run('test -f %s || curl -L -s -o %s %s' % (dest, dest, path),
                 user=user, ignore_error=True)
1231258
def _download_image(self, imageURL):
        

        # If the required count of images have been download,
        # refrain from downloading the remainder of the images
        if(self._imageCounter >= self._imageCount):
            return

        try:
            imageResponse = requests.get(imageURL)

            # Generate image file name as <_imageQuery>_<_imageCounter>.<extension>
            imageType, imageEncoding = mimetypes.guess_type(imageURL)

            if imageType is not None:
                imageExtension = mimetypes.guess_extension(imageType)
            else:
                imageExtension = mimetypes.guess_extension(
                    imageResponse.headers['Content-Type'])

            imageFileName = self._imageQuery.replace(
                ' ', '_') + '_' + str(self._imageCounter) + imageExtension

            imageFileName = os.path.join(self._storageFolder, imageFileName)

            image = Image.open(BytesIO(imageResponse.content))
            image.save(imageFileName)

            self._imageCounter += 1
            self._downloadProgressBar.update(self._imageCounter)

        except Exception as exception:
            pass
1236496
async def import_image(self, data, stream: bool = False):
        
        headers = {"Content-Type": "application/x-tar"}
        response = await self.docker._query_chunked_post(
            "images/load", "POST", data=data, headers=headers
        )
        return await json_stream_result(response, stream=stream)
229389
def update_base_image(path: str):
    """"""
    with open(path, 'r') as file_handle:
        contents = file_handle.read()

    regex = re.compile('from\s+(?P<source>[^\s]+)', re.IGNORECASE)
    matches = regex.findall(contents)

    if not matches:
        return None

    match = matches[0]
    os.system('docker pull {}'.format(match))
    return match
520582
def push_image(self, image, insecure=False):
        
        logger.info("pushing image '%s'", image)
        logger.debug("image: '%s', insecure: '%s'", image, insecure)
        try:
            # push returns string composed of newline separated jsons; exactly what 'docker push'
            # outputs
            command_result = self.retry_generator(self.d.push,
                                                  image.to_str(tag=False),
                                                  tag=image.tag, insecure_registry=insecure,
                                                  decode=True, stream=True)
        except TypeError:
            # because changing api is fun
            command_result = self.retry_generator(self.d.push,
                                                  image.to_str(tag=False),
                                                  tag=image.tag, decode=True, stream=True)

        self.last_logs = command_result.logs
        return command_result.parsed_logs
168708
def get_progress(self):
        """"""
        progress = self.json_response.get("progress", None)
        self.logger.info("%s\t%s" % (self.request_method, self.request_url))
        return progress
970128
def get_task_progress(self, task_name):
        

        params = {'instanceprogress': task_name, 'taskname': task_name}

        resp = self._client.get(self.resource(), params=params)
        return Instance.Task.TaskProgress.parse(self._client, resp)
885056
def get_image(self):
        
        try:
            result = yield self._fetch(
                self.context.request.image_url
            )

            if not result.successful:
                if result.loader_error == LoaderResult.ERROR_NOT_FOUND:
                    self._error(404)
                    return
                elif result.loader_error == LoaderResult.ERROR_UPSTREAM:
                    # Return a Bad Gateway status if the error came from upstream
                    self._error(502)
                    return
                elif result.loader_error == LoaderResult.ERROR_TIMEOUT:
                    # Return a Gateway Timeout status if upstream timed out (i.e. 599)
                    self._error(504)
                    return
                elif isinstance(result.loader_error, int):
                    self._error(result.loader_error)
                    return
                elif hasattr(result, 'engine_error') and result.engine_error == EngineResult.COULD_NOT_LOAD_IMAGE:
                    self._error(400)
                    return
                else:
                    self._error(500)
                    return

        except Exception as e:
            msg = '[BaseHandler] get_image failed for url `{url}`. error: `{error}`'.format(
                url=self.context.request.image_url,
                error=e
            )

            self.log_exception(*sys.exc_info())

            if 'cannot identify image file' in e.message:
                logger.warning(msg)
                self._error(400)
            else:
                logger.error(msg)
                self._error(500)
            return

        normalized = result.normalized
        buffer = result.buffer
        engine = result.engine

        req = self.context.request

        if engine is None:
            if buffer is None:
                self._error(504)
                return

            engine = self.context.request.engine
            try:
                engine.load(buffer, self.context.request.extension)
            except Exception:
                self._error(504)
                return

        self.context.transformer = Transformer(self.context)

        def transform():
            self.normalize_crops(normalized, req, engine)

            if req.meta:
                self.context.transformer.engine = \
                    self.context.request.engine = \
                    JSONEngine(engine, req.image_url, req.meta_callback)

            self.context.transformer.transform(self.after_transform)

        self.filters_runner.apply_filters(thumbor.filters.PHASE_AFTER_LOAD, transform)
875027
def send_image_url(url, opt, refer, &callback)
      begin
        opt = {} if opt.nil?
        http = EM::HttpRequest.new(url, :connect_timeout => 2, :inactivity_timeout => 5).get opt
        file = Tempfile.new(['image', 'jpg'])
        http.stream { |chunk|
          file.write(chunk)
        }
        http.callback {
          file.close
          type = FastImage.type(file.path)
          if %i(jpeg png gif).include?(type)
            send_image(file.path, refer, &callback)
          else
            fail_back(&callback)
          end
        }
      rescue Exception => e
        logger.error("An error occurred during the image downloading: #{e.inspect} #{e.backtrace}")
        fail_back(&callback)
      end
    end
1316216
def build_image_from_inherited_image(self, image_name: str, image_tag: str,
                                         repo_path: Path,
                                         requirements_option: RequirementsOptions):
        

        base_name, base_tag = self.get_inherit_image()

        if requirements_option == RequirementsOptions.no_requirements:
            image = self.get_image(base_name, base_tag)
            image.tag(image_name, image_tag)  # so ``build_image`` doesn't have to be called next time

            return image

        dockerfile = self.get_install_requirements_dockerfile(base_name, base_tag, repo_path, requirements_option)

        self.get_or_build_image(image_name, image_tag, dockerfile, build_context=repo_path.parent, pull=False)

        return self.get_image(image_name, image_tag)
937885
def progress(progress):
    
    if isinstance(progress, int) or isinstance(progress, float):
        progress = float(progress)
    else:
        try:
            progress = float(json.loads(progress))
        except (TypeError, ValueError):
            return warning("Progress must be a float.")

    if not 0 <= progress <= 1:
        return warning("Progress must be a float between 0 and 1.")

    return json.dumps({'proc.progress': progress})
591873
def get_image_entrypoint(image):
    ''''''
    info = docker_inspect_or_pull(image)
    try:
        return info['Config']['Entrypoint']
    except KeyError as ke:
        raise DockerError('Failed to inspect image: JSON result missing key {}'.format(ke))
1087254
def base_image_inspect(self):
        
        if self._base_image_inspect is None:

            if self.base_from_scratch:
                self._base_image_inspect = {}
            elif self.parents_pulled or self.custom_base_image:
                try:
                    self._base_image_inspect = self.tasker.inspect_image(self.base_image)

                except docker.errors.NotFound:
                    # If the base image cannot be found throw KeyError -
                    # as this property should behave like a dict
                    raise KeyError("Unprocessed base image Dockerfile cannot be inspected")
            else:
                self._base_image_inspect =\
                    atomic_reactor.util.get_inspect_for_image(self.base_image,
                                                              self.base_image.registry,
                                                              self.base_image_insecure,
                                                              self.base_image_dockercfg_path)

            base_image_str = str(self.base_image)
            if base_image_str not in self._parent_images_inspect:
                self._parent_images_inspect[base_image_str] = self._base_image_inspect

        return self._base_image_inspect