Skip to content

Instantly share code, notes, and snippets.

@aaron-prindle
Created November 15, 2017 00:24
Show Gist options
  • Save aaron-prindle/254600593d76ee4a9f086b731dc76eca to your computer and use it in GitHub Desktop.
Save aaron-prindle/254600593d76ee4a9f086b731dc76eca to your computer and use it in GitHub Desktop.
def GetCacheKey(self, descriptor_files):
descriptor = None
for f in descriptor_files:
if self.ctx.Contains(f):
descriptor = f
descriptor_contents = self.ctx.GetFile(descriptor)
break
if not descriptor:
logging.info('No package descriptor found. No packages installed.')
return None
return hashlib.sha256(descriptor_contents).hexdigest()
def GetCachedDepsImage(self, checksum):
# def GetCachedImageIfExists(self, base_image, cache, use_cache=True):
if not checksum:
# TODO(aaron-prindle) verify this makes sense to use None
# as sentinel for no descriptor and to handle this here
return base_image
hit = cache.Get(base_image, _PYTHON_NAMESPACE, checksum)
if hit:
logging.info('Found cached dependency layer for %s' % checksum)
last_created = _timestamp_to_time(_creation_time(hit))
now = datetime.datetime.now()
if last_created > now - datetime.timedelta(
seconds=_DEFAULT_TTL_WEEKS):
return hit
else:
logging.info('TTL expired for cached image, rebuilding %s'
% checksum)
else:
logging.info('No cached dependency layer for %s' % checksum)
return None
def GenerateFTLImage(self):
with docker_image.FromRegistry(self.base_name, self.base_creds,
self.transport) as base_image:
# Create (or pull from cache) the base image with the
# package descriptor installation overlaid.
logging.info('Generating dependency layer...')
descriptor_files = ""
checksum = self.GetCacheKey(descriptor_files)
deps_image = self.GetCachedDepsImage(checksum)
if not deps_image:
deps = self.bldr.CreatePackageBase(base_image, self.cash,
self.args.cache)
# Construct the application layer from the context.
logging.info('Generating app layer...')
app_layer, diff_id = self.bldr.BuildAppLayer()
with append.Layer(
deps, app_layer, diff_id=diff_id) as app_image:
if self.args.output_path:
with tarfile.open(
name=self.args.output_path, mode='w') as tar:
save.tarball(self.target_image, app_image, tar)
logging.info("{0} tarball located at {1}".format(
str(self.target_image), self.args.output_path))
return
with docker_session.Push(
self.target_image,
self.target_creds,
self.transport,
threads=_THREADS,
mount=[self.base_name]) as session:
logging.info('Pushing final image...')
session.upload(app_image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment