Пример #1
0
def make_docker_image(userId, input_string=None, tag=None, digest=None, imageId=None, dockerfile=None, dockerfile_mode=None, registry_lookup=True, registry_creds=[], annotations={}):
    ret = {}

    if input_string:
        image_info = get_image_info(userId, "docker", input_string, registry_lookup=registry_lookup, registry_creds=registry_creds)
    else:
        if digest:
            image_info = get_image_info(userId, "docker", digest, registry_lookup=registry_lookup, registry_creds=registry_creds)
            digest = image_info['digest']
            
        if tag:
            image_info = get_image_info(userId, "docker", tag, registry_lookup=registry_lookup, registry_creds=registry_creds)
            if digest and not image_info['digest']:
                image_info['digest'] = digest
        
    if 'digest' in image_info:
        #imageDigest = urllib.base64.urlsafe_b64encode(str(image_info['digest']))
        imageDigest = str(image_info['digest'])
    else:
        raise Exception("input image_info needs to have a digest")
        
    if imageId:
        image_info['imageId'] = imageId

    new_input = db.CatalogImage().make()
    new_input['imageDigest'] = imageDigest
    new_input['userId'] = userId
    new_input['image_type'] = 'docker'
    new_input['dockerfile_mode'] = dockerfile_mode

    final_annotation_data = {}
    for k,v in annotations.items():
        if v != 'null':
            final_annotation_data[k] = v
    new_input['annotations'] = json.dumps(final_annotation_data)
    
    new_image_obj = db.CatalogImage(**new_input)
    new_image = dict((key,value) for key, value in vars(new_image_obj).iteritems() if not key.startswith('_'))
    new_image['image_detail'] = []

    if image_info['tag']:
        new_input = db.CatalogImageDocker().make()
        new_input['imageDigest'] = imageDigest
        new_input['userId'] = userId
        new_input['dockerfile'] = dockerfile

        for t in ['registry', 'repo', 'tag', 'digest', 'imageId']:
            if t in image_info:
                new_input[t] = image_info[t]
        
        new_docker_image_obj = db.CatalogImageDocker(**new_input)
        new_docker_image = dict((key,value) for key, value in vars(new_docker_image_obj).iteritems() if not key.startswith('_'))
        new_image['image_detail'] = [new_docker_image]

    ret = new_image
    return(ret)
Пример #2
0
def make_docker_image(
    userId,
    input_string=None,
    tag=None,
    digest=None,
    imageId=None,
    parentdigest=None,
    created_at=None,
    dockerfile=None,
    dockerfile_mode=None,
    registry_lookup=True,
    registry_creds=[],
    annotations={},
):
    ret = {}

    if input_string:
        image_info = get_image_info(
            userId,
            "docker",
            input_string,
            registry_lookup=registry_lookup,
            registry_creds=registry_creds,
        )
    else:
        if digest:
            image_info = get_image_info(
                userId,
                "docker",
                digest,
                registry_lookup=registry_lookup,
                registry_creds=registry_creds,
            )
            digest = image_info["digest"]

        if tag:
            image_info = get_image_info(
                userId,
                "docker",
                tag,
                registry_lookup=registry_lookup,
                registry_creds=registry_creds,
            )
            if digest and not image_info["digest"]:
                image_info["digest"] = digest

    if "digest" in image_info:
        imageDigest = str(image_info["digest"])
    else:
        raise Exception("input image_info needs to have a digest")

    if imageId:
        image_info["imageId"] = imageId

    new_input = db.CatalogImage().make()
    new_input["imageDigest"] = imageDigest
    new_input["userId"] = userId
    new_input["image_type"] = "docker"
    new_input["dockerfile_mode"] = dockerfile_mode

    if not parentdigest:
        parentdigest = imageDigest
    new_input["parentDigest"] = parentdigest

    if created_at:
        new_input["created_at"] = created_at

    final_annotation_data = {}
    for k, v in list(annotations.items()):
        if v != "null":
            final_annotation_data[k] = v
    new_input["annotations"] = json.dumps(final_annotation_data)

    new_image_obj = db.CatalogImage(**new_input)
    new_image = dict((key, value)
                     for key, value in vars(new_image_obj).items()
                     if not key.startswith("_"))
    new_image["image_detail"] = []

    if image_info["tag"]:
        new_input = db.CatalogImageDocker().make()
        new_input["imageDigest"] = imageDigest
        new_input["userId"] = userId
        new_input["dockerfile"] = dockerfile

        for t in ["registry", "repo", "tag", "digest", "imageId"]:
            if t in image_info:
                new_input[t] = image_info[t]

        new_docker_image_obj = db.CatalogImageDocker(**new_input)
        new_docker_image = dict(
            (key, value) for key, value in vars(new_docker_image_obj).items()
            if not key.startswith("_"))
        new_image["image_detail"] = [new_docker_image]

    ret = new_image
    return ret