# Helper functions for docker

import docker
import os

DEFAULT_DOCKER_API_VERSION = '1.10'
BASEIMAGETAG = "glusterbase:latest"
GLUSTERIMAGENAME = "gluster:latest"
BASEDIR=os.getcwd()

class DockerHelper (docker.Client):
    """
    Extended docker client with some helper functions
    """
    def __init__ (self):
        super(DockerHelper, self).__init__(version=DEFAULT_DOCKER_API_VERSION)

    def image_by_id (self, id):
        """
        Return image with given Id
        """
        if not id:
            return None
        return next((image for image in self.images() if image['Id'] == id), None)

    def image_by_tag(self, tag):
        """
        Return image with given tag
        """
        if not tag:
            return None

        return next((image for image in self.images() if tag in image['RepoTags']), None)

    def image_exists (self, id = None, tag = None):
        """
        Check if specified image exists
        """
        exists = False
        if id and self.image_by_id(id):
            exists = True
        elif tag and self.image_by_tag (tag):
            exists = True

        return exists

    def container_ip (self, container):
        """
        Returns the internal ip of the container if available
        """
        info = self.inspect_container(container)
        if not info:
            return None

        netInfo = info['NetworkSettings']
        if not netInfo:
            return None

        ip = netInfo['IPAddress']
        if not ip:
            return None

        return ip

@@ -1137,16 +1137,855 @@
 exists%0A%0A
+    def container_by_id (self, id):%0A        %22%22%22%0A        Returns container with given id%0A        %22%22%22%0A        if not id:%0A            return None%0A        return next((container for container in self.containers(all = True) if container%5B'Id'%5D == id), None)%0A%0A    def container_by_name (self, name):%0A        %22%22%22%0A        Returns container with given name%0A        %22%22%22%0A        if not name:%0A            return None%0A        return next((container for container in self.containers(all = True) if name in container%5B'Names'%5D), None)%0A    def container_exists (self, id = None, name = None):%0A        %22%22%22%0A        Checks if container exists already%0A        %22%22%22%0A        exists = False%0A        if id and self.container_by_id (id):%0A            exists = True%0A        elif name and self.container_by_name (name):%0A            exists = True%0A%0A        return exists%0A%0A
     def 
@@ -2384,8 +2384,9 @@
 turn ip%0A
+%0A

Add docker helpers for getting containers