1
+ #! /bin/bash
2
+
3
+ # Copyright © 2024 Meroxa, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # The install script is based off of the MIT-licensed script from glide,
18
+ # the package manager for Go: https://github.com/Masterminds/glide.sh/blob/master/get
19
+
20
+ PROJECT_NAME=" benchi"
21
+
22
+ # Define color codes
23
+ reset=" \033[0m"
24
+ conduit_blue=" \033[38;5;45m"
25
+ red=" \033[31m"
26
+
27
+ fail () {
28
+ echo " $1 "
29
+ exit 1
30
+ }
31
+
32
+ # Function to print a string in bright blue
33
+ coloredEcho () {
34
+ local text=$1
35
+ printf " ${conduit_blue}${text}${reset} \n"
36
+ }
37
+
38
+ initArch () {
39
+ ARCH=$( uname -m)
40
+ case $ARCH in
41
+ aarch64) ARCH=" arm64" ;;
42
+ arm64) ARCH=" arm64" ;;
43
+ x86) ARCH=" i386" ;;
44
+ x86_64) ARCH=" x86_64" ;;
45
+ i686) ARCH=" i386" ;;
46
+ i386) ARCH=" i386" ;;
47
+ * )
48
+ fail " Error: Unsupported architecture: $ARCH "
49
+ ;;
50
+ esac
51
+ }
52
+
53
+ initOS () {
54
+ OS=$( echo $( uname) )
55
+ # We support Linux and Darwin, Windows (mingw, msys) are not supported.
56
+ if [[ " $OS " != " Linux" && " $OS " != " Darwin" ]]; then
57
+ fail " Error: Unsupported operating system: $OS "
58
+ fi
59
+ }
60
+
61
+ initDownloadTool () {
62
+ if type " curl" > /dev/null; then
63
+ DOWNLOAD_TOOL=" curl"
64
+ elif type " wget" > /dev/null; then
65
+ DOWNLOAD_TOOL=" wget"
66
+ else
67
+ fail " You need 'curl' or 'wget' as a download tool. Please install it first before continuing."
68
+ fi
69
+ }
70
+
71
+ getLatestTag () {
72
+ # GitHub releases URL
73
+ local url=" https://github.com/ConduitIO/benchi/releases/latest"
74
+ local latest_url # Variable to store the redirected URL
75
+
76
+ # Check if DOWNLOAD_TOOL is set to curl or wget
77
+ if [[ " $DOWNLOAD_TOOL " == " curl" ]]; then
78
+ # Use curl to get the redirected link
79
+ latest_url=$( curl -sL -o /dev/null -w " %{url_effective}" " $url " )
80
+ elif [[ " $DOWNLOAD_TOOL " == " wget" ]]; then
81
+ # Use wget to get the redirected link
82
+ latest_url=$( wget --spider --server-response --max-redirect=2 " $url " 2>&1 | grep " Location" | tail -1 | awk ' {print $2}' )
83
+ else
84
+ fail " Error: DOWNLOAD_TOOL is not set or not recognized. Use 'curl' or 'wget'."
85
+ fi
86
+
87
+ # Extract the tag from the redirected URL (everything after the last "/")
88
+ TAG=$( echo " $latest_url " | grep -oE " [^/]+$" )
89
+ }
90
+
91
+ get () {
92
+ local url=" $2 "
93
+ local body
94
+ local httpStatusCode
95
+ echo " Getting $url "
96
+ if [ " $DOWNLOAD_TOOL " = " curl" ]; then
97
+ httpResponse=$( curl -sL --write-out HTTPSTATUS:%{http_code} " $url " )
98
+ httpStatusCode=$( echo $httpResponse | tr -d ' \n' | sed -e ' s/.*HTTPSTATUS://' )
99
+ body=$( echo " $httpResponse " | sed -e ' s/HTTPSTATUS\:.*//g' )
100
+ elif [ " $DOWNLOAD_TOOL " = " wget" ]; then
101
+ tmpFile=$( mktemp)
102
+ body=$( wget --server-response --content-on-error -q -O - " $url " 2> $tmpFile || true)
103
+ httpStatusCode=$( cat $tmpFile | awk ' /^ HTTP/{print $2}' | tail -1)
104
+ rm -f $tmpFile
105
+ fi
106
+ if [ " $httpStatusCode " != 200 ]; then
107
+ echo " Request fail with http status code $httpStatusCode "
108
+ fail " Body: $body "
109
+ fi
110
+ eval " $1 ='$body '"
111
+ }
112
+
113
+ getFile () {
114
+ local url=" $1 "
115
+ local filePath=" $2 "
116
+ local httpStatusCode
117
+
118
+ if [ " $DOWNLOAD_TOOL " = " curl" ]; then
119
+ httpStatusCode=$( curl --progress-bar -w ' %{http_code}' -L " $url " -o " $filePath " )
120
+ echo " $httpStatusCode "
121
+ elif [ " $DOWNLOAD_TOOL " = " wget" ]; then
122
+ tmpFile=$( mktemp)
123
+ wget --server-response --content-on-error -q -O " $filePath " " $url " 2> $tmpFile
124
+ if [ $? -ne 0 ]; then
125
+ httpStatusCode=$( cat $tmpFile | awk ' /^ HTTP/{print $2}' | tail -1)
126
+ rm -f $tmpFile
127
+ echo " $httpStatusCode "
128
+ return 1
129
+ fi
130
+ rm -f $tmpFile
131
+ echo " 200"
132
+ fi
133
+ }
134
+
135
+ install () {
136
+ coloredEcho " Installing Benchi $TAG ..."
137
+ printf " \n"
138
+
139
+ local version=" ${TAG# v} " # Remove the leading 'v' from TAG and store it in 'version'
140
+ BENCHI_DIST=" benchi_${version} _${OS} _${ARCH} .tar.gz"
141
+ EXTRACTION_DIR=" /tmp/benchi_${version} _${OS} _${ARCH} "
142
+
143
+ DOWNLOAD_URL=" https://github.com/ConduitIO/benchi/releases/download/$TAG /$BENCHI_DIST "
144
+ BENCHI_TMP_FILE=" /tmp/$BENCHI_DIST "
145
+ echo " Downloading $DOWNLOAD_URL "
146
+
147
+ # Download the file
148
+ httpStatusCode=$( getFile " $DOWNLOAD_URL " " $BENCHI_TMP_FILE " )
149
+ if [ " $httpStatusCode " -ne 200 ]; then
150
+ echo " Did not find a release for your system: $OS $ARCH "
151
+ echo " Trying to find a release on the github api."
152
+ LATEST_RELEASE_URL=" https://api.github.com/repos/conduitio/$PROJECT_NAME /releases/tags/$TAG "
153
+ get LATEST_RELEASE_JSON $LATEST_RELEASE_URL
154
+ # || true forces this command to not catch error if grep does not find anything
155
+ DOWNLOAD_URL=$( echo " $LATEST_RELEASE_JSON " | grep ' browser_' | cut -d\" -f4 | grep " $BENCHI_DIST " ) || true
156
+ if [ -z " $DOWNLOAD_URL " ]; then
157
+ echo " Sorry, we dont have a dist for your system: $OS $ARCH "
158
+ fail " You can ask one here: https://github.com/conduitio/$PROJECT_NAME /issues"
159
+ else
160
+ echo " Downloading $DOWNLOAD_URL "
161
+ httpStatusCode=$( getFile " $DOWNLOAD_URL " " $BENCHI_TMP_FILE " )
162
+ if [ " $httpStatusCode " -ne 200 ]; then
163
+ fail " Failed to download from $DOWNLOAD_URL "
164
+ fi
165
+ fi
166
+ fi
167
+
168
+ # Create extraction directory if it doesn't exist
169
+ mkdir -p " $EXTRACTION_DIR "
170
+ if [ $? -ne 0 ]; then
171
+ fail " Failed to create extraction directory $EXTRACTION_DIR "
172
+ fi
173
+
174
+ # Extract the tar file
175
+ tar -xf " $BENCHI_TMP_FILE " -C " $EXTRACTION_DIR "
176
+ if [ $? -ne 0 ]; then
177
+ fail " Failed to extract $BENCHI_TMP_FILE to $EXTRACTION_DIR "
178
+ fi
179
+
180
+ # Check if binary exists after extraction
181
+ if [ ! -f " $EXTRACTION_DIR /benchi" ]; then
182
+ fail " Could not find benchi binary in extracted files"
183
+ fi
184
+
185
+ # Move the binary to /usr/local/bin with sudo (since it requires admin privileges)
186
+ sudo mv " $EXTRACTION_DIR /benchi" /usr/local/bin/
187
+ if [ $? -ne 0 ]; then
188
+ fail " Failed to move benchi to /usr/local/bin/"
189
+ fi
190
+
191
+ # Make sure it has the correct permissions
192
+ sudo chmod 755 /usr/local/bin/benchi
193
+ if [ $? -ne 0 ]; then
194
+ fail " Failed to set permissions on /usr/local/bin/benchi"
195
+ fi
196
+
197
+ # Clean up temporary files
198
+ rm -f " $BENCHI_TMP_FILE "
199
+ rm -rf " $EXTRACTION_DIR "
200
+ }
201
+
202
+ bye () {
203
+ result=$?
204
+ if [ " $result " != " 0" ]; then
205
+ echo -e " ${red} Failed to install Benchi${reset} "
206
+ fi
207
+ exit $result
208
+ }
209
+
210
+ testVersion () {
211
+ set +e
212
+ BENCHI_BIN=" $( which $PROJECT_NAME ) "
213
+ if [ " $? " = " 1" ]; then
214
+ fail " $PROJECT_NAME not found."
215
+ fi
216
+ set -e
217
+ BENCHI_VERSION=$( $PROJECT_NAME -v)
218
+ coloredEcho " \n$BENCHI_VERSION installed successfully"
219
+ }
220
+
221
+ # Execution
222
+
223
+ # Stop execution on any error
224
+ trap " bye" EXIT
225
+ set -e
226
+
227
+ initArch
228
+ initOS
229
+ initDownloadTool
230
+ getLatestTag
231
+ install
232
+ testVersion
0 commit comments