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': ''})
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': '' })
def remove_container(self, container_id): """Deletes a container asynchronously.""" delete_container_ip(container_id) try: client.stop(container_id) client.remove_container(container_id) except APIError as exception: raise self.retry(exc=exception) r.delete('containers:%s' % container_id)
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)