def _resolve_tag()

in appengine/runtime_builders/template_builder.py [0:0]


def _resolve_tag(image):
    """
    Given a path to a tagged Docker image in GCR, replace the tag with its
    corresponding sha256 digest.
    """
    if ':' not in image:
        logging.error('Image \'{0}\' must contain explicit tag or '
                      'digest!'.format(image))
        sys.exit(1)
    elif '@sha256' in image:
        return image
    else:
        parts = image.split(':')
        base_image = parts[0]
        target_tag = parts[1]

    command = ['gcloud', 'container', 'images',
               'list-tags', base_image, '--format=json']

    try:
        output = subprocess.check_output(command)
        entries = json.loads(output.decode("utf-8") )
        for image in entries:
            for tag in image.get('tags'):
                if tag == target_tag:
                    digest = image.get('digest')
                    return base_image + '@' + digest
        logging.error('Tag {0} not found on image {1}!'
                      .format(target_tag, base_image))
        sys.exit(1)
    except subprocess.CalledProcessError as e:
        logging.error(e)

    logging.error('No digest found for tag {0} on '
                  'image {1}'.format(target_tag, base_image))