From 0702ce98430ed09e4e73d1b7661bdcb039c20ca0 Mon Sep 17 00:00:00 2001
From: James Slagle <jslagle@redhat.com>
Date: Wed, 14 Aug 2013 14:12:15 -0400
Subject: [PATCH] Add a pip-cache element.

The element bind mounts a pip cache inside the image build chroot so
that pip downloads can be reused across image builds.  While similar
in purpose to the PyPi element that sets up a mirror, this element
just allows for a reusable download cache and doesn't require anything
to be setup beforehand.

The pip-cache element is not concurrency safe, and that is indicated
in the README for the element.  An upstream bug was file as well:
https://github.com/pypa/pip/issues/1141

Change-Id: Ibd1d4ea17c24923ed939357ada95b781e3179cfd
---
 elements/pip-cache/README.md                  | 18 ++++++++++++++++++
 elements/pip-cache/environment.d/10-pip-cache |  1 +
 elements/pip-cache/root.d/01-pip-cache        |  9 +++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 elements/pip-cache/README.md
 create mode 100755 elements/pip-cache/environment.d/10-pip-cache
 create mode 100755 elements/pip-cache/root.d/01-pip-cache

diff --git a/elements/pip-cache/README.md b/elements/pip-cache/README.md
new file mode 100644
index 000000000..86f1d9aa6
--- /dev/null
+++ b/elements/pip-cache/README.md
@@ -0,0 +1,18 @@
+# Use a cache for pip
+
+Using a download cache speeds up image builds.
+
+Including this element in an image build causes
+$HOME/.cache/image-create/pip to be bind mounted as /tmp/pip inside
+the image build chroot.  The $PIP_DOWNLOAD_CACHE environment variable
+is then defined as /tmp/pip, which causes pip to cache all downloads
+to the defined location.
+
+Note that pip and its use of $PIP_DOWNLOAD_CACHE is not concurrency
+safe.  Running multiple instances of diskimage-builder concurrently
+can cause issues.  Therefore, it is advised to only have one instance
+of diskimage-builder that includes the pip-cache element running at a
+time.
+
+The pip concurrency issue is being tracked upstream at
+https://github.com/pypa/pip/issues/1141
diff --git a/elements/pip-cache/environment.d/10-pip-cache b/elements/pip-cache/environment.d/10-pip-cache
new file mode 100755
index 000000000..1873a9c66
--- /dev/null
+++ b/elements/pip-cache/environment.d/10-pip-cache
@@ -0,0 +1 @@
+export PIP_DOWNLOAD_CACHE=/tmp/pip
diff --git a/elements/pip-cache/root.d/01-pip-cache b/elements/pip-cache/root.d/01-pip-cache
new file mode 100755
index 000000000..aecbdc27e
--- /dev/null
+++ b/elements/pip-cache/root.d/01-pip-cache
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -e
+
+PIP_CACHE_DIR=$HOME/.cache/image-create/pip
+mkdir -p $PIP_CACHE_DIR
+
+sudo mkdir -p $TMP_MOUNT_PATH/tmp/pip
+sudo mount --bind $PIP_CACHE_DIR $TMP_MOUNT_PATH/tmp/pip