Ejemplo n.º 1
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()
Ejemplo n.º 2
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()