Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / packer-install.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> packer-install.sh"
12 # The script checks for the packer binaries and installs the binary
13 # if its not available
14
15 # Ensure we fail the job if any steps fail.
16 set -eu -o pipefail
17
18 # $PACKER_VERSION        : Define a packer version passed as job paramter
19 PACKER_VERSION="${PACKER_VERSION:-1.2.3}"
20 export PATH="${WORKSPACE}/bin:$PATH"
21
22 packer_install() {
23     # Installs Hashicorp's Packer binary, required for verify & merge packer jobs
24     pushd "${WORKSPACE}"
25     wget -nv "https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip"
26     mkdir -p "${WORKSPACE}/bin"
27     unzip "packer_${PACKER_VERSION}_linux_amd64.zip" -d "${WORKSPACE}/bin/"
28     # rename packer to avoid conflict with binary in cracklib
29     mv "${WORKSPACE}/bin/packer" "${WORKSPACE}/bin/packer.io"
30     popd
31 }
32
33 # Functions to compare semantic versions x.y.z
34 version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
35 version_le() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" == "$1"; }
36 version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }
37 version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
38
39 if hash packer.io 2>/dev/null; then
40     CURRENT_VERSION="$(packer.io --version)"
41     if version_lt "$CURRENT_VERSION" "$PACKER_VERSION"; then
42         echo "Packer version $CURRENT_VERSION installed is less than $PACKER_VERSION available, updating Packer."
43         packer_install
44     else
45         echo "Packer version installed $CURRENT_VERSION is greater than or equal to"\
46         "the required minimum version $PACKER_VERSION."
47     fi
48 else
49     echo "Packer binary not available, installing Packer version $PACKER_VERSION."
50     packer_install
51 fi