data.oci_model: fix imports

This commit is contained in:
Jimmy Zelinskie 2017-03-23 00:21:21 -04:00
parent 069208f2f1
commit e872c310d0
5 changed files with 45 additions and 32 deletions

View file

@ -0,0 +1,9 @@
from data.oci_model import (
blob,
channel,
manifest,
manifest_list,
package,
release,
tag,
)

View file

@ -1,5 +1,5 @@
from data.oci_model import tag as tag_model
from data.database import Tag, Channel from data.database import Tag, Channel
from data.oci_model import tag as tag_model
def get_channel_releases(repo, channel): def get_channel_releases(repo, channel):

View file

@ -4,8 +4,8 @@ import json
from cnr.models.package_base import get_media_type from cnr.models.package_base import get_media_type
from data import oci_model
from data.database import db_transaction, Manifest, ManifestListManifest, MediaType, Blob, Tag from data.database import db_transaction, Manifest, ManifestListManifest, MediaType, Blob, Tag
from data.oci_model import tag as tag_model
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -38,14 +38,14 @@ def get_or_create_manifest(manifest_json, media_type_name):
def get_manifest_types(repo, release=None): def get_manifest_types(repo, release=None):
""" Returns an array of MediaTypes.name for a repo, can filter by tag """ """ Returns an array of MediaTypes.name for a repo, can filter by tag """
query = oci_model.tag.tag_alive_oci(Tag query = tag_model.tag_alive_oci(Tag
.select(MediaType.name) .select(MediaType.name)
.join(ManifestListManifest, .join(ManifestListManifest,
on=(ManifestListManifest.manifest_list == Tag.manifest_list)) on=(ManifestListManifest.manifest_list == Tag.manifest_list))
.join(MediaType, .join(MediaType,
on=(ManifestListManifest.media_type == MediaType.id)) on=(ManifestListManifest.media_type == MediaType.id))
.where(Tag.repository == repo, .where(Tag.repository == repo,
Tag.tag_kind == Tag.tag_kind.get_id('release'))) Tag.tag_kind == Tag.tag_kind.get_id('release')))
if release: if release:
query = query.where(Tag.name == release) query = query.where(Tag.name == release)

View file

@ -1,8 +1,10 @@
from cnr.models.package_base import get_media_type, manifest_media_type from cnr.models.package_base import get_media_type, manifest_media_type
from peewee import prefetch from peewee import prefetch
from data import model, oci_model
from data import model
from data.database import Repository, Namespace, Tag, ManifestListManifest from data.database import Repository, Namespace, Tag, ManifestListManifest
from data.oci_model import tag as tag_model
def list_packages_query(namespace=None, media_type=None, search_query=None, username=None): def list_packages_query(namespace=None, media_type=None, search_query=None, username=None):
@ -32,9 +34,9 @@ def list_packages_query(namespace=None, media_type=None, search_query=None, user
.order_by(Tag.lifetime_start)) .order_by(Tag.lifetime_start))
if media_type: if media_type:
tag_query = oci_model.tag.filter_tags_by_media_type(tag_query, media_type) tag_query = tag_model.filter_tags_by_media_type(tag_query, media_type)
tag_query = oci_model.tag.tag_alive_oci(tag_query) tag_query = tag_model.tag_alive_oci(tag_query)
query = prefetch(repo_query, tag_query) query = prefetch(repo_query, tag_query)
return query return query

View file

