-
Notifications
You must be signed in to change notification settings - Fork 17
/
install-packer.sh
executable file
·65 lines (55 loc) · 2.22 KB
/
install-packer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
# install-packer: install packer for the current architecture,
# in the directory specified as 1st argument, in the version specified as 2nd argument
packer_install_dir="${1:?First argument missing: directory where to install packer.}"
packer_version="${2:?Second argument missing: version of packer to install.}"
set -eu -o pipefail
packer_cmd="packer"
temp_dir="$(mktemp -d)"
mkdir -p "${packer_install_dir}"
## check if packer exists or install it
echo "===================================="
## Check for presence of requirements or fail fast
for cli in curl unzip
do
if ! command -v $cli >/dev/null 2>&1
then
echo "ERROR: command line ${cli} required but not found. Exiting."
exit 1
fi
done
echo "= Installing Packer version ${packer_version} to ${packer_install_dir}"
if ! command -v ${packer_cmd} >/dev/null 2>&1
then
if test -x "${packer_install_dir}/${packer_cmd}"
then
packer_cmd="${packer_install_dir}/packer"
else
echo "Detecting CPU architecture..."
arch=$(uname -m)
if [[ $arch == x86_64* ]]; then
echo "X64 Architecture"
packer_download_url="https://releases.hashicorp.com/packer/${packer_version}/packer_${packer_version}_linux_amd64.zip"
elif [[ $arch == i*86 ]]; then
echo "X32 Architecture"
packer_download_url="https://releases.hashicorp.com/packer/${packer_version}/packer_${packer_version}_linux_386.zip"
elif [[ $arch == arm* ]]; then
echo "ARM Architecture 32b"
packer_download_url="https://releases.hashicorp.com/packer/${packer_version}/packer_${packer_version}_linux_arm.zip"
elif [[ $arch == aarch64 ]]; then
echo "ARM Architecture 64b"
packer_download_url="https://releases.hashicorp.com/packer/${packer_version}/packer_${packer_version}_linux_arm64.zip"
else
echo "ERROR: unknwon architecture (${arch}). Exiting."
exit 2
fi
zip_file="${temp_dir}/packer.zip"
curl -sSL -o "${zip_file}" "${packer_download_url}"
unzip "${zip_file}" -d "${packer_install_dir}"
packer_cmd="${packer_install_dir}/packer"
fi
fi
echo "= Packer installed, running sanity check (command '${packer_cmd} version')..."
"${packer_cmd}" version
echo "===================================="
exit 0