Skip to content

Commit

Permalink
update for 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpeteuil committed May 13, 2018
1 parent 7b5792f commit 7a14e01
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 32 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

## Automatically Download, Extract and Install Latest or Specific Version

[![](https://img.shields.io/github/release/robertpeteuil/terraform-installer.svg?colorB=2067b8)](https://github.com/robertpeteuil/terraform-installer)
[![](https://img.shields.io/badge/language-bash-89e051.svg?style=flat-square)](https://github.com/robertpeteuil/terraform-installer)
[![](https://img.shields.io/github/license/robertpeteuil/terraform-installer.svg?colorB=2067b8)](https://github.com/robertpeteuil/terraform-installer)
[![release](https://img.shields.io/github/release/robertpeteuil/terraform-installer.svg?colorB=2067b8)](https://github.com/robertpeteuil/terraform-installer)
[![bash](https://img.shields.io/badge/language-bash-89e051.svg?style=flat-square)](https://github.com/robertpeteuil/terraform-installer)
[![license](https://img.shields.io/github/license/robertpeteuil/terraform-installer.svg?colorB=2067b8)](https://github.com/robertpeteuil/terraform-installer)

---

Expand All @@ -20,13 +20,13 @@ Options:
- `-h`: help
- `-v`: display version

### Installation with this Installer
### Installation using this Installer

Download the installer and make executable
Download the installer with `curl` or `wget` and make executable

``` shell
wget https://raw.github.com/robertpeteuil/terraform-installer/master/terraform-install.sh
# or curl -LO https://raw.github.com/robertpeteuil/terraform-installer/master/terraform-install.sh
curl -LO https://raw.github.com/robertpeteuil/terraform-installer/master/terraform-install.sh
# OR wget https://raw.github.com/robertpeteuil/terraform-installer/master/terraform-install.sh
chmod +x terraform-install.sh
```

Expand All @@ -36,7 +36,7 @@ Run the installer
./terraform-install.sh
```

Optional Parameters
Examples with Optional Parameters

``` shell
# -i = Install specific version
Expand All @@ -46,25 +46,25 @@ Optional Parameters
./terraform-install.sh -a
```

### Terraform's Official Installation Process

- visit website download page
- locate version for OS/CPU and download
- find and extract binary from downloaded zip file
- copy binary to a directory on the PATH

### System Requirements

- System with Bash Shell (Linux, macOS, Windows Subsystem for Linux)
- `unzip` - terraform downloads are in zip format
- `wget` - used to retrieve metadata and download package
- `curl` or `wget` - script will use either one to retrieve metadata and download

Optional

- `jq` - if installed, latest version parsed from hashicorp downloads
- Useful if latest github release differs from version on hashicorp downloads
- Avoids github api limit of 60 requests per hour (unauthenticated)

### Script Process Details

- Determines Version to Download and Install
- Uses Version specified by `-i VERSION` parameter (if specified)
- Otherwise determines Latest Version
- Uses GitHub API to retrieve latest version number
- If `jq` installed parse version from hashicorp downloads
- Otherwise use GitHub API to retrieve latest version
- Calculates Download URL based on Version, OS and CPU-Architecture
- Verifies URL Validity before Downloading in Case:
- VERSION incorrectly specified with `-i`
Expand Down
67 changes: 52 additions & 15 deletions terraform-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,47 @@
# sudoInstall=true

scriptname=$(basename "$0")
scriptbuildnum="1.2.1"
scriptbuilddate="2018-04-25"
scriptbuildnum="1.3.0"
scriptbuilddate="2018-05-13"


# CHECK DEPENDANCIES AND SET NET RETRIEVAL TOOL
if ! unzip -h 2&> /dev/null; then
echo "aborting - unzip not installed and required"
exit 1
fi

if curl -h 2&> /dev/null; then
nettool="curl"
elif wget -h 2&> /dev/null; then
nettool="wget"
else
echo "aborting - wget or curl not installed and required"
exit 1
fi

if jq --help 2&> /dev/null; then
nettool="${nettool}jq"
fi

# USE NET RETRIEVAL TOOL TO GET LATEST VERSION
case "${nettool}" in
# jq installed - parse version from hashicorp website
wgetjq)
LATEST=$(wget -q -O- https://releases.hashicorp.com/index.json 2>/dev/null | jq -r '.terraform.versions[].version' | sort --version-sort -r | head -n 1)
;;
curljq)
LATEST=$(curl -s https://releases.hashicorp.com/index.json 2>/dev/null | jq -r '.terraform.versions[].version' | sort --version-sort -r | head -n 1)
;;
# parse version from github API
wget)
LATEST=$(wget -q -O- https://api.github.com/repos/hashicorp/terraform/releases/latest 2> /dev/null | awk '/tag_name/ {print $2}' | cut -d '"' -f 2 | cut -d 'v' -f 2)
;;
curl)
LATEST=$(curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest 2> /dev/null | awk '/tag_name/ {print $2}' | cut -d '"' -f 2 | cut -d 'v' -f 2)
;;
esac

LATEST=$(wget -q -O- https://api.github.com/repos/hashicorp/terraform/releases/latest 2> /dev/null | awk '/tag_name/ {print $2}' | cut -d '"' -f 2 | cut -d 'v' -f 2)

displayVer() {
echo -e "${scriptname} ver ${scriptbuildnum} - ${scriptbuilddate}"
Expand All @@ -30,16 +67,6 @@ usage() {
echo -e " -v\t\t: display ${scriptname} version"
}

if ! unzip -h 2&> /dev/null; then
echo "aborting - unzip not installed and required for installation"
exit 1
fi

if ! wget -h 2&> /dev/null; then
echo "aborting - wget not installed and required for installation"
exit 1
fi

while getopts ":i:ahv" arg; do
case "${arg}" in
a) sudoInstall=true;;
Expand Down Expand Up @@ -70,7 +97,12 @@ fi
# CREATE FILENAME AND DOWNLOAD LINK BASED ON GATHERED PARAMETERS
FILENAME="terraform_${VERSION}_${OS}_${PROC}.zip"
LINK="https://releases.hashicorp.com/terraform/${VERSION}/${FILENAME}"
LINKVALID=$(wget --spider -S "$LINK" 2>&1 | grep "HTTP/" | awk '{print $2}')
case "${nettool}" in
wget*)
LINKVALID=$(wget --spider -S "$LINK" 2>&1 | grep "HTTP/" | awk '{print $2}') ;;
curl*)
LINKVALID=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' "$LINK") ;;
esac

# VERIFY LINK VALIDITY
if [[ "$LINKVALID" != 200 ]]; then
Expand Down Expand Up @@ -118,7 +150,12 @@ mkdir -p "$UTILTMPDIR"
cd "$UTILTMPDIR" || exit 1

# DOWNLOAD AND EXTRACT
wget -q "$LINK" -O "$FILENAME"
case "${nettool}" in
wget*)
wget -q "$LINK" -O "$FILENAME" ;;
curl*)
curl -s -o "$FILENAME" "$LINK" ;;
esac
unzip -qq "$FILENAME" || exit 1

# COPY TO DESTINATION
Expand Down

0 comments on commit 7a14e01

Please sign in to comment.