-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·447 lines (411 loc) · 13 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/bin/bash
# Created by adamkoro
#
# Variables default values:
# HELM_VERSION="3.11.3"
# KUBENS_VERSION="0.9.4"
# KUBECTX_VERSION="0.9.4"
# KUBECOLOR_VERSION="0.0.21"
# Check variables if empty
function check_variables() {
if [[ -z "${HELM_VERSION}" ]]; then
HELM_VERSION="3.11.3"
fi
if [[ -z "${KUBENS_VERSION}" ]]; then
KUBENS_VERSION="0.9.4"
fi
if [[ -z "${KUBECTX_VERSION}" ]]; then
KUBECTX_VERSION=${KUBENS_VERSION}
fi
if [[ -z "${KUBECOLOR_VERSION}" ]]; then
KUBECOLOR_VERSION="0.0.21"
fi
if [[ -z "${TMP_DIR}" ]]; then
TMP_DIR="$(mktemp -d)"
fi
}
# Simple message
function message() {
echo -e "\n--------------"
echo "|${1}"
echo "--------------"
}
# Info message
function info() {
echo "INFO: ${1}"
}
# Error message
function error() {
echo "ERROR: ${1}"
exit 1
}
# Check system architecture
function check_arch() {
if [[ $(uname -m) != "x86_64" ]]; then
error "This script only supports x86_64 architecture"
fi
}
# Check if the system is Linux
function check_os() {
if [[ $(uname -s) != "Linux" ]]; then
error "This script only supports Linux"
fi
}
# Check curl
function check_curl() {
if (! command -v curl &> /dev/null); then
error "curl could not be found"
fi
}
# Check tar
function check_tar() {
if (! command -v tar &> /dev/null); then
error "tar could not be found"
fi
}
# Check install
function check_install() {
if (! command -v install &> /dev/null); then
error "install could not be found"
fi
}
# Check sha256sum
function check_sha256sum() {
if (! command -v sha256sum &> /dev/null); then
error "sha256sum could not be found"
fi
}
# Check if the user is root
function check_root() {
if [[ $EUID -ne 0 ]]; then
error "This script must be run as root"
fi
}
# Download component
function download_component() {
info "Downloading ${1}"
CR_DIR=$(pwd)
cd "${TMP_DIR}" || exit
if (curl -LO "${2}"); then
info "${1} downloaded successfully"
else
error "Could not download ${1}"
fi
cd "${CR_DIR}" || exit
}
# Check sha256sum
function check_sum() {
info "Downloading sha256sum"
if (curl -L "${2}" -o "${TMP_DIR}/${1}_checksum"); then
info "sha256sum downloaded successfully"
else
error "Could not download sha256sum"
fi
info "Checking sha256sum"
CR_DIR=$(pwd)
cd "${TMP_DIR}" || exit
# If checksum file contains the component name
if (grep -q "${1}" "${TMP_DIR}/${1}_checksum"); then
info "sha256sum file contains ${1}"
if (sha256sum --quiet --ignore-missing -c "${TMP_DIR}/${1}_checksum"); then
info "sha256sum checked successfully on ${1}"
else
error "Could not check sha256sum, probably the file is corrupted"
fi
else
info "sha256sum file does not contain ${1}"
if (echo "$(cat "${TMP_DIR}/${1}_checksum") ${1} | sha256sum --heck" &> /dev/null); then
info "sha256sum checked successfully on ${1}"
else
error "Could not check sha256sum, probably the file is corrupted"
fi
fi
cd "${CR_DIR}" || exit
}
# Install binary
function install_files() {
info "Installing ${1}"
if (install -o root -g root -m 0755 "${TMP_DIR}/${1}" /usr/local/bin/"${1##*/}"); then
info "${1} installed successfully"
else
error "Could not install ${1}"
fi
}
# Unpack tar.gz
function uncompress_file() {
info "Unpacking ${1}"
if (tar -xzf "${TMP_DIR}/${1}" -C "${TMP_DIR}"); then
info "${1} uncompressed successfully"
else
error "Could not uncompress ${1}"
fi
}
# Clean up
function clean_up() {
info "Cleaning up"
if (rm -rf "${TMP_DIR}"); then
info "Cleaned up successfully"
else
error "Could not clean up"
fi
}
# Check installed component
function check_installed_component() {
if (command -v "${1}" &> /dev/null); then
error "${1} is already installed"
fi
}
# Install kubectl component
function install_kubectl() {
check_installed_component "kubectl"
download_component "kubectl" https://storage.googleapis.com/kubernetes-release/release/"$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)"/bin/linux/amd64/kubectl
check_sum "kubectl" https://storage.googleapis.com/kubernetes-release/release/"$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)"/bin/linux/amd64/kubectl.sha256
install_files "kubectl"
}
# Install helm component
function install_helm() {
check_installed_component "helm"
download_component "helm" "https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz"
check_sum "helm" "https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz.sha256"
uncompress_file "helm-v${HELM_VERSION}-linux-amd64.tar.gz"
install_files linux-amd64/helm
}
# Install kubens component
function install_kubens() {
check_installed_component "kubens"
download_component "kubens" "https://github.com/ahmetb/kubectx/releases/download/v${KUBENS_VERSION}/kubens_v${KUBENS_VERSION}_linux_x86_64.tar.gz"
check_sum "kubens" "https://github.com/ahmetb/kubectx/releases/download/v${KUBENS_VERSION}/checksums.txt"
uncompress_file "kubens_v${KUBENS_VERSION}_linux_x86_64.tar.gz"
install_files "kubens"
}
# Install kubectx component
function install_kubectx() {
check_installed_component "kubectx"
download_component "kubectx" "https://github.com/ahmetb/kubectx/releases/download/v${KUBECTX_VERSION}/kubectx_v${KUBECTX_VERSION}_linux_x86_64.tar.gz"
check_sum "kubectx" "https://github.com/ahmetb/kubectx/releases/download/v${KUBECTX_VERSION}/checksums.txt"
uncompress_file "kubectx_v${KUBECTX_VERSION}_linux_x86_64.tar.gz"
install_files "kubectx"
}
# Install kubecolor component
function install_kubecolor() {
check_installed_component "kubecolor"
download_component "kubecolor" "https://github.com/kubecolor/kubecolor/releases/download/v${KUBECOLOR_VERSION}/kubecolor_${KUBECOLOR_VERSION}_Linux_x86_64.tar.gz"
check_sum "kubecolor" "https://github.com/kubecolor/kubecolor/releases/download/v${KUBECOLOR_VERSION}/checksums.txt"
uncompress_file "kubecolor_${KUBECOLOR_VERSION}_Linux_x86_64.tar.gz"
install_files "kubecolor"
}
# Remove component
function remove_component() {
info "Removing ${1}"
if (rm -f /usr/local/bin/"${1}"); then
info "${1} removed successfully"
else
error "Could not remove ${1}"
fi
}
# Install kubectl completion
function install_kubectl_completion() {
info "Installing kubectl completion"
if (kubectl completion bash > /etc/bash_completion.d/kubectl); then
info "kubectl completion installed successfully"
else
error "Could not install kubectl completion"
fi
}
# Install helm completion
function install_helm_completion() {
info "Installing helm completion"
if (helm completion bash > /etc/bash_completion.d/helm); then
info "helm completion installed successfully"
else
error "Could not install helm completion"
fi
}
# Auto complete for kubecolor
function install_kubecolor_completion() {
info "Auto complete for kubecolor"
if (echo "complete -F __start_kubectl k" >> ~/.bashrc); then
info "Auto complete for kubecolor successfully"
else
error "Could not auto complete for kubecolor"
fi
}
# Alias k as kubecolor
function alias_kubecolor() {
info "Alias k as kubecolor"
if (echo "alias k='kubecolor'" >> ~/.bashrc); then
info "Alias k as kubecolor successfully"
else
error "Could not alias k as kubecolor"
fi
}
# Help
function help() {
echo "Usage: install.sh [OPTION] [COMPONENT]"
echo "Install kubectl, helm, kubens, kubectx and kubecolor"
echo ""
echo "Options:"
echo " -i, --install install component"
echo " -r, --remove remove component"
echo " -h, --help display this help and exit"
echo " -v, --version display version information and exit"
echo ""
echo "Components:"
echo " -a, --all install all components"
echo " kubectl install kubectl"
echo " helm install helm"
echo " kubens install kubens"
echo " kubectx install kubectx"
echo " kubecolor install kubecolor"
echo ""
echo "Examples:"
echo "Install kubectl: install.sh -i kubectl"
echo "Install all the components: install.sh -i --all"
echo "Remove kubectl: install.sh -r kubectl"
echo "Help message: install.sh -h"
echo "Version: install.sh -v"
echo ""
exit 0
}
# Version
function version() {
echo "Version: 1.0.0"
exit 0
}
# Check dependencies
function check_dependencies() {
check_arch
check_os
check_curl
check_tar
check_install
check_sha256sum
check_root
}
if [[ "$#" -eq 0 ]]; then
help
fi
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help)
help
;;
-v|--version)
version
;;
-i | --install)
check_dependencies
check_variables
COMPONENT_NAME="$2"
case $2 in
-a|--all)
message "Installing kubectl"
install_kubectl
message "Installing helm"
install_helm
message "Installing kubens"
install_kubens
message "Installing kubectx"
install_kubectx
message "Installing kubecolor"
install_kubecolor
message "Setting up aliases and bash compleations"
install_kubectl_completion
install_kubecolor_completion
alias_kubecolor
clean_up
exit 0
;;
kubectl)
message "Installing kubectl"
install_kubectl
clean_up
exit 0
;;
helm)
message "Installing helm"
install_helm
clean_up
exit 0
;;
kubens)
message "Installing kubens"
install_kubens
clean_up
exit 0
;;
kubectx)
message "Installing kubectx"
install_kubectx
clean_up
exit 0
;;
kubecolor)
message "Installing kubecolor"
install_kubecolor
clean_up
exit 0
;;
*)
echo "ERROR: Invalid component option: ${2}"
echo "Use -h or --help to see the available options"
exit 1
;;
esac
;;
-r | --remove)
check_dependencies
COMPONENT_NAME="$2"
case $2 in
-a|--all)
message "Removing kubectl"
remove_component kubectl
message "Removing helm"
remove_component helm
message "Removing kubens"
remove_component kubens
message "Removing kubectx"
remove_component kubectx
message "Removing kubecolor"
remove_component kubecolor
exit 0
;;
kubectl)
message "Removing kubectl"
remove_component "${COMPONENT_NAME}"
exit 0
;;
helm)
message "Removing helm"
remove_component "${COMPONENT_NAME}"
exit 0
;;
kubens)
message "Removing kubens"
remove_component "${COMPONENT_NAME}"
exit 0
;;
kubectx)
message "Removing kubectx"
remove_component "${COMPONENT_NAME}"
exit 0
;;
kubecolor)
message "Removing kubecolor"
remove_component "${COMPONENT_NAME}"
exit 0
;;
*)
echo "ERROR: Invalid component option: ${2}"
echo "Use -h or --help to see the available options"
exit 1
;;
esac
;;
*)
echo "ERROR: Invalid option: ${1}"
echo "Use -h or --help to see the available options"
exit 1
;;
esac
done