Blob Blame History Raw
From 2153dcdb87571f8ed4ad90e187543e4dd6c89cb2 Mon Sep 17 00:00:00 2001
From: Radostin Stoyanov <rstoyanov1@gmail.com>
Date: Thu, 7 Dec 2017 20:42:28 +0000
Subject: [PATCH] docker-source: Avoid skopeo copy in cache

The `skopeo copy` command has changed it's behaviour to keep only a files for
single container image per directory. To get around this and keep cache of
downloaded images is used temporary destination directory for 'skopeo copy'
and image files are then moved in the cache folder.

(cherry picked from commit 76a945f9f2b88407bac8f7a8cc0d1464db183f21)
---
 src/virtBootstrap/sources/docker_source.py | 16 +++++++++++++---
 src/virtBootstrap/utils.py                 | 14 ++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/virtBootstrap/sources/docker_source.py b/src/virtBootstrap/sources/docker_source.py
index ef72d64..32252b2 100644
--- a/src/virtBootstrap/sources/docker_source.py
+++ b/src/virtBootstrap/sources/docker_source.py
@@ -133,10 +133,16 @@ class DockerSource(object):
         """
         Download image layers using "skopeo copy".
         """
+
+        if self.no_cache:
+            dest_dir = self.images_dir
+        else:
+            dest_dir = utils.get_image_dir(no_cache=True)
+
         # Note: we don't want to expose --src-cert-dir to users as
         #       they should place the certificates in the system
         #       folders for broader enablement
-        skopeo_copy = ["skopeo", "copy", self.url, "dir:" + self.images_dir]
+        skopeo_copy = ["skopeo", "copy", self.url, "dir:" + dest_dir]
 
         if self.insecure:
             skopeo_copy.append('--src-tls-verify=false')
@@ -146,8 +152,12 @@ class DockerSource(object):
         self.progress("Downloading container image", value=0, logger=logger)
         # Run "skopeo copy" command
         self.read_skopeo_progress(skopeo_copy)
-        # Remove the manifest file as it is not needed
-        os.remove(os.path.join(self.images_dir, "manifest.json"))
+
+        if not self.no_cache:
+            os.remove(os.path.join(dest_dir, "manifest.json"))
+            os.remove(os.path.join(dest_dir, "version"))
+            utils.copytree(dest_dir, self.images_dir)
+            shutil.rmtree(dest_dir)
 
     def parse_output(self, proc):
         """
diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index 84d1629..8dbf948 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -32,6 +32,7 @@ import subprocess
 import sys
 import tempfile
 import logging
+import shutil
 
 import guestfs
 import passlib.hosts
@@ -350,6 +351,19 @@ def get_mime_type(path):
         return output.read().decode('utf-8').split()[1]
 
 
+def copytree(src, dst, symlinks=False, ignore=None):
+    """
+    Copy an entire directory of files into an existing directory.
+    """
+    for item in os.listdir(src):
+        src_item = os.path.join(src, item)
+        dst_item = os.path.join(dst, item)
+        if os.path.isdir(src_item):
+            shutil.copytree(src_item, dst_item, symlinks, ignore)
+        else:
+            shutil.copy2(src_item, dst_item)
+
+
 def get_image_dir(no_cache=False):
     """
     Get the directory where image layers are stored.
-- 
2.17.0