-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathinstall.sh
executable file
·360 lines (310 loc) · 11 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/sh
set -eu
# Copyright 2020-2022 The NATS Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# We are sh, not bash; we might want bash/zsh for associative arrays but some
# OSes are currently on bash3 and removing bash, while we don't want a zsh
# dependency; so we're sticking to "pretty portable shell" even if it's a
# little more convoluted as a result.
#
# We rely upon the following beyond basic POSIX shell:
# 1. A `local` command (built-in to shell; almost all sh does have this)
# 2. A `curl` command (to download files)
# 3. An `unzip` command (to extract content from .zip files)
# 4. A `mktemp` command (to stage a downloaded zip-file)
# We rely upon naming conventions for release assets from GitHub to avoid
# having to parse JSON from their API, to avoid a dependency upon jq(1).
#
# <https://help.github.com/en/github/administering-a-repository/linking-to-releases>
# guarantees that:
# /<owner>/<name>/releases/latest/download/<asset-name>.zip
# will be available; going via the API we got, for 'latest':
# https://github.com/nats-io/nsc/releases/download/0.4.0/nsc-linux-amd64.zip
# ie:
# /<owner>/<name>/releases/download/<release>/<asset-name>.zip
# Inspecting headers, the documented guarantee redirects to the API-returned
# URL, which redirects to the S3 bucket download URL.
# This is a list of the architectures we support, which should be listed in
# the Go architecture naming format.
readonly SUPPORTED_ARCHS="amd64 arm64"
# Finding the releases to download
readonly GITHUB_OWNER_REPO='nats-io/nsc'
readonly HTTP_USER_AGENT='nsc_install/0.2 (@nats-io)'
# Where to install to, relative to home-dir
readonly NSC_RELATIVE_BIN_DIR='.nsccli/bin'
# Binary name we are looking for (might have .exe extension on some platforms)
readonly NSC_BINARY_BASENAME='nsc'
progname="$(basename "$0" .sh)"
note() { printf >&2 '%s: %s\n' "$progname" "$*"; }
die() { note "$@"; exit 1; }
main() {
parse_options "$@"
shift $((OPTIND - 1))
# error early if missing commands; put it after option processing
# so that if we need to, we can add options to handle alternatives.
check_have_external_commands
# mkdir -m does not set permissions of parents; -v is not portable
# We don't create anything private, so stick to inherited umask.
mkdir -p -- "$opt_install_dir"
zipfile_url="$(determine_zip_download_url)"
[ -n "${zipfile_url}" ] || die "unable to determine a download URL"
want_filename="$(exe_filename_per_os)"
# The unzip command does not work well with piped stdin, we need to have
# the complete zip-file on local disk. The portability of mktemp(1) is
# an unpleasant situation.
# This is the sanest way to get a temporary directory which only we can
# even look inside.
old_umask="$(umask)"
umask 077
zip_dir="$(mktemp -d 2>/dev/null || mktemp -d -t 'ziptmpdir')" || \
die "failed to create a temporary directory with mktemp(1)"
umask "$old_umask"
# POSIX does not give rm(1) a `-v` flag.
trap "rm -rf -- '${zip_dir}'" EXIT
stage_zipfile="${zip_dir}/$(zip_filename_per_os)"
note "Downloading <${zipfile_url}>"
curl_cmd --progress-bar --location --output "$stage_zipfile" "$zipfile_url"
note "Extracting ${want_filename} from $stage_zipfile"
# But unzip(1) does not let us override permissions and it does not obey
# umask so the file might now exist with overly broad permissions, depending
# upon whether or not the local environment has a per-user group which was
# used. We don't know that the extracting user wants everyone else in their
# current group to be able to write to the file.
# So: extract into the temporary directory, which we've forced via umask to
# be self-only, chmod while it's safe in there, and then move it into place.
# -b is not in busybox, so we rely on unzip handling binary safely
# -j junks paths inside the zipfile; none expected, enforce that
unzip -j -d "$zip_dir" "$stage_zipfile" "$want_filename"
chmod 0755 "$zip_dir/$want_filename"
# prompt the user to overwrite if need be
mv -i -- "$zip_dir/$want_filename" "$opt_install_dir/./"
link_and_show_instructions "$want_filename"
}
usage() {
local ev="${1:-1}"
[ "$ev" = 0 ] || exec >&2
cat <<EOUSAGE
Usage: $progname [-t <tag>] [-d <dir>] [-s <dir>]
-d dir directory to download into [default: ~/$NSC_RELATIVE_BIN_DIR]
-s dir directory in which to place a symlink to the binary
[default: ~/bin] [use '-' to forcibly not place a symlink]
-t tag retrieve a tagged release instead of the latest
-a arch force choosing a specific processor architecture [allowed: $SUPPORTED_ARCHS]
EOUSAGE
exit "$ev"
}
opt_tag=''
opt_install_dir=''
opt_symlink_dir=''
opt_arch=''
parse_options() {
while getopts ':a:d:hs:t:' arg; do
case "$arg" in
(h) usage 0 ;;
(d) opt_install_dir="$OPTARG" ;;
(s) opt_symlink_dir="$OPTARG" ;;
(t) opt_tag="$OPTARG" ;;
(a)
if validate_arch "$OPTARG"; then
opt_arch="$OPTARG"
else
die "unsupported arch for -a, try one of: $SUPPORTED_ARCHS"
fi ;;
(:) die "missing required option for -$OPTARG; see -h for help" ;;
(\?) die "unknown option -$OPTARG; see -h for help" ;;
(*) die "unhandled option -$arg; CODE BUG" ;;
esac
done
if [ "$opt_install_dir" = "" ]; then
opt_install_dir="${HOME:?}/${NSC_RELATIVE_BIN_DIR}"
fi
if [ "$opt_symlink_dir" = "" ] && [ -d "$HOME/bin" ]; then
opt_symlink_dir="$HOME/bin"
elif [ "$opt_symlink_dir" = "-" ]; then
opt_symlink_dir=""
fi
}
check_have_external_commands() {
local cmd
# Only those commands which take --help :
for cmd in curl unzip
do
"$cmd" --help >/dev/null || die "missing command: $cmd"
done
# Our invocation of mktemp has to handle multiple variants; if that's not
# installed, let it fail later.
test -e /dev/stdin || die "missing device /dev/stdin"
}
normalized_ostype() {
local ostype
# We only need to worry about ASCII here
ostype="$(uname -s | tr A-Z a-z)"
case "$ostype" in
(*linux*) ostype="linux" ;;
(win32) ostype="windows" ;;
(ming*_nt) ostype="windows" ;;
esac
printf '%s\n' "$ostype"
}
validate_arch() {
local check="$1"
local x
# Deliberately not quoted, setting $@ within this function
set $SUPPORTED_ARCHS
for x; do
if [ "$x" = "$check" ]; then
return 0
fi
done
return 1
}
normalized_arch() {
# We are normalising to the Golang nomenclature, which is how the binaries are released.
# The main ones are: amd64 arm64
# There is no universal standard here. Go's is as good as any.
# Command-line flag is the escape hatch.
if [ -n "${opt_arch:-}" ]; then
printf '%s\n' "$opt_arch"
return 0
fi
# Beware `uname -m` vs `uname -p`.
# Nominally, -m is machine, -p is processor. But what does this mean in practice?
# In practice, -m tends to be closer to the absolute truth of what the CPU is,
# while -p is adaptive to personality, binary type, etc.
# On Alpine Linux inside Docker, -p can fail `unknown` while `-m` works.
#
# uname -m uname -p
# Darwin/x86 x86_64 i386
# Darwin/M1 arm64 arm
# Alpine/docker x86_64 unknown
# Ubuntu/x86/64b x86_64 x86_64
# RPi 3B Linux armv7l unknown (-m depends upon boot flags & kernel)
#
# SUSv4 requires that uname exist and that it have the -m flag, but does not document -p.
local narch
narch="$(uname -m)"
case "$narch" in
(x86_64) narch="amd64" ;;
(amd64) true ;;
(aarch64) narch="arm64" ;;
(arm64) true ;;
(*) die "Unhandled architecture '$narch', use -a flag to select a supported arch" ;;
esac
if validate_arch "$narch"; then
printf '%s\n' "$narch"
else
die "Unhandled architecture '$narch', use -a flag to select a supported arch"
fi
}
zip_filename_per_os() {
# We break these out into a separate variable instead of passing directly
# to printf, so that if there's a failure in normalization then the printf
# won't swallow the exit status of the $(...) subshell and will instead
# abort correctly.
local zipname
zipname="nsc-$(normalized_ostype)-$(normalized_arch).zip" || exit $?
printf '%s\n' "${zipname}"
}
exe_filename_per_os() {
local fn="$NSC_BINARY_BASENAME"
case "$(normalized_ostype)" in
(windows) fn="${fn}.exe" ;;
esac
printf '%s\n' "$fn"
}
curl_cmd() {
curl --user-agent "$HTTP_USER_AGENT" "$@"
}
determine_zip_download_url() {
local want_filename download_url
want_filename="$(zip_filename_per_os)"
if [ -n "$opt_tag" ]; then
printf 'https://github.com/%s/releases/download/%s/%s\n' \
"$GITHUB_OWNER_REPO" "$opt_tag" "$want_filename"
else
printf 'https://github.com/%s/releases/latest/download/%s\n' \
"$GITHUB_OWNER_REPO" "$want_filename"
fi
}
dir_is_in_PATH() {
local needle="$1"
local oIFS="$IFS"
local pathdir
case "$(normalized_ostype)" in
(windows) IFS=';' ;;
(*) IFS=':' ;;
esac
set $PATH
IFS="$oIFS"
for pathdir
do
if [ "$pathdir" = "$needle" ]; then
return 0
fi
done
return 1
}
# Returns true if no further installation instructions are needed;
# Returns false otherwise.
maybe_make_symlink() {
local target="${1:?need a file to link to}"
local symdir="${2:?need a directory within which to create a symlink}"
local linkname="${3:?need a name to give the symlink}"
if ! [ -d "$symdir" ]; then
note "skipping symlink because directory does not exist: $symdir"
return 1
fi
# ln(1) `-v` is busybox but is not POSIX
if ! ln -sf -- "$target" "$symdir/$linkname"
then
note "failed to create a symlink in: $symdir"
return 1
fi
ls -ld -- "$symdir/$linkname"
if dir_is_in_PATH "$symdir"; then
note "Symlink dir '$symdir' is already in your PATH"
return 0
fi
note "Symlink dir '$symdir' is not in your PATH?"
return 1
}
link_and_show_instructions() {
local new_cmd="${1:?need a command which has been installed}"
local target="$opt_install_dir/$new_cmd"
echo
note "NSC: $target"
ls -ld -- "$target"
if [ -n "$opt_symlink_dir" ]; then
if maybe_make_symlink "$target" "$opt_symlink_dir" "$new_cmd"
then
return 0
fi
fi
echo
printf 'Now manually add %s to your $PATH\n' "$opt_install_dir"
case "$(normalized_ostype)" in
(windows) cat <<EOWINDOWS ;;
Windows Cmd Prompt Example:
setx path %path;"${opt_install_dir}"
EOWINDOWS
(*) cat <<EOOTHER ;;
Bash Example:
echo 'export PATH="\${PATH}:${opt_install_dir}"' >> ~/.bashrc
source ~/.bashrc
Zsh Example:
echo 'path+=("${opt_install_dir}")' >> ~/.zshrc
source ~/.zshrc
EOOTHER
esac
}
main "$@"