-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
482 lines (420 loc) · 11.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/bash
set -u
abort() {
printf "%s\n" "$@" >&2
exit 1
}
# Fail fast with a concise message when not using bash
# Single brackets are needed here for POSIX compatibility
if [ -z "${BASH_VERSION:-}" ]
then
abort "Bash is required to interpret this script."
fi
# Check if script is run with force-interactive mode in CI
if [[ -n "${CI-}" && -n "${INTERACTIVE-}" ]]
then
abort "Cannot run force-interactive mode in CI."
fi
# Check if both `INTERACTIVE` and `NONINTERACTIVE` are set
# Always use single-quoted strings with `exp` expressions
if [[ -n "${INTERACTIVE-}" && -n "${NONINTERACTIVE-}" ]]
then
abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.'
fi
# Check if script is run in POSIX mode
if [[ -n "${POSIXLY_CORRECT+1}" ]]
then
abort 'Bash must not run in POSIX mode. Please unset POSIXLY_CORRECT and try again.'
fi
# string formatters
if [[ -t 1 ]]
then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"
do
printf " "
printf "%s" "${arg// /\ }"
done
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
ohai() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
warn() {
printf "${tty_red}Warning${tty_reset}: %s\n" "$(chomp "$1")"
}
# Check if script is run non-interactively (e.g. CI)
# If it is run non-interactively we should not prompt for passwords.
# Always use single-quoted strings with `exp` expressions
if [[ -z "${NONINTERACTIVE-}" ]]
then
if [[ -n "${CI-}" ]]
then
warn 'Running in non-interactive mode because `$CI` is set.'
NONINTERACTIVE=1
elif [[ ! -t 0 ]]
then
if [[ -z "${INTERACTIVE-}" ]]
then
warn 'Running in non-interactive mode because `stdin` is not a TTY.'
NONINTERACTIVE=1
else
warn 'Running in interactive mode despite `stdin` not being a TTY because `$INTERACTIVE` is set.'
fi
fi
else
ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.'
fi
# USER isn't always set so provide a fall back for the installer and subprocesses.
if [[ -z "${USER-}" ]]
then
USER="$(chomp "$(id -un)")"
export USER
fi
# First check OS.
OS="$(uname)"
if [[ "${OS}" == "Linux" ]]
then
LIFERAY_CLI_ON_LINUX=1
elif [[ "${OS}" == "Darwin" ]]
then
LIFERAY_CLI_ON_MACOS=1
else
abort "This installer is only supported on macOS and Linux."
fi
# Required installation paths. To install elsewhere, you can download
# a binary for your platform and place it anywhere you like.
if [[ -n "${LIFERAY_CLI_ON_MACOS-}" ]]
then
UNAME_MACHINE="$(/usr/bin/uname -m)"
LIFERAY_CLI_PREFIX="/usr/local"
STAT_PRINTF=("stat" "-f")
PERMISSION_FORMAT="%A"
CHOWN=("/usr/sbin/chown")
CHGRP=("/usr/bin/chgrp")
GROUP="admin"
INSTALL=("/usr/bin/install" -o "root" -g "wheel" -m "0755")
else
UNAME_MACHINE="$(uname -m)"
LIFERAY_CLI_PREFIX="/usr/local"
STAT_PRINTF=("stat" "--printf")
PERMISSION_FORMAT="%a"
CHOWN=("/bin/chown")
CHGRP=("/bin/chgrp")
GROUP="$(id -gn)"
INSTALL=("/usr/bin/install" -o "root" -g "root" -m "0755")
fi
CHMOD=("/bin/chmod")
MKDIR=("/bin/mkdir" "-p")
unset HAVE_SUDO_ACCESS # unset this from the environment
have_sudo_access() {
if [[ ! -x "/usr/bin/sudo" ]]
then
return 1
fi
local -a SUDO=("/usr/bin/sudo")
if [[ -n "${SUDO_ASKPASS-}" ]]
then
SUDO+=("-A")
elif [[ -n "${NONINTERACTIVE-}" ]]
then
SUDO+=("-n")
fi
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]
then
if [[ -n "${NONINTERACTIVE-}" ]]
then
"${SUDO[@]}" -l mkdir &>/dev/null
else
"${SUDO[@]}" -v && "${SUDO[@]}" -l mkdir &>/dev/null
fi
HAVE_SUDO_ACCESS="$?"
fi
if [[ -n "${LIFERAY_CLI_ON_MACOS-}" ]] && [[ "${HAVE_SUDO_ACCESS}" -ne 0 ]]
then
abort "Need sudo access on macOS (e.g. the user ${USER} needs to be an Administrator)!"
fi
return "${HAVE_SUDO_ACCESS}"
}
execute() {
if ! "$@"
then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
execute_sudo() {
local -a args=("$@")
if have_sudo_access
then
if [[ -n "${SUDO_ASKPASS-}" ]]
then
args=("-A" "${args[@]}")
fi
ohai "/usr/bin/sudo" "${args[@]}"
execute "/usr/bin/sudo" "${args[@]}"
else
ohai "${args[@]}"
execute "${args[@]}"
fi
}
getc() {
local save_state
save_state="$(/bin/stty -g)"
/bin/stty raw -echo
IFS='' read -r -n 1 -d '' "$@"
/bin/stty "${save_state}"
}
ring_bell() {
# Use the shell's audible bell.
if [[ -t 1 ]]
then
printf "\a"
fi
}
wait_for_user() {
local c
echo
echo "Press ${tty_bold}RETURN${tty_reset}/${tty_bold}ENTER${tty_reset} to continue or any other key to abort:"
getc c
# we test for \r and \n because some stuff does \r instead
if ! [[ "${c}" == $'\r' || "${c}" == $'\n' ]]
then
exit 1
fi
}
check_run_command_as_root() {
[[ "${EUID:-${UID}}" == "0" ]] || return
# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
[[ -f /.dockerenv ]] && return
[[ -f /proc/1/cgroup ]] && grep -E "azpl_job|actions_job|docker|garden|kubepods" -q /proc/1/cgroup && return
abort "Don't run this as root!"
}
get_owner() {
"${STAT_PRINTF[@]}" "%u" "$1"
}
file_not_owned() {
[[ "$(get_owner "$1")" != "$(id -u)" ]]
}
get_group() {
"${STAT_PRINTF[@]}" "%g" "$1"
}
file_not_grpowned() {
[[ " $(id -G "${USER}") " != *" $(get_group "$1") "* ]]
}
# Search for the given executable in PATH (avoids a dependency on the `which` command)
which() {
# Alias to Bash built-in command `type -P`
type -P "$@"
}
# Invalidate sudo timestamp before exiting (if it wasn't active before).
if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null
then
trap '/usr/bin/sudo -k' EXIT
fi
####################################################################### script
ohai 'Checking for `sudo` access (which may request your password)...'
if [[ -n "${LIFERAY_CLI_ON_MACOS-}" ]]
then
have_sudo_access
elif ! [[ -w "${LIFERAY_CLI_PREFIX}" ]] &&
! have_sudo_access
then
abort "$(
cat <<EOABORT
Insufficient permissions to install liferay cli to \"${LIFERAY_CLI_PREFIX}\" (the default prefix).
EOABORT
)"
fi
check_run_command_as_root
if [[ -d "${LIFERAY_CLI_PREFIX}" && ! -x "${LIFERAY_CLI_PREFIX}" ]]
then
abort "$(
cat <<EOABORT
The liferay cli prefix ${tty_underline}${LIFERAY_CLI_PREFIX}${tty_reset} exists but is not searchable.
If this is not intentional, please restore the default permissions and
try running the installer again:
sudo chmod 775 ${LIFERAY_CLI_PREFIX}
EOABORT
)"
fi
if [[ -n "${LIFERAY_CLI_ON_MACOS-}" ]]
then
# On macOS, support 64-bit Intel and ARM
if [[ "${UNAME_MACHINE}" != "arm64" ]] && [[ "${UNAME_MACHINE}" != "x86_64" ]]
then
abort "liferay cli is only supported on Intel and ARM processors!"
fi
else
# On Linux, support 64-bit Intel and ARM
if [[ "${UNAME_MACHINE}" != "aarch64" ]] && [[ "${UNAME_MACHINE}" != "x86_64" ]]
then
abort "liferay cli is only supported on Intel and ARM processors!"
fi
fi
ohai "This script will install:"
echo "${LIFERAY_CLI_PREFIX}/bin/liferay"
group_chmods=()
directories=(bin)
mkdirs=()
for dir in "${directories[@]}"
do
if ! [[ -d "${LIFERAY_CLI_PREFIX}/${dir}" ]]
then
mkdirs+=("${LIFERAY_CLI_PREFIX}/${dir}")
fi
done
user_chmods=()
mkdirs_user_only=()
chmods=()
if [[ "${#group_chmods[@]}" -gt 0 ]]
then
chmods+=("${group_chmods[@]}")
fi
if [[ "${#user_chmods[@]}" -gt 0 ]]
then
chmods+=("${user_chmods[@]}")
fi
chowns=()
chgrps=()
if [[ "${#chmods[@]}" -gt 0 ]]
then
for dir in "${chmods[@]}"
do
if file_not_owned "${dir}"
then
chowns+=("${dir}")
fi
if file_not_grpowned "${dir}"
then
chgrps+=("${dir}")
fi
done
fi
if [[ "${#group_chmods[@]}" -gt 0 ]]
then
ohai "The following existing directories will be made group writable:"
printf "%s\n" "${group_chmods[@]}"
fi
if [[ "${#user_chmods[@]}" -gt 0 ]]
then
ohai "The following existing directories will be made writable by user only:"
printf "%s\n" "${user_chmods[@]}"
fi
if [[ "${#chowns[@]}" -gt 0 ]]
then
ohai "The following existing directories will have their owner set to ${tty_underline}${USER}${tty_reset}:"
printf "%s\n" "${chowns[@]}"
fi
if [[ "${#chgrps[@]}" -gt 0 ]]
then
ohai "The following existing directories will have their group set to ${tty_underline}${GROUP}${tty_reset}:"
printf "%s\n" "${chgrps[@]}"
fi
if [[ "${#mkdirs[@]}" -gt 0 ]]
then
ohai "The following new directories will be created:"
printf "%s\n" "${mkdirs[@]}"
fi
additional_shellenv_commands=()
if [[ -z "${NONINTERACTIVE-}" ]]
then
ring_bell
wait_for_user
fi
if [[ -d "${LIFERAY_CLI_PREFIX}" ]]
then
if [[ "${#chmods[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "u+rwx" "${chmods[@]}"
fi
if [[ "${#group_chmods[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "g+rwx" "${group_chmods[@]}"
fi
if [[ "${#user_chmods[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "go-w" "${user_chmods[@]}"
fi
if [[ "${#chowns[@]}" -gt 0 ]]
then
execute_sudo "${CHOWN[@]}" "${USER}" "${chowns[@]}"
fi
if [[ "${#chgrps[@]}" -gt 0 ]]
then
execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}"
fi
else
execute_sudo "${INSTALL[@]}" "${LIFERAY_CLI_PREFIX}"
fi
if [[ "${#mkdirs[@]}" -gt 0 ]]
then
execute_sudo "${MKDIR[@]}" "${mkdirs[@]}"
execute_sudo "${CHMOD[@]}" "ug=rwx" "${mkdirs[@]}"
if [[ "${#mkdirs_user_only[@]}" -gt 0 ]]
then
execute_sudo "${CHMOD[@]}" "go-w" "${mkdirs_user_only[@]}"
fi
execute_sudo "${CHOWN[@]}" "${USER}" "${mkdirs[@]}"
execute_sudo "${CHGRP[@]}" "${GROUP}" "${mkdirs[@]}"
fi
ohai "Downloading and installing liferay cli..."
(
LIFERAY_CLI_RELEASE=$(curl -fsSL https://api.github.com/repos/liferay/liferay-cli/releases/latest)
LIFERAY_CLI_VERSION=$(egrep -o '"tag_name":.*?[^\\]",' <<< $LIFERAY_CLI_RELEASE)
LIFERAY_CLI_VERSION=${LIFERAY_CLI_VERSION:13:-2}
LIFERAY_CLI_CHECKSUMS_FILE="https://github.com/liferay/liferay-cli/releases/download/${LIFERAY_CLI_VERSION}/checksums.txt"
LIFERAY_CLI_CHECKSUMS=$(curl -fsSL $LIFERAY_CLI_CHECKSUMS_FILE)
if [[ -n "${LIFERAY_CLI_ON_MACOS-}" ]]
then
if [[ "${UNAME_MACHINE}" == "arm64" ]]
then
LIFERAY_CLI_BINARY="liferay-darwin-arm64"
elif [[ "${UNAME_MACHINE}" == "x86_64" ]]
then
LIFERAY_CLI_BINARY="liferay-darwin-amd64"
fi
else
if [[ "${UNAME_MACHINE}" == "aarch64" ]]
then
LIFERAY_CLI_BINARY="liferay-linux-arm64"
elif [[ "${UNAME_MACHINE}" == "x86_64" ]]
then
LIFERAY_CLI_BINARY="liferay-linux-amd64"
fi
fi
LIFERAY_CLI_BINARY_FILE="https://github.com/liferay/liferay-cli/releases/download/${LIFERAY_CLI_VERSION}/${LIFERAY_CLI_BINARY}"
curl -fsSL "$LIFERAY_CLI_BINARY_FILE" -o "/tmp/${LIFERAY_CLI_BINARY}"
LIFERAY_CLI_BINARY_SUM=$(cd /tmp && shasum -a 256 "${LIFERAY_CLI_BINARY}")
LIFERAY_CLI_CHECKSUM=$(echo "$LIFERAY_CLI_CHECKSUMS" | grep "$LIFERAY_CLI_BINARY")
if [[ "${LIFERAY_CLI_BINARY_SUM}" == "${LIFERAY_CLI_CHECKSUM}" ]]
then
execute_sudo "${INSTALL[@]}" "/tmp/${LIFERAY_CLI_BINARY}" "${LIFERAY_CLI_PREFIX}/bin/liferay"
rm "/tmp/${LIFERAY_CLI_BINARY}"
else
abort "$(
cat <<EOABORT
liferay cli failed checksum!
Calculated Sum: ${LIFERAY_CLI_BINARY_SUM}
Downloaded Sum: ${LIFERAY_CLI_CHECKSUM}
EOABORT
)"
fi
) || exit 1
ohai "Installation successful!"
echo
ring_bell