-
Notifications
You must be signed in to change notification settings - Fork 0
/
libautomated.sh
965 lines (684 loc) · 22.4 KB
/
libautomated.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
#!/usr/bin/env bash
# MIT license
# Copyright (c) 2016-2022 Sergej Alikov <[email protected]>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -euo pipefail
AUTOMATED_DEBUG="${AUTOMATED_DEBUG:-FALSE}"
# https://no-color.org/
if [[ -n "${NO_COLOR:-}" ]]; then
AUTOMATED_DISABLE_COLOUR="${AUTOMATED_DISABLE_COLOUR:-TRUE}"
else
AUTOMATED_DISABLE_COLOUR="${AUTOMATED_DISABLE_COLOUR:-FALSE}"
fi
AUTOMATED_EXIT_RUNNING_IN_TMUX=68
AUTOMATED_EXIT_MULTIPLEXER_ALREADY_RUNNING_TMUX=69
declare -a AUTOMATED_SUPPORTED_MULTIPLEXERS=(tmux)
AUTOMATED_TMUX_SOCK_PREFIX="${AUTOMATED_TMUX_SOCK_PREFIX:-/tmp/tmux-automated}"
AUTOMATED_TMUX_FIFO_PREFIX="${AUTOMATED_TMUX_FIFO_PREFIX:-/tmp/tmux-fifo}"
declare -a AUTOMATED_ANSWER_READ_COMMAND=('read' '-r')
is_true () {
[[ "${1,,}" =~ ^(yes|true|on|1)$ ]]
}
printable_only () {
tr -cd '\11\12\15\40-\176'
}
translated () {
declare str="$1"
shift
declare a b
while [[ "$#" -gt 1 ]]; do
a="$1"
b="$2"
str="${str//"${a}"/"${b}"}"
shift 2
done
printf '%s\n' "$str"
}
sed_replacement () {
declare str="$1"
# shellcheck disable=SC1003
translated "$str" '\' '\\' '/' '\/' '&' '\&' $'\n' '\n'
}
colorized () {
declare fg_colour="$1"
declare -A colour_mappings=(
[BLACK]=30
[RED]=31
[GREEN]=32
[YELLOW]=33
[BLUE]=34
[MAGENTA]=35
[CYAN]=36
[WHITE]=37
[BRIGHT_BLACK]=90
[BRIGHT_RED]=91
[BRIGHT_GREEN]=92
[BRIGHT_YELLOW]=93
[BRIGHT_BLUE]=94
[BRIGHT_MAGENTA]=95
[BRIGHT_CYAN]=96
[BRIGHT_WHITE]=97
)
[[ -n "${colour_mappings[$fg_colour]:-}" ]] || throw "Invalid colour ${fg_colour}"
if is_true "$AUTOMATED_DISABLE_COLOUR"; then
cat
else
sed -u -e s/^/$'\e'\["${colour_mappings[${fg_colour}]}"m/ -e s/$/$'\e'\[0m/
fi
}
text_block () {
declare name="$1"
declare sed_replacement_begin
sed_replacement_begin=$(set -e; sed_replacement "BEGIN ${name}")
declare sed_replacement_end
sed_replacement_end=$(sed_replacement "END ${name}")
sed -u -e 1s/^/"${sed_replacement_begin}"\\$'\n'/ -e \$s/$/\\$'\n'"${sed_replacement_end}"/
}
prefixed_lines () {
declare prefix="$1"
declare sed_replacement
sed_replacement=$(set -e; sed_replacement "$prefix")
sed -u -e s/^/"${sed_replacement}"/
}
# shellcheck disable=SC2120
to_debug () {
declare fg_colour="${1:-YELLOW}"
if is_true "$AUTOMATED_DEBUG"; then
colorized "$fg_colour" >&2
else
cat >/dev/null
fi
}
pipe_debug () {
tee >(to_debug)
}
log_info () {
declare msg="$1"
printf '%s\n' "$msg" | colorized WHITE >&2
}
log_error () {
declare msg="$1"
printf 'ERROR %s\n' "$msg" | colorized RED >&2
}
log_debug () {
declare msg="$1"
declare fg_colour="${2:-YELLOW}"
if is_true "$AUTOMATED_DEBUG"; then
printf 'DEBUG %s\n' "$msg" | colorized "$fg_colour" >&2
fi
}
# WARNING: this function is a trap which might be executed
# for each command, including process substitutions.
# Do not use anything which might alter global built-in Bash variables,
# for example do not use regex matches as they alter BASH_REMATCH.
# Do not use pipes - they break process substitution FIFOs (in some
# Bash versions, like 4.2 it's obvious, in some - it might be a rarely
# reproducible race condition which only manifests occassionally.
log_cmd_trap () {
declare bash_command_firstline
bash_command_firstline="${BASH_COMMAND%%$'\n'*}"
# shellcheck disable=SC2086
set -- $bash_command_firstline
[[ "$#" -gt 0 ]] || return 0
declare stack_depth="${#FUNCNAME[@]}"
[[ "$stack_depth" -gt 0 ]] || return 0
! [[ "$stack_depth" -gt $((AUTOMATED_FUNCTRACE_DEPTH+1)) ]] || return 0
declare current_fn="${FUNCNAME[1]:-}"
# Only trace supported commands
declare current_command="$1"
# Filter out function entrypoints
! [[ "$current_fn" == "$current_command" ]] || return 0
declare current_command_type
current_command_type=$(type -t "$current_command") || return 0
if ! [[ "$current_command_type" == file || "$current_command_type" == function ]]; then
return 0
fi
declare indent
indent=$((stack_depth-1))
declare padding
padding=$(printf "%${indent}s\n" '')
declare format
if is_true "$AUTOMATED_DISABLE_COLOUR"; then
format='CMD %s%s%s\n'
else
format=$'\e[32mCMD %s%s%s\e[0m\\n'
fi
# shellcheck disable=SC2059
printf "$format" "${padding// /|}${padding:+ }" "${current_fn:+${current_fn}(): }" "$*" >&2
} <&- >&-
throw () {
declare msg="$1"
printf '%s\n' "$msg" | colorized RED >&2
exit 1
}
# This function reads from STDIN. Any command which has a potential to swallow
# the STDIN data must have it's STDIN descriptor closed.
to_file () {
declare target_path="$1"
declare callback="${2:-}"
if [[ -n "$callback" ]]; then
declare mtime_before mtime_after
if [[ -e "$target_path" ]]; then
mtime_before=$(set -e; file_mtime "$target_path" 2>/dev/null <&-)
else
mtime_before=0
fi
fi
declare diff_mode
if cmd_is_available 'diff' <&- && cmd_is_available 'patch' <&-; then
diff_mode=1
else
log_debug 'Please consider installing patch and diff commands to enable diff support for to_file()' <&-
diff_mode=0
fi
if [[ -e "$target_path" ]] && is_true "$diff_mode"; then
(
set +e
# TODO: AUT-125 Wait for process substitution subshell using { tee >(...); wait $!; }
# to prevent the script from moving onto the next command after the pipeline has
# completed, but the subshell is still sending output in background.
diff -duaN "$target_path" - | tee >(printable_only | text_block "$target_path" | to_debug BRIGHT_BLACK >/dev/null) | patch -s -p0 "$target_path" >/dev/null
declare -a exit_codes=("${PIPESTATUS[@]}")
set -e
if [[ "${exit_codes[0]}" -eq 0 ]]; then
# No diff and we don't care about the patch exit code
log_debug "${target_path} is up to date" <&-
exit 0
elif [[ "${exit_codes[0]}" -gt 1 ]]; then
log_error "diff ${target_path} failed" <&-
exit "${exit_codes[0]}"
elif [[ "${exit_codes[2]}" -gt 0 ]]; then
log_error "patch ${target_path} failed" <&-
exit "${exit_codes[2]}"
fi
)
else
# TODO: AUT-126 Write files atomically.
# shellcheck disable=SC2094
tee >(printable_only | text_block "$target_path" | to_debug BRIGHT_BLACK >/dev/null) >"$target_path"
fi
if [[ -n "$callback" ]]; then
mtime_after=$(set -e; file_mtime "$target_path")
if [[ "$mtime_before" -ne "$mtime_after" ]]; then
eval "$callback"
fi
fi
}
quoted () {
declare -a result=()
declare item
for token in "$@"; do
item="$(printf "%q\n" "$token")"
result+=("$item")
done
printf '%s\n' "${result[*]}"
}
md5 () {
md5sum -b | cut -f 1 -d ' '
}
joined () {
declare sep="$1"
shift
declare item
[[ "$#" -gt 0 ]] || return 0
printf '%s' "$1"
shift
for item in "$@"; do
printf "${sep}%s" "$item"
done
printf '\n'
}
cmd_is_available () {
command -v "$1" >/dev/null 2>&1
}
readable_file () {
[[ -f "$1" && -r "$1" ]]
}
readable_directory () {
[[ -d "$1" && -r "$1" ]]
}
# TODO: Replace with $OSTYPE?
local_kernel () {
uname -s
}
file_mode () {
declare path="$1"
case "$(set -e; local_kernel)" in
FreeBSD|OpenBSD|Darwin)
stat -f '%#Mp%03Lp' "$path"
;;
Linux)
stat -c "%#03a" "$path"
;;
*)
python_interpreter -c "from __future__ import print_function; import sys, os, stat; print('0{:o}'.format((stat.S_IMODE(os.stat(sys.argv[1]).st_mode))))" "$path"
;;
esac
}
file_owner () {
declare path="$1"
case "$(set -e; local_kernel)" in
FreeBSD|OpenBSD|Darwin)
stat -f '%Su:%Sg' "$path"
;;
Linux)
stat -c "%U:%G" "$path"
;;
*)
python_interpreter -c "from __future__ import print_function; import sys, os, stat, pwd, grp; st = os.stat(sys.argv[1]); print('{}:{}'.format(pwd.getpwuid(st.st_uid)[0], grp.getgrgid(st.st_gid)[0]))" "$path"
;;
esac
}
file_mtime () {
declare path="$1"
case "$(set -e; local_kernel)" in
FreeBSD|OpenBSD|Darwin)
stat -f '%Um' "$path"
;;
Linux)
stat -c "%Y" "$path"
;;
*)
python_interpreter -c "from __future__ import print_function; import sys, os, stat; print(os.stat(sys.argv[1])[stat.ST_MTIME])" "$path"
;;
esac
}
tmux_command () {
log_debug "tmux command ${*} over the ${AUTOMATED_TMUX_SOCK_PREFIX}-${AUTOMATED_OWNER_UID} socket" BRIGHT_BLUE
tmux -S "${AUTOMATED_TMUX_SOCK_PREFIX}-${AUTOMATED_OWNER_UID}" "$@"
}
multiplexer_present () {
declare multiplexer
for multiplexer in "${AUTOMATED_SUPPORTED_MULTIPLEXERS[@]}"; do
if cmd_is_available "$multiplexer"; then
printf '%s\n' "$multiplexer"
return 0
fi
done
return 1
}
run_in_tmux () {
declare command="$1"
if tmux_command ls 2>/dev/null | to_debug; then
log_debug "Multiplexer is already running"
exit "$AUTOMATED_EXIT_MULTIPLEXER_ALREADY_RUNNING_TMUX"
fi
declare sock_file="${AUTOMATED_TMUX_SOCK_PREFIX}-${AUTOMATED_OWNER_UID}"
declare fifo_file="${AUTOMATED_TMUX_FIFO_PREFIX}-${AUTOMATED_OWNER_UID}"
declare fifo_file_quoted
fifo_file_quoted=$(set -e; quoted "$fifo_file")
log_debug "Starting multiplexer and executing commands"
log_debug "$command"
mkfifo "$fifo_file"
declare tmux_version tmux_major tmux_minor
tmux_version=$(tmux_command -V)
if [[ "$tmux_version" =~ ^tmux\ ([0-9]+)\.([0-9]+) ]]; then
tmux_major="${BASH_REMATCH[1]}"
tmux_minor="${BASH_REMATCH[2]}"
else
throw "Failed to parse tmux version ${tmux_version}"
fi
tmux_command \
new-session \
-d \
"/usr/bin/env bash ${fifo_file_quoted}"
# https://github.com/tmux/tmux/issues/3220
if [[ "$tmux_major" -eq 3 && "$tmux_minor" -gt 2 ]] || [[ "$tmux_major" -gt 3 ]]; then
declare current_uid
current_uid=$(id -u)
if [[ "$current_uid" -ne "$AUTOMATED_OWNER_UID" ]]; then
declare owner_username
owner_username=$(id -un "$AUTOMATED_OWNER_UID")
tmux_command server-access -aw "$owner_username"
fi
fi
# shellcheck disable=SC2094
{
cat <<EOF
rm -f -- ${fifo_file_quoted}
EOF
automated_bootstrap_environment "$AUTOMATED_CURRENT_TARGET"
cat <<EOF
${command}
EOF
} >"$fifo_file"
chown "$AUTOMATED_OWNER_UID" "$sock_file"
exit "$AUTOMATED_EXIT_RUNNING_IN_TMUX"
}
run_in_multiplexer () {
declare multiplexer
if ! multiplexer=$(set -e; multiplexer_present); then
throw "Multiplexer is not available. Please install one of the following: ${AUTOMATED_SUPPORTED_MULTIPLEXERS[*]}"
fi
case "$multiplexer" in
tmux)
run_in_tmux "$@"
;;
esac
}
# shellcheck disable=SC2016
interactive_multiplexer_session () {
declare current_target_quoted
current_target_quoted=$(set -e; quoted "$AUTOMATED_CURRENT_TARGET")
declare script_quoted
script_quoted=$(set -e; quoted 'source $1; exec </dev/tty')
run_in_multiplexer "bash -i -s -- <(automated_bootstrap_environment ${current_target_quoted}) <<< ${script_quoted}"
}
interactive_answer () {
declare target="$1"
declare prompt="$2"
declare default_value="${3:-}"
declare answer
declare -a message=("$prompt" "(${target})")
[[ -z "$default_value" ]] || message+=("[${default_value}]")
{
printf '%s: ' "${message[*]}"
"${AUTOMATED_ANSWER_READ_COMMAND[@]}" answer
printf '\n'
} </dev/tty >/dev/tty
if [[ -n "$default_value" && -z "$answer" ]]; then
printf '%s\n' "$default_value"
else
printf '%s\n' "$answer"
fi
}
interactive_secret () {
declare -a AUTOMATED_ANSWER_READ_COMMAND=('read' '-r' '-s')
interactive_answer "$@"
}
confirm () {
declare target="$1"
declare prompt="$2"
declare default_value="${3:-N}"
declare answer
while true; do
answer=$(set -e; interactive_answer "$target" "${prompt} Y/N?" "$default_value")
case "$answer" in
[yY]) return 0
;;
[nN]) return 1
;;
esac
done
}
target_as_vars () {
declare target="$1"
declare username_var="${2:-username}"
declare address_var="${3:-address}"
declare port_var="${4:-port}"
declare username address port
declare -a args=()
if [[ "$target" =~ ^((.+)@)?(\[([:0-9A-Fa-f]+)\])(:([0-9]+))?$ ]] ||
[[ "$target" =~ ^((.+)@)?(([-.0-9A-Za-z]+))(:([0-9]+))?$ ]]; then
printf '%s=%q\n' "$username_var" "${BASH_REMATCH[2]}"
printf '%s=%q\n' "$address_var" "${BASH_REMATCH[4]}"
printf '%s=%q\n' "$port_var" "${BASH_REMATCH[6]}"
else
return 1
fi
}
# TODO: AUT-127 IPv6 addresses must be enclosed in square brackets.
target_address_only () {
declare target="$1"
declare username address port
declare var_definitions
var_definitions=$(set -e; target_as_vars "$target" username address port)
eval "$var_definitions"
printf '%s\n' "${address}${port:+:${port}}"
}
target_as_ssh_arguments () {
declare target="$1"
declare username address port
declare -a args=()
declare var_definitions
var_definitions=$(set -e; target_as_vars "$target" username address port)
eval "$var_definitions"
if [[ -n "$port" ]]; then
args+=(-p "$port")
fi
if [[ -n "$username" ]]; then
args+=(-l "$username")
fi
args+=("$address")
printf '%s\n' "${args[@]}"
}
is_function () {
declare name="$1"
[[ "$(type -t "$name")" = 'function' ]]
}
stdin_as_code () {
declare dst="${1:-}"
if [[ -n "$dst" ]]; then
printf 'base64_decode <<"EOF" | gzip -d >%q\n' "$dst"
else
printf 'base64_decode <<"EOF" | gzip -d\n'
fi
gzip -n -6 - | base64_encode
printf 'EOF\n'
}
file_as_code () {
declare src="$1"
declare dst="$2"
declare mode
if [[ -d "$src" ]]; then
throw "${src} is a directory. directories are not supported"
fi
if ! [[ -e "$src" ]]; then
throw "${src} does not exist"
fi
if [[ -f "$src" ]]; then
mode=$(set -e; file_mode "$src")
fi
stdin_as_code "$dst" <"$src"
if [[ -f "$src" ]]; then
printf 'chmod %s %q\n' "$mode" "$dst"
fi
printf 'log_debug %q\n' "copied ${src} to ${dst} on the target"
}
stdin_as_function () {
declare file_id="$1"
file_id_hash=$(md5 <<< "$file_id")
printf 'drop_%s_body () {\n' "$file_id_hash"
stdin_as_code
printf '}\n'
printf 'log_debug %q\n' "shipped STDIN as the file id ${file_id}"
}
file_as_function () {
declare src="$1"
declare file_id="${2:-${src}}"
declare mode file_id_hash
if [[ -d "$src" ]]; then
throw "${src} is a directory. directories are not supported"
fi
if ! [[ -e "$src" ]]; then
throw "${src} does not exist"
fi
file_id_hash=$(md5 <<< "$file_id")
if [[ -f "$src" ]]; then
mode=$(set -e; file_mode "$src")
printf 'AUTOMATED_DROP_%s_MODE=%s\n' "${file_id_hash^^}" "$mode"
fi
printf 'drop_%s_body () {\n' "$file_id_hash"
stdin_as_code <"$src"
printf '}\n'
printf 'log_debug %q\n' "shipped ${src} as the file id ${file_id}"
}
declared_var () {
declare var="$1"
(
set -e
if [[ -n "${2+defined}" ]]; then
unset "$var"
declare "${var}=${2}"
fi
declare -p "$var"
)
declare var_quoted
var_quoted=$(set -e; quoted "$var")
printf 'log_debug "declared variable %s"\n' "$var_quoted"
}
declared_function () {
declare fn="$1"
declare -f "$fn"
declare fn_quoted
fn_quoted=$(set -e; quoted "$fn")
printf 'log_debug "declared function %s"\n' "$fn_quoted"
}
drop () {
declare file_id="$1"
declare dst="${2:-}"
declare mode owner file_id_hash mode_var
file_id_hash=$(md5 <<< "$file_id")
is_function "drop_${file_id_hash}_body" || throw "File id ${file_id} is not dragged"
if [[ -n "$dst" ]]; then
mode_var="AUTOMATED_DROP_${file_id_hash^^}_MODE"
if [[ -n "${!mode_var:-}" ]]; then
mode="${3:-${!mode_var}}"
else
mode="${3:-}"
fi
owner="${4:-}"
# TODO: AUT-128 Support writing to pipes.
if ! [[ -e "$dst" ]]; then
touch "$dst"
fi
if [[ -n "${mode:-}" ]]; then
chmod "$mode" "$dst"
fi
if [[ -n "${owner:-}" ]]; then
chown "$owner" "$dst"
fi
"drop_${file_id_hash}_body" "${file_id}" | to_file "$dst"
else
"drop_${file_id_hash}_body" "$file_id"
fi
}
sourced_drop () {
declare file_id="$1"
declare file_id_hash
file_id_hash=$(md5 <<< "$file_id")
declare error_message_quoted log_message_quoted
error_message_quoted=$(set -e; quoted "File id ${file_id} is not dragged")
log_message_quoted=$(set -e; quoted "sourced file id ${file_id}")
cat <<EOF
is_function "drop_${file_id_hash}_body" || throw ${error_message_quoted}
source <(drop_${file_id_hash}_body)
log_debug ${log_message_quoted}
EOF
}
exit_after () {
declare exit_code="$1"
shift
"$@"
exit "$exit_code"
}
base64_encode () {
if [[ "$(set -e; local_kernel)" = 'Linux' ]] && cmd_is_available base64; then
base64 -w 0
elif cmd_is_available openssl; then
openssl base64 -A
else
python_interpreter -c 'from __future__ import unicode_literals, print_function; import sys; import base64; stdout = sys.stdout.buffer.write if hasattr(sys.stdout, "buffer") else sys.stdout.write; stdin = sys.stdin.buffer.read if hasattr(sys.stdin, "buffer") else sys.stdin.read; list(filter(None, (stdout(base64.b64encode(i)) for i in iter(lambda: stdin(3072), b""))))'
fi
printf '\n'
}
base64_decode () {
if [[ "$(set -e; local_kernel)" = 'Linux' ]] && cmd_is_available base64; then
base64 -d
elif cmd_is_available openssl; then
openssl base64 -d -A
else
python_interpreter -c 'from __future__ import unicode_literals, print_function; import sys; import base64; stdout = sys.stdout.buffer.write if hasattr(sys.stdout, "buffer") else sys.stdout.write; stdin = sys.stdin.buffer.read if hasattr(sys.stdin, "buffer") else sys.stdin.read; list(filter(None, (stdout(base64.b64decode(i)) for i in iter(lambda: stdin(3072), b""))))'
fi
}
python_interpreter () {
if cmd_is_available python3; then
python3 "$@"
else
python2 "$@"
fi
}
semver_matches_one_of () {
declare version_to_match="$1"
shift
declare -r SEMVER_RE='^([0-9]+).([0-9]+).([0-9]+)(-([0-9A-Za-z.-]+))?(\+([0-9A-Za-z.-]))?$'
declare -r VER_RE='^([0-9]+)(.([0-9]+))?(.([0-9]+))?$'
[[ "$version_to_match" =~ $SEMVER_RE ]] || return 1
declare major="${BASH_REMATCH[1]}"
declare minor="${BASH_REMATCH[2]}"
declare patch="${BASH_REMATCH[3]}"
declare version
for version in "$@"; do
[[ "$version" =~ $VER_RE ]] || continue
[[ "$major" -eq "${BASH_REMATCH[1]}" ]] || continue
if [[ -n "${BASH_REMATCH[3]:-}" ]]; then
[[ "$minor" -eq "${BASH_REMATCH[3]}" ]] || continue
fi
if [[ -n "${BASH_REMATCH[5]:-}" ]]; then
[[ "$patch" -eq "${BASH_REMATCH[5]}" ]] || continue
fi
return 0
done
return 1
}
bash_minor_version_is_higher_than () {
declare major="$1"
declare minor="$2"
! [[ "${BASH_VERSINFO[0]}" -lt "$major" ]] || return 1
! [[ "${BASH_VERSINFO[0]}" -eq "$major" && "${BASH_VERSINFO[1]}" -lt "$minor" ]] || return 1
}
supported_automated_versions () {
if ! semver_matches_one_of "$AUTOMATED_VERSION" "$@"; then
declare supported_versions
supported_versions=$(set -e; joined ', ' "$@")
throw "Unsupported version ${AUTOMATED_VERSION} of Automated detected. Supported versions are: ${supported_versions}"
fi
}
automated_bootstrap_environment () {
declare target="$1"
cat <<EOF
#!/usr/bin/env bash
set -euo pipefail
log_debug () {
# Mock for the bootstrap envirnment
return 0
}
# Inception :)
automated_environment_script () {
drop '__automated_environment'
}
EOF
declared_function 'cmd_is_available'
declared_function 'python_interpreter'
declared_function 'local_kernel'
declared_function 'base64_decode'
declared_function 'is_function'
declared_function 'throw'
# shellcheck disable=SC2064
automated_environment_script "$target" | file_as_function /dev/stdin '__automated_environment'
sourced_drop '__automated_environment'
}
deprecated_with () {
log_debug "${FUNCNAME[1]}() is DEPRECATED. Use ${1}() instead."
"$@"
}
deprecated_with_alternatives () {
log_debug "${FUNCNAME[1]}() is DEPRECATED. Alternatives ${*}"
}
deprecated_function () {
log_debug "${FUNCNAME[1]}() is DEPRECATED."
}