Ejemplo n.º 1
0
 def get_container_logs(self,
                        container,
                        config,
                        stderr=True,
                        stdout=False,
                        limit=100):
     wrapper = DockerWrapper(container.host.hostname, container.host.port,
                             config)
     return wrapper.logs(container.id,
                         stdout=stdout,
                         stderr=stderr,
                         tail=limit)
Ejemplo n.º 2
0
    def launch_deployment(self, deployment_id, config):
        db = self.get_session(self.get_engine(config))

        deployment = db.query(Deployment).get(deployment_id)
        encoded = deployment.encode_with_deps(db)
        logging.info("Launching deployment {}:{}/{}".format(
            deployment.app_name, deployment.image_tag, deployment.environment))
        for host in deployment.hosts:
            logging.info("Launching deployment {}:{}/{} on {}".format(
                deployment.app_name, deployment.image_tag,
                deployment.environment, host.alias))
            wrapper = DockerWrapper(host.hostname, host.port, config)
            wrapper.deploy_with_deps(encoded)

        try:
            self.update_deployment_containers(db, deployment, config)
            self.update_deployment_status(db, deployment)
        except Exception as e:
            logging.error(e)
            db.rollback()
        logging.info("Finished deployment {}:{}/{}".format(
            deployment.app_name, deployment.image_tag, deployment.environment))
Ejemplo n.º 3
0
    def update_host_containers(self, db, host, config):
        """
        For a given host, update the application status and container
        state for all containers.
        """
        wrapper = DockerWrapper(host.hostname, host.port, config)
        host_container_info = []

        # Keep track of containers that go away
        previous_host_containers = dict([(container.id, container)
                                         for container in host.containers])
        new_host_containers = set()

        try:
            host_container_info = wrapper.state_of_the_universe()
        except Exception as e:
            logging.error(e)
            host.status = 'down'
            db.add(host)

        for info in host_container_info:
            container = Container.get(db, info['id'])
            new_host_containers.add(info['id'])
            if container:
                logging.info(
                    "Found existing container {}, updating state to: {}".
                    format(info['id'], info['state']))
                container.state = info['state']
                container.finished_at = parser.parse(
                    info['finished_at']).astimezone(
                        tz.tzlocal()).replace(tzinfo=None)
                db.add(container)
            else:
                logging.info(
                    "Got new container {}, setting state to: {}".format(
                        info['id'], info['state']))
                image_layer = info['image_id'][0:8]
                image = Image.get(db, image_layer)
                container = Container(
                    id=info['id'],
                    image_ref=info['image_ref'],
                    state=info['state'],
                    started_at=parser.parse(info['started_at']).astimezone(
                        tz.tzlocal()).replace(tzinfo=None),
                    finished_at=parser.parse(info['finished_at']).astimezone(
                        tz.tzlocal()).replace(tzinfo=None))
                if 'command' in info:
                    container.command = info['command']
                if image:
                    container.image = image
                host.containers.append(container)
                db.add(container)

        for c_id in previous_host_containers:
            if c_id not in new_host_containers:
                logging.info(
                    "Previous container {} not found on host, removing".format(
                        c_id))
                container = previous_host_containers[c_id]
                db.delete(container)

        db.add(host)
        db.commit()