This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.sh
executable file
·464 lines (397 loc) · 14.4 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/bin/sh
# shellcheck disable=SC2039,SC3043
# ... we use 'local' as covered below (just beneath the license)
# ... SC2039 became SC3043 for the case of local
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 get, for 'latest':
# https://github.com/connecteverything/ngs-cli/releases/download/v0.11.0/ngs-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.
# https://github.com/connecteverything/ngs-cli/releases/latest/download/ngs-linux-amd64.zip ->
# * https://github.com/connecteverything/ngs-cli/releases/download/v0.11.0/ngs-linux-amd64.zip ->
# <s3-bucket-url-with-auth-query-params>
# 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_NGS='connecteverything/ngs-cli'
readonly GITHUB_OWNER_REPO_NSC='nats-io/nsc'
readonly HTTP_USER_AGENT='ngs_install/0.3 (@ConnectEverything)'
# Where to install to, relative to home-dir; both nsc and ngs will go here
readonly RELATIVE_BIN_DIR='.nsc/bin'
# Binary name we are looking for (might have .exe extension on some platforms)
readonly NGS_BINARY_BASENAME='ngs'
readonly NSC_BINARY_BASENAME='nsc'
# Explanations for commands
readonly PURPOSE_NSC='is used to edit, view and deploy NATS security JWTS.'
readonly PURPOSE_NGS='can be used to signup for the Synadia global service and manage your billing plan.'
if [ -n "${ZSH_VERSION:-}" ]; then
# We mostly just need to unset LOCAL_TRAPS but we might as well
# emulate sh more fully.
emulate sh
fi
progname="$(basename "$0" .sh)"
note() { printf >&2 '%s: %s\n' "$progname" "$*"; }
die() { note "$@"; exit 1; }
usage() {
local ev="${1:-1}"
[ "$ev" = 0 ] || exec >&2
cat <<EOUSAGE
Usage: $progname [-N <tag>] [-G <tag>] [-d <dir>] [-s <dir>]
-d dir directory to download into [default: ~/$RELATIVE_BIN_DIR]
-s dir directory in which to place a symlink to the binary
[default: ~/bin] [use '-' to forcibly not place a symlink]
-N tag retrieve a tagged release of NSC instead of the latest
-G tag retrieve a tagged release of NGS instead of the latest
-a arch force choosing a specific processor architecture [allowed: $SUPPORTED_ARCHS]
EOUSAGE
exit "$ev"
}
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
# This makes a global $ZIPDIR_PARENT and registers an exit trap:
make_temp_zipdir_parent
# 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"
download_and_install "NGS" "$NGS_BINARY_BASENAME" "$GITHUB_OWNER_REPO_NGS" "$opt_tag_ngs"
download_and_install "NSC" "$NSC_BINARY_BASENAME" "$GITHUB_OWNER_REPO_NSC" "$opt_tag_nsc"
show_instructions \
"$NSC_BINARY_BASENAME" "$PURPOSE_NSC" \
"$NGS_BINARY_BASENAME" "$PURPOSE_NGS"
}
opt_tag_nsc=''
opt_tag_ngs=''
opt_install_dir=''
opt_symlink_dir=''
opt_arch=''
parse_options() {
while getopts ':a:d:hs:G:N:' arg; do
case "$arg" in
(h) usage 0 ;;
(d) opt_install_dir="$OPTARG" ;;
(s) opt_symlink_dir="$OPTARG" ;;
(N) opt_tag_nsc="$OPTARG" ;;
(G) opt_tag_ngs="$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:?}/${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
}
download_and_install() {
local bin_label bin_basename github_owner_repo explicit_release_tag
bin_label="${1:?}"
bin_basename="${2:?}"
github_owner_repo="${3:?}"
# the release tag can be empty, so '?' not ':?' :
explicit_release_tag="${4?}"
zipfile_url="$(determine_zip_download_url "$bin_basename" "$github_owner_repo" "$explicit_release_tag")"
[ -n "${zipfile_url}" ] || die "unable to determine a download URL"
want_filename="$(exe_filename_per_os "$bin_basename")"
# The unzip command does not work well with piped stdin, we need to have
# the complete zip-file on local disk.
zip_dir="$ZIPDIR_PARENT/$bin_basename"
mkdir -- "$zip_dir"
stage_zipfile="${zip_dir}/$(zip_filename_per_os "$bin_basename")"
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_one_command "$bin_label" "$want_filename"
}
# SIDE-EFFECTS:
# 1. Sets $ZIPDIR_PARENT global
# 2. Registers traps to clean up that directory
make_temp_zipdir_parent() {
old_umask="$(umask)"
umask 077
# This is about as sane as can be managed to be as portable as possible:
ZIPDIR_PARENT="$(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.
# We explicitly want to expand the variable now, shellcheck
# shellcheck disable=SC2064
trap "rm -rf -- '${ZIPDIR_PARENT}'" EXIT
}
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
# shellcheck disable=SC2018,SC2019
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
# shellcheck disable=SC2086
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() {
local binname="${1:?}"
# 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="${binname}-$(normalized_ostype)-$(normalized_arch).zip" || exit $?
printf '%s\n' "${zipname}"
}
exe_filename_per_os() {
local fn="${1:?}"
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 binname owner_repo
binname="${1:?}"
owner_repo="${2:?}"
explicit_tag="${3:-}"
local want_filename
want_filename="$(zip_filename_per_os "$binname")"
if [ -n "$explicit_tag" ]; then
printf 'https://github.com/%s/releases/download/%s/%s\n' \
"$owner_repo" "$explicit_tag" "$want_filename"
else
printf 'https://github.com/%s/releases/latest/download/%s\n' \
"$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
# We are explicitly doing splitting to the only array available in portable
# shell, so yes shellcheck, unquoted is correct
# shellcheck disable=SC2086
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"
echo
return 0
fi
note "Symlink dir '$symdir' is not in your PATH?"
echo
return 1
}
link_one_command() {
local label="${1:?need a label}"
local new_cmd="${2:?need a command which has been installed}"
local target="$opt_install_dir/$new_cmd"
echo
note "${label}: ${target}"
ls -ld -- "$target"
echo
if [ -n "$opt_symlink_dir" ]; then
if maybe_make_symlink "$target" "$opt_symlink_dir" "$new_cmd"
then
return 0
fi
fi
}
show_instructions() {
local cmd purpose
local show_symlink_alternative=false
local show_path_bits=true
printf '\nThe following commands have been installed to %s:\n\n' "$opt_install_dir"
while [ $# -gt 0 ]; do
cmd="${1:?}"
purpose="${2:?}"
shift 2
printf ' %s %s\n' "$cmd" "$purpose"
done
if [ -n "$opt_symlink_dir" ] && [ -d "$opt_symlink_dir" ]; then
if dir_is_in_PATH "$opt_symlink_dir"; then
printf '\nNo PATH manipulation should be needed: symlinks have been put into\n'
printf 'a dir already in your PATH: %s\n' "$opt_symlink_dir"
show_path_bits=false
else
show_symlink_alternative=true
fi
fi
if $show_path_bits; then
# We explicitly don't want to expand the $ sign
# shellcheck disable=SC2016
printf '\nTo add these commands to your $PATH:\n'
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
fi
if $show_symlink_alternative; then
printf '\nAlternatively, ensure that %s is in your PATH\nas symlinks has been placed there.' \
"$opt_symlink_dir"
fi
cat <<'EOTRAILER'
If successful, invoke each command with -h to see help options.
Learn more about signing up at synadia.com.
EOTRAILER
}
main "$@"