-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
k8s-installer.sh
executable file
·107 lines (79 loc) · 3.19 KB
/
k8s-installer.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
#!/usr/bin/env bash
# vim ft=sh
#
# BASHMATIC EXAMPLES
#
# Kuberneties & Minikube downloader and installer.
[[ -d ${BASHMATIC_HOME} ]] || bash -c "$(curl -fsSL https://bashmatic.re1.re); bashmatic-install -v"
[[ -f ${BASHMATIC_HOME}/init.sh ]] || {
echo "Can't find or install Bashmatic. Exiting."
exit 1
}
source ${BASHMATIC_HOME}/init.sh
bashmatic.bash.exit-unless-version-four-or-later
k8s.ensure-no-brew() {
local package="$1"
[[ $(brew.package.is-installed "${package}") == "false" ]]
}
main() {
local kubectl_version="$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)"
local -A binaries=()
# To install additional binaries, add them to the associative array
binaries[kubectl]="https://storage.googleapis.com/kubernetes-release/release/${kubectl_version}/bin/darwin/amd64/kubectl"
binaries[minikube]="https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64"
output.has-stdin || {
h2 "This script downloads and installs several executables, such as: ${!binaries[*]}" \
"Binaries are downloaded into the /tmp folder" \
"${bldylw}Press any key to continue, or Ctrl-C to abort."
run.ui.press-any-key
}
# Print = lines...
local install_dir="/usr/local/bin"
local current_dir=$(pwd -P)
trap "cd ${current_dir}" EXIT
local package_count=0
for binary in "${!binaries[@]}"; do
local temp_binary="/tmp/${binary}"
hl.subtle Setting up ${binary}...
k8s.ensure-no-brew "${binary}" || {
error "It appears you have ${binary} already installed via Homebrew."
info "Please uninstall it first, and re-run this script."
return 1
}
local url=${binaries[${binary}]}
[[ -x ${binary} ]] || run "curl -L ${url} -o ${temp_binary}"
[[ -f ${temp_binary} ]] && run "chmod 755 ${temp_binary}"
# let's compare them
if [[ -f ${install_dir}/${binary} ]]; then
diff ${temp_binary} ${install_dir}/${binary} >/dev/null && {
info "Destination file ${bldylw}${install_dir}/${binary} is identical, skipping."
continue
}
fi
if [[ -f ${install_dir}/${binary} ]] ; then
# NOTE: this version command is specific to kubectl and minikube
local version="$( ${install_dir}/${binary} version --short=true 2>/dev/null | head -1 | tr -d '\d' | cut -d: -f2 | tr -d ' ')"
[[ -n ${version} ]] && info "Found previous version ${version}, moving to ${install_dir}/${binary}-${version}..."
[[ -z ${version} ]] && version="$(time.now.db)"
run "mv ${install_dir}/${binary} ${install_dir}/${binary}-${version} || true"
fi
run "mv ${temp_binary} ${install_dir}/${binary}"
# Let's validate they work
# In this case we rely on the fact that running -h with each command produces
# "<command> --help" string in the output
hr
set -e
echo
inf "verifying ${binary} is valid..."
${install_dir}/${binary} -h 2>&1 | grep -q "<command> --help" || {
not-ok:
error "It appears ${binary} did not get installed properly."
exit 1
}
ok:
echo; echo
package_count=$(( package_count + 1 ))
done
success "Install successful, ${package_count} binaries were installed in ${install_dir}..."
}
main "$@"