docker: create tarball from Docker's storage when called http api

parent 596dc51b
......@@ -163,7 +163,7 @@ class LayerDownload(web.RequestHandler):
self.set_header("cache-control", "max-age=31536000")
@staticmethod
def prepare_tar_archive(layer_id: str) -> str:
async def prepare_tar_archive(layer_id: str) -> str:
"""
Finds docker layer path and prepare a tar archive for `layer_id`.
......@@ -182,35 +182,33 @@ class LayerDownload(web.RequestHandler):
except DockerLayer.DoesNotExist:
raise HTTPError(status_code=404, log_message="Layer Not Found")
if not layer.cache_path:
if not layer.cache_path and not layer.docker_path:
raise HTTPError(status_code=404, log_message="Layer Not Found")
# Do not create tarball from storage of docker!!!
# The digest of the tarball varies depending on the mtime of the file to be added
# layer.cache_path = Services.docker_util.layer_storage_path(layer_id).split('.gz')[0] # type: ignore # pylint: disable=line-too-long
# if not os.path.isfile(layer.cache_path):
# create_tar_archive(layer.docker_path, layer.cache_path)
# layer.save()
# not deal with .tar.gz in cache directory now
if not layer.cache_path and layer.docker_path:
layer.cache_path = await Services.docker_util.assemble_layer_tar(layer.diff_id) # type: ignore
layer.save()
return layer.cache_path
# pylint: disable=arguments-differ
def head(self, layer_id: str):
async def head(self, layer_id: str):
"""Head response with actual Content-Lenght of layer"""
self._set_headers(layer_id)
tar_path = self.prepare_tar_archive(layer_id)
tar_path = await self.prepare_tar_archive(layer_id)
self.set_header("Content-Length", str(os.path.getsize(tar_path)))
self.finish()
# pylint: enable=arguments-differ
# pylint: disable=arguments-differ
def get(self, layer_id):
async def get(self, layer_id):
"""
Get layer info by given layer_id
"""
self._set_headers(layer_id)
tar_path = self.prepare_tar_archive(layer_id)
tar_path = await self.prepare_tar_archive(layer_id)
with open(tar_path, 'rb') as file:
while True:
......
......@@ -665,12 +665,10 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
# check docker storage
if layer.docker_path:
tar_layer_path = await self.assemble_layer_tar(layer.diff_id)
layer.cache_path = await self.assemble_layer_tar(layer.diff_id)
self.logger.debug("Found layer %s in Docker's storage", diff_id)
layer.cache_path = tar_layer_path
layer.save()
return 'cache', tar_layer_path
return 'cache', layer.cache_path
# try to download layer from node that has tarball in own cache directory
for node_id in layer.available_at:
......@@ -941,11 +939,13 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
for layer_d in descriptors:
# check layer existence, then set status
layer = DockerLayer.get(DockerLayer.digest == layer_d['digest'])
status = self.DL_INIT
if layer.cache_path or layer.cache_gz_path or layer.docker_path:
status = self.DL_ALREADY
try:
layer = DockerLayer.get(DockerLayer.digest == layer_d['digest'])
if layer.cache_path or layer.cache_gz_path or layer.docker_path:
status = self.DL_ALREADY
except DockerLayer.DoesNotExist:
pass
self.queues[jobid][layer_d['digest']] = {
'queue': asyncio.Queue(),
......@@ -1161,11 +1161,11 @@ class DockerUtil: # pylint: disable=too-many-instance-attributes
self.layerdb_path, del_idpref(self.layerdb_mapping[diff_id]), "tar-split.json.gz")
if not os.path.exists(input_file):
raise DockerUtil.LayerMetadataNotFound(
"Docker doesn't have metadata of the layer %s", input_file)
"Docker doesn't have metadata of the layer %s" % input_file)
relative_path = self.docker_find_layer_dir_by_digest(self.diffid_mapping[diff_id])
if not relative_path:
raise DockerUtil.LayerNotFound("Layer doesn't exist in %s", relative_path)
raise DockerUtil.LayerNotFound("Layer doesn't exist in %s" % relative_path)
output_file = os.path.join(self.layer_tar_path, del_idpref(diff_id) + '.tar')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment