forked from opsgang/alpine_build_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_terraform.sh
executable file
·146 lines (128 loc) · 3.84 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh
# vim: et smartindent sr sw=4 ts=4:
# CAVEAT: script only works with single binary versions
# i.e versions greater than $XVER
#
# Use to install multiple versions of terraform.
# and switch between them.
#
# /usr/local/bin/terraform is symlinked to desired
# versioned one under /usr/local/bin.
#
# Also installs vim plugin if pathogen detected.
#
APP=terraform
XVER=0.6.16
VER=${1:-$TERRAFORM_VERSION}
BIN="/usr/local/bin"
ZIP="$BIN/${APP}-$VER.zip"
APK_TMP="/var/cache/apk"
if [[ -z "$VER" ]]; then
echo "ERROR $0: you must supply a version of ${APP} to use"
exit 1
fi
if [[ "$VER" == "list" ]] || [[ "$VER" == "show" ]] ; then
# ... ideally we have recent sort that supports -V
sort_opts="-rV"
! apk info | grep "^sort$" >/dev/null 2>&1 && sort_opts=""
echo "Versions available under $BIN:"
if [[ -L $BIN/${APP} ]]; then
echo "using: $(ls -l $BIN/${APP} | sed -e 's/.* \([^ ]\+ -> [^ ]\+\)/\1/')"
fi
f=$(
set -o pipefail ;
ls -1 $BIN/${APP}-* 2>/dev/null \
| sed -e 's/.*${APP}-//' \
| sort $sort_opts \
|| echo "None found"
)
echo "$f" && exit 0
fi
if [[ $(echo -e "$VER\n$XVER" | sort -V | head -n 1) == "$VER" ]]; then
echo "ERROR $0: script only works with versions greater than $XVER" >&2
exit 1
fi
if [[ -f $BIN/${APP} ]]; then
if [[ -L $BIN/${APP} ]]; then
if ! rm $BIN/${APP}
then
echo "ERROR $0: ... could not delete symlink $BIN/${APP}"
exit 1
fi
elif [[ -x $BIN/${APP} ]]; then
OLDVER=$($BIN/${APP} --version | grep '^Terraform v' | sed -e 's/.* v\(.*\)$/\1/')
if ! mv $BIN/${APP} $BIN/${APP}-$OLDVER
then
echo "ERROR $0: ... could not move existing to $BIN/${APP}-$OLDVER"
exit 1
fi
else
echo "ERROR $0: ... $BIN/${APP} is not a bin or symlink to bin"
exit 1
fi
fi
if [[ ! -x $BIN/${APP}-$VER ]]; then
BASE_URI="https://releases.hashicorp.com/${APP}"
DOWNLOAD_URI="$BASE_URI/$VER/${APP}_${VER}_linux_amd64.zip"
REQ_PKGS="wget unzip ca-certificates"
for p in $REQ_PKGS; do
if ! apk info | grep "^${p}$" >/dev/null 2>&1
then
BUILD_PKGS="$BUILD_PKGS $p"
fi
done
if [[ ! -z $(echo "$BUILD_PKGS" | sed -e 's/ //g') ]] ; then
echo "INFO $0: installing helper pkgs"
apk --no-cache add --update $BUILD_PKGS
fi
echo "INFO $0: ... downloading ${APP} $VER"
if ! wget -q -T 60 -O $ZIP $DOWNLOAD_URI
then
echo "ERROR $0: could not download $VER from $DOWNLOAD_URI"
echo "ERROR $0: ... are you sure it exists?"
rm $ZIP >/dev/null 2>&1
exit 1
fi
if ! unzip -p $ZIP | cat >$BIN/${APP}-$VER
then
echo "ERROR $0: could not extract ${APP} from zip"
exit 1
fi
chmod a+x $BIN/${APP}-$VER
rm -f $ZIP
else
echo "INFO $0: ... $BIN/${APP}-$VER available ."
fi
echo "INFO $0: ... pointing $BIN/${APP} to $VER"
if ! ln -s $BIN/${APP}-$VER $BIN/${APP}
then
echo "ERROR $0: could not repoint $BIN/${APP}"
exit 1
fi
if ${APP} --version | grep "v${VER}$" 2>/dev/null
then
echo "INFO $0: installed ${APP} $VER successfully"
else
echo "INFO $0: failed to install"
exit 1
fi
# ... install vim plugin if appropriate
if [[ -w /etc/vim/bundle ]] && [[ ! -d /etc/vim/bundle/vim-${APP} ]]; then
echo "INFO $0: installing vim ${APP} plugin"
if ! apk info | grep '^git$' >/dev/null 2>&1
then
BUILD_PKGS="$BUILD_PKGS git"
apk --no-cache add --update git
fi
(
cd /etc/vim/bundle
git clone https://github.com/hashivim/vim-${APP}.git
rm -rf vim-${APP}/.git
)
fi
if [[ ! -z $(echo "$BUILD_PKGS" | sed -e 's/ //g') ]] ; then
echo "INFO $0: deleting helper pkgs"
apk --no-cache --purge del $BUILD_PKGS
fi
rm -rf $APK_TMP/* >/dev/null 2>&1
exit 0