Esempio n. 1
0
    def test_mesos_executor_driver_status_update(self):
        os.environ['MESOS_SLAVE_PID'] = str(self.slave.pid)

        executor = Executor()
        driver = PesosExecutorDriver(executor, context=self.context)
        assert driver.start() == mesos_pb2.DRIVER_RUNNING

        # wait until registered
        driver.executor_process.connected.wait(timeout=MAX_TIMEOUT)
        assert driver.executor_process.connected.is_set()

        task_status = mesos_pb2.TaskStatus(
            task_id=mesos_pb2.TaskID(value='task-id'),
            state=mesos_pb2.TASK_FAILED,
        )

        assert driver.send_status_update(
            task_status) == mesos_pb2.DRIVER_RUNNING
        self.slave.status_update_event.wait(timeout=MAX_TIMEOUT)
        assert self.slave.status_update_event.is_set()

        assert len(self.slave.status_updates) == 1
        assert self.slave.status_updates[0][0] == driver.executor_process.pid

        status_update_message = self.slave.status_updates[0][1]
        status_update = status_update_message.update
        assert status_update.slave_id == self.slave.slave_id
        assert status_update.framework_id == mesos_pb2.FrameworkID(
            value=self.FRAMEWORK_ID)
        assert status_update.executor_id == mesos_pb2.ExecutorID(
            value=self.EXECUTOR_ID)
        assert status_update.status.task_id == task_status.task_id
        assert status_update.status.state == task_status.state
        assert status_update.HasField('timestamp')
        assert status_update.HasField('uuid')
Esempio n. 2
0
        def run_task():
            logger.info("Launching task %s", task.task_id.value)

            update = mesos_pb2.TaskStatus()
            update.task_id.value = task.task_id.value
            update.state = mesos_pb2.TASK_RUNNING
            driver.send_status_update(update)

            time.sleep(15)

            update = mesos_pb2.TaskStatus()
            update.task_id.value = task.task_id.value
            update.state = mesos_pb2.TASK_FINISHED

            # Send the terminal update
            driver.send_status_update(update)
Esempio n. 3
0
    def launch_task(self, driver, task):
        log.info('Got launch_task:')
        log.info('  task: %r' % task)

        status = mesos_pb2.TaskStatus()
        status.task_id.MergeFrom(task.task_id)
        status.state = mesos_pb2.TASK_FINISHED

        driver.send_status_update(status)
Esempio n. 4
0
    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 timeout > 1 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)

            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 not is_stream or (is_stream and buildTask.stream):
                        driver.sendFrameworkMessage(
                            ("%s: %s" % (image_name, message)).encode('unicode-escape')
                        )
            else:
                sandbox_dir = os.environ.get("MESOS_SANDBOX", os.environ["MESOS_DIRECTORY"])
                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 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)

            # 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
            ))
Esempio n. 5
0
                        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