Beispiel #1
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': ''})
Beispiel #2
0
def start_container(container_id):
    """Starts a container. Never actually invoked asynchronously."""
    client.start(container_id, publish_all_ports=True)

    container_details = client.inspect_container(container_id)

    # Gather info about the new container
    container = {'container_id': container_id,
                 'name': container_details['Name'],
                 'image': container_details['Config']['Image'],
                 'status': RUNNING,
                 'active': 0}

    # Get new container's ports
    ports = get_container_ports(container_details['NetworkSettings']['Ports'])
    container.update(ports)

    r.hmset('containers:%s' % container_id, container)

    container_ip = container_details['NetworkSettings']['IPAddress']
    r.set('ips:%s' % container_ip, container_id)

    if not DISABLE_TIMEOUTS:
        check_container_activity.apply_async(
            args=(container_id,), countdown=INITIAL_TIMEOUT_INTERVAL)

    return container
Beispiel #3
0
def start_container(container_id):
    """Starts a container. Never actually invoked asynchronously."""
    client.start(container_id, publish_all_ports=True)

    container_details = client.inspect_container(container_id)

    # Gather info about the new container
    container = {
        'container_id': container_id,
        'name': container_details['Name'],
        'image': container_details['Config']['Image'],
        'status': RUNNING,
        'active': 0
    }

    # Get new container's ports
    ports = get_container_ports(container_details['NetworkSettings']['Ports'])
    container.update(ports)

    r.hmset('containers:%s' % container_id, container)

    container_ip = container_details['NetworkSettings']['IPAddress']
    r.set('ips:%s' % container_ip, container_id)

    if not DISABLE_TIMEOUTS:
        check_container_activity.apply_async(
            args=(container_id, ), countdown=INITIAL_TIMEOUT_INTERVAL)

    return container
Beispiel #4
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': ''
            })
Beispiel #5
0
    def test_check_in(self):
        container = self._create_container()
        container_id = container['container_id']

        self.app.post('/v1/check-in',
            headers=self.auth,
            data={'active': 1},
            environ_base={'REMOTE_ADDR': client.inspect_container(container_id)['NetworkSettings']['IPAddress'],})

        container_active = r.hget('containers:%s' % container_id, 'active')

        self.assertEqual(container_active, '1')
Beispiel #6
0
def delete_container_ip(container_id):
    """Deletes a container's IP mapping in redis."""
    container_ip = client.inspect_container(
        container_id)['NetworkSettings']['IPAddress']
    r.delete('ips:%s' % container_ip)
Beispiel #7
0
def delete_container_ip(container_id):
    """Deletes a container's IP mapping in redis."""
    container_ip = client.inspect_container(
        container_id)['NetworkSettings']['IPAddress']
    r.delete('ips:%s' % container_ip)