-
Notifications
You must be signed in to change notification settings - Fork 38
/
install.sh
executable file
·1122 lines (965 loc) · 40.2 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
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
# This script contains functions with different licenses. Some functions specifically annotated with the author/license information.
# Functions that are not annotated with author/license information released under Apache License 2.0: https://www.apache.org/licenses/LICENSE-2.0.txt
set -uo pipefail
main() {
REQUIRED_BASH_VERSION="3.2.57"
REQUIRED_GIT_VERSION="2.18.0"
validate_bash_version "$REQUIRED_BASH_VERSION"
OPT_DEFAULT_CI="no"
OPT_DEFAULT_INTERACTIVE="auto"
OPT_DEFAULT_JOIN_DOCKER_GROUP="auto"
OPT_DEFAULT_SETUP_BIN_PATH="auto"
OPT_DEFAULT_AUTOACTIVATE_WERF="auto"
OPT_DEFAULT_INSTALL_WERF_SYSTEM_DEPENDENCIES="auto"
OPT_DEFAULT_SETUP_BUILDAH="auto"
OPT_DEFAULT_VERIFY_TRDL_SIGNATURE="yes"
OPT_DEFAULT_SHELL="auto"
OPT_DEFAULT_WERF_AUTOACTIVATE_VERSION="2"
OPT_DEFAULT_WERF_AUTOACTIVATE_CHANNEL="stable"
OPT_DEFAULT_INITIAL_TRDL_VERSION="0.7.0"
OPT_DEFAULT_TRDL_TUF_REPO="https://tuf.trdl.dev"
OPT_DEFAULT_TRDL_PGP_PUBKEY_URL="https://trdl.dev/trdl-client.asc"
OPT_DEFAULT_WERF_TUF_REPO="https://tuf.werf.io"
OPT_DEFAULT_WERF_TUF_ROOT_VERSION="1"
OPT_DEFAULT_WERF_TUF_ROOT_SHA="b7ff6bcbe598e072a86d595a3621924c8612c7e6dc6a82e919abe89707d7e3f468e616b5635630680dd1e98fc362ae5051728406700e6274c5ed1ad92bea52a2"
OPT_REGEX_SHELL='^(bash|zsh|auto)?$'
OPT_REGEX_WERF_AUTOACTIVATE_VERSION='^[.0-9]+$'
OPT_REGEX_WERF_AUTOACTIVATE_CHANNEL='.+'
OPT_REGEX_INITIAL_TRDL_VERSION='^[0-9]+.[0-9]+.[0-9]+$'
OPT_REGEX_TRDL_TUF_REPO='^https?://.+'
OPT_REGEX_TRDL_PGP_PUBKEY_URL='^https?://.+'
OPT_REGEX_WERF_TUF_REPO='^https?://.+'
OPT_REGEX_WERF_TUF_ROOT_VERSION='^[0-9]+$'
OPT_REGEX_WERF_TUF_ROOT_SHA='^[a-fA-F0-9]+$'
eval "$(getoptions getoptions_config) abort 'Failure parsing options.'"
OPT_CI="${OPT_CI:-$OPT_DEFAULT_CI}"
if [ "$OPT_CI" == "yes" ]; then
OPT_INTERACTIVE="${OPT_INTERACTIVE:-"no"}"
OPT_JOIN_DOCKER_GROUP="${OPT_JOIN_DOCKER_GROUP:-"no"}"
OPT_SETUP_BIN_PATH="${OPT_SETUP_BIN_PATH:-"no"}"
OPT_AUTOACTIVATE_WERF="${OPT_AUTOACTIVATE_WERF:-"no"}"
OPT_INSTALL_WERF_SYSTEM_DEPENDENCIES="${OPT_INSTALL_WERF_SYSTEM_DEPENDENCIES:-"no"}"
OPT_SETUP_BUILDAH="${OPT_SETUP_BUILDAH:-"no"}"
else
OPT_INTERACTIVE="${OPT_INTERACTIVE:-$OPT_DEFAULT_INTERACTIVE}"
OPT_JOIN_DOCKER_GROUP="${OPT_JOIN_DOCKER_GROUP:-$OPT_DEFAULT_JOIN_DOCKER_GROUP}"
OPT_SETUP_BIN_PATH="${OPT_SETUP_BIN_PATH:-$OPT_DEFAULT_SETUP_BIN_PATH}"
OPT_AUTOACTIVATE_WERF="${OPT_AUTOACTIVATE_WERF:-$OPT_DEFAULT_AUTOACTIVATE_WERF}"
OPT_INSTALL_WERF_SYSTEM_DEPENDENCIES="${OPT_INSTALL_WERF_SYSTEM_DEPENDENCIES:-$OPT_DEFAULT_INSTALL_WERF_SYSTEM_DEPENDENCIES}"
OPT_SETUP_BUILDAH="${OPT_SETUP_BUILDAH:-$OPT_DEFAULT_SETUP_BUILDAH}"
fi
OPT_VERIFY_TRDL_SIGNATURE="${OPT_VERIFY_TRDL_SIGNATURE:-$OPT_DEFAULT_VERIFY_TRDL_SIGNATURE}"
OPT_SHELL="${OPT_SHELL:-$OPT_DEFAULT_SHELL}"
OPT_WERF_AUTOACTIVATE_VERSION="${OPT_WERF_AUTOACTIVATE_VERSION:-$OPT_DEFAULT_WERF_AUTOACTIVATE_VERSION}"
OPT_WERF_AUTOACTIVATE_CHANNEL="${OPT_WERF_AUTOACTIVATE_CHANNEL:-$OPT_DEFAULT_WERF_AUTOACTIVATE_CHANNEL}"
OPT_INITIAL_TRDL_VERSION="${OPT_INITIAL_TRDL_VERSION:-$OPT_DEFAULT_INITIAL_TRDL_VERSION}"
OPT_TRDL_TUF_REPO="${OPT_TRDL_TUF_REPO:-$OPT_DEFAULT_TRDL_TUF_REPO}"
OPT_TRDL_PGP_PUBKEY_URL="${OPT_TRDL_PGP_PUBKEY_URL:-$OPT_DEFAULT_TRDL_PGP_PUBKEY_URL}"
OPT_WERF_TUF_REPO="${OPT_WERF_TUF_REPO:-$OPT_DEFAULT_WERF_TUF_REPO}"
OPT_WERF_TUF_ROOT_VERSION="${OPT_WERF_TUF_ROOT_VERSION:-$OPT_DEFAULT_WERF_TUF_ROOT_VERSION}"
OPT_WERF_TUF_ROOT_SHA="${OPT_WERF_TUF_ROOT_SHA:-$OPT_DEFAULT_WERF_TUF_ROOT_SHA}"
install_werf_system_dependencies "$OPT_INSTALL_WERF_SYSTEM_DEPENDENCIES"
ensure_cmds_available uname git grep tee install
validate_git_version "$REQUIRED_GIT_VERSION"
setup_buildah "$OPT_SETUP_BUILDAH"
[[ "$OPT_VERIFY_TRDL_SIGNATURE" == "no" ]] && ensure_cmds_available gpg
declare arch
arch="$(get_arch)" || abort "Failure getting system architecture."
declare os
os="$(get_os)" || abort "Failure getting OS."
declare shell
if [[ "$OPT_SHELL" == "auto" ]]; then
set_shell || abort "Failure getting user shell."
else
shell="$OPT_SHELL"
fi
[[ $os == "linux" ]] && propose_joining_docker_group "$OPT_JOIN_DOCKER_GROUP"
setup_trdl_bin_path "$shell" "$OPT_SETUP_BIN_PATH"
install_trdl "$os" "$arch" "$OPT_TRDL_TUF_REPO" "$OPT_TRDL_PGP_PUBKEY_URL" "$OPT_INITIAL_TRDL_VERSION" "$OPT_VERIFY_TRDL_SIGNATURE"
add_trdl_werf_repo "$OPT_WERF_TUF_REPO" "$OPT_WERF_TUF_ROOT_VERSION" "$OPT_WERF_TUF_ROOT_SHA"
enable_automatic_werf_activation "$shell" "$OPT_AUTOACTIVATE_WERF" "$OPT_WERF_AUTOACTIVATE_VERSION" "$OPT_WERF_AUTOACTIVATE_CHANNEL"
finalize "$OPT_WERF_AUTOACTIVATE_VERSION" "$OPT_WERF_AUTOACTIVATE_CHANNEL"
}
getoptions_config() {
setup REST on:"yes" no:"no" init:"" help:usage -- "Usage: $(basename "$0") [options]" ''
msg -- "Options (use + instead of - for short options to disable them):"
flag OPT_CI --{no-}ci -- \
"CI mode. Configures a few other options in a way recommended for CI (i.e. enables non-interactive mode). Full list of configured options can be found in the installer script itself. Default: $OPT_DEFAULT_CI"
flag OPT_INTERACTIVE -i --{no-}interactive -- \
"If run non-interactively, then choose recommended answers for all prompts. Default: $OPT_DEFAULT_INTERACTIVE"
flag OPT_VERIFY_TRDL_SIGNATURE --{no-}verify-trdl-signature -- \
"Verify PGP signature of trdl binary. Default: $OPT_DEFAULT_VERIFY_TRDL_SIGNATURE"
flag OPT_JOIN_DOCKER_GROUP --{no-}join-docker-group -- \
"Add user to the docker group? Default: $OPT_DEFAULT_JOIN_DOCKER_GROUP"
flag OPT_SETUP_BIN_PATH -p --{no-}setup-bin-path -- \
"Add \"$HOME/bin\" to \$PATH for trdl? Default: $OPT_DEFAULT_SETUP_BIN_PATH"
flag OPT_AUTOACTIVATE_WERF -a --{no-}autoactivate-werf -- \
"Enable werf autoactivation? Default: $OPT_DEFAULT_AUTOACTIVATE_WERF"
flag OPT_INSTALL_WERF_SYSTEM_DEPENDENCIES --{no-}install-werf-system-depedencies -- \
"Install system dependencies for werf? Default: $OPT_DEFAULT_INSTALL_WERF_SYSTEM_DEPENDENCIES"
flag OPT_SETUP_BUILDAH --{no-}setup-buildah -- \
"Install and set up Buildah backend? Default: $OPT_DEFAULT_SETUP_BUILDAH"
param OPT_SHELL -s --shell validate:"validate_option_by_regex '$OPT_REGEX_SHELL'" -- \
"Shell, for which werf/trdl should be set up. Default: $OPT_DEFAULT_SHELL. Allowed values regex: $OPT_REGEX_SHELL"
param OPT_WERF_AUTOACTIVATE_VERSION -v --version validate:"validate_option_by_regex '$OPT_REGEX_WERF_AUTOACTIVATE_VERSION'" -- \
"Autoactivated (if enabled) werf version. Default: $OPT_DEFAULT_WERF_AUTOACTIVATE_VERSION. Allowed values regex: $OPT_REGEX_WERF_AUTOACTIVATE_VERSION"
param OPT_WERF_AUTOACTIVATE_CHANNEL -c --channel validate:"validate_option_by_regex '$OPT_REGEX_WERF_AUTOACTIVATE_CHANNEL'" -- \
"Autoactivated (if enabled) werf channel. Default: $OPT_DEFAULT_WERF_AUTOACTIVATE_CHANNEL. Allowed values regex: $OPT_REGEX_WERF_AUTOACTIVATE_CHANNEL"
param OPT_INITIAL_TRDL_VERSION --initial-trdl-version validate:"validate_option_by_regex '$OPT_REGEX_INITIAL_TRDL_VERSION'" -- \
"Initially installed version of trdl (self-updates automatically). Default: $OPT_DEFAULT_INITIAL_TRDL_VERSION. Allowed values regex: $OPT_REGEX_INITIAL_TRDL_VERSION"
param OPT_TRDL_TUF_REPO --trdl-tuf-repo validate:"validate_option_by_regex '$OPT_REGEX_TRDL_TUF_REPO'" -- \
"trdl TUF-repository URL. Default: $OPT_DEFAULT_TRDL_TUF_REPO. Allowed values regex: $OPT_REGEX_TRDL_TUF_REPO"
param OPT_TRDL_PGP_PUBKEY_URL --trdl-pgp-pubkey-url validate:"validate_option_by_regex '$OPT_REGEX_TRDL_PGP_PUBKEY_URL'" -- \
"trdl GPG public key URL. Default: $OPT_DEFAULT_TRDL_PGP_PUBKEY_URL. Allowed values regex: $OPT_REGEX_TRDL_PGP_PUBKEY_URL"
param OPT_WERF_TUF_REPO --werf-tuf-repo validate:"validate_option_by_regex '$OPT_REGEX_WERF_TUF_REPO'" -- \
"werf TUF-repository URL. Default: $OPT_DEFAULT_WERF_TUF_REPO. Allowed values regex: $OPT_REGEX_WERF_TUF_REPO"
param OPT_WERF_TUF_ROOT_VERSION --werf-tuf-root-version validate:"validate_option_by_regex '$OPT_REGEX_WERF_TUF_ROOT_VERSION'" -- \
"werf TUF-repository root version. Default: $OPT_DEFAULT_WERF_TUF_ROOT_VERSION. Allowed values regex: $OPT_REGEX_WERF_TUF_ROOT_VERSION"
param OPT_WERF_TUF_ROOT_SHA --werf-tuf-root-sha validate:"validate_option_by_regex '$OPT_REGEX_WERF_TUF_ROOT_SHA'" -- \
"werf TUF-repository root SHA-hash. Default: $OPT_DEFAULT_WERF_TUF_ROOT_SHA. Allowed values regex: $OPT_REGEX_WERF_TUF_ROOT_SHA"
disp :usage -h --help
}
validate_option_by_regex() {
declare value="$OPTARG"
declare valid_regex="$1"
[[ $value =~ $valid_regex ]] || return 1
}
validate_bash_version() {
declare required_bash_version="$1"
declare current_bash_version="${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]}"
compare_versions "$current_bash_version" "$required_bash_version"
[[ $? -gt 1 ]] && abort "Bash version must be at least \"$required_bash_version\"."
}
validate_git_version() {
declare required_git_version="$1"
declare current_git_version
current_git_version="$(git --version | awk '{print $3}' | awk -F. '{printf("%s.%s.%s", $1, $2, $3)}')" || abort "Unable to parse git version."
compare_versions "$current_git_version" "$required_git_version"
case $? in
1) log::info "Current version is greater than or equal to required version." ;;
2) abort "Git version must be at least \"$required_git_version\"." ;;
0) log::info "Versions are equal." ;;
*) abort "Unexpected error comparing versions." ;;
esac
}
get_arch() {
declare arch
arch="$(uname -m)" || abort "Can't get system architecture."
case "$arch" in
x86_64) arch="amd64" ;;
arm64 | armv8* | aarch64*) arch="arm64" ;;
i386 | i486 | i586 | i686) abort "werf is not available for x86 architecture." ;;
arm | armv7*) abort "werf is not available for 32-bit ARM architectures." ;;
*) abort "werf is not available for \"$arch\" architecture." ;;
esac
printf '%s' "$arch"
}
get_os() {
declare os
os="$(uname | tr '[:upper:]' '[:lower:]')" || abort "Can't detect OS."
case "$os" in
linux*) os="linux" ;;
darwin*) os="darwin" ;;
msys* | cygwin* | mingw* | nt | win*) abort "This installer does not support Windows." ;;
*) abort "Unknown OS." ;;
esac
printf '%s' "$os"
}
set_shell() {
shell="$(printf '%s' "$SHELL" | rev | cut -d'/' -f1 | rev)" || abort "Can't determine login shell."
declare default_shell="$shell"
declare answer
while :; do
case "$shell" in
bash | zsh)
printf '[INPUT REQUIRED] Current login shell is "%s". Press ENTER to setup werf for this shell or choose another one.\n[b]ash/[z]sh/[a]bort? Default: %s.\n' "$shell" "$default_shell"
;;
*)
default_shell="bash"
printf '[INPUT REQUIRED] Current login shell is "%s". This shell is unsupported. Choose another shell or abort installation.\n[b]ash/[z]sh/[a]bort? Default: %s.\n' "$shell" "$default_shell"
;;
esac
if [[ $OPT_INTERACTIVE == "no" ]]; then
answer=""
else
read -r answer
fi
case "${answer:-$default_shell}" in
[bB] | [bB][aA][sS][hH])
shell="bash"
break
;;
[zZ] | [zZ][sS][hH])
shell="zsh"
break
;;
[aA] | [aA][bB][oO][rR][tT])
printf 'Aborted by the user.\n' 1>&2
exit 0
;;
*)
printf 'Invalid choice, please retry.\n' 1>&2
continue
;;
esac
done
}
propose_joining_docker_group() {
declare override_join_docker_group="$1"
[[ $override_join_docker_group == "no" ]] && return 0
if is_command_exists docker; then
is_user_in_group "$(get_user)" docker && return 0
is_root && return 0
ensure_cmds_available usermod
[[ $override_join_docker_group == "auto" ]] && prompt_yes_no_skip 'werf needs access to the Docker daemon. Add current user to the "docker" group? (root required)' "yes" || return 0
run_as_root "usermod -aG docker '$(get_user)'" || abort "Can't add user \"$(get_user)\" to group \"docker\"."
else
return 0
fi
}
setup_trdl_bin_path() {
declare shell="$1"
declare override_setup_trdl_bin_path="$2"
[[ $override_setup_trdl_bin_path == "no" ]] && return 0
case "$shell" in
bash)
declare active_bash_profile_file="$(get_active_bash_profile_file)"
[[ $override_setup_trdl_bin_path == "auto" ]] && prompt_yes_no_skip "trdl is going to be installed in \"$HOME/bin/\". Add this directory to your \$PATH in \"$HOME/.bashrc\" and \"$active_bash_profile_file\"? (strongly recommended)" "yes" || return 0
add_trdl_bin_path_setup_to_file "$HOME/.bashrc"
add_trdl_bin_path_setup_to_file "$active_bash_profile_file"
;;
zsh)
declare active_zsh_profile_file="$(get_active_zsh_profile_file)"
[[ $override_setup_trdl_bin_path == "auto" ]] && prompt_yes_no_skip "trdl is going to be installed in \"$HOME/bin/\". Add this directory to your \$PATH in \"$HOME/.zshrc\" and \"$active_zsh_profile_file\"? (strongly recommended)" "yes" || return 0
add_trdl_bin_path_setup_to_file "$HOME/.zshrc"
add_trdl_bin_path_setup_to_file "$active_zsh_profile_file"
;;
*) abort "Shell \"$shell\" is not supported." ;;
esac
}
add_trdl_bin_path_setup_to_file() {
declare file="$1"
declare path_append_cmd='[[ "$PATH" == *"$HOME/bin:"* ]] || export PATH="$HOME/bin:$PATH"'
grep -qsxF -- "$path_append_cmd" "$file" && log::info "Skipping adding \"$HOME/bin/\" to \$PATH in \"$file\": already added." && return 0
append_line_to_file "$path_append_cmd" "$file"
}
install_trdl() {
declare os="$1"
declare arch="$2"
declare trdl_tuf_repo="$3"
declare trdl_gpg_pubkey_url="$4"
declare trdl_version="$5"
declare skip_signature_verification="$6"
is_trdl_installed_and_up_to_date "$trdl_version" && log::info "Skipping trdl installation: already installed in \"$HOME/bin/\"." && return 0
log::info "Installing trdl to \"$HOME/bin/\"."
mkdir -p "$HOME/bin" || abort "Can't create \"$HOME/bin\" directory."
declare tmp_dir
tmp_dir="$(mktemp -d)" || abort "Can't create temporary directory."
download "${trdl_tuf_repo}/targets/releases/${trdl_version}/${os}-${arch}/bin/trdl" "$tmp_dir/trdl"
download "${trdl_tuf_repo}/targets/signatures/${trdl_version}/${os}-${arch}/bin/trdl.sig" "$tmp_dir/trdl.sig"
if [[ $skip_signature_verification == "no" ]]; then
log::info "Verifying trdl binary signatures."
download "$trdl_gpg_pubkey_url" "$tmp_dir/trdl.pub"
gpg -q --import <"$tmp_dir/trdl.pub" || abort "Can't import trdl public gpg key."
gpg -q --verify "$tmp_dir/trdl.sig" "$tmp_dir/trdl" || abort "Can't verify trdl binary with a gpg signature."
fi
install "$tmp_dir/trdl" "$HOME/bin" || abort "Can't install trdl binary from \"$tmp_dir/trdl\" to \"$HOME/bin\"."
rm -rf "$tmp_dir" 2>/dev/null
}
is_trdl_installed_and_up_to_date() {
declare required_trdl_version="$1"
[[ -x "$HOME/bin/trdl" ]] || return 1
declare current_trdl_version
current_trdl_version="$("$HOME/bin/trdl" version 2>/dev/null | cut -c2-)" || return 1
compare_versions "$current_trdl_version" "$required_trdl_version"
test $? -lt 2
return
}
add_trdl_werf_repo() {
declare werf_tuf_repo_url="$1"
declare werf_tuf_repo_root_version="$2"
declare werf_tuf_repo_root_sha="$3"
log::info "Adding werf repo to trdl."
"$HOME/bin/trdl" add werf "$werf_tuf_repo_url" "$werf_tuf_repo_root_version" "$werf_tuf_repo_root_sha" || abort "Can't add \"werf\" repo to trdl."
}
enable_automatic_werf_activation() {
declare shell="$1"
declare override_werf_enable_autoactivation="$2"
declare werf_autoactivate_version="$3"
declare werf_autoactivate_channel="$4"
[[ $override_werf_enable_autoactivation == "no" ]] && return 0
case "$shell" in
bash)
declare active_bash_profile_file="$(get_active_bash_profile_file)"
[[ $override_werf_enable_autoactivation == "auto" ]] && prompt_yes_no_skip "Add automatic werf activation to \"$HOME/.bashrc\" and \"$active_bash_profile_file\"? (recommended for interactive usage, not recommended for CI)" "yes" || return 0
add_automatic_werf_activation_to_file "$HOME/.bashrc" "$werf_autoactivate_version" "$werf_autoactivate_channel"
add_automatic_werf_activation_to_file "$active_bash_profile_file" "$werf_autoactivate_version" "$werf_autoactivate_channel"
;;
zsh)
declare active_zsh_profile_file="$(get_active_zsh_profile_file)"
[[ $override_werf_enable_autoactivation == "auto" ]] && prompt_yes_no_skip "Add automatic werf activation to \"$HOME/.zshrc\" and \"$active_zsh_profile_file\"? (recommended for interactive usage, not recommended for CI)" "yes" || return 0
add_automatic_werf_activation_to_file "$HOME/.zshrc" "$werf_autoactivate_version" "$werf_autoactivate_channel"
add_automatic_werf_activation_to_file "$active_zsh_profile_file" "$werf_autoactivate_version" "$werf_autoactivate_channel"
;;
*) abort "Shell \"$shell\" is not supported." ;;
esac
}
add_automatic_werf_activation_to_file() {
declare file="$1"
declare werf_autoactivate_version="$2"
declare werf_autoactivate_channel="$3"
declare werf_activation_cmd_grep1="trdl use werf"
declare werf_activation_cmd_grep2="trdl\" use werf"
declare werf_activation_cmd="! { which werf | grep -qsE \"^$HOME/.trdl/\"; } && [[ -x \"\$HOME/bin/trdl\" ]] && source \$(\"\$HOME/bin/trdl\" use werf \"$werf_autoactivate_version\" \"$werf_autoactivate_channel\")"
if grep -qsF -- "$werf_activation_cmd_grep1" "$file" || \
grep -qsF -- "$werf_activation_cmd_grep2" "$file"; then
log::info "Skipping adding werf activation to \"$file\": already found \"trdl use werf\" command."
return 0
fi
append_line_to_file "$werf_activation_cmd" "$file"
}
get_active_bash_profile_file() {
declare result="$HOME/.bash_profile"
for file in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do
[[ -r $file ]] && result="$file" && break
done
printf '%s' "$result"
}
get_active_zsh_profile_file() {
declare result="$HOME/.zprofile"
for file in "$HOME/.zprofile" "$HOME/.zlogin"; do
[[ -r $file ]] && result="$file" && break
done
printf '%s' "$result"
}
finalize() {
declare werf_autoactivate_version="$1"
declare werf_autoactivate_channel="$2"
log::info "werf installation finished successfully!"
log::info "Open new shell session if you have enabled werf autoactivation or activate werf manually with:\n$ source \$(\"$HOME/bin/trdl\" use werf \"$werf_autoactivate_version\" \"$werf_autoactivate_channel\")"
}
append_line_to_file() {
declare line="$1"
declare file="$2"
create_file "$file"
declare append_cmd="tee -a '$file' <<< '$line' 1>/dev/null"
[[ -w $file ]] || append_cmd="run_as_root '$append_cmd'"
eval $append_cmd || abort "Can't append line \"$line\" to file \"$file\"."
}
create_file() {
declare file="$1"
[[ -e $file ]] && return 0
declare stderr
if ! stderr="$(touch "$file" 1>/dev/null 2>&1)" && [[ $stderr == *"Permission denied"* ]]; then
run_as_root "touch '$file'" || abort "Unable to create file \"$file\"."
fi
}
download() {
declare url="$1"
declare path="$2"
declare download_cmd
if is_command_exists wget; then
download_cmd="wget -qO '$path' '$url'"
elif is_command_exists curl; then
download_cmd="curl -sSLo '$path' '$url'"
else
abort 'Neither "wget" nor "curl" available. Install and retry.'
fi
eval $download_cmd || abort "Unable to download \"$url\" to \"$path\"."
}
is_user_in_group() {
declare user="$1"
declare group="$2"
id -nG "$user" 2>/dev/null | grep -wqs "$group"
return
}
ensure_cmds_available() {
declare cmd
for cmd in "${@}"; do
is_command_exists "$cmd" || abort "\"$cmd\" required but not available."
done
}
run_as_root() {
declare cmd="$1"
if is_root; then
eval $cmd || return 1
else
[[ $OPT_INTERACTIVE == "no" ]] && log::warn "Non-interactive mode enabled, but current user doesn't have enough privilege, so the next command will be called with sudo (this might hang): sudo $cmd"
ensure_cmds_available sudo
eval sudo $cmd || return 1
fi
return 0
}
get_user() {
id -un
}
is_root() {
test "$(id -u)" -eq 0
return
}
is_command_exists() {
declare command="$1"
command -v "$command" 1>/dev/null 2>&1
return
}
# Returns 0 if answer is "yes".
# Returns 1 if answer is "skip".
# Aborts if answer is "abort".
# Returns exit code corresponding to the $def_arg if non-interactive.
prompt_yes_no_skip() {
declare prompt_msg="$1"
declare def_arg="$2"
case "$def_arg" in
[yY] | [yY][eE][sS])
def_arg="yes"
;;
[aA] | [aA][bB][oO][rR][tT])
def_arg="abort"
;;
[sS] | [sS][kK][iI][pP])
def_arg="skip"
;;
esac
declare answer
while :; do
printf '[INPUT REQUIRED] %s\n[y]es/[a]bort/[s]kip? Default: %s.\n' "$prompt_msg" "$def_arg"
if [[ $OPT_INTERACTIVE == "no" ]]; then
answer=""
else
read -r answer
fi
case "${answer:-$def_arg}" in
[yY] | [yY][eE][sS])
return 0
;;
[aA] | [aA][bB][oO][rR][tT])
printf 'Aborted by the user.\n'
exit 0
;;
[sS] | [sS][kK][iI][pP])
return 1
;;
*)
printf 'Invalid choice, please retry.\n'
continue
;;
esac
done
}
# Returns 0 if versions equal.
# Returns 1 if $version1 is greater than $version2.
# Returns 2 if $version1 is less than $version2.
compare_versions() {
declare version1="$1"
declare version2="$2"
for ver in "$version1" "$version2"; do
[[ $ver =~ ^[.0-9]*$ ]] || abort "Invalid version passed to compare_versions(): $ver"
done
[[ $version1 == "$version2" ]] && return 0
declare IFS=.
declare -a ver1 ver2
read -r -a ver1 <<<"$version1"
read -r -a ver2 <<<"$version2"
for ((i = ${#ver1[@]}; i < ${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i = 0; i < ${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
return 2
fi
done
return 0
}
install_werf_system_dependencies() {
local override_install_werf_system_dependencies="$1"
[[ $override_install_werf_system_dependencies == "no" ]] && return 0
[[ $override_install_werf_system_dependencies == "auto" ]] && prompt_yes_no_skip "Install system dependencies for werf?" "skip" || return 0
local missing_packages=""
is_command_exists git || missing_packages="$missing_packages git"
is_command_exists curl || missing_packages="$missing_packages curl"
is_command_exists gpg || missing_packages="$missing_packages gpg"
if [ "$missing_packages" == "" ]; then
return 0
fi
install_package "$missing_packages"
}
setup_buildah() {
local override_setup_buildah="$1"
[[ $override_setup_buildah == "no" ]] && return 0
[[ $override_setup_buildah == "auto" ]] && prompt_yes_no_skip "Install and set up Buildah backend?" "skip" || return 0
is_command_exists buildah || install_buildah
if is_command_exists sysctl; then
# Check if kernel.unprivileged_userns_clone exists
if ! [ -z "$(sysctl -ne kernel.unprivileged_userns_clone)" ]; then
KERNEL_SETTING="$(sysctl -ne kernel.unprivileged_userns_clone)"
if [ "$KERNEL_SETTING" = "0" ]; then
echo 'kernel.unprivileged_userns_clone = 1' | run_as_root "tee -a /etc/sysctl.conf"
run_as_root "sysctl -w kernel.unprivileged_userns_clone=1"
log::info "Updated kernel.unprivileged_userns_clone value"
fi
fi
# Check if kernel.apparmor_restrict_unprivileged_userns exists
if ! [ -z "$(sysctl -ne kernel.apparmor_restrict_unprivileged_userns)" ]; then
KERNEL_APPARMOR="$(sysctl -ne kernel.apparmor_restrict_unprivileged_userns)"
if [ "$KERNEL_APPARMOR" = "1" ]; then
log::info "Set kernel.apparmor_restrict_unprivileged_userns to 0"
echo "kernel.apparmor_restrict_unprivileged_userns = 0" | run_as_root "tee -a /etc/sysctl.conf" \ &&
run_as_root "sysctl -w kernel.apparmor_restrict_unprivileged_userns=0"
fi
fi
# Check the sysctl value: user.max_user_namespaces
USER_NAMESPACES_SETTING="$(sysctl -n user.max_user_namespaces)"
if [[ "$USER_NAMESPACES_SETTING" < "15000" ]]; then
echo 'user.max_user_namespaces = 15000' | run_as_root "tee -a /etc/sysctl.conf"
run_as_root "sysctl -w user.max_user_namespaces=15000"
log::info "Updated user.max_user_namespaces value"
fi
else
log::warn "sysctl not found"
fi
}
get_linux_distro() {
declare distro
if [[ -f /etc/os-release ]]; then
. "/etc/os-release"
distro="$NAME"
elif type lsb_release 1>/dev/null 2>&1; then
distro="$(lsb_release -si)"
elif [[ -f "/etc/lsb-release" ]]; then
. "/etc/lsb-release"
distro="$DISTRIB_ID"
elif [[ -f "/etc/debian_version" ]]; then
distro="debian"
elif [[ -f "/etc/SuSe-release" ]]; then
distro="suse"
elif [[ -f "/etc/redhat-release" ]]; then
distro="redhat"
else
abort "Unable to detect Linux distro."
fi
echo "$distro"
}
install_package() {
local package="$1"
distro=$(get_linux_distro)
log::info "Detected Linux distribution: $distro"
case "$distro" in
*CentOS*|*Red\ Hat*|*Fedora*)
run_as_root "yum -y install $package"
;;
*Debian*|*Ubuntu*)
run_as_root "apt-get -y update"
run_as_root "DEBIAN_FRONTEND=noninteractive apt-get -y install $package"
;;
*SUSE*|*openSUSE*)
run_as_root "zypper install $package"
;;
*RHEL*)
if [[ "$distro" == *"7"* ]]; then
run_as_root "yum -y install $package"
elif [[ "$distro" == *"8"* ]]; then
run_as_root "dnf module install -y $package"
fi
;;
*)
abort "Unsupported distribution: $distro"
;;
esac
}
install_buildah(){
distro=$(get_linux_distro)
log::info "Installing Buildah and configuring user namespaces..."
case "$distro" in
*Debian*|*Ubuntu*)
install_package "buildah uidmap"
;;
*CentOS*|*Red\ Hat*|*Fedora*)
run_as_root "yum groupinstall -y 'Development Tools'"
install_package "curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl autoconf"
;;
*SUSE*|*openSUSE*)
install_package "buildah shadow"
;;
*RHEL*)
if [[ "$distro" == *"8"* ]]; then
run_as_root "dnf module enable -y container-tools:2.0"
install_package "buildah uidmap"
elif [[ "$distro" == *"7"* ]]; then
run_as_root "subscription-manager repos --enable=rhel-7-server-extras-rpms"
install_package "buildah uidmap"
fi
;;
*)
abort "Unsupported distribution."
;;
esac
set_user_subuids_subgids
# Migrate Podman system if applicable
if is_command_exists podman; then
log::info "Migrating Podman system configuration..."
run_as_root "podman system migrate"
fi
log::info "Buildah installation and configuration completed."
}
get_free_range() {
local start=100000
local end=6000000
local range_size=65536
local used_ranges
if [[ -s /etc/subuid || -s /etc/subgid ]]; then
used_ranges=$(awk -F: '{print $2 ":" $3}' /etc/subuid /etc/subgid 2>/dev/null | sort -n)
else
echo "$start:$range_size"
return 0
fi
while read -r range; do
local range_start range_size_in_use
range_start=$(echo "$range" | cut -d: -f1)
range_size_in_use=$(echo "$range" | cut -d: -f2)
local range_end=$((range_start + range_size_in_use - 1))
if ((start + range_size - 1 < range_start)); then
echo "$start:$range_size"
return 0
fi
start=$((range_end + 1))
done <<< "$used_ranges"
echo "$start:$range_size"
return 0
}
set_user_subuids_subgids() {
local min_range_size=65536
local current_user=$(get_user)
local free_range=$(get_free_range)
if grep -q "^$current_user:" /etc/subuid; then
local current_range=$(awk -F: -v user="$current_user" '$1 == user {print $3}' /etc/subuid)
if (( current_range < min_range_size )); then
abort "User $current_user has a subuid range smaller than $min_range_size. Please adjust it manually."
else
log::info "User $current_user already has a valid subuid range."
fi
else
echo "$current_user:$free_range" | run_as_root "tee -a /etc/subuid"
log::info "Assigned subuid range $free_range to user $current_user."
fi
if grep -q "^$current_user:" /etc/subgid; then
local current_range=$(awk -F: -v user="$current_user" '$1 == user {print $3}' /etc/subgid)
if (( current_range < min_range_size )); then
abort "User $current_user has a subgid range smaller than $min_range_size. Please adjust it manually."
else
log::info "User $current_user already has a valid subgid range."
fi
else
echo "$current_user:$free_range" | run_as_root "tee -a /etc/subgid"
log::info "Assigned subgid range $free_range to user $current_user."
fi
}
log::info() {
declare msg="$1"
printf '[INFO] %b\n' "$msg"
}
log::warn() {
declare msg="$1"
printf '[WARNING] %b\n' "$msg" >&2
}
log::err() {
declare msg="$1"
printf '[ERROR] %b\n' "$msg" >&2
}
abort() {
declare msg="$1"
printf '[FATAL] %b\n' "$msg" >&2
printf '[FATAL] Aborting.\n' >&2
exit 1
}
# Author of this function: ko1nksm (Koichi Nakashima)
# License of this function: CC0-1.0 License https://github.com/ko1nksm/getoptions/blob/v3.3.0/LICENSE
getoptions() {
_error='' _on=1 _no='' _export='' _plus='' _mode='' _alt='' _rest='' _def=''
_flags='' _nflags='' _opts='' _help='' _abbr='' _cmds='' _init=@empty IFS=' '
[ $# -lt 2 ] && set -- "${1:?No parser definition}" -
[ "$2" = - ] && _def=getoptions_parse
i=' '
while eval "_${#i}() { echo \"$i\$@\"; }"; [ "$i" ]; do i=${i#?}; done
quote() {
q="$2'" r=''
while [ "$q" ]; do r="$r${q%%\'*}'\''" && q=${q#*\'}; done
q="'${r%????}'" && q=${q#\'\'} && q=${q%\'\'}
eval "$1=\${q:-\"''\"}"
}
code() {
[ "${1#:}" = "$1" ] && c=3 || c=4
eval "[ ! \${$c:+x} ] || $2 \"\$$c\""
}
sw() { sw="$sw${sw:+|}$1"; }
kv() { eval "${2-}${1%%:*}=\${1#*:}"; }
loop() { [ $# -gt 1 ] && [ "$2" != -- ]; }
invoke() { eval '"_$@"'; }
prehook() { invoke "$@"; }
for i in setup flag param option disp msg; do
eval "$i() { prehook $i \"\$@\"; }"
done
args() {
on=$_on no=$_no export=$_export init=$_init _hasarg=$1 && shift
while loop "$@" && shift; do
case $1 in
-?) [ "$_hasarg" ] && _opts="$_opts${1#-}" || _flags="$_flags${1#-}" ;;
+?) _plus=1 _nflags="$_nflags${1#+}" ;;
[!-+]*) kv "$1"
esac
done
}
defvar() {
case $init in
@none) : ;;
@export) code "$1" _0 "export $1" ;;
@empty) code "$1" _0 "${export:+export }$1=''" ;;
@unset) code "$1" _0 "unset $1 ||:" "unset OPTARG ||:; ${1#:}" ;;
*)
case $init in @*) eval "init=\"=\${${init#@}}\""; esac
case $init in [!=]*) _0 "$init"; return 0; esac
quote init "${init#=}"
code "$1" _0 "${export:+export }$1=$init" "OPTARG=$init; ${1#:}"
esac
}
_setup() {
[ "${1#-}" ] && _rest=$1
while loop "$@" && shift; do kv "$1" _; done
}
_flag() { args '' "$@"; defvar "$@"; }
_param() { args 1 "$@"; defvar "$@"; }
_option() { args 1 "$@"; defvar "$@"; }
_disp() { args '' "$@"; }
_msg() { args '' _ "$@"; }
cmd() { _mode=@ _cmds="$_cmds${_cmds:+|}'$1'"; }
"$@"
cmd() { :; }
_0 "${_rest:?}=''"
_0 "${_def:-$2}() {"
_1 'OPTIND=$(($#+1))'
_1 'while OPTARG= && [ $# -gt 0 ]; do'
[ "$_abbr" ] && getoptions_abbr "$@"
args() {
sw='' validate='' pattern='' counter='' on=$_on no=$_no export=$_export
while loop "$@" && shift; do
case $1 in
--\{no-\}*) i=${1#--?no-?}; sw "'--$i'|'--no-$i'" ;;
--with\{out\}-*) i=${1#--*-}; sw "'--with-$i'|'--without-$i'" ;;
[-+]? | --*) sw "'$1'" ;;
*) kv "$1"
esac
done
quote on "$on"
quote no "$no"
}
setup() { :; }
_flag() {
args "$@"
[ "$counter" ] && on=1 no=-1 v="\$((\${$1:-0}+\$OPTARG))" || v=''
_3 "$sw)"
_4 '[ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break'
_4 "eval '[ \${OPTARG+x} ] &&:' && OPTARG=$on || OPTARG=$no"
valid "$1" "${v:-\$OPTARG}"
_4 ';;'
}
_param() {
args "$@"
_3 "$sw)"
_4 '[ $# -le 1 ] && set "required" "$1" && break'
_4 'OPTARG=$2'
valid "$1" '$OPTARG'
_4 'shift ;;'
}
_option() {
args "$@"
_3 "$sw)"
_4 'set -- "$1" "$@"'
_4 '[ ${OPTARG+x} ] && {'
_5 'case $1 in --no-*|--without-*) set "noarg" "${1%%\=*}"; break; esac'
_5 '[ "${OPTARG:-}" ] && { shift; OPTARG=$2; } ||' "OPTARG=$on"
_4 "} || OPTARG=$no"
valid "$1" '$OPTARG'
_4 'shift ;;'
}
valid() {
set -- "$validate" "$pattern" "$1" "$2"
[ "$1" ] && _4 "$1 || { set -- ${1%% *}:\$? \"\$1\" $1; break; }"
[ "$2" ] && {
_4 "case \$OPTARG in $2) ;;"
_5 '*) set "pattern:'"$2"'" "$1"; break'
_4 "esac"
}
code "$3" _4 "${export:+export }$3=\"$4\"" "${3#:}"
}
_disp() {
args "$@"
_3 "$sw)"
code "$1" _4 "echo \"\${$1}\"" "${1#:}"
_4 'exit 0 ;;'
}
_msg() { :; }
[ "$_alt" ] && _2 'case $1 in -[!-]?*) set -- "-$@"; esac'
_2 'case $1 in'
_wa() { _4 "eval 'set -- $1' \${1+'\"\$@\"'}"; }
_op() {
_3 "$1) OPTARG=\$1; shift"
_wa '"${OPTARG%"${OPTARG#??}"}" '"$2"'"${OPTARG#??}"'
_4 "$3"
}
_3 '--?*=*) OPTARG=$1; shift'
_wa '"${OPTARG%%\=*}" "${OPTARG#*\=}"'
_4 ';;'
_3 '--no-*|--without-*) unset OPTARG ;;'
[ "$_alt" ] || {
[ "$_opts" ] && _op "-[$_opts]?*" '' ';;'
[ ! "$_flags" ] || _op "-[$_flags]?*" - 'OPTARG= ;;'
}
[ "$_plus" ] && {
[ "$_nflags" ] && _op "+[$_nflags]?*" + 'unset OPTARG ;;'
_3 '+*) unset OPTARG ;;'
}
_2 'esac'
_2 'case $1 in'
"$@"
rest() {
_4 'while [ $# -gt 0 ]; do'
_5 "$_rest=\"\${$_rest}" '\"\${$(($OPTIND-$#))}\""'
_5 'shift'
_4 'done'
_4 'break ;;'
}
_3 '--)'
[ "$_mode" = @ ] || _4 'shift'
rest
_3 "[-${_plus:++}]?*)" 'set "unknown" "$1"; break ;;'
_3 '*)'
case $_mode in
@)
_4 "case \$1 in ${_cmds:-*}) ;;"
_5 '*) set "notcmd" "$1"; break'
_4 'esac'
rest ;;
+) rest ;;
*) _4 "$_rest=\"\${$_rest}" '\"\${$(($OPTIND-$#))}\""'
esac
_2 'esac'
_2 'shift'
_1 'done'