Skip to content

Commit

Permalink
Cache downloaded container images
Browse files Browse the repository at this point in the history
Store container images downloaded with skopeo to avoid re-download.
Allow user to disable this option by passing "--no-cache" parameter
  • Loading branch information
rst0git authored and cbosdo committed Jun 7, 2017
1 parent 4c45f17 commit 0b998e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
35 changes: 27 additions & 8 deletions sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
import subprocess
import tempfile
import getpass
import os


# default_image_dir - Path where Docker images (tarballs) will be stored
if os.geteuid() == 0:
default_image_dir = "/var/lib/virt-bootstrap/docker_images"
else:
default_image_dir = \
os.environ['HOME'] + "/.local/share/virt-bootstrap/docker_images"


def checksum(path, sum_type, sum_expected):
Expand All @@ -40,7 +49,7 @@ def checksum(path, sum_type, sum_expected):


class FileSource:
def __init__(self, url, username, password, insecure):
def __init__(self, url, *args):
self.path = url.path

def unpack(self, dest):
Expand All @@ -51,18 +60,26 @@ def unpack(self, dest):


class DockerSource:
def __init__(self, url, username, password, insecure):
def __init__(self, url, username, password, insecure, no_cache):
self.registry = url.netloc
self.image = url.path
self.username = username
self.password = password
self.insecure = insecure
self.no_cache = no_cache
if self.image and not self.image.startswith('/'):
self.image = '/' + self.image
self.url = "docker://" + self.registry + self.image

def unpack(self, dest):
tmpDest = tempfile.mkdtemp('virt-bootstrap')

if self.no_cache:
tmp_dest = tempfile.mkdtemp('virt-bootstrap')
images_dir = tmp_dest
else:
if not os.path.exists(default_image_dir):
os.makedirs(default_image_dir)
images_dir = default_image_dir

try:
# Run skopeo copy into a tmp folder
Expand All @@ -71,7 +88,7 @@ def unpack(self, dest):
# folders for broader enablement
cmd = ["skopeo", "copy",
self.url,
"dir:%s" % tmpDest]
"dir:%s" % images_dir]
if self.insecure:
cmd.append('--src-tls-verify=false')
if self.username:
Expand All @@ -83,13 +100,13 @@ def unpack(self, dest):
subprocess.check_call(cmd)

# Get the layers list from the manifest
mf = open("%s/manifest.json" % tmpDest, "r")
mf = open("%s/manifest.json" % images_dir, "r")
manifest = json.load(mf)

# FIXME We suppose the layers are ordered, is this true?
for layer in manifest['layers']:
sum_type, sum_value = layer['digest'].split(':')
layer_file = "%s/%s.tar" % (tmpDest, sum_value)
layer_file = "%s/%s.tar" % (images_dir, sum_value)
print('layer_file: (%s) %s' % (sum_type, layer_file))

# Verify the checksum
Expand All @@ -100,7 +117,9 @@ def unpack(self, dest):
subprocess.check_call(["tar", "xf", layer_file, "-C", dest])

except Exception:
shutil.rmtree(tmpDest)
raise

shutil.rmtree(tmpDest)
finally:
# Clean up
if self.no_cache:
shutil.rmtree(tmp_dest)
8 changes: 7 additions & 1 deletion virt-bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def get_source(args):
try:
class_name = "%sSource" % scheme.capitalize()
clazz = getattr(sources, class_name)
return clazz(url, args.username, args.password, args.not_secure)
return clazz(url,
args.username,
args.password,
args.not_secure,
args.no_cache)
except Exception:
raise Exception("Invalid image URI scheme: '%s'" % url.scheme)

Expand Down Expand Up @@ -106,6 +110,8 @@ def main():
"to connect to the source registry"))
parser.add_argument("--root-password", default=None,
help=_("Root password to set in the created rootfs"))
parser.add_argument("--no-cache", action="store_true",
help=_("Do not store downloaded Docker images"))
# TODO add --format [qcow2,dir] parameter
# TODO add UID / GID mapping parameters

Expand Down

0 comments on commit 0b998e5

Please sign in to comment.