Exemplo n.º 1
0
def try_restart_on_exited_containers():
    docker = get_docker_client()
    restarted_instances = []
    # 'exited': 0 throws exception, 'exited': '0' does not work.
    # Because of this I have felt forced to use regular expressions :-(
    pattern = re.compile(r"Exited [(](\d+)[)]")
    for container in docker.containers(filters={'status': 'exited'}):
        # Ignore containers not created from image 'packettracer'
        if container.get('Image') == 'packettracer':
            match = pattern.match(container.get('Status'))
            if match:  # Stopped containers only
                container_id = container.get('Id')
                instance = Instance.get_by_docker_id(container_id)
                if instance:
                    if match.group(1) == '0':
                        # Restart stopped containers (which exited successfully)
                        instance.mark_starting()
                        try:
                            logger.info('Restarting %s.' % instance)
                            docker.start(container=container_id)
                            wait_for_ready_container.s(instance.id).delay()
                            restarted_instances.append(instance.id)
                        except APIError as ae:
                            logger.error('Error restarting container.')
                            logger.error('Docker API exception. %s.' % ae)
                            instance.mark_error()
                    else:
                        # TODO Check more thoroughly if containers with other
                        # type of exit errors could be restarted too.
                        instance.mark_error()
    return restarted_instances
Exemplo n.º 2
0
def try_restart_on_exited_containers():
    docker = get_docker_client()
    restarted_instances = []
    # 'exited': 0 throws exception, 'exited': '0' does not work.
    # Because of this I have felt forced to use regular expressions :-(
    pattern = re.compile(r"Exited [(](\d+)[)]")
    for container in docker.containers(filters={'status': 'exited'}):
        # Ignore containers not created from image 'packettracer'
        if container.get('Image')=='packettracer':
            match = pattern.match(container.get('Status'))
            if match:  # Stopped containers only
                container_id = container.get('Id')
                instance = Instance.get_by_docker_id(container_id)
                if instance:
                    if match.group(1)=='0':
                        # Restart stopped containers (which exited successfully)
                        instance.mark_starting()
                        try:
                            logger.info('Restarting %s.' % instance)
                            docker.start(container=container_id)
                            wait_for_ready_container.s(instance.id).delay()
                            restarted_instances.append(instance.id)
                        except APIError as ae:
                            logger.error('Error restarting container.')
                            logger.error('Docker API exception. %s.' % ae)
                            instance.mark_error()
                    else:
                        # TODO Check more thoroughly if containers with other
                        # type of exit errors could be restarted too.
                        instance.mark_error()
    return restarted_instances