forked from rancher/ecm-distro-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·122 lines (101 loc) · 2.85 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
#!/bin/sh
set -e
TMP_DIR=""
REPO_NAME="ecm-distro-tools"
REPO_URL="https://github.com/rancher/${REPO_NAME}"
REPO_RELEASE_URL="${REPO_URL}/releases"
INSTALL_DIR="$HOME/.local/bin/ecm-distro-tools"
SUFFIX=""
DOWNLOADER=""
# setup_arch set arch and suffix fatal if architecture not supported.
setup_arch() {
case $(uname -m) in
x86_64|amd64)
ARCH=amd64
SUFFIX=$(uname -s | tr '[:upper:]' '[:lower:]')-${ARCH}
;;
aarch64|arm64)
ARCH=arm64
SUFFIX=$(uname -s | tr '[:upper:]' '[:lower:]')-${ARCH}
;;
*)
fatal "unsupported architecture ${ARCH}"
;;
esac
}
# setup_tmp creates a temporary directory and cleans up when done.
setup_tmp() {
TMP_DIR=$(mktemp -d -t ecm-distro-tools-install.XXXXXXXXXX)
cleanup() {
code=$?
set +e
trap - EXIT
rm -rf "${TMP_DIR}"
exit "$code"
}
trap cleanup INT EXIT
}
# verify_downloader verifies existence of network downloader executable.
verify_downloader() {
cmd="$(command -v "${1}")"
if [ -z "${cmd}" ]; then
return 1
fi
if [ ! -x "${cmd}" ]; then
return 1
fi
DOWNLOADER=${cmd}
return 0
}
# download downloads a file from a url using either curl or wget.
download() {
case "${DOWNLOADER}" in
*curl)
cd "$1" && { curl -fsSLO "$2" ; cd -; }
;;
*wget)
wget -qO "$1" "$2"
;;
esac
if [ $? -ne 0 ]; then
echo "error: download failed"
exit 1
fi
}
# download_tarball downloads the tarbal for the given version.
download_tarball() {
TARBALL_URL="${REPO_RELEASE_URL}/download/${RELEASE_VERSION}/ecm-distro-tools.${SUFFIX}.tar.gz"
echo "downloading tarball from ${TARBALL_URL}"
download "${TMP_DIR}" "${TARBALL_URL}"
}
# install_binaries installs the binaries from the downloaded tar.
install_binaries() {
cd "${TMP_DIR}"
tar -xf "${TMP_DIR}/ecm-distro-tools.${SUFFIX}.tar.gz"
rm "${TMP_DIR}/ecm-distro-tools.${SUFFIX}.tar.gz"
mkdir -p "${INSTALL_DIR}"
for f in * ; do
file_name="${f}"
if echo "${f}" | grep -q "${SUFFIX}"; then
file_name=${file_name%"-${SUFFIX}"}
fi
cp "${TMP_DIR}/${f}" "${INSTALL_DIR}/${file_name}"
done
}
{ # main
RELEASE_VERSION=$1
if [ -n "${ECM_VERSION}" ]; then
RELEASE_VERSION=${ECM_VERSION}
fi
if [ -z "$RELEASE_VERSION" ]; then
RELEASE_VERSION=$(basename "$(curl -Ls -o /dev/null -w %\{url_effective\} https://github.com/rancher/ecm-distro-tools/releases/latest)")
fi
echo "Installing ECM Distro Tools: ${RELEASE_VERSION}"
setup_tmp
setup_arch
verify_downloader curl || verify_downloader wget || fatal "error: cannot find curl or wget"
download_tarball
install_binaries
printf "Run command to access tools:\n\nPATH=%s:%s\n\n" "${PATH}" "${INSTALL_DIR}"
exit 0
}