-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcy-wrapper.sh
More file actions
284 lines (249 loc) · 8.89 KB
/
Copy pathcy-wrapper.sh
File metadata and controls
284 lines (249 loc) · 8.89 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env bash
# Warning: this script is expected to be used in cycloid-toolkit docker image.
# We do not recommand to use it in another context.
## Environment variables:
# CY_DEBUG: enable "set -x" for debugging purpose
# CY_API_URL: Override the default API url "https://http-api.cycloid.io" required for Onprem setup.
# Also used to try downloading cy directly from the backend (fly-style, see
# get_binary_from_backend) before falling back to GitHub releases.
# CY_BINARIES_PATH: Specify the path where cy binaries will be stored, default "/usr/local/bin"
# CY_WAIT_NETWORK: Ensure or wait to have internet access before trying to download the binary by trying to curl "cli-release.owl.cycloid.io/releases", default "false"
# CY_BINARY: Enforce the usage of a specific local binary. Default path "${CY_BINARIES_PATH}/cy-${CY_VERSION}"
# CY_DOWNLOAD_RETRIES: In case you have network failure, you can specify number of retries on download for binaries. Default "1"
# CY_RELEASES_URL: Override Cycloid CLI release API URL, default https://cli-release.owl.cycloid.io/releases
if [ -n "$CY_DEBUG" ]; then
echo "CY_DEBUG provided, wrapper running in DEBUG mode" >&2
set -x
fi
if test -z "$BASH_VERSION"; then
echo "Please run this script using bash, not sh or any other shell." >&2
exit 1
fi
export CY_API_URL="${CY_API_URL:-https://http-api.cycloid.io}"
export CY_BINARIES_PATH="${CY_BINARIES_PATH:-/usr/local/bin}"
export CY_WAIT_NETWORK="${CY_WAIT_NETWORK:-false}"
export CY_DOWNLOAD_RETRIES="${CY_DOWNLOAD_RETRIES:-1}"
export CY_RELEASES_URL="${CY_RELEASES_URL:-https://cli-release.owl.cycloid.io/releases}"
detect_platform() {
os=$(uname -s)
arch=$(uname -m)
case "$os" in
Darwin)
case "$arch" in
arm64)
echo "darwin-arm64"
return 0
;;
x86_64)
echo "darwin-amd64"
return 0
;;
esac
;;
Linux)
case "$arch" in
aarch64 | armv7l | armv8l)
echo "linux-arm64"
return 0
;;
x86_64)
echo "linux-amd64"
return 0
;;
esac
;;
esac
# Default case
echo "linux-amd64"
return 0
}
UPSTREAM_BINARY_NAME="cy-$(detect_platform)"
export UPSTREAM_BINARY_NAME
# Compating version, this is used when there is no CLI matching your API version.
# We compare your version and the one released to find the closest n-1 version
vercomp() {
# Src https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
# Return code
# 0) =
# 1) >
# 2) <
if [[ $1 == "$2" ]]; then
return 0
fi
local IFS=.
# shellcheck disable=SC2206
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i = ${#ver1[@]}; i < ${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i = 0; i < ${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
return 2
fi
done
return 0
}
# Remove extra prefix and suffix from version to compare them
format_version() {
echo "$1" | sed 's/^v//;s/-.*$//'
return
}
# Find the closest n-1 version. This function is used only after we already check if version and version-rc are not available.
# So we only expect to find and take the n-1 version
find_version_below() {
api_version=$(format_version "$1")
for cli_release in $(curl --fail --retry-all-errors --retry-delay 2 --retry 2 --silent "$CY_RELEASES_URL" | jq -r '.[] | .name'); do
cli_version=$(format_version "$cli_release")
# Ignoring the dev release from github
if [[ "$cli_version" == "0.0-dev" ]]; then
continue
fi
# If the API version format does not match release version like "1.0.81" (eg local dev run provide the short commit ID as version).
# Use the latest CLI version found
if ! [[ "$api_version" =~ ^[0-9]+\..+$ ]]; then
echo "$cli_release"
return 0
fi
# Take the first CLI version lower than the API version
# ret 1 means >
vercomp "$api_version" "$cli_version"
if [ $? -eq 1 ]; then
echo "$cli_release"
return 0
fi
done
return 2
}
# Look if the binary is present locally or try to download it
get_binary() {
# Download the binary if not present
export CY_BINARY="${CY_BINARY:-"${CY_BINARIES_PATH}/cy-${CY_VERSION}"}"
if [[ -f "${CY_BINARY}" ]]; then
return 0
fi
CY_VERSION=$(format_version "$CY_VERSION")
# Allow usage of version 0.0 and dev to test latest CLI version from pull requests
if [[ "$CY_VERSION" == "0.0" ]] || [[ "$CY_VERSION" == "dev" ]]; then
CY_VERSION="0.0-dev"
fi
# Download the exact CLI version
CY_URL="https://github.com/cycloidio/cycloid-cli/releases/download/v${CY_VERSION}/${UPSTREAM_BINARY_NAME}"
wget --retry-connrefused --wait 2 --tries 2 -q -O "${CY_BINARY}" "$CY_URL"
STATUS=$?
if [ $STATUS != 0 ]; then
rm -f "${CY_BINARY}"
fi
# In case of error, download RC CLI version
if [ $STATUS != 0 ]; then
echo "Warning: Unable to download CLI version ${CY_VERSION}. Fallback to RC version" >&2
export CY_BINARY="${CY_BINARIES_PATH}/cy-${CY_VERSION}-rc"
if [[ -f "${CY_BINARY}" ]]; then
STATUS=0
else
CY_URL="https://github.com/cycloidio/cycloid-cli/releases/download/v${CY_VERSION}-rc/${UPSTREAM_BINARY_NAME}"
wget --retry-connrefused --wait 2 --tries 2 -q -O "${CY_BINARY}" "$CY_URL"
STATUS=$?
if [ $STATUS != 0 ]; then
rm -f "${CY_BINARY}"
fi
fi
fi
# In case of error, fallback on latest lower version
if [ $STATUS != 0 ]; then
if ! CY_LOWER_VERSION=$(find_version_below "${CY_VERSION}"); then
echo "Error: find_version_below unable to obtain closest n-1 version from $CY_RELEASES_URL" >&2
return 2
fi
echo "Warning: Unable to download CLI version ${CY_VERSION}-rc. Fallback to the closest n-1 version ${CY_LOWER_VERSION}" >&2
# Removing the v prefix as we don't let it in the binary name
CY_BINARY="${CY_BINARIES_PATH}/cy-$(echo "$CY_LOWER_VERSION" | sed 's/^v//')"
export CY_BINARY
if [[ -f "${CY_BINARY}" ]]; then
STATUS=0
else
CY_URL="https://github.com/cycloidio/cycloid-cli/releases/download/${CY_LOWER_VERSION}/${UPSTREAM_BINARY_NAME}"
wget --retry-connrefused --wait 2 --tries 2 -q -O "${CY_BINARY}" "$CY_URL"
STATUS=$?
if [ $STATUS != 0 ]; then
rm -f "${CY_BINARY}"
fi
fi
fi
return $STATUS
}
# Try downloading cy straight from the backend (served fly-style, see CLI-13),
# only when the backend's own cy matches CY_VERSION. Returns non-zero on any
# failure so the caller falls back to the unchanged GitHub-releases flow.
get_binary_from_backend() {
export CY_BINARY="${CY_BINARY:-"${CY_BINARIES_PATH}/cy-${CY_VERSION}"}"
if [[ -f "${CY_BINARY}" ]]; then
return 0
fi
backend_version=$(curl --fail -k --retry-all-errors --retry-delay 2 --retry 2 -s "${CY_API_URL}/api/v1/cli/version" | jq -r .data.version)
if [[ -z "$backend_version" ]]; then
return 1
fi
if [[ "$(format_version "$backend_version")" != "$(format_version "$CY_VERSION")" ]]; then
return 1
fi
platform=$(detect_platform)
backend_os="${platform%-*}"
backend_arch="${platform#*-}"
CY_URL="${CY_API_URL}/api/v1/cli/download?os=${backend_os}&arch=${backend_arch}"
wget --retry-connrefused --wait 2 --tries 2 -q -O "${CY_BINARY}" "$CY_URL"
STATUS=$?
if [ $STATUS != 0 ]; then
rm -f "${CY_BINARY}"
fi
return $STATUS
}
# Ensure we have Cycloid directory created
if ! [ -d "$CY_BINARIES_PATH" ]; then
mkdir -p "$CY_BINARIES_PATH"
fi
# If specified in the commandline use it
if [[ $(echo "$@" | grep -E -- "--api-url(=|\s+)([^\]+)") ]]; then
CY_API_URL=$(echo "$@" | sed -r "s/.*--api-url(=|\s+)([^ ]+).*/\2/")
fi
# Remove trailing /
CY_API_URL=${CY_API_URL%/}
# Wait for network access. This ensure in case of usage in a pipeline to ensure and wait in case network is not started.
if [[ "$CY_WAIT_NETWORK" == "true" ]]; then
timeout 120 bash -c 'while [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' CY_RELEASES_URL != "200" ]]; do sleep 3; done'
fi
# Get Cycloid API version
CY_API_VERSION=$(curl --fail -k --retry-all-errors --retry-delay 2 --retry 2 -s "${CY_API_URL}/version" | jq -r .data.version)
export CY_API_VERSION
export CY_VERSION="${CY_VERSION:-$CY_API_VERSION}"
if [[ -z "$CY_VERSION" ]]; then
echo "Error: Unable to get Cycloid API version on ${CY_API_URL}/version" >&2
exit 1
fi
# Adding 3 retry to maximize changes when there is issue in CI tools
for i in $(seq 1 "$CY_DOWNLOAD_RETRIES"); do
if get_binary_from_backend; then
STATUS=0
break
fi
get_binary
STATUS=$?
if [ $STATUS == 0 ]; then
break
fi
done
# If no binaries have been downloaded after 3 tries raise an error
if [ "$STATUS" != 0 ]; then
echo "Global error: Unable to download Cycloid CLI from github ${CY_URL}" >&2
exit 1
fi
chmod +x "${CY_BINARY}"
# Run Cycloid CLI
exec "${CY_BINARY}" "$@"