Advertisement
Red_Fisher

tfswitch

Jan 12th, 2021 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.99 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##
  4. ## VARS
  5. ##
  6.  
  7. # USER-DEFINED
  8. SYSTEM="linux"          # linux/freebsd/solaris/openbsd
  9. SYSTEM_ARCH="amd64"     # 386/amd64/arm
  10. DOWNLOAD_PATH="/home/$(whoami)/bin/terraform_archive"   # where to store ZIP archives
  11. INSTALL_PATH="/home/$(whoami)/bin"                      # where to put executables (should be in PATH variable)
  12.  
  13. # GREPPED (DO NOT CHANGE)
  14. TF_VERSION_LONG=$(grep required_version ./* 2>/dev/null | sort -r | awk -F'"' '{ print $2 }' | head -n 1)           # TF version + constraint
  15. TF_FILE=$(grep required_version ./* 2>/dev/null | grep "$TF_VERSION_LONG" | awk -F':' '{ print $1 }' | head -n 1)   # file where version was found
  16. TF_VERSION=$(echo $TF_VERSION_LONG | awk -F' ' '{ print $NF }')                             # TF version
  17. TF_VERSION_CONSTRAINT=$(echo $TF_VERSION_LONG | awk -F' ' '{ print $1 }')                   # version constraint
  18.  
  19. ##
  20. ##
  21. ##
  22.  
  23. if [ "$TF_VERSION" == "" ]; then
  24.     echo "No 'required_version' specified - Nothing to do."
  25. else
  26.     if [ "$(echo '~>>=' | grep -w $TF_VERSION_CONSTRAINT)" != "" ] && \
  27.             [ "$(echo $TF_VERSION_CONSTRAINT | wc -c)" == "3" ]; then
  28.             TF_VERSION=$(curl https://releases.hashicorp.com/terraform/ | grep "$(echo $TF_VERSION | \
  29.                    awk -F'.' '{ print $1 }').$(echo $TF_VERSION | awk -F'.' '{ print $2 }')".* | \
  30.                     awk -F'_' '{ print $NF }' | awk -F'<' '{ print $1 }' | sort | grep -v - | tail -n1)
  31.     fi
  32.     echo -e "\nTF_VERSION:\tRequested:\t$TF_VERSION_LONG ($TF_FILE)"
  33.     echo -e "\t\tDownloading:\t$TF_VERSION\n"
  34.     mkdir -pv $DOWNLOAD_PATH
  35.     if [ ! -f "$DOWNLOAD_PATH"/terraform_"$TF_VERSION"_"$SYSTEM"_"$SYSTEM_ARCH".zip ]; then
  36.         cd $DOWNLOAD_PATH
  37.         wget https://releases.hashicorp.com/terraform/"$TF_VERSION"/terraform_"$TF_VERSION"_"$SYSTEM"_"$SYSTEM_ARCH".zip
  38.     fi
  39.     mkdir -pv $INSTALL_PATH
  40.     cd $INSTALL_PATH
  41.     rm terraform
  42.     unzip "$DOWNLOAD_PATH"/terraform_"$TF_VERSION"_"$SYSTEM"_"$SYSTEM_ARCH".zip
  43.     chmod +x terraform
  44. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement