From 7e79a933dbe9d22dcfd9069742fcf66923d4a641 Mon Sep 17 00:00:00 2001
From: Michael Johnson <johnsomor@gmail.com>
Date: Tue, 17 Apr 2018 14:20:41 -0700
Subject: [PATCH] Add pip cache cleanup to pip-and-virtualenv

The pip-and-virtualenv element provides pip inside the created images.
When this pip is used inside the chroot of the image it, by default, creates
a cache directory with the http packages and the resulting wheels.
In the case of the octavia ubuntu-minimal image this cache is using ~50MB of
cache that is not needed after the image finished building.

This package creates a finalise.d task that removes the cache from the
default location.

Change-Id: I4715437b068d04993ef755bd1e27963db1d22417
---
 .../post-install.d/89-clean-up-pip-cache            | 13 +++++++++++++
 1 file changed, 13 insertions(+)
 create mode 100755 diskimage_builder/elements/pip-and-virtualenv/post-install.d/89-clean-up-pip-cache

diff --git a/diskimage_builder/elements/pip-and-virtualenv/post-install.d/89-clean-up-pip-cache b/diskimage_builder/elements/pip-and-virtualenv/post-install.d/89-clean-up-pip-cache
new file mode 100755
index 000000000..1e464a1d5
--- /dev/null
+++ b/diskimage_builder/elements/pip-and-virtualenv/post-install.d/89-clean-up-pip-cache
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
+    set -x
+fi
+set -eu
+set -o pipefail
+
+# TODO(johnsom) Fix this when https://github.com/pypa/pip/issues/4685 is
+#               available.
+if [ "${DIB_DISABLE_PIP_CLEANUP:-0}" != "1" ]; then
+    rm -rf ~/.cache/pip
+fi