-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
242 lines (188 loc) · 7.02 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
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
#!/usr/bin/env bash
# Execute this file using command "bash install.sh"
# Devops CLI location
: ${SPACE_CLI_INSTALL_DIR:="/usr/local/bin"}
# sudo is required to copy binary to SPACE_CLI_INSTALL_DIR for linux
: ${USE_SUDO:="false"}
# Http request CLI
SPACE_CLI_HTTP_REQUEST_CLI=curl
# GitHub Organization and repo name to download release
GITHUB_ORG=sharadregoti
GITHUB_REPO=devops-cli
# Dapr CLI filename
SPACE_CLI_FILENAME=devops
SPACE_CLI_FILE="${SPACE_CLI_INSTALL_DIR}/${SPACE_CLI_FILENAME}"
getSystemInfo() {
ARCH=$(uname -m)
case $ARCH in
armv7*) ARCH="arm";;
aarch64) ARCH="arm64";;
x86_64) ARCH="x86_64";;
386) ARCH="i386";;
esac
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
# Most linux distro needs root permission to copy the file to /usr/local/bin
if [ "$OS" == "linux" ] && [ "$SPACE_CLI_INSTALL_DIR" == "/usr/local/bin" ]; then
USE_SUDO="true"
fi
}
verifySupported() {
local supported=(darwin-amd64 linux-amd64 darwin-x86_64 linux-x86_64 darwin-i386 linux-i386)
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
}
runAsRoot() {
local CMD="$*"
if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then
CMD="sudo $CMD"
fi
$CMD
}
checkHttpRequestCLI() {
if type "curl" > /dev/null; then
SPACE_CLI_HTTP_REQUEST_CLI=curl
elif type "wget" > /dev/null; then
SPACE_CLI_HTTP_REQUEST_CLI=wget
else
echo "Either curl or wget is required"
exit 1
fi
}
checkExistingDapr() {
if [ -f "$SPACE_CLI_FILE" ]; then
echo -e "\nDevops CLI is detected:"
$SPACE_CLI_FILE version
echo -e "Reinstalling Devops CLI - ${SPACE_CLI_FILE}...\n"
else
echo -e "Installing Devops CLI...\n"
fi
}
getLatestRelease() {
local spaceCliReleaseUrl="https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases"
local latest_release=""
if [ "$SPACE_CLI_HTTP_REQUEST_CLI" == "curl" ]; then
latest_release=$(curl -s $spaceCliReleaseUrl | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p')
else
latest_release=$(wget -q --header="Accept: application/json" -O - $spaceCliReleaseUrl | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p')
fi
ret_val=$latest_release
}
downloadFile() {
LATEST_RELEASE_TAG=$1
SPACE_CLI_ARTIFACT="devops-cli_${LATEST_RELEASE_TAG}_${OS}_${ARCH}.tar.gz"
# https://storage.googleapis.com/devops-cli-artifacts/releases/devops/0.1.0/devops_0.1.0_Linux_arm64.tar.gz
DOWNLOAD_BASE="https://storage.googleapis.com/devops-cli-artifacts/releases/devops/${LATEST_RELEASE_TAG}"
DOWNLOAD_URL="${DOWNLOAD_BASE}/${SPACE_CLI_ARTIFACT}"
# Create the temp directory
SPACE_TMP_ROOT=$(mktemp -dt space-cli-install-XXXXXX)
ARTIFACT_TMP_FILE="${SPACE_TMP_ROOT}/${SPACE_CLI_ARTIFACT}"
echo "Downloading $DOWNLOAD_URL ..."
if [ "$SPACE_CLI_HTTP_REQUEST_CLI" == "curl" ]; then
curl "$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
# For plugins
# Check if the ".devops" directory already exists in the home directory
if [ ! -d "$HOME/.devops" ]; then
# If it doesn't exist, create it
mkdir "$HOME/.devops"
fi
# Check if the ".devops/plugins" directory already exists in the home directory
if [ ! -d "$HOME/.devops/plugins" ]; then
# If it doesn't exist, create it
mkdir "$HOME/.devops/plugins"
fi
######################## Kubernetes plugin ########################
if [ ! -d "$HOME/.devops/plugins/kubernetes" ]; then
# If it doesn't exist, create it
mkdir "$HOME/.devops/plugins/kubernetes"
fi
SPACE_CLI_ARTIFACT="devops-kubernetes-plugin_${LATEST_RELEASE_TAG}_${OS}_${ARCH}.tar.gz"
DOWNLOAD_URL="${DOWNLOAD_BASE}/${SPACE_CLI_ARTIFACT}"
echo "Downloading kubernetes plugin"
wget -O "$HOME/.devops/plugins/kubernetes/devops.tar.gz" "${DOWNLOAD_URL}"
tar -xzf "$HOME/.devops/plugins/kubernetes/devops.tar.gz" -C "$HOME/.devops/plugins/kubernetes"
######################## Helm plugin ########################
if [ ! -d "$HOME/.devops/plugins/helm" ]; then
# If it doesn't exist, create it
mkdir "$HOME/.devops/plugins/helm"
fi
SPACE_CLI_ARTIFACT="devops-helm-plugin_${LATEST_RELEASE_TAG}_${OS}_${ARCH}.tar.gz"
DOWNLOAD_URL="${DOWNLOAD_BASE}/${SPACE_CLI_ARTIFACT}"
echo "Downloading helm plugin"
wget -O "$HOME/.devops/plugins/helm/devops.tar.gz" "${DOWNLOAD_URL}"
tar -xzf "$HOME/.devops/plugins/helm/devops.tar.gz" -C "$HOME/.devops/plugins/helm"
SPACE_CLI_ARTIFACT="devops-helm-plugin_${LATEST_RELEASE_TAG}_${OS}_${ARCH}.tar.gz"
DOWNLOAD_URL="${DOWNLOAD_BASE}/ui.tar.gz"
echo "Remove Old UI"
rm -rf $HOME/.devops/dist $HOME/.devops/ui.tar.gz
echo "Downloading UI"
wget -O "$HOME/.devops/ui.tar.gz" "${DOWNLOAD_URL}"
tar -xzvf "$HOME/.devops/ui.tar.gz" -C "$HOME/.devops"
}
installFile() {
echo "Starting installation... ${ARTIFACT_TMP_FILE}"
tar xf "$ARTIFACT_TMP_FILE" -C "$SPACE_TMP_ROOT"
mv "$SPACE_TMP_ROOT/devops-cli" "$SPACE_TMP_ROOT/$SPACE_CLI_FILENAME"
local tmp_root_space_cli="$SPACE_TMP_ROOT/$SPACE_CLI_FILENAME"
if [ ! -f "$tmp_root_space_cli" ]; then
echo "Failed to unpack Devops CLI executable."
exit 1
fi
chmod o+x $tmp_root_space_cli
echo "Removing old installation"
runAsRoot rm "$SPACE_CLI_INSTALL_DIR/devops"
runAsRoot cp "$tmp_root_space_cli" "$SPACE_CLI_INSTALL_DIR"
if [ -f "$SPACE_CLI_FILE" ]; then
echo "$SPACE_CLI_FILENAME installed into $SPACE_CLI_INSTALL_DIR successfully."
RED='\033[0;34m'
NC='\033[0m' # No Color
# echo -e "For enabling auto complete follow instructions provided by this command ${RED}space-cli completion --help${NC}"
# $SPACE_CLI_FILE --version
else
echo "Failed to install $SPACE_CLI_FILENAME"
exit 1
fi
}
fail_trap() {
result=$?
if [ "$result" != "0" ]; then
echo "Failed to install Devops CLI"
echo "For support, go to https://github.com/sharadregoti/devops-cli"
fi
cleanup
exit $result
}
cleanup() {
if [[ -d "${SPACE_TMP_ROOT:-}" ]]; then
rm -rf "SPACE_TMP_ROOT"
fi
}
installCompleted() {
echo -e "\nTo get started with Devops CLI, please visit https://github.com/sharadregoti/devops-cli"
}
# -----------------------------------------------------------------------------
# main
# -----------------------------------------------------------------------------
trap "fail_trap" EXIT
getSystemInfo
verifySupported
checkExistingDapr
checkHttpRequestCLI
getLatestRelease
downloadFile $ret_val
installFile
cleanup
installCompleted