From 81a5e9db553008ac94b279e7a5ed5e186813e40a Mon Sep 17 00:00:00 2001 From: Anil Belur Date: Mon, 26 Mar 2018 15:03:25 -0700 Subject: [PATCH] Validate packer version before install Packer 1.1.3 is the minimum version required to be installed to build images on vexxhost. The install script does not check the version and only checks the availability of the binary in the $PATH before updating packer. JIRA: RELENG-838 Change-Id: I8c343176d8f39e3e70b04dddbdec94ffed4354b1 Signed-off-by: Anil Belur --- shell/packer-install.sh | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/shell/packer-install.sh b/shell/packer-install.sh index 8b3ef6d5..90780cbf 100644 --- a/shell/packer-install.sh +++ b/shell/packer-install.sh @@ -12,18 +12,14 @@ echo "---> packer-install.sh" # The script checks for the packer binaries and installs the binary # if its not available -# $PACKER_VERSION : Define a packer version passed as job paramter +# Ensure we fail the job if any steps fail. +set -eu -o pipefail +# $PACKER_VERSION : Define a packer version passed as job paramter PACKER_VERSION="${PACKER_VERSION:-1.1.3}" export PATH="${WORKSPACE}/bin:$PATH" -# Ensure we fail the job if any steps fail. -set -eu -o pipefail - -if hash packer.io 2>/dev/null; then - echo "packer.io command is available." -else - echo "packer.io command not is available. Installing packer ..." +packer_install() { # Installs Hashicorp's Packer binary, required for verify & merge packer jobs pushd "${WORKSPACE}" wget -nv "https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip" @@ -32,4 +28,23 @@ else # rename packer to avoid conflict with binary in cracklib mv "${WORKSPACE}/bin/packer" "${WORKSPACE}/bin/packer.io" popd +} + +# Functions to compare semantic versions x.y.z +version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; } +version_le() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" == "$1"; } +version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; } +version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; } + +if hash packer.io 2>/dev/null; then + CURRENT_VERSION="$(packer.io --version)" + if version_lt $CURRENT_VERSION $PACKER_VERSION; then + echo "Packer version $CURRENT_VERSION installed is less than $PACKER_VERSION available, updating Packer." + packer_install + else + echo "Packer version installed $CURRENT_VERSION is greater than or equal to the required minimum version $PACKER_VERSION." + fi +else + echo "Packer binary not available, installing Packer version $PACKER_VERSION." + packer_install fi -- 2.16.6