@ -3,9 +3,11 @@ import bisect
from cnr.exception import PackageAlreadyExists from cnr.exception import PackageAlreadyExists
from cnr.models.package_base import manifest_media_type from cnr.models.package_base import manifest_media_type
from data import oci_model
from data.database import (db_transaction, get_epoch_timestamp, Manifest, ManifestList, Tag, from data.database import (db_transaction, get_epoch_timestamp, Manifest, ManifestList, Tag,
ManifestListManifest, Blob, ManifestBlob) ManifestListManifest, Blob, ManifestBlob)
from data.oci_model import (blob as blob_model, manifest as manifest_model,
manifest_list as manifest_list_model,
tag as tag_model)
LIST_MEDIA_TYPE = 'application/vnd.cnr.manifest.list.v0.json' LIST_MEDIA_TYPE = 'application/vnd.cnr.manifest.list.v0.json'
@ -14,7 +16,7 @@ SCHEMA_VERSION = 'v0'
def get_app_release(repo, tag_name, media_type): def get_app_release(repo, tag_name, media_type):
""" Returns (tag, manifest, blob) given a repo object, tag_name, and media_type). """ """ Returns (tag, manifest, blob) given a repo object, tag_name, and media_type). """
tag = oci_model.tag.get_tag(repo, tag_name, tag_kind='release') tag = tag_model.get_tag(repo, tag_name, tag_kind='release')
media_type_id = ManifestListManifest.media_type.get_id(manifest_media_type(media_type)) media_type_id = ManifestListManifest.media_type.get_id(manifest_media_type(media_type))
manifestlistmanifest = (tag.manifest_list.manifestlistmanifest_set manifestlistmanifest = (tag.manifest_list.manifestlistmanifest_set
.join(Manifest) .join(Manifest)
@ -33,16 +35,16 @@ def create_app_release(repo, tag_name, manifest, digest):
""" """
with db_transaction(): with db_transaction():
# Create/get the package manifest # Create/get the package manifest
manifest = oci_model.manifest.get_or_create_manifest(manifest, manifest['mediaType']) manifest = manifest_model.get_or_create_manifest(manifest, manifest['mediaType'])
# get the tag # get the tag
tag = oci_model.tag.get_or_initialize_tag(repo, tag_name) tag = tag_model.get_or_initialize_tag(repo, tag_name)
if tag.manifest_list is None: if tag.manifest_list is None:
tag.manifest_list = ManifestList(media_type=ManifestList.media_type.get_id(LIST_MEDIA_TYPE), tag.manifest_list = ManifestList(media_type=ManifestList.media_type.get_id(LIST_MEDIA_TYPE),
schema_version=SCHEMA_VERSION, schema_version=SCHEMA_VERSION,
manifest_list_json=[]) manifest_list_json=[])
elif oci_model.tag.tag_media_type_exists(tag, manifest.media_type): elif tag_model.tag_media_type_exists(tag, manifest.media_type):
raise PackageAlreadyExists("package exists already") raise PackageAlreadyExists("package exists already")
list_json = tag.manifest_list.manifest_list_json list_json = tag.manifest_list.manifest_list_json
@ -53,12 +55,12 @@ def create_app_release(repo, tag_name, manifest, digest):
insert_point = bisect.bisect_left(list_manifest_ids, manifest.id) insert_point = bisect.bisect_left(list_manifest_ids, manifest.id)
list_json.insert(insert_point, manifest.manifest_json) list_json.insert(insert_point, manifest.manifest_json)
list_manifest_ids.insert(insert_point, manifest.id) list_manifest_ids.insert(insert_point, manifest.id)
manifestlist = oci_model.manifest_list.get_or_create_manifest_list(list_json, LIST_MEDIA_TYPE, manifestlist = manifest_list_model.get_or_create_manifest_list(list_json, LIST_MEDIA_TYPE,
SCHEMA_VERSION) SCHEMA_VERSION)
oci_model.manifest_list.create_manifestlistmanifest(manifestlist, list_manifest_ids, list_json) manifest_list_model.create_manifestlistmanifest(manifestlist, list_manifest_ids, list_json)
tag = oci_model.tag.create_or_update_tag(repo, tag_name, manifest_list=manifestlist, tag = tag_model.create_or_update_tag(repo, tag_name, manifest_list=manifestlist,
tag_kind="release") tag_kind="release")
blob_digest = digest blob_digest = digest
try: try:
@ -67,7 +69,7 @@ def create_app_release(repo, tag_name, manifest, digest):
.join(Blob) .join(Blob)
.where(ManifestBlob.manifest == manifest, Blob.digest == blob_digest).get()) .where(ManifestBlob.manifest == manifest, Blob.digest == blob_digest).get())
except ManifestBlob.DoesNotExist: except ManifestBlob.DoesNotExist:
blob = oci_model.blob.get_blob(blob_digest) blob = blob_model.get_blob(blob_digest)
ManifestBlob.create(manifest=manifest, blob=blob) ManifestBlob.create(manifest=manifest, blob=blob)
return tag return tag
@ -77,7 +79,7 @@ def delete_app_release(repo, tag_name, media_type):
media_type_id = ManifestListManifest.media_type.get_id(manifest_media_type(media_type)) media_type_id = ManifestListManifest.media_type.get_id(manifest_media_type(media_type))
with db_transaction(): with db_transaction():
tag = oci_model.tag.get_tag(repo, tag_name) tag = tag_model.get_tag(repo, tag_name)
manifest_list = tag.manifest_list manifest_list = tag.manifest_list
list_json = manifest_list.manifest_list_json list_json = manifest_list.manifest_list_json
mlm_query = (ManifestListManifest mlm_query = (ManifestListManifest
@ -96,12 +98,12 @@ def delete_app_release(repo, tag_name, media_type):
tag.lifetime_end = get_epoch_timestamp() tag.lifetime_end = get_epoch_timestamp()
tag.save() tag.save()
else: else:
manifestlist = oci_model.manifest_list.get_or_create_manifest_list(list_json, LIST_MEDIA_TYPE, manifestlist = manifest_list_model.get_or_create_manifest_list(list_json, LIST_MEDIA_TYPE,
SCHEMA_VERSION) SCHEMA_VERSION)
oci_model.manifest_list.create_manifestlistmanifest(manifestlist, list_manifest_ids, manifest_list_model.create_manifestlistmanifest(manifestlist, list_manifest_ids,
list_json) list_json)
tag = oci_model.tag.create_or_update_tag(repo, tag_name, manifest_list=manifestlist, tag = tag_model.create_or_update_tag(repo, tag_name, manifest_list=manifestlist,
tag_kind="release") tag_kind="release")
return tag return tag
@ -112,5 +114,5 @@ def get_releases(repo, media_type=None):
.where(Tag.repository == repo, .where(Tag.repository == repo,
Tag.tag_kind == Tag.tag_kind.get_id("release"))) Tag.tag_kind == Tag.tag_kind.get_id("release")))
if media_type: if media_type:
release_query = oci_model.tag.filter_tags_by_media_type(release_query, media_type) release_query = tag_model.filter_tags_by_media_type(release_query, media_type)
return [t.name for t in oci_model.tag.tag_alive_oci(release_query)] return [t.name for t in tag_model.tag_alive_oci(release_query)]