2017-03-23 01:51:28 +00:00
|
|
|
import logging
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
|
2017-08-10 00:45:46 +00:00
|
|
|
from cnr.models.package_base import get_media_type
|
2017-03-23 01:51:28 +00:00
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
from data.database import db_transaction, MediaType
|
2018-05-23 21:08:00 +00:00
|
|
|
from data.appr_model import tag as tag_model
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-03-23 16:37:32 +00:00
|
|
|
def _ensure_sha256_header(digest):
|
|
|
|
if digest.startswith('sha256:'):
|
|
|
|
return digest
|
|
|
|
return 'sha256:' + digest
|
|
|
|
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
def _digest(manifestjson):
|
2017-03-23 16:37:32 +00:00
|
|
|
return _ensure_sha256_header(hashlib.sha256(json.dumps(manifestjson, sort_keys=True)).hexdigest())
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_manifest_query(digest, media_type, models_ref):
|
|
|
|
Manifest = models_ref.Manifest
|
2017-03-23 16:37:32 +00:00
|
|
|
return Manifest.select().where(Manifest.digest == _ensure_sha256_header(digest),
|
2017-03-23 01:51:28 +00:00
|
|
|
Manifest.media_type == Manifest.media_type.get_id(media_type))
|
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_manifest_with_blob(digest, media_type, models_ref):
|
|
|
|
Blob = models_ref.Blob
|
|
|
|
query = get_manifest_query(digest, media_type, models_ref)
|
2017-03-23 01:51:28 +00:00
|
|
|
return query.join(Blob).get()
|
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_or_create_manifest(manifest_json, media_type_name, models_ref):
|
|
|
|
Manifest = models_ref.Manifest
|
2017-03-23 01:51:28 +00:00
|
|
|
digest = _digest(manifest_json)
|
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
manifest = get_manifest_query(digest, media_type_name, models_ref).get()
|
2017-03-23 01:51:28 +00:00
|
|
|
except Manifest.DoesNotExist:
|
|
|
|
with db_transaction():
|
|
|
|
manifest = Manifest.create(digest=digest,
|
|
|
|
manifest_json=manifest_json,
|
|
|
|
media_type=Manifest.media_type.get_id(media_type_name))
|
|
|
|
return manifest
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_manifest_types(repo, models_ref, release=None):
|
2017-03-23 01:51:28 +00:00
|
|
|
""" Returns an array of MediaTypes.name for a repo, can filter by tag """
|
2018-05-24 21:54:51 +00:00
|
|
|
Tag = models_ref.Tag
|
|
|
|
ManifestListManifest = models_ref.ManifestListManifest
|
|
|
|
|
|
|
|
query = tag_model.tag_is_alive(Tag
|
2017-03-23 04:21:21 +00:00
|
|
|
.select(MediaType.name)
|
|
|
|
.join(ManifestListManifest,
|
|
|
|
on=(ManifestListManifest.manifest_list == Tag.manifest_list))
|
|
|
|
.join(MediaType,
|
|
|
|
on=(ManifestListManifest.media_type == MediaType.id))
|
|
|
|
.where(Tag.repository == repo,
|
2018-05-24 21:54:51 +00:00
|
|
|
Tag.tag_kind == Tag.tag_kind.get_id('release')), Tag)
|
2017-03-23 01:51:28 +00:00
|
|
|
if release:
|
|
|
|
query = query.where(Tag.name == release)
|
|
|
|
|
|
|
|
manifests = set()
|
|
|
|
for m in query.distinct().tuples():
|
|
|
|
manifests.add(get_media_type(m[0]))
|
|
|
|
return manifests
|