2013-09-30 23:10:27 +00:00
|
|
|
import logging
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
import contextlib
|
|
|
|
import tempfile
|
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
from app import app
|
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
__all__ = ['load']
|
2013-09-30 23:10:27 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Storage(object):
|
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
"""Storage is organized as follow:
|
|
|
|
$ROOT/images/<image_id>/json
|
|
|
|
$ROOT/images/<image_id>/layer
|
|
|
|
$ROOT/repositories/<namespace>/<repository_name>/<tag_name>
|
|
|
|
"""
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
# Useful if we want to change those locations later without rewriting
|
|
|
|
# the code which uses Storage
|
|
|
|
repositories = 'repositories'
|
|
|
|
images = 'images'
|
|
|
|
# Set the IO buffer to 64kB
|
|
|
|
buffer_size = 64 * 1024
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
#FIXME(samalba): Move all path resolver in each module (out of the base)
|
|
|
|
def images_list_path(self, namespace, repository):
|
|
|
|
return '{0}/{1}/{2}/_images_list'.format(self.repositories,
|
2013-10-01 16:13:25 +00:00
|
|
|
namespace,
|
|
|
|
repository)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def image_json_path(self, namespace, repository, image_id):
|
|
|
|
return '{0}/{1}/{2}/{3}/json'.format(self.images, namespace,
|
2013-10-01 16:13:25 +00:00
|
|
|
repository, image_id)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def image_mark_path(self, namespace, repository, image_id):
|
|
|
|
return '{0}/{1}/{2}/{3}/_inprogress'.format(self.images, namespace,
|
2013-10-01 16:13:25 +00:00
|
|
|
repository, image_id)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def image_checksum_path(self, namespace, repository, image_id):
|
|
|
|
return '{0}/{1}/{2}/{3}/_checksum'.format(self.images, namespace,
|
2013-10-01 16:13:25 +00:00
|
|
|
repository, image_id)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def image_layer_path(self, namespace, repository, image_id):
|
|
|
|
return '{0}/{1}/{2}/{3}/layer'.format(self.images, namespace,
|
2013-10-01 16:13:25 +00:00
|
|
|
repository, image_id)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def image_ancestry_path(self, namespace, repository, image_id):
|
|
|
|
return '{0}/{1}/{2}/{3}/ancestry'.format(self.images, namespace,
|
2013-10-01 16:13:25 +00:00
|
|
|
repository, image_id)
|
|
|
|
|
|
|
|
def repository_namespace_path(self, namespace, repository):
|
|
|
|
return '{0}/{1}/{2}/'.format(self.images, namespace, repository)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-10-17 22:25:19 +00:00
|
|
|
def image_file_trie_path(self, namespace, repository, image_id):
|
|
|
|
return '{0}/{1}/{2}/{3}/files.trie'.format(self.images, namespace,
|
|
|
|
repository, image_id)
|
|
|
|
|
2013-10-18 18:31:14 +00:00
|
|
|
def image_file_diffs_path(self, namespace, repository, image_id):
|
|
|
|
return '{0}/{1}/{2}/{3}/diffs.json'.format(self.images, namespace,
|
|
|
|
repository, image_id)
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def get_content(self, path):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def put_content(self, path, content):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def stream_read(self, path):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-10-18 18:31:14 +00:00
|
|
|
def stream_read_file(self, path):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def stream_write(self, path, fp):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def list_directory(self, path=None):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def exists(self, path):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def remove(self, path):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def get_size(self, path):
|
|
|
|
raise NotImplementedError
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def store_stream(stream):
|
2013-09-30 23:10:27 +00:00
|
|
|
"""Stores the entire stream to a temporary file."""
|
|
|
|
tmpf = tempfile.TemporaryFile()
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
buf = stream.read(4096)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
tmpf.write(buf)
|
|
|
|
except IOError:
|
|
|
|
break
|
|
|
|
tmpf.seek(0)
|
|
|
|
yield tmpf
|
|
|
|
tmpf.close()
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def temp_store_handler():
|
2013-09-30 23:10:27 +00:00
|
|
|
tmpf = tempfile.TemporaryFile()
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
def fn(buf):
|
|
|
|
try:
|
|
|
|
tmpf.write(buf)
|
|
|
|
except IOError:
|
|
|
|
pass
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
return tmpf, fn
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
from local import LocalStorage
|
|
|
|
from s3 import S3Storage
|
|
|
|
|
|
|
|
|
|
|
|
_storage = {}
|
|
|
|
|
|
|
|
|
|
|
|
def load(kind=None):
|
2013-09-30 23:10:27 +00:00
|
|
|
"""Returns the right storage class according to the configuration."""
|
|
|
|
global _storage
|
|
|
|
|
|
|
|
# TODO hard code to local for now
|
|
|
|
kind = app.config['STORAGE_KIND']
|
|
|
|
# if not kind:
|
|
|
|
# kind = cfg.storage.lower()
|
|
|
|
if kind in _storage:
|
|
|
|
return _storage[kind]
|
|
|
|
if kind == 's3':
|
|
|
|
logger.debug('Using s3 storage.')
|
|
|
|
store = S3Storage('', app.config['AWS_ACCESS_KEY'],
|
|
|
|
app.config['AWS_SECRET_KEY'],
|
|
|
|
app.config['REGISTRY_S3_BUCKET'])
|
|
|
|
elif kind == 'local':
|
|
|
|
logger.debug('Using local storage.')
|
|
|
|
store = LocalStorage(app.config['LOCAL_STORAGE_DIR'])
|
|
|
|
else:
|
|
|
|
raise ValueError('Not supported storage \'{0}\''.format(kind))
|
|
|
|
_storage[kind] = store
|
|
|
|
return store
|