-
Notifications
You must be signed in to change notification settings - Fork 11
/
install.sh
607 lines (498 loc) · 17.1 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#!/usr/bin/env bash
# MetaCall Install Script by Parra Studios
# Cross-platform set of scripts to install MetaCall infrastructure.
#
# Copyright (C) 2016 - 2020 Vicente Eduardo Ferrer Garcia <vic798@gmail.com>
#
# 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.
# Program options
OPT_DOCKER_INSTALL=0
OPT_NO_CHECK_CERTIFICATE=0
OPT_NO_DOCKER_FALLBACK=0
OPT_UPDATE=0
OPT_UNINSTALL=0
# Program commands
CMD_DOWNLOAD=""
CMD_SHEBANG=""
# Check for command line arguments
for option in "$@"
do
if [ "$option" = '--docker-install' ]; then
OPT_DOCKER_INSTALL=1
fi
if [ "$option" = '--no-check-certificate' ]; then
OPT_NO_CHECK_CERTIFICATE=1
fi
if [ "$option" = '--no-docker-fallback' ]; then
OPT_NO_DOCKER_FALLBACK=1
fi
if [ "$option" = '--update' ]; then
OPT_UPDATE=1
fi
if [ "$option" = '--uninstall' ]; then
OPT_UNINSTALL=1
fi
done
# Check if program exists
program() {
command -v $1 >/dev/null 2>&1
}
# Set up colors
if program tput; then
ncolors=$(tput colors)
if [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
bold="$(tput bold || echo)"
normal="$(tput sgr0 || echo)"
black="$(tput setaf 0 || echo)"
red="$(tput setaf 1 || echo)"
green="$(tput setaf 2 || echo)"
yellow="$(tput setaf 3 || echo)"
blue="$(tput setaf 4 || echo)"
magenta="$(tput setaf 5 || echo)"
cyan="$(tput setaf 6 || echo)"
white="$(tput setaf 7 || echo)"
fi
fi
# Title message
title() {
printf "%b\n\n" "${normal:-}${bold:-}$@${normal:-}"
}
# Warning message
warning() {
printf "%b\n" "${yellow:-}‼${normal:-} $@"
}
# Error message
err() {
printf "%b\n" "${red:-}✘${normal:-} $@"
}
# Print message
print() {
printf "%b\n" "${normal:-}▷ $@"
}
# Success message
success() {
printf "%b\n" "${green:-}✔${normal:-} $@"
}
# Ask message
ask() {
while true; do
printf "${normal:-}▷ $@? [Y/n] "
read -r yn < /dev/tty
case $yn in
[Yy]* ) break;;
[Nn]* ) exit 1;;
* ) warning "Please answer yes [Yy] or no [Nn].";;
esac
done
}
# Check if a list of programs exist or aborts
programs_required() {
for prog in "$@"; do
if ! program ${prog}; then
err "The program '${prog}' is not found, it is required to run the installer. Aborting installation."
exit 1
fi
done
}
# Check if at least one program exists in the list or aborts
programs_required_one() {
for prog in "$@"; do
if program ${prog}; then
echo "${prog}"
return
fi
done
}
# Find proper shebang for the launcher script
find_shebang() {
# Detect where is the 'env' found in order to set properly the shebang
local shebang_dependencies="/usr/bin/env /bin/env /bin/sh"
local shebang_program=$(programs_required_one ${shebang_dependencies})
if [ -z "${shebang_program}" ]; then
err "None of the following programs are installed: ${shebang_dependencies}. One of them is required at least to find the shell. Aborting installation."
exit 1
fi
# Set up shebang command
CMD_SHEBANG="${shebang_program}"
}
# Check all dependencies
dependencies() {
print "Checking system dependencies."
# Check if required programs are installed
programs_required tar grep tail awk rev cut uname echo rm id find head chmod ln
if [ $(id -u) -ne 0 ]; then
programs_required tee
fi
# Check if download programs are installed
local download_dependencies="curl wget"
local download_program=$(programs_required_one ${download_dependencies})
if [ -z "${download_program}" ]; then
err "None of the following programs are installed: ${download_dependencies}. One of them is required at least to download the tarball. Aborting installation."
exit 1
fi
# Set up download command
CMD_DOWNLOAD="${download_program}"
# Locate shebang
find_shebang
# Detect sudo or run with root
if ! program sudo && [ $(id -u) -ne 0 ]; then
err "You need either having sudo installed or running this script as root. Aborting installation."
exit 1
fi
success "Dependencies satisfied."
}
# Get operative system name
operative_system() {
local os=$(uname)
# TODO: Implement other operative systems in metacall/distributable
case ${os} in
# Darwin)
# echo "osx"
# return
# ;;
# FreeBSD)
# echo "freebsd"
# return
# ;;
Linux)
echo "linux"
return
;;
esac
err "Operative System detected (${os}) is not supported." \
" Please, refer to https://github.com/metacall/install/issues and create a new issue." \
" Aborting installation."
exit 1
}
# Get architecture name
architecture() {
local arch=$(uname -m)
# TODO: Implement other architectures in metacall/distributable
case ${arch} in
x86_64)
echo "amd64"
return
;;
# armv7l)
# echo "arm"
# return
# ;;
# aarch64)
# echo "arm64"
# return
# ;;
esac
err "Architecture detected (${arch}) is not supported." \
" Please, refer to https://github.com/metacall/install/issues and create a new issue." \
" Aborting installation."
exit 1
}
# Download tarball
download() {
local url="https://github.com/metacall/distributable/releases/latest"
local tmp="/tmp/metacall-tarball.tar.gz"
local os="$1"
local arch="$2"
print "Start to download the tarball."
# TODO: Use ${CMD_DOWNLOAD} for improving this code?
# Skip certificate checks
if [ $OPT_NO_CHECK_CERTIFICATE -eq 1 ]; then
if program curl; then
local curl_cmd='curl --insecure'
elif program wget; then
local wget_cmd='wget --no-check-certificate'
fi
else
if program curl; then
local curl_cmd='curl'
elif program wget; then
local wget_cmd='wget'
fi
fi
if program curl; then
local tag_url=$(${curl_cmd} -Ls -o /dev/null -w %{url_effective} ${url})
elif program wget; then
local tag_url=$(${wget_cmd} -S -O /dev/null ${url} 2>&1 | grep Location: | tail -n 1 | awk '{print $2}')
fi
local version=$(printf "${tag_url}" | rev | cut -d '/' -f1 | rev)
local final_url=$(printf "https://github.com/metacall/distributable/releases/download/${version}/metacall-tarball-${os}-${arch}.tar.gz")
local fail=false
if program curl; then
${curl_cmd} --retry 10 -f --create-dirs -LS ${final_url} --output ${tmp} || fail=true
elif program wget; then
${wget_cmd} --tries 10 -O ${tmp} ${final_url} || fail=true
fi
if "${fail}" == true; then
rm -rf ${tmp}
err "The tarball metacall-tarball-${os}-${arch}.tar.gz could not be downloaded." \
" Please, refer to https://github.com/metacall/install/issues and create a new issue." \
" Aborting installation."
exit 1
fi
success "Tarball ${version} downloaded."
}
# Extract the tarball (requires root or sudo)
uncompress() {
local tmp="/tmp/metacall-tarball.tar.gz"
print "Uncompress the tarball (needs sudo or root permissions)."
if [ $(id -u) -eq 0 ]; then
tar xzf ${tmp} -C /
chmod -R 755 /gnu
else
sudo tar xzf ${tmp} -C /
sudo chmod -R 755 /gnu
sudo chown -R $(id -u):$(id -g) /gnu
fi
success "Tarball uncompressed successfully."
# Add links for certificates
local openssl_base="/gnu/store/`ls /gnu/store/ | grep openssl | head -n 1`/share"
local openssl_dir="${openssl_base}/`ls ${openssl_base} | grep openssl`"
local openssl_cert_dir="${openssl_dir}/certs"
local openssl_cert_file="${openssl_dir}/cert.pem"
local nss_cert_dir="/gnu/etc/ssl/certs"
local nss_cert_file="/gnu/etc/ssl/certs/ca-certificates.crt"
print "Linking certificates: ${openssl_cert_dir} => ${nss_cert_dir}"
print "Linking certificate CA: ${openssl_cert_file} => ${nss_cert_file}"
if [ $(id -u) -eq 0 ]; then
rmdir ${openssl_cert_dir}
ln -s ${nss_cert_dir} ${openssl_cert_dir}
ln -s ${nss_cert_file} ${openssl_cert_file}
else
sudo rmdir ${openssl_cert_dir}
sudo ln -s ${nss_cert_dir} ${openssl_cert_dir}
sudo ln -s ${nss_cert_file} ${openssl_cert_file}
fi
# Clean the tarball
print "Cleaning the tarball."
rm -rf ${tmp}
success "Tarball cleaned successfully."
}
# Install the CLI
cli() {
local cli="/gnu/store/`ls /gnu/store/ | grep metacall | head -n 1`"
print "Installing the Command Line Interface shortcut (needs sudo or root permissions)."
# Write shell script pointing to MetaCall CLI
if [ $(id -u) -eq 0 ]; then
# Create folder if it does not exist
mkdir -p /usr/local/bin/
# Write the shebang
echo "#!${CMD_SHEBANG} sh" &> /usr/local/bin/metacall
# MetaCall Environment
echo "export LOADER_LIBRARY_PATH=\"${cli}/lib\"" >> /usr/local/bin/metacall
echo "export SERIAL_LIBRARY_PATH=\"${cli}/lib\"" >> /usr/local/bin/metacall
echo "export DETOUR_LIBRARY_PATH=\"${cli}/lib\"" >> /usr/local/bin/metacall
echo "export PORT_LIBRARY_PATH=\"${cli}/lib\"" >> /usr/local/bin/metacall
echo "export CONFIGURATION_PATH=\"${cli}/configurations/global.json\"" >> /usr/local/bin/metacall
echo "export LOADER_SCRIPT_PATH=\"\${LOADER_SCRIPT_PATH:-\`pwd\`}\"" >> /usr/local/bin/metacall
# Certificates
echo "export SSL_CERT_DIR=\"/gnu/etc/ssl/certs\"" >> /usr/local/bin/metacall
echo "export SSL_CERT_FILE=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" >> /usr/local/bin/metacall
echo "export GIT_SSL_FILE=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" >> /usr/local/bin/metacall
echo "export GIT_SSL_CAINFO=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" >> /usr/local/bin/metacall
echo "export CURL_CA_BUNDLE=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" >> /usr/local/bin/metacall
# Locale
echo "export GUIX_LOCPATH=\"/gnu/lib/locale\"" >> /usr/local/bin/metacall
# Set up command line
echo "CMD=\`ls -a /gnu/bin | grep \"\$1\" | head -n 1\`" >> /usr/local/bin/metacall
echo "if [ \"\${CMD}\" = \"\$1\" ]; then" >> /usr/local/bin/metacall
echo " if [ -z \"\${PATH-}\" ]; then export PATH=\"/gnu/bin\"; else PATH=\"/gnu/bin:\${PATH}\"; fi" >> /usr/local/bin/metacall
echo " \$@" >> /usr/local/bin/metacall
echo " exit \$?" >> /usr/local/bin/metacall
echo "fi" >> /usr/local/bin/metacall
# CLI
echo "${cli}/metacallcli \$@" >> /usr/local/bin/metacall
chmod 755 /usr/local/bin/metacall
else
# Create folder if it does not exist
sudo mkdir -p /usr/local/bin/
# Write the shebang
echo "#!${CMD_SHEBANG} sh" | sudo tee /usr/local/bin/metacall > /dev/null
# MetaCall Environment
echo "export LOADER_LIBRARY_PATH=\"${cli}/lib\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export SERIAL_LIBRARY_PATH=\"${cli}/lib\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export DETOUR_LIBRARY_PATH=\"${cli}/lib\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export PORT_LIBRARY_PATH=\"${cli}/lib\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export CONFIGURATION_PATH=\"${cli}/configurations/global.json\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export LOADER_SCRIPT_PATH=\"\${LOADER_SCRIPT_PATH:-\`pwd\`}\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
# Certificates
echo "export SSL_CERT_DIR=\"/gnu/etc/ssl/certs\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export SSL_CERT_FILE=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export GIT_SSL_FILE=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export GIT_SSL_CAINFO=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "export CURL_CA_BUNDLE=\"/gnu/etc/ssl/certs/ca-certificates.crt\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
# Locale
echo "export GUIX_LOCPATH=\"/gnu/lib/locale\"" | sudo tee -a /usr/local/bin/metacall > /dev/null
# Set up command line
echo "CMD=\`ls -a /gnu/bin | grep \"\$1\" | head -n 1\`" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "if [ \"\${CMD}\" = \"\$1\" ]; then" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo " if [ -z \"\${PATH-}\" ]; then export PATH=\"/gnu/bin\"; else PATH=\"/gnu/bin:\${PATH}\"; fi" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo " \$@" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo " exit \$?" | sudo tee -a /usr/local/bin/metacall > /dev/null
echo "fi" | sudo tee -a /usr/local/bin/metacall > /dev/null
# CLI
echo "${cli}/metacallcli \$@" | sudo tee -a /usr/local/bin/metacall > /dev/null
sudo chmod 755 /usr/local/bin/metacall
fi
success "CLI shortcut installed successfully."
}
binary_install() {
# Show title
title "MetaCall Self-Contained Binary Installer"
# Check dependencies
dependencies
# Detect operative system and architecture
print "Detecting Operative System and Architecture."
# Run to check if the operative system is supported
operative_system > /dev/null 2>&1
architecture > /dev/null 2>&1
# Get the operative system and architecture into a variable
local os="$(operative_system)"
local arch="$(architecture)"
success "Operative System (${os}) and Architecture (${arch}) detected."
# Download tarball
download ${os} ${arch}
# Extract
uncompress
# Install CLI
cli
}
docker_install() {
# Show title
title "MetaCall Docker Installer"
# Check if Docker command is installed
print "Checking Docker Dependency."
programs_required docker echo chmod
if [ $(id -u) -ne 0 ]; then
programs_required tee
fi
# Locate shebang
find_shebang
# Pull MetaCall CLI Docker Image
print "Pulling MetaCall CLI Image."
docker pull metacall/cli:latest
result=$?
if [ $result -ne 0 ]; then
err "Docker image could not be pulled. Aborting installation."
exit 1
fi
# Install Docker based CLI
print "Installing the Command Line Interface shortcut (needs sudo or root permissions)."
local command="docker run --rm --network host -e \"LOADER_SCRIPT_PATH=/metacall/source\" -v \`pwd\`:/metacall/source -w /metacall/source -it metacall/cli \$@"
# Write shell script wrapping the Docker run of MetaCall CLI image
if [ $(id -u) -eq 0 ]; then
mkdir -p /usr/local/bin/
echo "#!${CMD_SHEBANG} sh" &> /usr/local/bin/metacall
echo "${command}" >> /usr/local/bin/metacall
chmod 755 /usr/local/bin/metacall
else
sudo mkdir -p /usr/local/bin/
echo "#!${CMD_SHEBANG} sh" | sudo tee /usr/local/bin/metacall > /dev/null
echo "${command}" | sudo tee -a /usr/local/bin/metacall > /dev/null
sudo chmod 755 /usr/local/bin/metacall
fi
}
check_path_env() {
# Check if the PATH contains the install path
echo "${PATH}" | grep -i ":/usr/local/bin:"
echo "${PATH}" | grep -i "^/usr/local/bin:"
echo "${PATH}" | grep -i ":/usr/local/bin$"
echo "${PATH}" | grep -i "^/usr/local/bin$"
}
uninstall() {
# Delete all the previously installed files
if [ $(id -u) -eq 0 ]; then
rm -rf /usr/local/bin/metacall || true
rm -rf /gnu || true
else
sudo rm -rf /usr/local/bin/metacall || true
sudo rm -rf /gnu || true
fi
}
main() {
if program metacall; then
# Skip asking for updates if the update flag is enabled
if [ $OPT_UPDATE -eq 0 ] && [ $OPT_UNINSTALL -eq 0 ]; then
ask "MetaCall is already installed. Do you want to update it?\n ${red:-}${bold:-}Warning: This operation will delete the /gnu folder${normal:-}. Continue"
fi
uninstall
fi
# Exit if the user only wants to uninstall
if [ $OPT_UNINSTALL -eq 1 ]; then
success "MetaCall has been successfully uninstalled"
exit 0
fi
# Required program for recursive calls
programs_required wait
if [ $OPT_DOCKER_INSTALL -eq 1 ]; then
# Run docker install
docker_install $@ &
proc=$!
wait ${proc}
result=$?
if [ $result -ne 0 ]; then
exit 1
fi
else
# Run binary install
binary_install $@ &
proc=$!
wait ${proc}
result=$?
if [ $result -ne 0 ]; then
# Exit if Docker fallback is disabled
if [ $OPT_NO_DOCKER_FALLBACK -eq 1 ]; then
exit 1
fi
# Required program for ask question to the user
programs_required read
# Check if the sell is interactive
case $- in
*i*) local interactive=1;;
*) local interactive=0;;
esac
if [ $interactive -ne 0 ]; then
# Ask for Docker fallback if we are in a terminal
ask "Binary installation has failed, do you want to fallback to Docker installation"
else
# Run Docker fallback otherwise
warning "Binary installation has failed, fallback to Docker installation."
fi
# On error, fallback to docker install
docker_install $@ &
proc=$!
wait ${proc}
result=$?
if [ $result -ne 0 ]; then
exit 1
fi
fi
fi
local path="$(check_path_env)"
# Check if /usr/local/bin is in PATH
if [ -z "${path}" ]; then
# Add /usr/local/bin to PATH
if [ $(id -u) -eq 0 ]; then
echo "export PATH=\${PATH}:/usr/local/bin" >> /etc/profile
else
echo "export PATH=\${PATH}:/usr/local/bin" | sudo tee -a /etc/profile > /dev/null
fi
warning "MetaCall install path is not present in PATH so we added it for you." \
" The command 'metacall' will be available in your subsequent terminal instances." \
" Run 'source /etc/profile' to make 'metacall' command available to your current terminal instance."
fi
# Show information
success "MetaCall has been installed." \
" Run 'metacall' command for start the CLI and type help for more information about CLI commands."
}
# Run main
main