forked from open-cluster-management-io/clusteradm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
196 lines (157 loc) · 4.69 KB
/
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
# Copyright Contributors to the Open Cluster Management project
#!/usr/bin/env bash
# Clusteradm CLI location
: ${INSTALL_DIR:="/usr/local/bin"}
# sudo is required to copy binary to INSTALL_DIR for linux
: ${USE_SUDO:="false"}
# Http request CLI
HTTP_REQUEST_CLI=curl
# GitHub Organization and repo name to download release
GITHUB_ORG=open-cluster-management-io
GITHUB_REPO=clusteradm
# CLI filename
CLI_FILENAME=clusteradm
CLI_FILE="${INSTALL_DIR}/${CLI_FILENAME}"
getSystemInfo() {
ARCH=$(uname -m)
case $ARCH in
armv7*) ARCH="arm";;
aarch64) ARCH="arm64";;
x86_64) ARCH="amd64";;
esac
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
# Most linux distro needs root permission to copy the file to /usr/local/bin
if [[ "$OS" == "linux" || "$OS" == "darwin" ]] && [ "$INSTALL_DIR" == "/usr/local/bin" ]; then
USE_SUDO="true"
fi
}
verifySupported() {
local supported=(darwin-amd64 darwin-arm64 linux-amd64 linux-arm64 windows-amd64)
local current_osarch="${OS}-${ARCH}"
for osarch in "${supported[@]}"; do
if [ "$osarch" == "$current_osarch" ]; then
echo "Your system is ${OS}_${ARCH}"
return
fi
done
echo "No prebuilt binary for ${current_osarch}"
exit 1
}
checkHttpRequestCLI() {
if type "curl" > /dev/null; then
HTTP_REQUEST_CLI=curl
elif type "wget" > /dev/null; then
HTTP_REQUEST_CLI=wget
else
echo "Either curl or wget is required"
exit 1
fi
}
checkExisting() {
if [ -f "$CLI_FILE" ]; then
echo -e "\nclusteradm CLI is detected:"
echo -e "Reinstalling clusteradm CLI - ${CLI_FILE}...\n"
else
echo -e "Installing clusteradm CLI...\n"
fi
}
runAsRoot() {
local CMD="$*"
if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then
CMD="sudo $CMD"
fi
$CMD
}
downloadFile() {
TARGET_VERSION=$1
DOWNLOAD_BASE="https://github.com/${GITHUB_ORG}/${GITHUB_REPO}"
CLI_ARTIFACT="${CLI_FILENAME}_${OS}_${ARCH}.tar.gz"
if [ "$TARGET_VERSION" == "latest" ]; then
DOWNLOAD_URL="${DOWNLOAD_BASE}/releases/latest/download/${CLI_ARTIFACT}"
else
DOWNLOAD_URL="${DOWNLOAD_BASE}/releases/download/${TARGET_VERSION}/${CLI_ARTIFACT}"
fi
# Create the temp directory
TMP_ROOT=$(mktemp -dt clusteradm-install-XXXXXX)
ARTIFACT_TMP_FILE="$TMP_ROOT/$CLI_ARTIFACT"
echo "Downloading $DOWNLOAD_URL ..."
if [ "$HTTP_REQUEST_CLI" == "curl" ]; then
curl -SsL "$DOWNLOAD_URL" -o "$ARTIFACT_TMP_FILE"
else
wget -q -O "$ARTIFACT_TMP_FILE" "$DOWNLOAD_URL"
fi
if [ ! -f "$ARTIFACT_TMP_FILE" ]; then
echo "failed to download $DOWNLOAD_URL ..."
exit 1
fi
}
isReleaseAvailable() {
LATEST_RELEASE_TAG=$1
CLI_ARTIFACT="${CLI_FILENAME}_${OS}_${ARCH}.tar.gz"
DOWNLOAD_BASE="https://github.com/${GITHUB_ORG}/${GITHUB_REPO}/releases/download"
DOWNLOAD_URL="${DOWNLOAD_BASE}/${LATEST_RELEASE_TAG}/${CLI_ARTIFACT}"
if [ "$HTTP_REQUEST_CLI" == "curl" ]; then
httpstatus=$(curl -sSLI -o /dev/null -w "%{http_code}" "$DOWNLOAD_URL")
if [ "$httpstatus" == "200" ]; then
return 0
fi
else
wget -q --spider "$DOWNLOAD_URL"
exitstatus=$?
if [ $exitstatus -eq 0 ]; then
return 0
fi
fi
return 1
}
installFile() {
tar xf "$ARTIFACT_TMP_FILE" -C "$TMP_ROOT"
local tmp_root_cli="$TMP_ROOT/$CLI_FILENAME"
if [ ! -f "$tmp_root_cli" ]; then
echo "Failed to unpack clusteradm CLI executable."
exit 1
fi
chmod o+x $tmp_root_cli
runAsRoot cp "$tmp_root_cli" "$INSTALL_DIR"
if [ -f "$CLI_FILE" ]; then
echo "$CLI_FILENAME installed into $INSTALL_DIR successfully."
else
echo "Failed to install $CLI_FILENAME"
exit 1
fi
}
fail_trap() {
result=$?
if [ "$result" != "0" ]; then
echo "Failed to install clusteradm CLI"
echo "For support, go to https://open-cluster-management.io/"
fi
cleanup
exit $result
}
cleanup() {
if [[ -d "${TMP_ROOT:-}" ]]; then
rm -rf "$TMP_ROOT"
fi
}
installCompleted() {
echo -e "\nTo get started with clusteradm, please visit https://open-cluster-management.io/getting-started/"
}
# -----------------------------------------------------------------------------
# main
# -----------------------------------------------------------------------------
trap "fail_trap" EXIT
getSystemInfo
checkHttpRequestCLI
if [ -z "$1" ]; then
TARGET_VERSION="latest"
else
TARGET_VERSION=v$1
fi
verifySupported
checkExisting
echo "Installing $TARGET_VERSION OCM clusteradm CLI..."
downloadFile $TARGET_VERSION
installFile
cleanup
installCompleted