def _build_image(self, driver, taskInfo, buildTask):
        """Build an image for the given buildTask."""

        # Tell mesos that we're starting the task
        driver.sendStatusUpdate(mesos_pb2.TaskStatus(
            task_id=taskInfo.task_id,
            state=self.TASK_STARTING
        ))

        logger.info("Waiting for docker daemon to be available")

        # Wait for the docker daemon to be ready (up to 30 seconds)
        timeout = 30
        while 1 > timeout and not self.docker_daemon_up:
            timeout -= 1
            time.sleep(1)

        try:
            if not self.docker_daemon_up:
                raise Exception("Timed out waiting for docker daemon")

            # Now that docker is up, let's go and do stuff
            driver.sendStatusUpdate(mesos_pb2.TaskStatus(
                task_id=taskInfo.task_id,
                state=self.TASK_RUNNING
            ))

            if not buildTask:
                raise Exception("Failed to decode the BuildTask protobuf data")

            if not buildTask.context and not buildTask.dockerfile:
                raise Exception("Either a build context or dockerfile is required")

            registry_url = None
            if buildTask.image.HasField("registry"):
                registry_url = buildTask.image.registry.hostname
                if buildTask.image.registry.HasField("port"):
                    registry_url += ":%d" % buildTask.image.registry.port
                registry_url += "/"

            if not registry_url:
                raise Exception("No registry URL provided")

            image_name = registry_url + buildTask.image.repository
            logger.info("Building image %s", image_name)

            # Used to store the ID of the image the build is based on, if any
            base_image_id = None

            sandbox_dir = os.environ.get("MESOS_SANDBOX", os.environ["MESOS_DIRECTORY"])
            if buildTask.dockerfile:
                build_request = self.docker.build(
                    fileobj=io.StringIO(buildTask.dockerfile),
                    stream=True
                )

                for message, is_stream in self._wrap_docker_stream(build_request):
                    if message.startswith(u" ---\u003e") and not base_image_id:
                        base_image_id = message[6:]
                    if not is_stream or (is_stream and buildTask.stream):
                        driver.sendFrameworkMessage(
                            ("%s: %s" % (image_name, message)).encode('unicode-escape')
                        )
            else:
                context_path = os.path.join(sandbox_dir, buildTask.context)

                if not os.path.exists(context_path):
                    raise Exception("Context %s does not exist" % (context_path))

                with open(context_path, "r") as context:
                    build_request = self.docker.build(
                        fileobj=context,
                        custom_context=True,
                        encoding="gzip",
                        stream=True
                    )

                    for message, is_stream in self._wrap_docker_stream(build_request):
                        if message.startswith(u" ---\u003e") and not base_image_id:
                            base_image_id = message[6:]
                        if not is_stream or (is_stream and buildTask.stream):
                            driver.sendFrameworkMessage(
                                ("%s: %s" % (image_name, message)).encode('unicode-escape')
                            )

            # Extract the newly created image ID
            match = re.search(r'built (.*)$', message)
            if not match:
                raise Exception("Failed to match image ID from %r" % message)
            image_id = match.group(1)

            # If we've been asked to squash all of the layers for this build into
            # one, do it.
            if buildTask.HasField("squash") and buildTask.squash:
                if not base_image_id:
                    raise Exception("Failed to extract the base image ID to squash against")

                image_id = self._squash_image(
                    driver,
                    sandbox_dir,
                    image_name,
                    base_image_id,
                    image_id
                )

            # Tag the image with all the required tags
            tags = buildTask.image.tag or ["latest"]
            driver.sendFrameworkMessage(str("%s: Tagging image %s" % (image_name, image_id)))
            for tag in tags:
                try:
                    self.docker.tag(
                        image=image_id,
                        repository=image_name,
                        tag=tag,
                        force=True
                    )
                    driver.sendFrameworkMessage(str("%s:    -> %s" % (image_name, tag)))
                except Exception, e:
                    raise e

            # Push the image to the registry
            driver.sendFrameworkMessage(str("%s: Pushing image" % image_name))
            push_request = self.docker.push(image_name, stream=True)
            for message, is_stream in self._wrap_docker_stream(push_request):
                if not is_stream or (is_stream and buildTask.stream):
                    driver.sendFrameworkMessage(
                        str("%s: %s" % (image_name, message))
                    )

            driver.sendStatusUpdate(mesos_pb2.TaskStatus(
                task_id=taskInfo.task_id,
                state=self.TASK_FINISHED
            ))
        except Exception, e:
            logger.error("Caught exception building image: %s", e)
            driver.sendStatusUpdate(mesos_pb2.TaskStatus(
                task_id=taskInfo.task_id,
                state=self.TASK_FAILED,
                message=str(e),
                data=traceback.format_exc()
            ))

            # Re-raise the exception for logging purposes and to terminate the thread
            raise
dataset/ETHPy150Open duedil-ltd/portainer/portainer/app/executor.py/Executor._build_image/swapped operands of `>`