forked from tprasadtp/ubuntu-post-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafter-effects
executable file
·3168 lines (2731 loc) · 107 KB
/
after-effects
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# shellcheck disable=SC2059,SC2154,SC2034,SC2155
# vim: filetype=sh
# Author: Prasad Tengse
# Licence: GPLv3
# Github Repository: https://github.com/tprasadtp/ubuntu-post-install
# Requirements - Bash v4 and above
# - whiptail, wget, awk, sed, grep
#
set -o pipefail
readonly AE_EXEC_START=$(date +%s)
readonly SCRIPT=$(basename "$0")
# SCRIPT METADATA
readonly dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
# Version
readonly AE_VERSION="8.0.2"
# Handle Use interrupt
# trap ctrl-c and call ctrl_c()
trap ctrl_c_handler INT
function ctrl_c_handler() {
printf "${RED}• User Interrupt! CTRL-C${NC}\n"
exit 4
}
function _init_printf_variables() {
#Initialize printf variables
readonly _phase_repo="(REPO)"
readonly _phase_ppa="(PPA)"
readonly _phase_install="(INST)"
readonly _phase_purge="(PURGE)"
readonly _phase_deb="(DPKG)"
readonly _phase_apt="(APT)"
readonly _phase_apt_key="(KEYS)"
readonly _phase_snap="(SNAP)"
# Log Premitives
readonly _info="[INF]"
readonly _dev="[DEV]"
readonly _debug="[DBG]"
readonly _cleanup="[DEL]"
readonly _success="[ OK]"
readonly _warn="[WRN]"
readonly _error="[ERR]"
readonly _variable="[VAR]"
readonly _crit="[CRT]"
#colors for display
readonly YELLOW=$'\e[38;5;220m'
readonly GREEN=$'\e[32m'
readonly RED=$'\e[31m'
readonly BLUE=$'\e[34m'
readonly LIGHT_BLUE=$'\e[38;5;159m'
readonly CYAN=$'\e[38;5;51m'
readonly NC=$'\e[0m'
readonly ORANGE=$'\e[38;5;208m'
readonly TEAL=$'\e[38;5;192m'
readonly PINK=$'\e[38;5;212m'
readonly GRAY=$'\e[38;5;246m'
readonly LOG_GRAY=$'\e[38;5;240m'
readonly LIGHT_GRAY=$'\e[38;5;242m'
readonly MAGENTA=$'\e[38;5;219m'
}
function _init_logging() {
# Initialize phase 2
# Only variables necessary for logging & start logging
# Script related variables are defined in _init_script_variables
readonly log_file="$dir"/logs/after-effects.log
local bool_created_logfile
{
mkdir -p "$dir"/logs
} ||
{
printf "${RED}✕ Failed to create logs folder${NC}\n"
exit 2
}
# tmp dir
{
rm -rf /tmp/ae/*
mkdir -p /tmp/ae/
} ||
{
printf "${RED}✕ Failed to create tmp folder${NC}\n"
exit 2
}
# if file not exists touch it
if [[ ! -f ${log_file} ]]; then
if touch "${log_file}"; then
bool_created_logfile="true"
else
printf "${RED}✕ Failed to create logfile!${NC}\n"
exit 2
fi
fi
# check if logs can be written
if [[ -w $log_file ]]; then
{
log_debug "$(_line_fill)"
[[ $bool_created_logfile == "true" ]] && log_debug "Created log file: ${log_file}"
log_debug "Initialized logging"
} ||
{
printf "${RED}✕ Failed to write to log file ${log_file} ${NC}\n"
exit 2
}
else
printf "${RED}✕ Log file is not writable!${NC}\n"
exit 2
fi
}
# Utility Functions
function _line_fill() {
printf "_______________________________________________________\n"
}
function log_and_exit() {
# ARG-1 log msg
# ARG-2 exit code int
local msg="$1"
printf "${RED}✖ $msg ${NC}\n"
printf "$(date) ${_crit} $msg\n" >>"$log_file"
exit "$2"
}
function _script_exit_log() {
# Script time
log_stage "Cleanup and Exit"
readonly AE_EXEC_END=$(date +%s)
readonly AE_EXEC_TIME=$((AE_EXEC_END - AE_EXEC_START))
log_debug "$SCRIPT took $AE_EXEC_TIME seconds to complete."
exit 0
}
function log_info() {
local msg="$1"
printf " • $msg\n"
printf "$(date) ${_info} $msg\n" >>"$log_file"
}
function log_debug() {
local msg="$1"
if [[ ${AE_DEBUG} -gt 0 ]]; then
printf "${GRAY} • $msg ${NC}\n"
fi
printf "$(date) ${_debug} $msg\n" >>"$log_file"
}
function log_success() {
local msg="$1"
printf "${TEAL} • $msg ${NC}\n"
printf "$(date) ${_success} $msg\n" >>"$log_file"
}
function log_warn() {
local msg="$1"
printf "${YELLOW} • $msg ${NC}\n"
printf "$(date) ${_warn} $msg\n" >>"$log_file"
}
function log_stage() {
local msg="$1"
printf "${MAGENTA}➜ $msg ${NC}\n"
printf "$(date) ${_info} $msg\n" >>"$log_file"
}
function log_error() {
local msg="$1"
printf "${RED} • $msg ${NC}\n"
printf "$(date) ${_error} $msg\n" >>"$log_file"
}
function log_dev() {
local msg="$1"
printf "${ORANGE} • $msg ${NC}\n"
printf "$(date) ${_dev} $msg\n" >>"$log_file"
}
function log_notice() {
local msg="$1"
printf "${CYAN} • $msg${NC}\n"
printf "$(date) ${_info} $msg\n" >>"$log_file"
}
function log_var() {
local var
var="$1"
if [[ $AE_DEBUG -gt 0 ]]; then
printf "${GRAY} » %-22s - %-10s${NC}\n" "${var}" "${!var}"
fi
printf "$(date) ${_variable} %-22s - %-10s\n" "${var}" "${!var}" >>"$log_file"
}
function log_trace() {
# This function adds time stamp to logs without using external utilities
# Output will be automatically written to $log_file
# Arguments : 1
# ARG -1: printf variable for formatting the log
# Usage command | _add_timestamp_to_logs "$1"
while IFS= read -r line; do
printf "$(date) [EXT] ${1} %s\n" "$line" &>>"$log_file"
if [[ $AE_DEBUG -gt 1 ]]; then
printf "${LOG_GRAY} » %s %s${NC}\n" "$1" "$line"
fi
done
}
function log_property() {
# Args (4)
# Arg 1 : Property name
# Arg 2 : Property value
# Arg 3 : log level can be 0-debug(default)
# 1-info
# 2-notice
# 3-dev
# 4-warning
# 5-error
# other defaults to debug.
# Arg 4 : Format string spacing (default=18)
local desc value fmt lvl
local __msg_string
desc="${1}"
value="${2}"
lvl="${3:-0}"
fmt="%-${4:-22}s"
__msg_string="$(printf "${fmt} : %s" "${desc}" "${value}")"
case ${lvl} in
0) log_debug "${__msg_string}" ;;
1) log_info "${__msg_string}" ;;
2) log_notice "${__msg_string}" ;;
3) log_dev "${__msg_string}" ;;
4) log_warn "${__msg_string}" ;;
5) log_error "${__msg_string}" ;;
*) log_debug "${__msg_string}" ;;
esac
}
#shellcheck disable=SC2120
function detect_distribution() {
# Read /etc/os-release and get
local OS_RELEASE_FILE
OS_RELEASE_FILE="${1:-/etc/os-release}"
if [[ -r "${OS_RELEASE_FILE}" ]]; then
log_debug "Found os-release file ${OS_RELEASE_FILE}"
# Read Version Code Name
readonly AE_DISTRO_CODENAME="$(awk '/VERSION_CODENAME=/' "${OS_RELEASE_FILE}" | sed 's/VERSION_CODENAME=//' | tr '[:upper:]' '[:lower:]')"
# Read Human Readable Full Version Name
readonly AE_DISTRO_PRETTY_NAME="$(awk '/PRETTY_NAME=/' "${OS_RELEASE_FILE}" | sed 's/PRETTY_NAME=//' | tr -d '"')"
# Read Human Readable Distro Name
readonly AE_DISTRO_NAME="$(awk '/^NAME=/' "${OS_RELEASE_FILE}" | sed 's/^NAME=//' | tr -d '"')"
else
log_error "Hey, What kind of system is this?"
log_and_exit "I cannot determine distro/codename!" "5"
fi
}
function _init_script_variables() {
# Function defines Script variables
# Necessary variables used by the script are initialized here. This function
# should be called first before choices are made, always.
#shellcheck disable=SC2119
detect_distribution
# Achtung: Do not set code_name as readonly!!
code_name="${AE_DISTRO_CODENAME}"
readonly architecture="$(dpkg --print-architecture)"
case "${architecture}" in
amd64)
log_debug "Architecture is 64 bit."
readonly ARCH="amd64"
;;
armhf)
log_debug "Running on ARM CPU with HW Floating point Processor"
readonly ARCH="armhf"
;;
arm64)
log_debug "This is an ARM64. Please be advised that not all repositories support this arch."
readonly ARCH="arm64"
;;
*)
log_error "Sorry! This architecture is not supported by this script!"
log_and_exit "Unsupported Architecture. $(architecture)" "11"
;;
esac
# Get Hostname
readonly CLIENT_NAME=$(hostname)
# etc sources list dir
readonly SOURCES_FILE_DIR=/etc/apt/sources.list.d
# Trusted keys
if [[ -v ${TRUSTED_KEYRINGS_DIR} ]]; then
log_and_exit "Reserved variable (TRUSTED_KEYRINGS_DIR) is set!" "66"
else
readonly TRUSTED_KEYRINGS_DIR=/usr/share/keyrings
fi
# AE_OBS_PREFIX check
if [[ -v ${AE_OBS_PREFIX} ]]; then
log_and_exit "Reserved variable (AE_OBS_PREFIX) is set!" "66"
fi
if [[ -v ${AE_BASE_CODENAME} ]]; then
log_and_exit "Reserved variable (AE_BASE_CODENAME) is set!" "66"
fi
# Ping URL
readonly PING_URL="http://connectivitycheck.gstatic.com/generate_204"
#-------------------------- Release codenames --------------------------------------
readonly codename_latest_release="hirsute"
readonly codename_previous_release="groovy"
# 404 is a placeholder which is set when there are
# no suitable releases which can use this feature or
# the release is unknown or too early in developement.
readonly codename_upcoming_release="impish"
readonly codename_current_lts="focal"
readonly codename_previous_lts="bionic"
readonly codename_latest_debian_release="buster"
readonly codename_upcoming_debian_release="bullseye"
#============================ Switches/ booleans & Vars ================================
# Fix Repo not available for latest release
bool_fix_repo_not_available_for_latest="false"
# Allow repo fixes for Upcoming releases of ubuntu and its derivatives.
bool_fix_repo_not_available_for_upcoming_release="false"
# By default version checks are enabled
bool_check_version="true"
# Not used
readonly api_version_url="https://ae.prasadt.com/get/v3/version"
# Azure
readonly AZURE_METADATA_URL="http://169.254.169.254/metadata/instance/compute/location?api-version=2017-08-01&format=text"
# Achtung!!
# ========================================================================================= #
# Purge not required packages
# Default is false
# Requires command line option -d to be passed via command line otherwise it will not work.
# Set this to true if you don't want to pass -d every time
bool_purge_pkgs="false"
# Keep Downloaded DEB packages
bool_preserve_debs="false"
#
# ================================================================================== #
# Achtung!
# Do not change any of the variables below this if you don't know what they do.
# They are essential for correct working of the script.
# Check Debian Flags
bool_is_debian="false"
# Boolean to hold if --fix or --fix-mode-lts or --pre-release was applied
bool_codename_fix_appied="false"
}
function _init_print_basic_info() {
# This function logs and displays the Necessary details which helps in debugging.
# Should be used after _init_script_variables function.
log_property "Hostname" "${CLIENT_NAME}" 1
log_property "OS" "${AE_DISTRO_PRETTY_NAME}" 1
log_property "Distribution" "${AE_DISTRO_NAME}" 1
log_property "Release" "${AE_DISTRO_CODENAME}" 1
log_property "Architecture" "${ARCH}" 1
log_property "Version" "${AE_VERSION}" 1
log_property "SOURCES_FILE_DIR" "${SOURCES_FILE_DIR}"
log_property "TRUSTED_KEYRINGS_DIR" "${TRUSTED_KEYRINGS_DIR}"
#disable hist chars so that I can print "!!"" properly
histchars=
}
function delete_log_file() {
printf "➜ Deleting log file\n"
if rm "${dir}/logs/after-effects.log"; then
printf "${GREEN}• Done${NC}\n"
exit 0
else
printf "${RED}• Failed!${NC}\n"
exit 1
fi
}
function _check_dependencies() {
#Checks if commands in array are available.
# Accepts one argument as array.
local dependencies=("$@")
local dependency_check_failed_count dependency
dependency_check_failed_count=0
for dependency in "${dependencies[@]}"; do
if command -v "$dependency" >/dev/null; then
log_property "${dependency}" "INSTALLED"
else
log_property "$dependency" "NOT INSTALLED"
dependency_check_failed_count=$((dependency_check_failed_count + 1))
fi
done
if [ "$dependency_check_failed_count" -gt 0 ]; then
log_error "One or more dependencies not installed."
log_and_exit "Sorry! $SCRIPT cannot continue!" "1"
fi
}
function _eol_message() {
# Display EOL Message and upgrade instruction URLs.
# Arguments: 1
# ARG 1: EOL Date
log_error "This version of ${AE_DISTRO_NAME} is no longer supported."
log_and_exit "${AE_DISTRO_PRETTY_NAME} will reach EOL pretty soon ($1)." "12"
}
function _fix_ubuntu_derivatives() {
# Some Ubuntu based distributions use their own codename (Linux mint, Elementary)
# Some repositories require that ubuntu codename is used.
# This function fixes that for
# Linux Mint 20.x : Ubuntu 20.04 Xenial,
# Linux Mint 19.x : Ubuntu 18.04 Xenial,
# Elementary OS Juno,Hera: Ubuntu 18.04 Xenial
# Pop!_OS uses Ubuntu codenames (No need to apply fix)
# KDE Neon, Bodhi, Peppermint use Ubuntu codenames
# If the release is not known this function exits the script for safety.
log_stage "Checking for Ubuntu derivates/Debian"
log_var "code_name"
case ${code_name} in
# Ubuntu 18.04 and its derivatives
bionic)
readonly enable_ppa="true"
readonly distro_name="ubuntu"
log_info "This is a LTS release of Ubuntu (18.04)"
log_success "Keeping the codename as $code_name."
;;
tara | tessa | tina | tricia)
log_info "This is Linux-Mint 19.X."
log_info "Setting additional repositories to follow Ubuntu 18.04."
code_name="bionic"
readonly distro_name="ubuntu"
readonly enable_ppa="true"
log_success "Changed codename to $code_name"
;;
juno | hera)
log_info "This release of Elementary OS is based on Ubuntu 18.04 Bionic."
log_info "External repositories will use Ubuntu 18.04."
code_name="bionic"
readonly distro_name="ubuntu"
readonly enable_ppa="true"
log_success "Changed codename to $code_name"
;;
# Ubuntu 20.04 and its derivatives (Current Ubuntu LTS release)
focal)
readonly enable_ppa="true"
readonly distro_name="ubuntu"
log_info "This is latest LTS release of Ubuntu"
log_success "Keeping the codename as $code_name."
;;
ulyana | ulyssa | uma)
log_info "Release is Linux mint 20.X."
log_info "Setting additional repositories to follow Ubuntu 20.04."
code_name="focal"
readonly distro_name="ubuntu"
readonly AE_SNAP_REMOVE_APT_BLOCK="true"
readonly enable_ppa="true"
log_success "Changed codename to $code_name"
;;
# Current Ubuntu release
hirsute)
readonly enable_ppa="true"
readonly distro_name="ubuntu"
log_notice "Release is Ubuntu 20.10 Groovy Gorilla"
;;
# Upcoming Ubuntu release
impish)
readonly enable_ppa="true"
readonly distro_name="ubuntu"
log_warn "Release is Ubuntu 21.10 (Unstable!!)"
;;
# Old Debian release
stretch)
bool_is_debian="true"
readonly distro_name="debian"
readonly enable_ppa="false"
log_notice "This is Debian. PPAs are disabled."
;;
# Current Debian release
buster)
bool_is_debian="true"
readonly distro_name="debian"
readonly enable_ppa="false"
log_notice "This is current stable release of Debian. PPAs are disabled."
;;
# Upcoming Debian release
bullseye)
bool_is_debian="true"
readonly enable_ppa="false"
readonly distro_name="debian"
log_dev "You are running Debian Testing!"
log_warn "This is pre release version of Debian. Use with caution!"
log_notice "PPAs are disabled."
;;
# Unsupported releases
groovy) _eol_message "June 2021" ;;
xenial) _eol_message "April 2021" ;;
sarah | serena | sonya | sylvia) _eol_message "April 2021" ;;
loki) _eol_message "April 2021" ;;
*)
log_and_exit "Unknown or unsupported release($code_name) !!" "16"
;;
esac
log_success "Applied release specific settings and fixes."
log_var "code_name"
# Save a copy of code_name
declare -gr AE_BASE_CODENAME="${code_name}"
log_var "AE_BASE_CODENAME"
}
function _test_internet_connection() {
# Function to check internet connection
log_info "Checking connectivity"
if wget --tries=2 --timeout=10 "$PING_URL" -O /dev/null -o /dev/null; then
log_success "Connected!"
else
log_error "You are not connected to the Internet!. "
log_error "Please check your Internet connection and try again."
log_and_exit "No internet connection!" "14"
fi
}
function _test_conflicting_apps() {
# Function checks if any apps like syanptic aptitude are running.
local lock
for lock in synaptic update-manager software-center apt-get dpkg aptitude; do
# shellcheck disable=SC2009
if ps -U root -u root u | grep $lock | grep -v grep >/dev/null; then
log_and_exit "Installation won't work. Please close $lock first then try again." "15"
else
log_property "$lock" "not running"
fi
done
log_success "No conflicts detected"
}
function parse_yaml() {
local yaml_file="${1}"
local prefix="${2}"
local s
local w
local fs
s='[[:space:]]*'
w='[a-zA-Z0-9_.-]*'
fs="$(echo @ | tr @ '\034')"
(
#shellcheck disable=SC1003
sed -e '/- [^\"]'"[^\']"'.*: /s|\([ ]*\)- \([[:space:]]*\)|\1-\'$'\n'' \1\2|g' |
sed -ne '/^--/s|--||g; s|\"|\\\"|g; s/[[:space:]]*$//g;' \
-e "/#.*[\"\']/!s| #.*||g; /^#/s|#.*||g;" \
-e "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)${s}[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" |
awk -F"$fs" '{
indent = length($1)/2;
if (length($2) == 0) { conj[indent]="+";} else {conj[indent]="";}
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("__")}
printf("%s%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, conj[indent-1],$3);
}
}' |
sed -e 's/_=/+=/g' |
awk 'BEGIN {
FS="=";
OFS="="
}
/(-|\.).*=/ {
gsub("-|\\.", "_", $1)
}
{ print }'
) <"$yaml_file"
}
function _get_remote_file() {
# Function to get remote file/response
# exits script if it fails.
# Accepts 2 arguments.
# ARG 1: File name; Name of the local file the response or file should be saved as.
# ARG 2: URL to the file
local file_name="${1}"
local exit_status
local file_url="${2}"
log_property "URL is set to" "${file_url}"
if [ "$#" -eq 2 ]; then
# Remote files
wget -q "${file_url}" -O "$file_name" >>$"$log_file"
exit_status="$?"
log_property "Exit code (wget)" "$exit_status"
if [[ $exit_status -ne 0 ]]; then
log_error "Something went wrong while retrieving ${file_name}."
log_and_exit "Error Getting file." "28"
fi
else
log_and_exit "Invalid number of arguments <_get_remote_file> Requires 2, got $#." "19"
fi
}
function compare_versions() {
# Function to compare two semver version strings.
#
# REQUIRED ARGUMENTS:
# Accepts two arguments both of which are semver version strings.
#
# ARG1 - CURRENT_SEMVER. Usually obtained from
# a) Within the script
# b) Executing {binary} --version --short
# c) Version marker files from ~/bin or /usr/share/vmark-files/{binary}
# d) Package manager's metadata
# ARG2 - UPSTREAM_SEMVER. Usually obtained from
# a) Upcheck API endpoint
# b) GitHub releases API
# This function will NOT handle fetching/reading these values.
#
# DEPENDENCIES: One of the following is required.
# a) 'sh-logger' snippet from tprasadtp/templates repository.
# b) 'log_debug' function. Minimum dummy implementation is below.
# ```bash
# function log_debug(){}
# ```
# LOGGING: All logs generated by this function are at debug level (handled by `log_debug`).
# Compares CURRENT_SEMVER and UPSTREAM_SEMVER mostly according to semver specs and sets a
# global variable VCOMPARE_RESULT to save the result.
# RESULTS:
# If CURRENT_SEMVER > UPSTREAM_SEMVER VCOMPARE_RESULT is -1. (no update required)
# If CURRENT_SEMVER < UPSTREAM_SEMVER VCOMPARE_RESULT is 1. (update is required)
# If CURRENT_SEMVER == UPSTREAM_SEMVER VCOMPARE_RESULT is 0. (no update required)
# ERRORS:
# a) ERROR_INVALID_SEMVER_1 - CURRENT_SEMVER is invalid
# b) ERROR_INVALID_SEMVER_2 - UPSTREAM_SEMVER is invalid
# c) ERROR_DEPENDENCY - 'log_debug' function is not defined
# d) ERROR_INCOMPLETE_EVAL - For some reason, evaluation isn't completed
# e) ERROR_UNHANDLED_CONDITION - Unhadled condition.
#
# NOTES:
# Please note that ERROR_INVALID_SEMVER indicates version is not compliant accoring
# to our versioning policy. It might be a valid semver according to semver specs,
# but is non compliant with our tagging policy, which follows
# MAJOR.MINOR.PATCH-<PRERELEASE_IDENTIFIER>.<PRERELEASE_VERSION>
# For pre releases, PRERELEASE_IDENTIFIER can be 'alpha', 'beta' or 'rc' with an
# optional <PRERELEASE_VERSION> which MUST be a non negative integer.
# Default error in case of incomplete evaluation for any other reason
declare -g VCOMPARE_RESULT="ERROR_INCOMPLETE_EVAL"
# Check if log_debug function is defined
if ! declare -F "log_debug" >/dev/null; then
VCOMPARE_RESULT="ERROR_DEPENDENCY"
return
fi
# Define semver regex according to policy tprasadtp/templates/semver-regex.md
declare -r SEMVER_REGEX="^[vV]?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-(alpha|beta|rc)(\.(0|[1-9][0-9]*))?)?\$"
local current_semver upstream_semver
current_semver="$1"
upstream_semver="$2"
log_debug "Current version : ${current_semver}"
log_debug "Upstream version : ${upstream_semver}"
local current_major current_minor current_patch current_pre current_pre_num
local upstream_major upstream_minor upstream_patch upstream_pre upstream_pre_num
# Parse current semver
if [[ $current_semver =~ $SEMVER_REGEX ]]; then
current_major="${BASH_REMATCH[1]}"
current_minor="${BASH_REMATCH[2]}"
current_patch="${BASH_REMATCH[3]}"
current_pre="${BASH_REMATCH[5]}"
current_pre_num="${BASH_REMATCH[7]}"
log_debug "Parsed version (current) -> major:${current_major} minor:${current_minor} patch:${current_patch} pre:${current_pre} prenum:${current_pre_num}"
else
VCOMPARE_RESULT="ERROR_INVALID_SEMVER_1"
return
fi
# Parse upstream semver.
if [[ ${upstream_semver} =~ $SEMVER_REGEX ]]; then
upstream_major="${BASH_REMATCH[1]}"
upstream_minor="${BASH_REMATCH[2]}"
upstream_patch="${BASH_REMATCH[3]}"
upstream_pre="${BASH_REMATCH[5]}"
upstream_pre_num="${BASH_REMATCH[7]}"
log_debug "Parsed version (upstream) -> major:${upstream_major} minor:${upstream_minor} patch:${upstream_patch} pre:${upstream_pre} prenum:${upstream_pre_num}"
else
VCOMPARE_RESULT="ERROR_INVALID_SEMVER_2"
return
fi
# Compare semver versions
# Major version
if [[ ${upstream_major} -gt ${current_major} ]]; then
log_debug "Upstream release is newer than current ${upstream_major} > ${current_major} (major)"
VCOMPARE_RESULT="1"
return
elif [[ ${upstream_major} -lt ${current_major} ]]; then
log_debug "Current version is newer than upstream available ${upstream_major} < ${current_major} (major)"
VCOMPARE_RESULT="-1"
return
fi
# Minor version
# This will only be executed if MAJOR versions match.
if [[ ${upstream_minor} -gt ${current_minor} ]]; then
log_debug "Upstream release is newer than current ${upstream_minor} > ${current_minor} (minor)"
VCOMPARE_RESULT="1"
return
elif [[ ${upstream_minor} -lt ${current_minor} ]]; then
log_debug "Current version is newer than upstream available ${upstream_minor} < ${current_minor} (minor)"
VCOMPARE_RESULT="-1"
return
fi
# Patch version
# This will only be executed if MAJOR AND MINOR versions match.
if [[ ${upstream_patch} -gt ${current_patch} ]]; then
log_debug "Upstream release is newer than current ${upstream_patch} > ${current_patch} (patch)"
VCOMPARE_RESULT="1"
return
elif [[ ${upstream_patch} -lt ${current_patch} ]]; then
log_debug "Current version is newer than upstream available ${upstream_patch} < ${current_patch} (patch)"
VCOMPARE_RESULT="-1"
return
fi
# What follows below is the mess of somewhat following semver specs.
log_debug "Major, Minor and Patch version match."
# Check if pre release strings are present in one of the versions. whichever lacks pre release string
# is the upstream one. For Eg. 2.3.6-beta.1 < 2.3.6
# upstream version has NO pre release fields but current one DOES
# current: 2.2.2-rc.1 upstream: 2.2.2, then upstream is newer.
if [[ -z ${upstream_pre} ]] && [[ -n $current_pre ]]; then
log_debug "Upstream version has no pre release fields, it is newer!"
VCOMPARE_RESULT="1"
return
# upstream version HAS prerelease fields but current one DOES NOT
# current: 2.2.2 upstream: 2.2.2-rc.1, then current is newer.
elif [[ -n ${upstream_pre} ]] && [[ -z $current_pre ]]; then
log_debug "Current version has no pre release fields, it is newer!"
VCOMPARE_RESULT="-1"
return
fi
# We do not follow complete semver specs.
# This is done for simplicity. All our versioning policies only include
# pre release tags of format MAJOR.MINOR.PATCH-<PRERELEASE_IDENTIFIER>.<PRERELEASE_VERSION>
# In our policies its strictly forbidden to include build numbers and build identifiers in
# the tags created. We only embed build number information in
# execultables themeselves or as packaging metadata like docker labels.
# In case of pre release versions, we only have <PRERELEASE_IDENTIFIER>.<PRERELEASE_VERSION>
# or <PRERELEASE_IDENTIFIER>. Thus, all other cases are ignored.
# Check if pre release identifiers match.
# Spec 11.4.2
# Identifiers with letters or hyphens are compared lexically in ASCII sort order.
if [[ $upstream_pre > $current_pre ]]; then
log_debug "Upstream pre release identider is lexographically greater than current one.(prerelease)"
VCOMPARE_RESULT="1"
return
elif [[ $upstream_pre < $current_pre ]]; then
log_debug "Current pre release identider is lexographically greater than upstream one.(prerelease)"
VCOMPARE_RESULT="-1"
return
else
log_debug "Current and upstream pre release identifiers match"
fi
# Spec 11.4.4: A larger set of pre-release fields has a higher precedence than a smaller set,
# if all of the preceding identifiers are equal.
# check if both PRERELEASE_VERSION fields are empty, if so both versions are equal.
if [[ -z $upstream_pre_num ]] && [[ -z $current_pre_num ]]; then
log_debug "pre-release version identifiers are empty for both fields. (all)"
VCOMPARE_RESULT="0"
return
fi
# upstream version has pre release version field but current one doesnt
# current: 2.2.2-rc upstream: 2.2.2.rc.1, then upstream is newer.
if [[ -z ${upstream_pre_num} ]] && [[ -n $current_pre_num ]]; then
log_debug "Upstream has pre release number but current one does not. Upstream is newer (prerelease-num)"
VCOMPARE_RESULT="1"
return
# upstream version has lacks pre release version field but current one does
# current: 2.2.2-rc.1 upstream: 2.2.2.rc, then current is newer.
elif [[ -n ${upstream_pre_num} ]] && [[ -z $upstream_pre_num ]]; then
log_debug "Current version pre release number but current one does not. Current version is newer. (prerelease-num)"
VCOMPARE_RESULT="-1"
return
fi
if [[ ${upstream_pre_num} -gt ${current_pre_num} ]]; then
log_debug "Upstream release is newer than current ${upstream_pre_num} > ${current_pre_num} (prerelease-num)"
VCOMPARE_RESULT="1"
return
elif [[ ${upstream_pre_num} -lt ${current_pre_num} ]]; then
log_debug "Current version is newer than upstream available ${upstream_pre_num} < ${current_pre_num} (prerelease-num)"
VCOMPARE_RESULT="-1"
return
elif [[ ${upstream_pre_num} -eq ${current_pre_num} ]]; then
log_debug "Current and upstream pre release nums match. ${upstream_pre_num} = ${current_pre_num} (all)"
VCOMPARE_RESULT="0"
return
fi
# We should never reach this condition. If we do, indicate we did not handle all conditions.
# This should trigger a bug report prompt/message in logs.
VCOMPARE_RESULT="ERROR_UNHANDLED_CONDITION"
}
function _version_checks() {
# Checks if its running latest version.
# Also suggest updating to latest version if current version is not latest,
# Allows to deprecate old version as it can be suggested.
# Check if Version Checks is enabled. (Default is enabled)
declare -g VCOMPARE_RESULT
local upcheck_gh_api_res
# Check if update checks are enabled (default=true)
if [ "$bool_check_version" == "true" ]; then
log_stage "Checking for updates"
# Get latest version from GitHub release endpoint
latest_semver_tag="$(wget -q --timeout 20 --tries 3 -O- https://api.github.com/repos/tprasadtp/ubuntu-post-install/releases/latest | grep -Po '"tag_name": "\K.*?(?=")')"
upcheck_gh_api_res="$?"
if [[ $upcheck_gh_api_res -ne 0 ]]; then
log_error "Failed to check for updates!"
log_and_exit "Either check your connectivity or disable version checks with --no-version-check" "31"
fi
# Remove leading v if necessary
latest_semver="${latest_semver_tag#v}"
log_info "Latest available version is ${latest_semver}"
# Compare versions <CURRR> <UPSTREAM>
log_debug "Comparing versions"
compare_versions "${AE_VERSION}" "${latest_semver}"
# Evaluate
case $VCOMPARE_RESULT in
1)
log_error "Thou art running older version of this script,"
log_error "Please download latest version and try again."
log_and_exit "You can disable version checks by passing --no-version-check" "24"
;;
0)
log_success "Running the latest version!"
;;
-1)
log_dev "You are running development version of the script"
;;
ERROR_INVALID_SEMVER_1)
log_warn "Script's semver is invalid! Please report this bug!"
log_warn "Disabling update checks!"
;;
ERROR_INVALID_SEMVER_2)
log_warn "Latest version is invalid! Please report this bug!"
log_warn "Disabling update checks!"
;;
*)
log_error "Failed to evaluate version check result- ${VCOMPARE_RESULT}"
log_warn "Disabling update checks!"
;;
esac
# If version checks have been disabled
else
log_warn "Version checks have been disabled."
fi
}
function _check_bool() {
# Function to check if config has valid values for bool.
# Accepted values are true and false
# If invalid, defaults to false.
# Accepts global var and bool to check as arguments
local g_var param_bool default_val
if [[ $# -eq 2 ]]; then
default_val="false"
else
log_and_exit "Internal error! bool validator takes only two arguments!" "19"
fi
g_var="${1}"
param_bool="${2}"
case "${param_bool}" in
true | True | TRUE | Yes | yes | YES | 1) # shellcheck disable=SC2086
declare -g ${g_var}="true"
log_var "$g_var"
;;
false | False | FALSE | No | no | NO | 0) # shellcheck disable=SC2086
declare -g ${g_var}="false"
log_var "$g_var"
;;
*) # shellcheck disable=SC2086
declare -g ${g_var}="${default_val}"
log_var "$g_var"
;;
esac
}
function _set_yaml_config() {
local config_yaml_file
# Get and parse the file
log_stage "Processing configuration"
if [ "$bool_local_config" != "true" ]; then
log_property "Config URL" "${url_remote_yaml}" 3
if [[ -e "/tmp/ae/api-config.yml" ]]; then
rm -f "/tmp/ae/api-config.yml" || log_and_exit "Deleting old version YAML failed." "38"
fi
readonly config_yaml_file="/tmp/ae/api-config.yml"
# Get response/file
_get_remote_file "$config_yaml_file" "$url_remote_yaml"
else
log_property "Config file" "$local_config_file" 2
readonly config_yaml_file="$local_config_file"
fi
# Parse YAML to variables
log_property "Parsing config" "$config_yaml_file" 2
eval "$(parse_yaml "$config_yaml_file")"
# Repo flags
#################################################################################
_check_bool "add_docker_repo" "${config__add_repo__docker[0]}"
_check_bool "add_winehq_repo" "${config__add_repo__winehq[0]}"
_check_bool "add_mendeley_repo" "${config__add_repo__mendeley[0]}"
_check_bool "add_spotify_repo" "${config__add_repo__spotify[0]}"
_check_bool "add_signal_repo" "${config__add_repo__signal[0]}"
_check_bool "add_sublimetext_repo" "${config__add_repo__sublimetext[0]}"
_check_bool "add_podman_repo" "${config__add_repo__podman[0]}"
# ROS
_check_bool "add_ros_repo" "${config__add_repo__ros[0]}"
_check_bool "add_ros2_repo" "${config__add_repo__ros2[0]}"
# GitHub
_check_bool "add_gh_repo" "${config__add_repo__github[0]}"
_check_bool "add_hashicorp_repo" "${config__add_repo__hashicorp[0]}"
# Google
_check_bool "add_gvisor_repo" "${config__add_repo__gvisor[0]}"
_check_bool "add_bazel_repo" "${config__add_repo__bazel[0]}"
_check_bool "add_gcp_repo" "${config__add_repo__googlecloud[0]}"