Skip to content

Instantly share code, notes, and snippets.

@mattwillsher
Last active August 4, 2019 17:21
Show Gist options
  • Save mattwillsher/dcfbabfd2aa1309920648ee65b4237a8 to your computer and use it in GitHub Desktop.
Save mattwillsher/dcfbabfd2aa1309920648ee65b4237a8 to your computer and use it in GitHub Desktop.
Script to install any of the Hashicorp tools to a given path, including GPG verification of download integrity. Write on Ubuntu, test on desktop and cloud image
#!/bin/bash
# A simple script to install Hashicorp tools with GPG key verification
set -euo pipefail
# Set DEST_PATH if not already set
HASHI_DEST_PATH="${HASHI_DEST_PATH:-/usr/local/bin}"
# Clean up downloaded files
HASHI_CLEAN="${HASHI_CLEAN:-true}"
# Install the Hashicorp Security GPG key.
# Verify key data at https://www.hashicorp.com/security.html
function hashiseckey {
gpg -kv 91A6E7F85D05C65630BEF18951852D87348FFC4C && return
cat <<EOF | gpg --import
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mQENBFMORM0BCADBRyKO1MhCirazOSVwcfTr1xUxjPvfxD3hjUwHtjsOy/bT6p9f
W2mRPfwnq2JB5As+paL3UGDsSRDnK9KAxQb0NNF4+eVhr/EJ18s3wwXXDMjpIifq
fIm2WyH3G+aRLTLPIpscUNKDyxFOUbsmgXAmJ46Re1fn8uKxKRHbfa39aeuEYWFA
3drdL1WoUngvED7f+RnKBK2G6ZEpO+LDovQk19xGjiMTtPJrjMjZJ3QXqPvx5wca
KSZLr4lMTuoTI/ZXyZy5bD4tShiZz6KcyX27cD70q2iRcEZ0poLKHyEIDAi3TM5k
SwbbWBFd5RNPOR0qzrb/0p9ksKK48IIfH2FvABEBAAG0K0hhc2hpQ29ycCBTZWN1
cml0eSA8c2VjdXJpdHlAaGFzaGljb3JwLmNvbT6JATgEEwECACIFAlMORM0CGwMG
CwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEFGFLYc0j/xMyWIIAIPhcVqiQ59n
Jc07gjUX0SWBJAxEG1lKxfzS4Xp+57h2xxTpdotGQ1fZwsihaIq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EOF
}
# Install the given hashitool
function hashiinstall {
local tool="$1"
local version="$2"
local arch="${3:-amd64}"
local archive="${tool}_${version}_linux_${arch}.zip"
local shasums="${tool}_${version}_SHA256SUMS"
local shasig="${tool}_${version}_SHA256SUMS.sig"
local url="https://releases.hashicorp.com/${tool}/${version}"
local toolpath="$HASHI_DEST_PATH/$tool"
for file in "$archive" "$shasums" "$shasig"; do
test -f "$file" || wget "$url/$file"
done
# If $tool is already in $HASHI_DEST_PATH, check if its version matches
# $version
if [ -x "$toolpath" ]; then
cversion=$( "$toolpath" --version | grep -E '[0-9]+\.[0-9]+\.[0-9]+' )
if [ "$version" == "$cversion" ]; then
echo "$tool $version already installed"
return
fi
fi
# Check SHASUM file signature against the one above
gpg --verify "$shasig"
# Find the downloaded file in the SHASUM file and check against the
# downloaded tool
grep "$archive" "$shasums" | shasum -c -
# Extract, prep and copy the tool binary into place
test -f "$tool" || unzip "$archive"
sudo chown root:root "$tool"
test -d "$HASHI_DEST_PATH" || mkdir -p "$HASHI_DEST_PATH"
sudo mv "$tool" "$toolpath"
# Clean up
if [ "$HASHI_CLEAN" = true ]; then
rm "$archive" "$shasums" "$shasig"
fi
}
command -v unzip || sudo apt install -y unzip
hashiseckey
hashiinstall consul 1.5.3
hashiinstall nomad 0.9.4
hashiinstall vault 1.2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment