From 6278371eaa13352249e385ab62aff99544e03659 Mon Sep 17 00:00:00 2001
From: Gregory Haynes <greg@greghaynes.net>
Date: Wed, 7 Dec 2016 12:26:43 -0800
Subject: [PATCH] Make dib-python use the default python for distro

Right now dib-python works by trying to find any python on a system in
an order of precedence. A much better way is if we are explicit about
the python we intend to be there which will allow us to make better
decisions in other elements (such as allowing for package-installs to
take into account DIB_PYTHON_VERSION) as well as allow for users to
specify a preferred python version.

Co-Authored-By: Adam Harwell <flux.adam@gmail.com>
Change-Id: Ie609de51cc5fcde701296c9474e315981d9778a2
---
 elements/debian-minimal/element-deps          |  1 +
 .../root.d/75-debian-minimal-baseinstall      | 11 ++++++++-
 elements/dib-python/README.rst                |  3 +++
 .../environment.d/50-dib-python-version       | 24 +++++++++++++++++++
 .../dib-python/pre-install.d/01-dib-python    |  4 ++--
 .../root.d/75-ubuntu-minimal-baseinstall      | 11 ++++++++-
 6 files changed, 50 insertions(+), 4 deletions(-)
 create mode 100644 elements/dib-python/environment.d/50-dib-python-version

diff --git a/elements/debian-minimal/element-deps b/elements/debian-minimal/element-deps
index 4867362bd..158afb109 100644
--- a/elements/debian-minimal/element-deps
+++ b/elements/debian-minimal/element-deps
@@ -1,2 +1,3 @@
 debootstrap
+dib-python
 pkg-map
diff --git a/elements/debian-minimal/root.d/75-debian-minimal-baseinstall b/elements/debian-minimal/root.d/75-debian-minimal-baseinstall
index f12774d15..7c967e699 100755
--- a/elements/debian-minimal/root.d/75-debian-minimal-baseinstall
+++ b/elements/debian-minimal/root.d/75-debian-minimal-baseinstall
@@ -54,4 +54,13 @@ $apt_get update
 $apt_get clean
 $apt_get dist-upgrade -y
 
-$apt_get install -y busybox python sudo
+$apt_get install -y busybox sudo
+
+if [ "$DIB_PYTHON_VERSION" == "2" ]; then
+    $apt_get install -y python
+elif [ "$DIB_PYTHON_VERSION" == "3" ]; then
+    $apt_get install -y python3
+else
+    echo "ERROR: DIB_PYTHON_VERSION is '$DIB_PYTHON_VERSION' but needs to be 2 or 3"
+    exit 1
+fi
diff --git a/elements/dib-python/README.rst b/elements/dib-python/README.rst
index 980f146de..974a85599 100644
--- a/elements/dib-python/README.rst
+++ b/elements/dib-python/README.rst
@@ -7,3 +7,6 @@ or python3 executable. This is useful for creating #! lines for scripts that
 are compatible with both python2 and python3.
 
 This does not install a python if one does not exist, and instead fails.
+
+This also exports a variable DIB_PYTHON_VERSION which will either be '2' or
+'3' depending on the python version which dib-python points to.
diff --git a/elements/dib-python/environment.d/50-dib-python-version b/elements/dib-python/environment.d/50-dib-python-version
new file mode 100644
index 000000000..82196b378
--- /dev/null
+++ b/elements/dib-python/environment.d/50-dib-python-version
@@ -0,0 +1,24 @@
+# Pick which distros we need to force python2
+if [ -z "${DIB_PYTHON_VERSION:-}" ]; then
+    if [ "$DISTRO_NAME" == "ubuntu" ]; then
+        if [ "$DIB_RELEASE" == "trusty" ]; then
+            DIB_PYTHON_VERSION=2
+        fi
+    elif [ "$DISTRO_NAME" == "debian" ]; then
+        DIB_PYTHON_VERSION=2
+    elif [ "$DISTRO_NAME" == "fedora" ]; then
+        if [ "$DIB_RELEASE" -le 22 ]; then
+            DIB_PYTHON_VERSION=2
+        fi
+    elif [ "$DISTRO_NAME" == "centos" ]; then
+        DIB_PYTHON_VERSION=2
+    elif [ "$DISTRO_NAME" == "centos7" ]; then
+        DIB_PYTHON_VERSION=2
+    fi
+fi
+
+if [ -z "${DIB_PYTHON_VERSION:-}" ]; then
+    DIB_PYTHON_VERSION=3
+fi
+
+export DIB_PYTHON_VERSION
diff --git a/elements/dib-python/pre-install.d/01-dib-python b/elements/dib-python/pre-install.d/01-dib-python
index 2b3f76383..bf4d1c81a 100755
--- a/elements/dib-python/pre-install.d/01-dib-python
+++ b/elements/dib-python/pre-install.d/01-dib-python
@@ -6,9 +6,9 @@ fi
 set -eu
 set -o pipefail
 
-python_path=$(command -v python2 || command -v python3)
+python_path=$(command -v python${DIB_PYTHON_VERSION})
 if [ -z "$python_path" ]; then
-    echo "Could not find python2 or python3 executable."
+    echo "Could not find python${DIB_PYTHON_VERSION} executable."
     exit 1
 fi
 
diff --git a/elements/ubuntu-minimal/root.d/75-ubuntu-minimal-baseinstall b/elements/ubuntu-minimal/root.d/75-ubuntu-minimal-baseinstall
index 6e6de8a0b..359389957 100755
--- a/elements/ubuntu-minimal/root.d/75-ubuntu-minimal-baseinstall
+++ b/elements/ubuntu-minimal/root.d/75-ubuntu-minimal-baseinstall
@@ -43,4 +43,13 @@ $apt_get update
 $apt_get clean
 $apt_get dist-upgrade -y
 
-$apt_get install -y busybox python sudo
+$apt_get install -y busybox sudo
+
+if [ "$DIB_PYTHON_VERSION" == "2" ]; then
+    $apt_get install -y python
+elif [ "$DIB_PYTHON_VERSION" == "3" ]; then
+    $apt_get install -y python3
+else
+    echo "ERROR: DIB_PYTHON_VERSION is '$DIB_PYTHON_VERSION' but needs to be 2 or 3"
+    exit 1
+fi