Example #1
0
def check_container_activity(container_id, final=False):
    """Checks a container's activity.

    Containers configured for spin-docker activity monitoring regularly POST to
    the /check-in URL indicating whether they are active or not.

    If spin-docker is configured to check container activity, this celery task
    is scheduled when the container is started to check on the container after
    the INITIAL_TIMEOUT_INTERVAL setting. For each check after that, it uses the
    TIMEOUT_INTERVAL setting.

    The first time this task finds no container activity, it schedules itself
    for one last check after another TIMEOUT_INTERVAL. If there is still no
    activity, it stops the container.
    """
    container = r.hgetall('containers:%s' % container_id)
    if container and container['status'] == RUNNING:
        inactive = container['active'] == '0'

        if inactive:
            if final:
                stop_container.delay(container_id)
                return 'stopping container'
            else:
                check_container_activity.apply_async(
                    args=(container_id,), kwargs={'final': True, }, countdown=TIMEOUT_INTERVAL)
                return 'container inactive'
        else:
            check_container_activity.apply_async(
                args=(container_id,), countdown=TIMEOUT_INTERVAL)
            return 'container active'
Example #2
0
def audit_containers():
    """Reviews all known containers and updates their information in redis."""
    for ip_address in r.keys('ips:*'):
        r.delete(ip_address)

    containers = [r.hgetall(container) for container in r.keys('containers:*')]

    for container in containers:
        container_id = container['container_id']
        try:
            docker_info = client.inspect_container(container_id)
        except APIError:
            r.delete('containers:%s' % container_id)
            break

        if docker_info['State']['Running']:
            ports = get_container_ports(docker_info['NetworkSettings']['Ports'])
            r.hmset('containers:%s' % container_id, {
                    'status': RUNNING, 'active': 0,
                    'ssh_port': ports['ssh_port'],
                    'app_port': ports['app_port']})
            r.set('ips:%s' %
                  docker_info['NetworkSettings']['IPAddress'], container_id)
        else:
            r.hmset('containers:%s' % container_id, {
                    'status': STOPPED, 'active': 0, 'ssh_port': '', 'app_port': ''})
Example #3
0
def audit_containers():
    """Reviews all known containers and updates their information in redis."""
    for ip_address in r.keys('ips:*'):
        r.delete(ip_address)

    containers = [r.hgetall(container) for container in r.keys('containers:*')]

    for container in containers:
        container_id = container['container_id']
        try:
            docker_info = client.inspect_container(container_id)
        except APIError:
            r.delete('containers:%s' % container_id)
            break

        if docker_info['State']['Running']:
            ports = get_container_ports(
                docker_info['NetworkSettings']['Ports'])
            r.hmset(
                'containers:%s' % container_id, {
                    'status': RUNNING,
                    'active': 0,
                    'ssh_port': ports['ssh_port'],
                    'app_port': ports['app_port']
                })
            r.set('ips:%s' % docker_info['NetworkSettings']['IPAddress'],
                  container_id)
        else:
            r.hmset('containers:%s' % container_id, {
                'status': STOPPED,
                'active': 0,
                'ssh_port': '',
                'app_port': ''
            })
Example #4
0
def check_container_activity(container_id, final=False):
    """Checks a container's activity.

    Containers configured for spin-docker activity monitoring regularly POST to
    the /check-in URL indicating whether they are active or not.

    If spin-docker is configured to check container activity, this celery task
    is scheduled when the container is started to check on the container after
    the INITIAL_TIMEOUT_INTERVAL setting. For each check after that, it uses the
    TIMEOUT_INTERVAL setting.

    The first time this task finds no container activity, it schedules itself
    for one last check after another TIMEOUT_INTERVAL. If there is still no
    activity, it stops the container.
    """
    container = r.hgetall('containers:%s' % container_id)
    if container and container['status'] == RUNNING:
        inactive = container['active'] == '0'

        if inactive:
            if final:
                stop_container.delay(container_id)
                return 'stopping container'
            else:
                check_container_activity.apply_async(
                    args=(container_id, ),
                    kwargs={
                        'final': True,
                    },
                    countdown=TIMEOUT_INTERVAL)
                return 'container inactive'
        else:
            check_container_activity.apply_async(args=(container_id, ),
                                                 countdown=TIMEOUT_INTERVAL)
            return 'container active'
Example #5
0
    def get(self):
        """Returns all containers for the /containers endpoint."""
        self.reqparse.add_argument('audit', type=types.boolean, default=False,)
        args = self.reqparse.parse_args()

        if args['audit']:
            audit_containers()

        containers = [r.hgetall(container) for container in r.keys('containers:*')]
        return [marshal(c, container_fields) for c in containers]
Example #6
0
    def patch(self, container_id):
        """Updates information on a single container. Currently just status."""
        args = self.reqparse.parse_args()

        if 'status' in args:
            if args['status'] == STOPPED:
                stop_container.delay(container_id)
                r.hset('containers:%s' % container_id, 'status', STOPPING)
            elif args['status'] == RUNNING:
                try:
                    start_container(container_id)
                except APIError as exception:
                    abort(500, message=exception.explanation)

        container = r.hgetall('containers:%s' % container_id)
        return marshal(container, container_fields)
Example #7
0
 def get(self, container_id):
     """Returns information about a single container."""
     abort_if_container_doesnt_exist(container_id)
     container = r.hgetall('containers:%s' % container_id)
     return marshal(container, container_fields)