Fetch an image if it isn't already present.

    This works like ``docker pull`` and will pull the tag ``latest`` if no tag
    is specified in the image name.

def fetch_image(client, name):
    """
    Fetch an image if it isn't already present.

    This works like ``docker pull`` and will pull the tag ``latest`` if no tag
    is specified in the image name.
    """
    try:
        image = client.images.get(name)
    except docker.errors.ImageNotFound:
        name, tag = _parse_image_tag(name)
        tag = 'latest' if tag is None else tag

        log.info("Pulling tag '{}' for image '{}'...".format(tag, name))
        image = client.images.pull(name, tag=tag)

    log.debug("Found image '{}' for tag '{}'".format(image.id, name))
    return image