forked from kanisterio/kanister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.sh
executable file
·155 lines (133 loc) · 4.43 KB
/
get.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
#!/usr/bin/env bash
# Copyright 2019 The Kanister Authors.
# This script is based off of the install script from helm, licensed under the
# Apache License, Version 2.0. The script was found here
# https://github.com/kubernetes/helm/blob/master/scripts/get
set -o errexit
set -o nounset
set -o xtrace
set -o pipefail
DIST_NAME="kanister"
BIN_NAMES=("kanctl")
RELEASES_URL="https://github.com/kanisterio/kanister/releases"
: ${KANISTER_INSTALL_DIR:="/usr/local/bin"}
# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5";;
armv6*) ARCH="armv6";;
armv7*) ARCH="armv7";;
aarch64) ARCH="arm64";;
x86) ARCH="386";;
x86_64) ARCH="amd64";;
i686) ARCH="386";;
i386) ARCH="386";;
esac
}
# initOS discovers the operating system for this system.
initOS() {
OS=$(uname | tr '[:upper:]' '[:lower:]')
case "$OS" in
# On linux we also support kando
linux) BIN_NAMES=("kanctl" "kando");;
# Minimalist GNU for Windows
mingw*) OS='windows';;
esac
}
# runs the given command as root (detects if we are root already)
runAsRoot() {
local cmd="$*"
if [ $EUID -ne 0 ]; then
cmd="sudo ${cmd}"
fi
${cmd}
}
# verifySupported checks that the os/arch combination is supported for
# binary builds.
verifySupported() {
local supported="\ndarwin-amd64\nlinux-amd64\nwindows-amd64"
if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
echo "No prebuilt binary for ${OS}-${ARCH}."
echo "To build from source, go to https://github.com/kanisterio/kanister"
exit 1
fi
local required_tools=("curl" "shasum")
for tool in "${required_tools[@]}"; do
if ! type "${tool}" > /dev/null; then
echo "${tool} is required"
exit 1
fi
done
}
# checkDesiredVersion checks if the desired version is available.
checkDesiredVersion() {
local version="${1}"
# Use the GitHub releases webpage for the project to find the desired version for this project.
local release_url="${RELEASES_URL}/tag/${version}"
local tag=$(curl -SsL ${release_url} | awk '/\/tag\//' | grep -v no-underline | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}')
if [ "x${tag}" == "x" ]; then
echo "Version tag ${version} not found."
exit 1
fi
}
# downloadFile downloads the binary and verifies the checksum.
downloadFile() {
local version="${1}"
local release_url="${RELEASES_URL}/download/${version}"
local kanister_dist="${DIST_NAME}_${version}_${OS}_${ARCH}.tar.gz"
local kanister_checksum="checksums.txt"
local download_url="${release_url}/${kanister_dist}"
local checksum_url="${release_url}/${kanister_checksum}"
KANISTER_TMP_ROOT="$(mktemp -dt kanister-installer-XXXXXX)"
KANISTER_TMP_FILE="${KANISTER_TMP_ROOT}/${kanister_dist}"
kanister_sum_file="${KANISTER_TMP_ROOT}/${kanister_checksum}"
echo "Downloading $download_url"
curl -SsL "${checksum_url}" -o "${kanister_sum_file}"
curl -SsL "${download_url}" -o "$KANISTER_TMP_FILE"
echo "Checking hash of ${kanister_dist}"
pushd "${KANISTER_TMP_ROOT}"
local filtered_checksum="./${kanister_dist}.sha256"
grep "${kanister_dist}" < "${kanister_checksum}" > "${filtered_checksum}"
shasum -a 256 -c "${filtered_checksum}"
popd
}
# installFile verifies the SHA256 for the file, then unpacks and
# installs it.
installFile() {
pushd "${KANISTER_TMP_ROOT}"
tar xvf "${KANISTER_TMP_FILE}"
rm "${KANISTER_TMP_FILE}"
echo "Preparing to install into ${KANISTER_INSTALL_DIR}"
for bin_name in "${BIN_NAMES[@]}"; do
runAsRoot cp "./${bin_name}" "${KANISTER_INSTALL_DIR}"
done
popd
}
# testBinaries tests the installed binaries make sure they're working.
testBinaries() {
for bin_name in "${BIN_NAMES[@]}"; do
echo "${bin_name} installed into ${KANISTER_INSTALL_DIR}/${bin_name}"
if ! type "${bin_name}" > /dev/null; then
echo "${bin_name} not found. Is ${KANISTER_INSTALL_DIR} on your PATH?"
exit 1
fi
done
}
cleanup() {
if [[ -d "${KANISTER_TMP_ROOT:-}" ]]; then
rm -rf "$KANISTER_TMP_ROOT"
fi
}
main() {
version="${1:-"0.26.0"}"
initArch
initOS
verifySupported
checkDesiredVersion "${version}"
downloadFile "${version}"
installFile
testBinaries
cleanup
}
main $@