Created
April 24, 2018 21:02
-
-
Save tlesh989/d3e2fcd60efdfb76863a896c07464976 to your computer and use it in GitHub Desktop.
Update Hashicorp products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# array listing requested hashicorp products all lowercase | |
HASHICORP=(packer terraform vagrant) | |
DOWNLOAD_URL="https://releases.hashicorp.com" | |
TMP_DIR="/tmp" | |
# install products to this directory and create symbolic links to USER_DIR | |
INSTALL_DIR="/opt/hashicorp" | |
USER_DIR="/usr/bin" | |
FILE_EXT="linux_amd64.zip" | |
# vagrant is packaged differently, currently this script only works with dpkg package manager | |
OS_VAGRANT_EXT="x86_64.deb" | |
# compares version numbers from strings | |
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } | |
for PRODUCT in "${HASHICORP[@]}"; do | |
# check Hashicorp's JSON file for info | |
HASHI_VER="$(curl -s https://checkpoint-api.hashicorp.com/v1/check/$PRODUCT | jq -r -M '.current_version' | egrep -o '[0-9].*')" | |
if [ -f $USER_DIR/$PRODUCT ]; then | |
LOCAL_VER="$($PRODUCT version | head -n 1 | egrep -o '[0-9].*')" | |
else | |
LOCAL_VER="0" | |
fi | |
# create the product directory | |
mkdir -p $INSTALL_DIR | |
if version_gt $HASHI_VER $LOCAL_VER; then | |
echo "time to upgrade: $PRODUCT - local ver: $LOCAL_VER - remote ver: $HASHI_VER" | |
# vagrant installs differently | |
if [ $PRODUCT = "vagrant" ]; then | |
FILENAME=${PRODUCT}_${HASHI_VER}_${OS_VAGRANT_EXT} | |
wget ${DOWNLOAD_URL}/${PRODUCT}/${HASHI_VER}/${FILENAME} -O ${TMP_DIR}/${FILENAME} | |
dpkg -i ${TMP_DIR}/${FILENAME} | |
vagrant plugin repair | |
else # other Hashicorp products are in zip files | |
FILENAME=${PRODUCT}_${HASHI_VER}_${FILE_EXT} | |
wget ${DOWNLOAD_URL}/${PRODUCT}/${HASHI_VER}/${FILENAME} -O ${TMP_DIR}/${FILENAME} | |
unzip -o $TMP_DIR/$FILENAME -d $TMP_DIR | |
rm -f $USER_DIR/$PRODUCT | |
mv $TMP_DIR/$PRODUCT $INSTALL_DIR | |
ln -s $INSTALL_DIR/$PRODUCT $USER_DIR/$PRODUCT | |
fi | |
rm -f $TMP_DIR/$FILENAME | |
else | |
echo "$PRODUCT is up-to-date" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment