2017-03-23 01:51:41 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2017-08-10 00:45:46 +00:00
|
|
|
import cnr.semver
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
from cnr.exception import raise_package_not_found, raise_channel_not_found, CnrException
|
2017-07-26 00:41:55 +00:00
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
import features
|
2017-07-26 00:41:55 +00:00
|
|
|
import data.model
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2017-04-13 12:25:47 +00:00
|
|
|
from app import storage, authentication
|
2018-05-23 21:08:00 +00:00
|
|
|
from data import appr_model
|
2018-05-24 21:54:51 +00:00
|
|
|
from data.database import Repository, MediaType, db_transaction
|
2018-07-16 21:12:04 +00:00
|
|
|
from data.appr_model.models import NEW_MODELS
|
2017-07-26 00:41:55 +00:00
|
|
|
from endpoints.appr.models_interface import (
|
|
|
|
ApplicationManifest, ApplicationRelease, ApplicationSummaryView, AppRegistryDataInterface,
|
|
|
|
BlobDescriptor, ChannelView, ChannelReleasesView)
|
2017-05-11 17:33:18 +00:00
|
|
|
from util.audit import track_and_log
|
|
|
|
from util.morecollections import AttrDict
|
2017-04-13 12:25:47 +00:00
|
|
|
from util.names import parse_robot_username
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2017-05-11 17:33:18 +00:00
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
|
|
|
|
class ReadOnlyException(CnrException):
|
|
|
|
status_code = 405
|
|
|
|
errorcode = "read-only"
|
|
|
|
|
|
|
|
|
2017-07-26 00:41:55 +00:00
|
|
|
def _strip_sha256_header(digest):
|
|
|
|
if digest.startswith('sha256:'):
|
|
|
|
return digest.split('sha256:')[1]
|
|
|
|
return digest
|
2017-05-11 17:33:18 +00:00
|
|
|
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def _split_package_name(package):
|
|
|
|
""" Returns the namespace and package-name """
|
|
|
|
return package.split("/")
|
|
|
|
|
|
|
|
|
|
|
|
def _join_package_name(ns, name):
|
|
|
|
""" Returns a app-name in the 'namespace/name' format """
|
|
|
|
return "%s/%s" % (ns, name)
|
|
|
|
|
|
|
|
|
|
|
|
def _timestamp_to_iso(timestamp, in_ms=True):
|
|
|
|
if in_ms:
|
|
|
|
timestamp = timestamp / 1000
|
|
|
|
return datetime.fromtimestamp(timestamp).isoformat()
|
|
|
|
|
|
|
|
|
2017-07-26 00:41:55 +00:00
|
|
|
def _application(package):
|
|
|
|
ns, name = _split_package_name(package)
|
|
|
|
repo = data.model.repository.get_app_repository(ns, name)
|
|
|
|
if repo is None:
|
|
|
|
raise_package_not_found(package)
|
|
|
|
return repo
|
|
|
|
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2018-05-23 21:08:00 +00:00
|
|
|
class CNRAppModel(AppRegistryDataInterface):
|
2018-05-24 21:54:51 +00:00
|
|
|
def __init__(self, models_ref, is_readonly):
|
|
|
|
self.models_ref = models_ref
|
|
|
|
self.is_readonly = is_readonly
|
|
|
|
|
2017-05-11 17:33:18 +00:00
|
|
|
def log_action(self, event_name, namespace_name, repo_name=None, analytics_name=None,
|
|
|
|
analytics_sample=1, metadata=None):
|
|
|
|
metadata = {} if metadata is None else metadata
|
|
|
|
|
|
|
|
repo = None
|
|
|
|
if repo_name is not None:
|
2017-07-26 00:41:55 +00:00
|
|
|
db_repo = data.model.repository.get_repository(namespace_name, repo_name,
|
2017-05-11 17:33:18 +00:00
|
|
|
kind_filter='application')
|
|
|
|
repo = AttrDict({
|
|
|
|
'id': db_repo.id,
|
|
|
|
'name': db_repo.name,
|
2017-07-26 00:41:55 +00:00
|
|
|
'namespace_name': db_repo.namespace_user.username,})
|
2017-05-11 17:33:18 +00:00
|
|
|
track_and_log(event_name, repo, analytics_name=analytics_name,
|
|
|
|
analytics_sample=analytics_sample, **metadata)
|
|
|
|
|
2017-03-23 01:51:41 +00:00
|
|
|
def list_applications(self, namespace=None, media_type=None, search=None, username=None,
|
|
|
|
with_channels=False):
|
|
|
|
""" Lists all repositories that contain applications, with optional filtering to a specific
|
|
|
|
namespace and view a specific user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
views = []
|
2018-05-24 21:54:51 +00:00
|
|
|
for repo in appr_model.package.list_packages_query(self.models_ref, namespace, media_type,
|
|
|
|
search, username=username):
|
|
|
|
tag_set_prefetch = getattr(repo, self.models_ref.tag_set_prefetch_name)
|
|
|
|
releases = [t.name for t in tag_set_prefetch]
|
2017-03-23 01:51:41 +00:00
|
|
|
if not releases:
|
|
|
|
continue
|
2017-04-13 14:00:50 +00:00
|
|
|
available_releases = [
|
2017-08-10 00:45:46 +00:00
|
|
|
str(x) for x in sorted(cnr.semver.versions(releases, False), reverse=True)]
|
2017-03-23 01:51:41 +00:00
|
|
|
channels = None
|
|
|
|
if with_channels:
|
2017-04-13 14:00:50 +00:00
|
|
|
channels = [
|
|
|
|
ChannelView(name=chan.name, current=chan.linked_tag.name)
|
2018-05-24 21:54:51 +00:00
|
|
|
for chan in appr_model.channel.get_repo_channels(repo, self.models_ref)]
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
app_name = _join_package_name(repo.namespace_user.username, repo.name)
|
|
|
|
manifests = self.list_manifests(app_name, available_releases[0])
|
|
|
|
view = ApplicationSummaryView(
|
|
|
|
namespace=repo.namespace_user.username,
|
|
|
|
name=app_name,
|
|
|
|
visibility=repo.visibility.name,
|
|
|
|
default=available_releases[0],
|
|
|
|
channels=channels,
|
|
|
|
manifests=manifests,
|
|
|
|
releases=available_releases,
|
2018-05-24 21:54:51 +00:00
|
|
|
updated_at=_timestamp_to_iso(tag_set_prefetch[-1].lifetime_start),
|
|
|
|
created_at=_timestamp_to_iso(tag_set_prefetch[0].lifetime_start),)
|
2017-03-23 01:51:41 +00:00
|
|
|
views.append(view)
|
|
|
|
return views
|
|
|
|
|
|
|
|
def application_is_public(self, package_name):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
* True if the repository is public
|
|
|
|
"""
|
|
|
|
namespace, name = _split_package_name(package_name)
|
2017-07-26 00:41:55 +00:00
|
|
|
return data.model.repository.repository_is_public(namespace, name)
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def create_application(self, package_name, visibility, owner):
|
|
|
|
""" Create a new app repository, owner is the user who creates it """
|
2018-05-24 21:54:51 +00:00
|
|
|
if self.is_readonly:
|
|
|
|
raise ReadOnlyException('Currently in read-only mode')
|
|
|
|
|
2017-03-23 01:51:41 +00:00
|
|
|
ns, name = _split_package_name(package_name)
|
2017-07-26 00:41:55 +00:00
|
|
|
data.model.repository.create_repository(ns, name, owner, visibility, 'application')
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def application_exists(self, package_name):
|
|
|
|
""" Create a new app repository, owner is the user who creates it """
|
|
|
|
ns, name = _split_package_name(package_name)
|
2017-07-26 00:41:55 +00:00
|
|
|
return data.model.repository.get_repository(ns, name, kind_filter='application') is not None
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def basic_search(self, query, username=None):
|
|
|
|
""" Returns an array of matching AppRepositories in the format: 'namespace/name'
|
|
|
|
Note:
|
|
|
|
* Only 'public' repositories are returned
|
|
|
|
|
|
|
|
Todo:
|
|
|
|
* Filter results with readeable reposistory for the user (including visibilitys)
|
|
|
|
"""
|
2017-04-13 14:00:50 +00:00
|
|
|
return [
|
|
|
|
_join_package_name(r.namespace_user.username, r.name)
|
2017-07-26 00:41:55 +00:00
|
|
|
for r in data.model.repository.get_app_search(lookup=query, username=username, limit=50)]
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def list_releases(self, package_name, media_type=None):
|
|
|
|
""" Return the list of all releases of an Application
|
|
|
|
Example:
|
|
|
|
>>> get_app_releases('ant31/rocketchat')
|
|
|
|
['1.7.1', '1.7.0', '1.7.2']
|
|
|
|
|
|
|
|
Todo:
|
|
|
|
* Paginate
|
|
|
|
"""
|
2018-05-24 21:54:51 +00:00
|
|
|
return appr_model.release.get_releases(_application(package_name), self.models_ref, media_type)
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def list_manifests(self, package_name, release=None):
|
|
|
|
""" Returns the list of all manifests of an Application.
|
|
|
|
|
|
|
|
Todo:
|
|
|
|
* Paginate
|
|
|
|
"""
|
|
|
|
try:
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2018-05-24 21:54:51 +00:00
|
|
|
return list(appr_model.manifest.get_manifest_types(repo, self.models_ref, release))
|
|
|
|
except (Repository.DoesNotExist, self.models_ref.Tag.DoesNotExist):
|
2017-03-23 01:51:41 +00:00
|
|
|
raise_package_not_found(package_name, release)
|
|
|
|
|
|
|
|
def fetch_release(self, package_name, release, media_type):
|
|
|
|
"""
|
|
|
|
Retrieves an AppRelease from it's repository-name and release-name
|
|
|
|
"""
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2017-03-23 01:51:41 +00:00
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
tag, manifest, blob = appr_model.release.get_app_release(repo, release, media_type,
|
|
|
|
self.models_ref)
|
2017-03-23 01:51:41 +00:00
|
|
|
created_at = _timestamp_to_iso(tag.lifetime_start)
|
|
|
|
|
2017-03-23 16:37:32 +00:00
|
|
|
blob_descriptor = BlobDescriptor(digest=_strip_sha256_header(blob.digest),
|
2017-04-13 14:00:50 +00:00
|
|
|
mediaType=blob.media_type.name, size=blob.size, urls=[])
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2017-04-13 14:00:50 +00:00
|
|
|
app_manifest = ApplicationManifest(
|
|
|
|
digest=manifest.digest, mediaType=manifest.media_type.name, content=blob_descriptor)
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2017-04-13 14:00:50 +00:00
|
|
|
app_release = ApplicationRelease(release=tag.name, created_at=created_at, name=package_name,
|
2017-03-23 01:51:41 +00:00
|
|
|
manifest=app_manifest)
|
|
|
|
return app_release
|
2018-05-24 21:54:51 +00:00
|
|
|
except (self.models_ref.Tag.DoesNotExist,
|
|
|
|
self.models_ref.Manifest.DoesNotExist,
|
|
|
|
self.models_ref.Blob.DoesNotExist,
|
|
|
|
Repository.DoesNotExist,
|
2017-03-23 01:51:41 +00:00
|
|
|
MediaType.DoesNotExist):
|
|
|
|
raise_package_not_found(package_name, release, media_type)
|
|
|
|
|
2017-08-10 00:45:46 +00:00
|
|
|
def store_blob(self, cnrblob, content_media_type):
|
2018-05-24 21:54:51 +00:00
|
|
|
if self.is_readonly:
|
|
|
|
raise ReadOnlyException('Currently in read-only mode')
|
|
|
|
|
2017-08-10 00:45:46 +00:00
|
|
|
fp = cnrblob.packager.io_file
|
|
|
|
path = cnrblob.upload_url(cnrblob.digest)
|
2017-03-23 01:51:41 +00:00
|
|
|
locations = storage.preferred_locations
|
|
|
|
storage.stream_write(locations, path, fp, 'application/x-gzip')
|
2018-05-23 21:08:00 +00:00
|
|
|
db_blob = appr_model.blob.get_or_create_blob(cnrblob.digest, cnrblob.size, content_media_type,
|
2018-05-24 21:54:51 +00:00
|
|
|
locations, self.models_ref)
|
2017-03-23 16:37:32 +00:00
|
|
|
return BlobDescriptor(mediaType=content_media_type,
|
2017-04-13 14:00:50 +00:00
|
|
|
digest=_strip_sha256_header(db_blob.digest), size=db_blob.size, urls=[])
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def create_release(self, package, user, visibility, force=False):
|
|
|
|
""" Add an app-release to a repository
|
2017-08-10 00:45:46 +00:00
|
|
|
package is an instance of data.cnr.package.Package
|
2017-03-23 01:51:41 +00:00
|
|
|
"""
|
2018-05-24 21:54:51 +00:00
|
|
|
if self.is_readonly:
|
|
|
|
raise ReadOnlyException('Currently in read-only mode')
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2017-07-26 00:41:55 +00:00
|
|
|
manifest = package.manifest()
|
2017-03-23 01:51:41 +00:00
|
|
|
ns, name = package.namespace, package.name
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = data.model.repository.get_or_create_repository(ns, name, user, visibility=visibility,
|
2018-05-24 21:54:51 +00:00
|
|
|
repo_kind='application')
|
2017-03-23 01:51:41 +00:00
|
|
|
tag_name = package.release
|
2018-05-24 21:54:51 +00:00
|
|
|
appr_model.release.create_app_release(repo, tag_name, package.manifest(),
|
|
|
|
manifest['content']['digest'], self.models_ref, force)
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def delete_release(self, package_name, release, media_type):
|
|
|
|
""" Remove/Delete an app-release from an app-repository.
|
|
|
|
It does not delete the entire app-repository, only a single release
|
|
|
|
"""
|
2018-05-24 21:54:51 +00:00
|
|
|
if self.is_readonly:
|
|
|
|
raise ReadOnlyException('Currently in read-only mode')
|
|
|
|
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2017-03-23 01:51:41 +00:00
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
appr_model.release.delete_app_release(repo, release, media_type, self.models_ref)
|
|
|
|
except (self.models_ref.Channel.DoesNotExist,
|
|
|
|
self.models_ref.Tag.DoesNotExist,
|
|
|
|
MediaType.DoesNotExist):
|
2017-03-23 01:51:41 +00:00
|
|
|
raise_package_not_found(package_name, release, media_type)
|
|
|
|
|
|
|
|
def release_exists(self, package, release):
|
|
|
|
""" Return true if a release with that name already exist or
|
|
|
|
have existed (include deleted ones) """
|
2018-05-24 21:54:51 +00:00
|
|
|
# TODO: Figure out why this isn't implemented.
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def channel_exists(self, package_name, channel_name):
|
|
|
|
""" Returns true if channel exists """
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2018-05-24 21:54:51 +00:00
|
|
|
return appr_model.tag.tag_exists(repo, channel_name, self.models_ref, "channel")
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def delete_channel(self, package_name, channel_name):
|
|
|
|
""" Delete an AppChannel
|
|
|
|
Note:
|
|
|
|
It doesn't delete the AppReleases
|
|
|
|
"""
|
2018-05-24 21:54:51 +00:00
|
|
|
if self.is_readonly:
|
|
|
|
raise ReadOnlyException('Currently in read-only mode')
|
|
|
|
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2017-03-23 01:51:41 +00:00
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
appr_model.channel.delete_channel(repo, channel_name, self.models_ref)
|
|
|
|
except (self.models_ref.Channel.DoesNotExist, self.models_ref.Tag.DoesNotExist):
|
2017-03-23 01:51:41 +00:00
|
|
|
raise_channel_not_found(package_name, channel_name)
|
|
|
|
|
|
|
|
def list_channels(self, package_name):
|
|
|
|
""" Returns all AppChannel for a package """
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2018-05-24 21:54:51 +00:00
|
|
|
channels = appr_model.channel.get_repo_channels(repo, self.models_ref)
|
2017-04-13 14:00:50 +00:00
|
|
|
return [ChannelView(name=chan.name, current=chan.linked_tag.name) for chan in channels]
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
def fetch_channel(self, package_name, channel_name, with_releases=True):
|
|
|
|
""" Returns an AppChannel """
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
channel = appr_model.channel.get_channel(repo, channel_name, self.models_ref)
|
|
|
|
except (self.models_ref.Channel.DoesNotExist, self.models_ref.Tag.DoesNotExist):
|
2017-03-23 01:51:41 +00:00
|
|
|
raise_channel_not_found(package_name, channel_name)
|
|
|
|
|
|
|
|
if with_releases:
|
2018-05-24 21:54:51 +00:00
|
|
|
releases = appr_model.channel.get_channel_releases(repo, channel, self.models_ref)
|
2017-04-13 14:00:50 +00:00
|
|
|
chanview = ChannelReleasesView(
|
|
|
|
current=channel.linked_tag.name, name=channel.name,
|
|
|
|
releases=[channel.linked_tag.name] + [c.name for c in releases])
|
2017-03-23 01:51:41 +00:00
|
|
|
else:
|
2017-04-13 14:00:50 +00:00
|
|
|
chanview = ChannelView(current=channel.linked_tag.name, name=channel.name)
|
2017-03-23 01:51:41 +00:00
|
|
|
|
|
|
|
return chanview
|
|
|
|
|
|
|
|
def list_release_channels(self, package_name, release, active=True):
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2017-03-23 01:51:41 +00:00
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
channels = appr_model.channel.get_tag_channels(repo, release, self.models_ref, active=active)
|
2017-03-23 01:51:41 +00:00
|
|
|
return [ChannelView(name=c.name, current=c.linked_tag.name) for c in channels]
|
2018-05-24 21:54:51 +00:00
|
|
|
except (self.models_ref.Channel.DoesNotExist, self.models_ref.Tag.DoesNotExist):
|
2017-03-23 01:51:41 +00:00
|
|
|
raise_package_not_found(package_name, release)
|
|
|
|
|
|
|
|
def update_channel(self, package_name, channel_name, release):
|
|
|
|
""" Append a new release to the AppChannel
|
|
|
|
Returns:
|
|
|
|
A new AppChannel with the release
|
|
|
|
"""
|
2018-05-24 21:54:51 +00:00
|
|
|
if self.is_readonly:
|
|
|
|
raise ReadOnlyException('Currently in read-only mode')
|
|
|
|
|
2017-07-26 00:41:55 +00:00
|
|
|
repo = _application(package_name)
|
2018-05-24 21:54:51 +00:00
|
|
|
channel = appr_model.channel.create_or_update_channel(repo, channel_name, release,
|
|
|
|
self.models_ref)
|
2017-04-13 14:00:50 +00:00
|
|
|
return ChannelView(current=channel.linked_tag.name, name=channel.name)
|
2017-03-23 01:51:41 +00:00
|
|
|
|
2017-07-26 00:41:55 +00:00
|
|
|
def get_blob_locations(self, digest):
|
2018-05-24 21:54:51 +00:00
|
|
|
return appr_model.blob.get_blob_locations(digest, self.models_ref)
|
2017-03-24 18:43:23 +00:00
|
|
|
|
2017-03-23 16:37:32 +00:00
|
|
|
|
2018-07-08 07:15:43 +00:00
|
|
|
# Phase 3: Read and write from new tables.
|
2018-07-08 07:15:43 +00:00
|
|
|
model = CNRAppModel(NEW_MODELS, features.READONLY_APP_REGISTRY)
|