From 49a9fba23ee5440fb3bc2bfadc0714a311f981e1 Mon Sep 17 00:00:00 2001
From: Sam Yaple <sam@yaple.net>
Date: Thu, 20 Aug 2015 14:24:58 +0000
Subject: [PATCH] Allow Docker to make better use of cache

Docker cache takes into account things like filesize, hash of file,
and even timestamp and access times when determining whether to use
the cache or not. This modifies the build script to adjust all the
timestamps to 0 epoch. Since we dont care about timestamps this allows
us to make far better use of the cache, including when downloading and
creating tarballs for source builds.

Change-Id: Id4287cf1fcaa2de63feaab9e6f4ebbd4defdb768
Partially-Implements: blueprint build-script
---
 tools/build.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/tools/build.py b/tools/build.py
index 89cffc061c..63477a1485 100755
--- a/tools/build.py
+++ b/tools/build.py
@@ -80,6 +80,9 @@ class WorkerThread(Thread):
                     'Failed to download tarball: status_code {}'.format(
                         r.status_code))
 
+            # Set time on destination tarball to epoch 0
+            os.utime(os.path.join(dest_dir, source['dest']), (0, 0))
+
     def builder(self, image):
         LOG.info('Processing: {}'.format(image['name']))
         image['status'] = "building"
@@ -204,6 +207,14 @@ class KollaWorker(object):
             shutil.copytree(self.images_dir, self.working_dir)
         LOG.debug('Created working dir: {}'.format(self.working_dir))
 
+    def set_time(self):
+        for root, dirs, files in os.walk(self.working_dir):
+            for file_ in files:
+                os.utime(os.path.join(root, file_), (0, 0))
+            for dir_ in dirs:
+                os.utime(os.path.join(root, dir_), (0, 0))
+        LOG.debug('Set atime and mtime to 0 for all content in working dir')
+
     def createDockerfiles(self):
         for path in self.docker_build_paths:
             template_name = "Dockerfile.j2"
@@ -390,6 +401,10 @@ def main():
     if args['template']:
         kolla.createDockerfiles()
 
+    # We set the atime and mtime to 0 epoch to preserve allow the Docker cache
+    # to work like we want. A different size or hash will still force a rebuild
+    kolla.set_time()
+
     pools = kolla.buildQueues()
 
     # Returns a list of Queues for us to loop through