Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·303 lines (264 loc) · 8.35 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·303 lines (264 loc) · 8.35 KB
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
#!/bin/bash
#
# Install script
#
# Downloads the latest devbox release and installs its binary. Release builds
# of devbox are their own launcher: the installed binary picks the devbox
# version to run, downloads it if needed, and checks for updates.
set -euo pipefail
# ========================
# Customize install script
# ========================
# Users install devbox with:
# curl -fsSL https://raw.githubusercontent.com/jetify-com/devbox/main/install.sh | bash
#
# Everything is downloaded from GitHub releases. The latest stable version is
# whichever release GitHub marks as "latest" (never a draft or prerelease).
readonly INSTALL_DIR="/usr/local/bin"
readonly BIN="devbox"
# Must match releasesURL in internal/launcher/install.go.
readonly RELEASES_URL="https://github.com/jetify-com/devbox/releases"
# Must match the version files that devbox reads (internal/launcher/files.go).
readonly CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/${BIN}"
readonly TITLE="Devbox 📦 by Jetify"
readonly DESCRIPTION=$(
cat <<EOF
Instant, easy and predictable development environments.
This script downloads and installs the latest devbox binary.
EOF
)
readonly DOCS_URL="https://github.com/jetify-com/devbox"
readonly COMMUNITY_URL="https://discord.gg/jetify"
# ====================
# flags.sh
# ====================
FORCE="${FORCE:-0}"
parse_flags() {
while [ "$#" -gt 0 ]; do
case "$1" in
-f | --force)
FORCE=1
shift 1
;;
*)
error "Unknown option: $1"
exit 1
;;
esac
done
}
# ====================
# format.sh
# ====================
readonly BOLD="$(tput bold 2>/dev/null || echo '')"
readonly GREY="$(tput setaf 8 2>/dev/null || echo '')"
readonly UNDERLINE="$(tput smul 2>/dev/null || echo '')"
readonly RED="$(tput setaf 1 2>/dev/null || echo '')"
readonly GREEN="$(tput setaf 2 2>/dev/null || echo '')"
readonly YELLOW="$(tput setaf 3 2>/dev/null || echo '')"
readonly BLUE="$(tput setaf 4 2>/dev/null || echo '')"
readonly MAGENTA="$(tput setaf 5 2>/dev/null || echo '')"
readonly CYAN="$(tput setaf 6 2>/dev/null || echo '')"
readonly NO_COLOR="$(tput sgr0 2>/dev/null || echo '')"
readonly CLEAR_LAST_MSG="\033[1F\033[0K"
title() {
local -r text="$*"
printf "%s\n" "${BOLD}${MAGENTA}${text}${NO_COLOR}"
}
header() {
local -r text="$*"
printf "%s\n" "${BOLD}${text}${NO_COLOR}"
}
plain() {
local -r text="$*"
printf "%s\n" "${text}"
}
warn() {
printf "%s\n" "${YELLOW}! $*${NO_COLOR}"
}
error() {
local -r text="$*"
printf "%s\n" "${RED}✘ ${text}${NO_COLOR}" >&2
}
success() {
local -r text="$*"
printf "%s\n" "${GREEN}✓${NO_COLOR} ${text}"
}
start_task() {
local -r text="$*"
printf "%s\n" "${BOLD}${GREY}→${NO_COLOR} ${text}..."
}
end_task() {
local -r text="$*"
printf "${CLEAR_LAST_MSG}%s\n" "${GREEN}✓${NO_COLOR} ${text}... [DONE]"
}
fail_task() {
local -r text="$*"
printf "${CLEAR_LAST_MSG}%s\n" "${RED}✘ ${text}... [FAILED]${NO_COLOR}" >&2
}
confirm() {
if [ ${FORCE-} -ne 1 ]; then
printf "%s " "${MAGENTA}?${NO_COLOR} $* ${BOLD}[Y/n]${NO_COLOR}"
set +e
read -r yn </dev/tty
rc=$?
set -e
if [ $rc -ne 0 ]; then
error "Error reading from prompt (re-run with '-f' flag to auto select Yes if running in a script)"
exit 1
fi
if [ "$yn" != "y" ] && [ "$yn" != "Y" ] && [ "$yn" != "yes" ] && [ "$yn" != "" ]; then
error 'Aborting (please answer "yes" to continue)'
exit 1
fi
fi
}
# =========
# util.sh
# =========
has() {
command -v "$1" 1>/dev/null 2>&1
}
download() {
local -r url="$1"
local -r file="$2"
local cmd=""
if has curl; then
cmd="curl --fail --silent --location --output $file $url"
elif has wget; then
cmd="wget --quiet --output-document=$file $url"
elif has fetch; then
cmd="fetch --quiet --output=$file $url"
else
error "No program to download files found. Please install one of: curl, wget, fetch"
exit 1
fi
$cmd && return 0 || rc=$?
error "Command failed (exit code $rc): ${BLUE}${cmd}${NO_COLOR}"
exit $rc
}
sha256() {
if has sha256sum; then
sha256sum "$1" | cut -d ' ' -f 1
elif has shasum; then
shasum -a 256 "$1" | cut -d ' ' -f 1
else
error "No program to verify checksums found. Please install one of: sha256sum, shasum"
exit 1
fi
}
# platform prints the platform part of release archive names, such as
# darwin_arm64 or linux_amd64.
platform() {
local os arch
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
case "${arch}" in
x86_64 | amd64) arch="amd64" ;;
aarch64 | arm64) arch="arm64" ;;
armv7*) arch="armv7l" ;;
i386 | i686) arch="386" ;;
esac
case "${os}" in
linux | darwin) ;;
*)
error "Unsupported operating system: ${os}"
exit 1
;;
esac
printf '%s_%s' "${os}" "${arch}"
}
# ==============
# Implementation
# ==============
intro_msg() {
title "${TITLE}"
plain "${DESCRIPTION}"
printf "\n"
header "Confirm Installation Details"
plain " Location: ${GREEN}${INSTALL_DIR}/${BIN}${NO_COLOR}"
plain " Download URL: ${UNDERLINE}${BLUE}${RELEASES_URL}${NO_COLOR}"
printf "\n"
}
install_flow() {
confirm "Install ${GREEN}${BIN}${NO_COLOR} to ${GREEN}${INSTALL_DIR}${NO_COLOR} (requires sudo)?"
printf "\n"
header "Downloading and Installing"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "${TMP_DIR}"' EXIT
local -r tmp_dir="${TMP_DIR}"
# The latest release's checksums.txt names the latest version (in its
# archive names) and the checksum to verify, in one request.
start_task "Finding the latest version"
download "${RELEASES_URL}/latest/download/checksums.txt" "${tmp_dir}/checksums.txt"
local -r platform="$(platform)"
local version
version="$(awk -v suffix="_${platform}.tar.gz" '
index($2, "devbox_") == 1 && substr($2, length($2) - length(suffix) + 1) == suffix {
print substr($2, 8, length($2) - 7 - length(suffix)); exit
}' "${tmp_dir}/checksums.txt")"
if [[ ! ${version} =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
fail_task "Finding the latest version"
error "The latest release has no ${BIN} for ${platform}"
exit 1
fi
end_task "Finding the latest version (${version})"
local -r archive="${BIN}_${version}_${platform}.tar.gz"
start_task "Downloading ${BIN} ${version}"
download "${RELEASES_URL}/download/${version}/${archive}" "${tmp_dir}/${archive}"
end_task "Downloading ${BIN} ${version}"
start_task "Verifying checksum"
local -r expected="$(awk -v name="${archive}" '$2 == name { print $1 }' "${tmp_dir}/checksums.txt")"
local -r actual="$(sha256 "${tmp_dir}/${archive}")"
if [[ -z ${expected} || ${actual} != "${expected}" ]]; then
fail_task "Verifying checksum"
plain " Expected checksum (sha256): ${expected:-none}"
plain " Actual checksum (sha256): ${actual}"
exit 1
fi
end_task "Verifying checksum"
mkdir "${tmp_dir}/unpacked"
tar -xzof "${tmp_dir}/${archive}" -C "${tmp_dir}/unpacked"
chmod 755 "${tmp_dir}/unpacked/${BIN}"
start_task "Installing in ${INSTALL_DIR}/${BIN} (requires sudo)"
# Only use sudo when the install dir (or its closest existing parent, if
# it doesn't exist yet) isn't writable.
local writable_dir="${INSTALL_DIR}"
while [[ ! -e ${writable_dir} ]]; do
writable_dir="$(dirname "${writable_dir}")"
done
local sudo=""
if [[ ! -w ${writable_dir} ]]; then
sudo="$(command -v sudo || true)"
fi
# Copy next to the destination and rename, so that a devbox command that
# starts during the install never runs a partial binary.
${sudo} bash -c 'mkdir -p "$1" && cp "$2" "$1/.$3.tmp" && mv -f "$1/.$3.tmp" "$1/$3"' \
bash "${INSTALL_DIR}" "${tmp_dir}/unpacked/${BIN}" "${BIN}"
end_task "Installing in ${INSTALL_DIR}/${BIN}"
# Run the installed version from now on, and don't wait to check for a
# newer one on the first run.
if mkdir -p "${CACHE_DIR}" 2>/dev/null &&
printf '%s\n' "${version}" >"${CACHE_DIR}/current-version" &&
printf '%s\n' "${version}" >"${CACHE_DIR}/latest-version"; then
:
else
warn "Couldn't write ${CACHE_DIR}/current-version. ${BIN} will check for the latest version on its first run."
fi
success "${BOLD}Successfully installed ${GREEN}${BIN} ${version}${NO_COLOR}${BOLD}${NO_COLOR} 🚀"
printf "\n"
}
next_steps_msg() {
header "Next Steps"
plain " 1. ${BOLD}Learn how to use ${BIN}${NO_COLOR}"
plain " ${GREY}Run ${CYAN}${BIN} help${GREY} or read the docs at ${UNDERLINE}${BLUE}${DOCS_URL}${NO_COLOR}"
plain " 2. ${BOLD}Get help and give feedback${NO_COLOR}"
plain " ${GREY}Join our community at ${UNDERLINE}${BLUE}${COMMUNITY_URL}${NO_COLOR}"
}
main() {
parse_flags "$@"
intro_msg
install_flow
next_steps_msg
}
main "$@"