-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_terraform.sh
executable file
·62 lines (49 loc) · 1.29 KB
/
install_terraform.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
# This script only works for linux_amd64 for now.
set -e
source functions.sh
install_command curl unzip
CDN=https://releases.hashicorp.com/terraform
#https://releases.hashicorp.com/terraform/1.3.7/terraform_1.3.7_linux_amd64.zip
if [ "$#" -eq 0 ]; then
VERSION=$(curl -s "$CDN/" \
| grep -v "alpha" | grep -v "beta" | grep -v "rc" \
| grep -o "terraform_[0-9].[0-9].[0-9]" \
| sort -ru \
| grep -o "[0-9].[0-9].[0-9]" \
| head -n 1)
echo "No version was provided, so retrieved latest from download url: $VERSION"
else
VERSION=$1
echo "Using provided version: $VERSION"
fi
if [ -z "$VERSION" ] ; then
echo "Couldn't determine latest version to download"
exit 1
fi
DIST_NAME="terraform_$VERSION"
ARCH_NAME="_linux_amd64"
URL="$CDN/$VERSION/$DIST_NAME$ARCH_NAME.zip"
echo "Package url is $URL"
BASE_PATH=/opt
DEST_PATH=$BASE_PATH/$DIST_NAME
TMP_FILE="$(mktemp).zip"
if ! is_sudo ; then
UNZIP_CMD="sudo unzip"
RM_CMD="sudo rm"
MK_CMD="sudo mkdir"
else
UNZIP_CMD="unzip"
RM_CMD="rm"
MK_CMD="mkdir"
fi
if [ -d $DEST_PATH ] ; then
echo "Replacing contents on $DEST_PATH"
$RM_CMD -r $DEST_PATH
fi
$MK_CMD $DEST_PATH
curl $URL -o $TMP_FILE
$UNZIP_CMD -d $DEST_PATH $TMP_FILE
rm $TMP_FILE
ENV_FILE=$(add_to_path "terraform" "$DEST_PATH")
echo "Created $ENV_FILE"