Skip to content

Instantly share code, notes, and snippets.

@blalor
Last active January 7, 2020 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blalor/f7928805e10be98cf9bfb1a04b35022d to your computer and use it in GitHub Desktop.
Save blalor/f7928805e10be98cf9bfb1a04b35022d to your computer and use it in GitHub Desktop.
download HashiCorp products (macOS)

dl.sh

Shell script for downloading HashiCorp binaries.

Requires jq (brew install jq) and gpg (brew cask install gpg-suite).

example

I structure all my binaries as ~/devel/HashiCorp/binaries/<product>-<version>/<product> and adjust paths as needed based on the project I'm working on.

Version to download is optional; defaults to latest.

[:~] $ ~/devel/HashiCorp/binaries/dl.sh consul
current version: 1.6.2
retrieving consul 1.6.2
:: binary
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 38.3M  100 38.3M    0     0  11.6M      0  0:00:03  0:00:03 --:--:-- 11.6M
:: SHA256SUMS
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1148  100  1148    0     0  14316      0 --:--:-- --:--:-- --:--:-- 14350
:: SHA256SUMS signature
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   310  100   310    0     0   3734      0 --:--:-- --:--:-- --:--:--  3780
verifying GPG signature
gpg: Signature made Wed Nov 13 16:32:50 2019 EST
gpg:                using RSA key 91A6E7F85D05C65630BEF18951852D87348FFC4C
gpg: Good signature from "HashiCorp Security <security@hashicorp.com>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 91A6 E7F8 5D05 C656 30BE  F189 5185 2D87 348F FC4C
consul_1.6.2_darwin_amd64.zip: OK
Archive:  consul_1.6.2_darwin_amd64.zip
  inflating: consul
=> /Users/blalor/devel/HashiCorp/binaries/consul-1.6.0/consul
#!/bin/bash
set -e -u -o pipefail
basedir=$( cd "$( dirname "${0}" )" && /bin/pwd -P )
if [ $# -lt 1 ]; then
echo "usage: ${0} <product> [<version>]"
exit 1
fi
product="${1}"
version="${2:-latest}"
tmpdir="$( mktemp -d )"
function cleanup() {
EXIT=$?
cd /
rm -rf "${tmpdir}"
exit ${EXIT}
}
trap cleanup EXIT
cd "${tmpdir}"
curl=( curl --retry 3 --retry-delay 5 -fSLO )
if [ "${version}" = "latest" ]; then
if ! version=$( curl -sfSL "https://checkpoint-api.hashicorp.com/v1/check/${product}" | jq -r .current_version ); then
## filter out semver whose point version is not a number
version="$(
curl -sfSL "https://releases.hashicorp.com/${product}/index.json" | \
jq -r '
[
.versions |
keys |
.[] |
split(".") |
[
.[] | try tonumber catch null
] |
select(.[2])
] |
sort |
.[-1] |
[ .[] | tostring ] |
join(".")
'
)"
fi
echo "current version: ${version}"
fi
bindir="${basedir}/${product}-${version}"
if [ -d "${bindir}" ]; then
echo "${product} ${version} already exists"
exit 1
fi
mkdir -p "${bindir}"
echo "retrieving ${product} ${version}"
basefn="${product}_${version}"
_U="https://releases.hashicorp.com/${product}/${version}/${basefn}"
echo ":: binary"
"${curl[@]}" "${_U}_darwin_amd64.zip"
echo ":: SHA256SUMS"
"${curl[@]}" "${_U}_SHA256SUMS"
echo ":: SHA256SUMS signature"
"${curl[@]}" "${_U}_SHA256SUMS.sig"
# if ! gpg --list-keys 91A6E7F85D05C65630BEF18951852D87348FFC4C >/dev/null; then
echo "verifying GPG signature"
gpg --verify "${basefn}_SHA256SUMS.sig" "${basefn}_SHA256SUMS"
grep -E '_darwin_amd64\.zip$' "${basefn}_SHA256SUMS" | shasum -c
unzip "${basefn}_darwin_amd64.zip"
mv "${product}" "${bindir}"
echo "=> ${bindir}/${product}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment