-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentinel-install.sh
executable file
·218 lines (192 loc) · 6.07 KB
/
sentinel-install.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
set -e
# SENTINEL INSTALLER - Automated HashiCorp Sentinel Installation
# Apache 2 License - Copyright (c) 2020 Robert Peteuil @RobertPeteuil
#
# Automatically Download, Extract and Install
# Latest or Specific Version of Sentinel
#
# from: https://github.com/robertpeteuil/sentinel-installer
# Uncomment line below to always use 'sudo' to install to /usr/local/bin/
# sudoInstall=true
scriptname=$(basename "$0")
scriptbuildnum="1.5.2"
scriptbuilddate="2020-05-14"
# 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
echo "aborting - jq not installed and required"
exit 1
fi
displayVer() {
echo -e "${scriptname} ver ${scriptbuildnum} - ${scriptbuilddate}"
}
usage() {
[[ "$1" ]] && echo -e "Download and install HashiCorp Sentinel - Latest Version unless '-i' specified\n"
echo -e "usage: ${scriptname} [-i VERSION] [-a] [-c] [-h] [-v]"
echo -e " -i VERSION\t: specify version to install in format '0.15.4' (OPTIONAL)"
echo -e " -a\t\t: automatically use sudo to install to /usr/local/bin"
echo -e " -c\t\t: leave binary in working directory (for CI/DevOps use)"
echo -e " -h\t\t: help"
echo -e " -v\t\t: display ${scriptname} version"
}
getLatest() {
# USE NET RETRIEVAL TOOL TO GET LATEST VERSION
case "${nettool}" in
# jq installed - parse version from HashiCorp website
wget)
LATEST_ARR=($(wget -q -O- https://releases.hashicorp.com/index.json 2>/dev/null | jq -r '.sentinel.versions[].version' | sort -t. -k 1,1nr -k 2,2nr -k 3,3nr))
;;
curl)
LATEST_ARR=($(curl -s https://releases.hashicorp.com/index.json 2>/dev/null | jq -r '.sentinel.versions[].version' | sort -t. -k 1,1nr -k 2,2nr -k 3,3nr))
;;
esac
# make sure latest version isn't beta or rc
for ver in "${LATEST_ARR[@]}"; do
if [[ ! $ver =~ beta ]] && [[ ! $ver =~ rc ]] && [[ ! $ver =~ alpha ]]; then
LATEST="$ver"
break
fi
done
echo -n "$LATEST"
}
while getopts ":i:achv" arg; do
case "${arg}" in
a) sudoInstall=true;;
c) cwdInstall=true;;
i) VERSION=${OPTARG};;
h) usage x; exit;;
v) displayVer; exit;;
\?) echo -e "Error - Invalid option: $OPTARG"; usage; exit;;
:) echo "Error - $OPTARG requires an argument"; usage; exit 1;;
esac
done
shift $((OPTIND-1))
# POPULATE VARIABLES NEEDED TO CREATE DOWNLOAD URL AND FILENAME
if [[ -z "$VERSION" ]]; then
VERSION=$(getLatest)
fi
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
if [[ "$OS" == "linux" ]]; then
PROC=$(lscpu 2> /dev/null | awk '/Architecture/ {if($2 == "x86_64") {print "amd64"; exit} else {print "386"; exit}}')
if [[ -z $PROC ]]; then
PROC=$(cat /proc/cpuinfo | awk '/flags/ {if($0 ~ /lm/) {print "amd64"; exit} else {print "386"; exit}}')
fi
else
PROC="amd64"
fi
[[ $PROC =~ arm ]] && PROC="arm" # sentinel downloads use "arm" not full arm type
# CREATE FILENAME AND URL FROM GATHERED PARAMETERS
FILENAME="sentinel_${VERSION}_${OS}_${PROC}.zip"
LINK="https://releases.hashicorp.com/sentinel/${VERSION}/${FILENAME}"
SHALINK="https://releases.hashicorp.com/sentinel/${VERSION}/sentinel_${VERSION}_SHA256SUMS"
# TEST CALCULATED LINKS
case "${nettool}" in
wget)
LINKVALID=$(wget --spider -S "$LINK" 2>&1 | grep "HTTP/" | awk '{print $2}')
SHALINKVALID=$(wget --spider -S "$SHALINK" 2>&1 | grep "HTTP/" | awk '{print $2}')
;;
curl)
LINKVALID=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' "$LINK")
SHALINKVALID=$(curl -o /dev/null --silent --head --write-out '%{http_code}\n' "$SHALINK")
;;
esac
# VERIFY LINK VALIDITY
if [[ "$LINKVALID" != 200 ]]; then
echo -e "Cannot Install - Download URL Invalid"
echo -e "\nParameters:"
echo -e "\tVER:\t$VERSION"
echo -e "\tOS:\t$OS"
echo -e "\tPROC:\t$PROC"
echo -e "\tURL:\t$LINK"
exit 1
fi
# VERIFY SHA LINK VALIDITY
if [[ "$SHALINKVALID" != 200 ]]; then
echo -e "Cannot Install - URL for Checksum File Invalid"
echo -e "\tURL:\t$SHALINK"
exit 1
fi
# DETERMINE DESTINATION
if [[ "$cwdInstall" ]]; then
BINDIR=$(pwd)
elif [[ -w "/usr/local/bin" ]]; then
BINDIR="/usr/local/bin"
CMDPREFIX=""
STREAMLINED=true
elif [[ "$sudoInstall" ]]; then
BINDIR="/usr/local/bin"
CMDPREFIX="sudo "
STREAMLINED=true
else
echo -e "Sentinel Installer\n"
echo "Specify install directory (a,b or c):"
echo -en "\t(a) '~/bin' (b) '/usr/local/bin' as root (c) abort : "
read -r -n 1 SELECTION
echo
if [ "${SELECTION}" == "a" ] || [ "${SELECTION}" == "A" ]; then
BINDIR="${HOME}/bin"
CMDPREFIX=""
elif [ "${SELECTION}" == "b" ] || [ "${SELECTION}" == "B" ]; then
BINDIR="/usr/local/bin"
CMDPREFIX="sudo "
else
exit 0
fi
fi
# CREATE TMPDIR FOR EXTRACTION
if [[ ! "$cwdInstall" ]]; then
TMPDIR=${TMPDIR:-/tmp}
UTILTMPDIR="sentinel_${VERSION}"
cd "$TMPDIR" || exit 1
mkdir -p "$UTILTMPDIR"
cd "$UTILTMPDIR" || exit 1
fi
# DOWNLOAD ZIP AND CHECKSUM FILES
case "${nettool}" in
wget*)
wget -q "$LINK" -O "$FILENAME"
wget -q "$SHALINK" -O SHAFILE
;;
curl*)
curl -s -o "$FILENAME" "$LINK"
curl -s -o SHAFILE "$SHALINK"
;;
esac
# VERIFY ZIP CHECKSUM
if shasum -h 2&> /dev/null; then
expected_sha=$(cat SHAFILE | grep "$FILENAME" | awk '{print $1}')
download_sha=$(shasum -a 256 "$FILENAME" | cut -d' ' -f1)
if [ $expected_sha != $download_sha ]; then
echo "Download Checksum Incorrect"
echo "Expected: $expected_sha"
echo "Actual: $download_sha"
exit 1
fi
fi
# EXTRACT ZIP
unzip -qq "$FILENAME" || exit 1
if [[ ! "$cwdInstall" ]]; then
# MOVE TO DESTINATION
mkdir -p "${BINDIR}" || exit 1
${CMDPREFIX} mv sentinel "$BINDIR" || exit 1
# CLEANUP AND EXIT
cd "${TMPDIR}" || exit 1
rm -rf "${UTILTMPDIR}"
[[ ! "$STREAMLINED" ]] && echo
echo "Sentinel Version ${VERSION} installed to ${BINDIR}"
else
echo "Sentinel Version ${VERSION} downloaded"
fi
exit 0