From ad67aa213b62ed4c0c6a9b665b3fa691ffb7b549 Mon Sep 17 00:00:00 2001
From: Colleen Murphy <colleen.murphy@suse.de>
Date: Mon, 2 Dec 2019 16:25:53 -0800
Subject: [PATCH] Allow zypper repos to be overrideable

Without this change, operating system elements that use the
zypper-minimal element always must use download.opensuse.org as their
repository source. This change makes ZYPPER_REPOS overrideable, which
allows the user to create custom operating system elements that can use
private repositories as their source for base packages. For example,
with only this change, it is possible to create a sles-minimal element
that generates a SLE 15 SP1 image just by overriding DIB_ZYPPER_REPOS
and DIB_OPENSUSE_PATTERNS.

Change-Id: I46e40fbe4408d4204056a27b182b21213f1176ff
---
 .../elements/zypper-minimal/README.rst        |  7 ++++
 .../zypper-minimal/root.d/08-zypper-chroot    | 42 ++++++++++---------
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/diskimage_builder/elements/zypper-minimal/README.rst b/diskimage_builder/elements/zypper-minimal/README.rst
index eb4335225..914aa5d51 100644
--- a/diskimage_builder/elements/zypper-minimal/README.rst
+++ b/diskimage_builder/elements/zypper-minimal/README.rst
@@ -6,3 +6,10 @@ Base element for creating minimal SUSE-based images
 This element is incomplete by itself so you probably want to use it along
 with the opensuse-minimal one. It requires 'zypper' to be installed on the
 host.
+
+To create a zypper-based image with non-default repositories, set
+``DIB_ZYPPER_REPOS`` to a mapping of repository names to URLs, for example::
+
+  DIB_ZYPPER_REPOS="update=>http://smt-mirror.example.com/SUSE:/SLE-15-SP1:/Update/standard/ "
+  DIB_ZYPPER_REPOS+="SLE-15-SP1=>http://smt-mirror.example.com/ibs/SUSE:/SLE-15-SP1:/GA/standard/ "
+  DIB_ZYPPER_REPOS+="SLE-15=>http://smt-mirror.example.com/ibs/SUSE:/SLE-15:/GA/standard/ "
diff --git a/diskimage_builder/elements/zypper-minimal/root.d/08-zypper-chroot b/diskimage_builder/elements/zypper-minimal/root.d/08-zypper-chroot
index b77802ad6..c5fc48fc5 100755
--- a/diskimage_builder/elements/zypper-minimal/root.d/08-zypper-chroot
+++ b/diskimage_builder/elements/zypper-minimal/root.d/08-zypper-chroot
@@ -41,25 +41,29 @@ ZYPPER_INSTALL_SYSTEM_PYTHON="python3"
 
 DIB_DISTRIBUTION_MIRROR=${DIB_DISTRIBUTION_MIRROR:-https://download.opensuse.org}
 
-case ${DIB_RELEASE} in
-    # Old Leap releases
-    42*)
-        ZYPPER_REPOS="update=>${DIB_DISTRIBUTION_MIRROR}/update/leap/${DIB_RELEASE}/oss/ "
-        ZYPPER_REPOS+="oss=>${DIB_DISTRIBUTION_MIRROR}/distribution/leap/${DIB_RELEASE}/repo/oss/"
-        ZYPPER_INSTALL_SYSTEM_PYTHON="python"
-        ;;
-    # New Leap releases
-    15*)
-        ZYPPER_REPOS="update=>${DIB_DISTRIBUTION_MIRROR}/update/leap/${DIB_RELEASE}/oss/ "
-        ZYPPER_REPOS+="oss=>${DIB_DISTRIBUTION_MIRROR}/distribution/leap/${DIB_RELEASE}/repo/oss/"
-        ;;
-    # Tumbleweed
-    tumbleweed)
-        ZYPPER_REPOS="update=>${DIB_DISTRIBUTION_MIRROR}/update/${DIB_RELEASE}/ "
-        ZYPPER_REPOS+="oss=>${DIB_DISTRIBUTION_MIRROR}/${DIB_RELEASE}/repo/oss/"
-        ;;
-    *) echo "Unsupported openSUSE release: ${DIB_RELEASE}"; exit 1 ;;
-esac
+ZYPPER_REPOS=${DIB_ZYPPER_REPOS:-''}
+
+if [ -z "${ZYPPER_REPOS}" ] ; then
+    case ${DIB_RELEASE} in
+        # Old Leap releases
+        42*)
+            ZYPPER_REPOS="update=>${DIB_DISTRIBUTION_MIRROR}/update/leap/${DIB_RELEASE}/oss/ "
+            ZYPPER_REPOS+="oss=>${DIB_DISTRIBUTION_MIRROR}/distribution/leap/${DIB_RELEASE}/repo/oss/"
+            ZYPPER_INSTALL_SYSTEM_PYTHON="python"
+            ;;
+        # New Leap releases
+        15*)
+            ZYPPER_REPOS="update=>${DIB_DISTRIBUTION_MIRROR}/update/leap/${DIB_RELEASE}/oss/ "
+            ZYPPER_REPOS+="oss=>${DIB_DISTRIBUTION_MIRROR}/distribution/leap/${DIB_RELEASE}/repo/oss/"
+            ;;
+        # Tumbleweed
+        tumbleweed)
+            ZYPPER_REPOS="update=>${DIB_DISTRIBUTION_MIRROR}/update/${DIB_RELEASE}/ "
+            ZYPPER_REPOS+="oss=>${DIB_DISTRIBUTION_MIRROR}/${DIB_RELEASE}/repo/oss/"
+            ;;
+        *) echo "Unsupported openSUSE release: ${DIB_RELEASE}"; exit 1 ;;
+    esac
+fi
 
 for repo in ${ZYPPER_REPOS}; do
     refresh_repo=""