forked from bottlerocket-os/bottlerocket-kernel-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-twoliter.sh
executable file
·183 lines (156 loc) · 5.3 KB
/
install-twoliter.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
#!/usr/bin/env bash
#
# Common error handling
#
exit_trap_cmds=()
on_exit() {
exit_trap_cmds+=( "$1" )
}
run_exit_trap_cmds() {
for cmd in "${exit_trap_cmds[@]}"; do
eval "${cmd}"
done
}
trap run_exit_trap_cmds EXIT
warn() {
>&2 echo "Warning: $*"
}
bail() {
if [[ $# -gt 0 ]]; then
>&2 echo "Error: $*"
fi
exit 1
}
usage() {
cat <<EOF
Usage: $0 -r GIT_REPO -v TWOLITER_VERSION -d INSTALL_DIR [-e REUSE_EXISTING] [-b BINARY_INSTALL] [ -s SOURCE_INSTALL ] [-h]
-r, --repo the git or GitHub repository from which to install. For source
install this can be any git repo, including a GitHub. For a binary
installation, this must be a GitHub repository that has binaries
attached to releases.
-v, --version the version (with the v prefix), or the git branch, sha or tag
-d, --directory the directory to install twoliter into
-e, --reuse-existing-install we will skip installation if we find the correct version installed
-b, --allow-binary-install we will try to install a GitHub release-attached binary if the
host we are on is Linux. Takes an expected sha256 sum for the
binary as input.
-s, --allow-from-source we will install from source using cargo install pointed to a git
repo and rev when binary install is either not allowed or not
possible
-k, --skip-version-check do not check to see if the installed version matches the one that
is requested by the --version argument. twoliter will not be
installed when the binary is present, regardless of what version
it is.
-h, --help show this help text
Example invocation:
This installs the twoliter program which is needed to build Bottlerocket
Example, installing binary and reusing-existing (if it exists):
$0 \\
-r https://github.com/bottlerocket-os/twoliter \\
-v v0.1.0 \\
-d tools/twoliter \\
-b \\
-e
Example, installing from source whether or not it is already installed:
$0 \\
-r https://github.com/myfork/twoliter \\
-v b0482f1 \\
-d tools/twoliter \\
-s
EOF
}
usage_error() {
>&2 usage
bail "$1"
}
#
# Parse arguments
#
while [[ $# -gt 0 ]]; do
case $1 in
-r|--repo)
shift; repo=$1 ;;
-v|--version)
shift; version=$1 ;;
-d|--directory)
shift; dir=$1 ;;
-e|--reuse-existing-install)
reuse_existing="true" ;;
-b|--allow-binary-install)
allow_bin="true"; shift; bin_checksum=$1 ;;
-s|--allow-from-source)
from_source="true" ;;
-k|--skip-version-check)
skip_version_check="true" ;;
-h|--help)
usage; exit 0 ;;
*)
usage_error "Invalid option '$1'" ;;
esac
shift
done
set -e
workdir="$(mktemp -d)"
on_exit "rm -rf ${workdir}"
mkdir -p "${dir}"
if [ "${reuse_existing}" = "true" ] ; then
if [ -x "${dir}/twoliter" ] ; then
if [ "${skip_version_check}" = "true" ]; then
echo "Twoliter binary found and --skip-version-check is true. Skipping install."
exit 0
fi
version_output="$("${dir}/twoliter" --version)"
found_version=v$(echo $version_output | awk '{print $2}')
echo "Found Twoliter ${found_version} installed."
if [ "${found_version}" = "${version}" ] ; then
echo "Skipping installation."
exit 0
fi
fi
fi
if [ "${allow_bin}" = "true" ] ; then
host_arch="$(uname -m)"
host_arch="${host_arch,,}"
host_kernel="$(uname -s)"
host_kernel="${host_kernel,,}"
case "${host_kernel}-${host_arch}" in
linux-x86_64 | linux-aarch64)
echo "Installing Twoliter from binary release."
twoliter_release="${repo}/releases/download/${version}"
twoliter_target="${host_arch}-unknown-${host_kernel}-musl"
cd "${workdir}"
curl -sSL "${twoliter_release}/twoliter-${twoliter_target}.tar.xz" -o "twoliter.tar.xz"
echo "Checking binary checksum..."
sha256sum -c <<< "${bin_checksum} twoliter.tar.xz"
tar xf twoliter.tar.xz
mv "./twoliter-${twoliter_target}/twoliter" "${dir}"
exit 0
;;
*)
echo "No pre-built binaries available for twoliter ${version}."
;;
esac
else
echo "Skipping binary installation of twoliter ${version} because --allow-binary-install was not set."
fi
if [ "${from_source}" = "true" ] ; then
echo "Installing Twoliter version ${version} from source"
cargo +nightly install \
-Z bindeps \
--locked \
--root "${workdir}" \
--git "${repo}" \
--rev "${version}" \
--bin twoliter \
--quiet \
twoliter
mv "${workdir}/bin/twoliter" "${dir}/twoliter"
echo "Installed twoliter ${version} from source."
exit 0
else
echo "Skipped installing twoliter ${version} from source."
fi
if [ ! -x "${dir}/twoliter" ] ; then
echo "Could not install twoliter ${version}" >&2
exit 1
fi