-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·1252 lines (969 loc) · 26.6 KB
/
run.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
#
# Copyright (c) 2017 - 2021 Roland Kujundzic <[email protected]>
#
# shellcheck disable=SC1091,SC1001,SC1090,SC2009,SC2033,SC2034,SC2046,SC2048,SC2068,SC2086,SC2119,SC2120,SC2153,SC2154,SC2183,SC2206
#
test -z "$RKBASH_DIR" && RKBASH_DIR="$HOME/.rkbash/$$"
if declare -A __hash=([key]=value) 2>/dev/null; then
test "${__hash[key]}" = 'value' || { echo -e "\nERROR: declare -A\n"; exit 1; }
unset __hash
else
echo -e "\nERROR: declare -A\n"
exit 1
fi
if test "${@: -1}" = 'help' 2>/dev/null; then
for a in ps tr xargs head grep awk find sed sudo cd chown chmod mkdir rm ls; do
command -v $a >/dev/null || { echo -e "\nERROR: missing $a\n"; exit 1; }
done
fi
function _abort {
local msg line rf brf nf
rf="\033[0;31m"
brf="\033[1;31m"
nf="\033[0m"
msg="$1"
if test -n "$2"; then
msg="$2"
line="[$1]"
fi
if test "$NO_ABORT" = 1; then
ABORT=1
echo -e "${rf}WARNING${line}: ${msg}${nf}"
return 1
fi
msg="${rf}${msg}${nf}"
local frame trace
if type -t caller >/dev/null 2>/dev/null; then
frame=0
trace=$(while caller $frame; do ((frame++)); done)
msg="$msg\n\n$trace"
fi
if [[ -n "$LOG_LAST" && -s "$LOG_LAST" ]]; then
msg="$msg\n\n$(tail -n+5 "$LOG_LAST")"
fi
test -n "$ABORT_MSG" && msg="$msg\n\n$ABORT_MSG"
echo -e "\n${brf}ABORT${line}:${nf} $msg\n" 1>&2
local other_pid=
if test -n "$APP_PID"; then
for a in $APP_PID; do
other_pid=$(ps aux | grep -E "^.+\\s+$a\\s+" | awk '{print $2}')
test -z "$other_pid" || kill "$other_pid" 2>/dev/null 1>&2
done
fi
if test -n "$APP"; then
other_pid=$(ps aux | grep "$APP" | awk '{print $2}')
test -z "$other_pid" || kill "$other_pid" 2>/dev/null 1>&2
fi
exit 1
}
function _add_abort_linenum {
local lines changes tmp_file fix_line
type -t caller >/dev/null 2>/dev/null && return
_mkdir "$RKBASH_DIR/add_abort_linenum"
tmp_file="$RKBASH_DIR/add_abort_linenum/"$(basename "$1")
test -f "$tmp_file" && _abort "$tmp_file already exists"
echo -n "add line number to _abort in $1"
changes=0
readarray -t lines < "$1"
for ((i = 0; i < ${#lines[@]}; i++)); do
fix_line=$(echo "${lines[$i]}" | grep -E -e '(;| \|\|| &&) _abort ["'"']" -e '^\s*_abort ["'"']" | grep -vE -e '^\s*#' -e '^\s*function ')
if test -z "$fix_line"; then
echo "${lines[$i]}" >> "$tmp_file"
else
changes=$((changes+1))
echo "${lines[$i]}" | sed -E 's/^(.*)_abort (.+)$/\1_abort '$((i+1))' \2/g' >> "$tmp_file"
fi
done
echo " ($changes)"
_cp "$tmp_file" "$1" >/dev/null
}
function _apt_install {
local curr_lne
curr_lne=$LOG_NO_ECHO
LOG_NO_ECHO=1
_require_program apt
_run_as_root 1
_rkbash_dir
for a in $*; do
if test -d "$RKBASH_DIR/apt/$a"; then
_msg "already installed, skip: apt -y install $a"
else
sudo apt -y install "$a" || _abort "apt -y install $a"
_log "apt -y install $a" "apt/$a"
fi
done
_rkbash_dir reset
LOG_NO_ECHO=$curr_lne
}
function _cd {
local has_realpath curr_dir goto_dir
has_realpath=$(command -v realpath)
if [[ -n "$has_realpath" && -n "$1" ]]; then
curr_dir=$(realpath "$PWD")
goto_dir=$(realpath "$1")
if test "$curr_dir" = "$goto_dir"; then
return
fi
fi
test -z "$2" && _msg "cd '$1'"
if test -z "$1"; then
if test -n "$LAST_DIR"; then
_cd "$LAST_DIR"
return
else
_abort "empty directory path"
fi
fi
if ! test -d "$1"; then
_abort "no such directory [$1]"
fi
LAST_DIR="$PWD"
cd "$1" || _abort "cd '$1' failed"
}
function _chmod {
local tmp cmd i priv
test -z "$1" && _abort "empty privileges parameter"
test -z "$2" && _abort "empty path"
tmp=$(echo "$1" | sed -E 's/[012345678]*//')
test -z "$tmp" || _abort "invalid octal privileges '$1'"
cmd="chmod -R"
if test -n "$CHMOD"; then
cmd="$CHMOD"
CHMOD=
fi
if test -z "$2"; then
for ((i = 0; i < ${#FOUND[@]}; i++)); do
priv=
if test -f "${FOUND[$i]}" || test -d "${FOUND[$i]}"; then
priv=$(stat -c "%a" "${FOUND[$i]}")
fi
if test "$1" != "$priv" && test "$1" != "0$priv"; then
_sudo "$cmd $1 '${FOUND[$i]}'" 1
fi
done
elif test -f "$2"; then
priv=$(stat -c "%a" "$2")
if [[ "$1" != "$priv" && "$1" != "0$priv" ]]; then
_sudo "$cmd $1 '$2'" 1
fi
elif test -d "$2"; then
_sudo "$cmd $1 '$2'" 1
fi
}
function _confirm {
local msg
msg="\033[0;35m$1\033[0m"
CONFIRM=
if test -n "$AUTOCONFIRM"; then
CONFIRM="${AUTOCONFIRM:0:1}"
echo -e "$msg <$CONFIRM>"
AUTOCONFIRM="${AUTOCONFIRM:1}"
return
fi
if test -z "$CONFIRM_COUNT"; then
CONFIRM_COUNT=1
else
CONFIRM_COUNT=$((CONFIRM_COUNT + 1))
fi
local flag cckey default
flag=$(($2 + 0))
if test $((flag & 2)) = 2; then
if test $((flag & 1)) = 1; then
CONFIRM=n
else
CONFIRM=y
fi
return
fi
while read -r -d $'\0'
do
cckey="--q$CONFIRM_COUNT"
if test "$REPLY" = "$cckey=y"; then
echo "found $cckey=y, accept: $1"
CONFIRM=y
elif test "$REPLY" = "$cckey=n"; then
echo "found $cckey=n, reject: $1"
CONFIRM=n
fi
done < /proc/$$/cmdline
if test -n "$CONFIRM"; then
CONFIRM_TEXT="$CONFIRM"
return
fi
if test $((flag & 1)) -ne 1; then
default=n
echo -n -e "$msg y [n] "
read -r -n1 -t 10 CONFIRM
echo
else
default=y
echo -n -e "$msg \033[0;35m[y]\033[0m n "
read -r -n1 -t 3 CONFIRM
echo
fi
if test -z "$CONFIRM"; then
CONFIRM="$default"
fi
CONFIRM_TEXT="$CONFIRM"
if test "$CONFIRM" != "y"; then
CONFIRM=n
fi
}
function _cp {
local curr_lno target_dir md1 md2 pdir
curr_lno="$LOG_NO_ECHO"
LOG_NO_ECHO=1
CP_FIRST=
CP_KEEP=
test -z "$2" && _abort "empty target"
target_dir=$(dirname "$2")
test -d "$target_dir" || _abort "no such directory [$target_dir]"
if test "$3" != 'md5'; then
:
elif ! test -f "$2"; then
CP_FIRST=1
elif test -f "$1"; then
md1=$(_md5 "$1")
md2=$(_md5 "$2")
if test "$md1" = "$md2"; then
_msg "_cp: keep $2 (same as $1)"
CP_KEEP=1
else
_msg "Copy file $1 to $2 (update)"
_sudo "cp '$1' '$2'" 1
fi
return
fi
if test -f "$1"; then
_msg "Copy file $1 to $2"
_sudo "cp '$1' '$2'" 1
elif test -d "$1"; then
if test -d "$2"; then
pdir="$2"
_confirm "Remove existing target directory '$2'?"
if test "$CONFIRM" = "y"; then
_rm "$pdir"
_msg "Copy directory $1 to $2"
_sudo "cp -r '$1' '$2'" 1
else
_msg "Copy directory $1 to $2 (use rsync)"
_rsync "$1/" "$2"
fi
else
_msg "Copy directory $1 to $2"
_sudo "cp -r '$1' '$2'" 1
fi
else
_abort "No such file or directory [$1]"
fi
LOG_NO_ECHO="$curr_lno"
}
function _docker_rm {
_docker_stop "$1"
if test -n "$(docker ps -a | grep "$1")"; then
echo "docker rm $1"
docker rm "$1"
fi
}
function _docker_run {
_docker_rm "$1"
if [[ -n "$WORKSPACE" && -n "$CURR" && -d "$WORKSPACE/linux/rkdocker" ]]; then
_cd "$WORKSPACE/linux/rkdocker"
else
_abort "Export WORKSPACE (where $WORKSPACE/linux/rkdocker exists) and CURR=path/current/directory"
fi
local config
if test -f "$CURR/$2"; then
config="$CURR/$2"
elif test -f "$2"; then
config="$2"
else
_abort "No such configuration $CURR/$2 ($PWD/$2)"
fi
echo "DOCKER_NAME=$1 ./run.sh $config start"
DOCKER_NAME=$1 ./run.sh $2 start
_cd "$CURR"
}
function _docker_stop {
if test -n "$(docker ps | grep "$1")"; then
echo "docker stop $1"
docker stop "$1"
fi
}
function _find {
FOUND=()
local a
_require_program find
_require_dir "$1"
while read -r a; do
FOUND+=("$a")
done < <(eval "find '$1' $2" || _abort "find '$1' $2")
}
function _is_running {
_os_type linux
local rx out res
res=0
if test "$1" = 'apache'; then
rx='[a]pache2.*k start'
elif test "$1" = 'nginx'; then
rx='[n]ginx.*master process'
elif test "${1:0:7}" = 'docker:'; then
rx="[d]ocker-proxy.* -host-port ${1:7}"
elif test "${1:0:5}" = 'port:'; then
out=$(netstat -tulpn 2>/dev/null | grep -E ":${1:5} .+:* .+LISTEN.*")
else
_abort "invalid [$1] use apache|nginx|docker:PORT|port:N|rx:[n]ame"
fi
test -z "$rx" || out=$(ps aux 2>/dev/null | grep -E "$rx")
test -z "$out" && res=1
return $res
}
declare -Ai LOG_COUNT # define hash (associative array) of integer
declare -A LOG_FILE # define hash
declare -A LOG_CMD # define hash
LOG_NO_ECHO=
function _log {
test -z "$LOG_NO_ECHO" && _msg "$1" -n
if test -z "$2"; then
test -z "$LOG_NO_ECHO" && echo
return
fi
LOG_COUNT[$2]=$((LOG_COUNT[$2] + 1))
LOG_FILE[$2]="$RKBASH_DIR/$2/${LOG_COUNT[$2]}.nfo"
LOG_CMD[$2]=">>'${LOG_FILE[$2]}' 2>&1"
LOG_LAST=
if ! test -d "$RKBASH_DIR/$2"; then
mkdir -p "$RKBASH_DIR/$2"
if test -n "$SUDO_USER"; then
chown -R $SUDO_USER.$SUDO_USER "$RKBASH_DIR" || _abort "chown -R $SUDO_USER.$SUDO_USER '$RKBASH_DIR'"
elif test "$UID" = "0"; then
chmod -R 777 "$RKBASH_DIR" || _abort "chmod -R 777 '$RKBASH_DIR'"
fi
fi
local now
now=$(date +'%d.%m.%Y %H:%M:%S')
echo -e "# _$2: $now\n# $PWD\n# $1 ${LOG_CMD[$2]}\n" > "${LOG_FILE[$2]}"
if test -n "$SUDO_USER"; then
chown $SUDO_USER.$SUDO_USER "${LOG_FILE[$2]}" || _abort "chown $SUDO_USER.$SUDO_USER '${LOG_FILE[$2]}'"
elif test "$UID" = "0"; then
chmod 666 "${LOG_FILE[$2]}" || _abort "chmod 666 '${LOG_FILE[$2]}'"
fi
test -z "$LOG_NO_ECHO" && _msg " ${LOG_CMD[$2]}"
test -s "${LOG_FILE[$2]}" && LOG_LAST="${LOG_FILE[$2]}"
}
function _md5 {
_require_program md5sum
if test -z "$1"; then
_abort "Empty parameter"
elif test -f "$1"; then
md5sum "$1" | awk '{print $1}'
elif test "$2" = "1"; then
echo -n "$1" | md5sum | awk '{print $1}'
else
_abort "No such file [$1]"
fi
}
function _merge_sh {
local a my_app mb_app sh_dir rkbash_inc tmp_app md5_new md5_old inc_sh scheck
my_app="${1:-$APP}"
sh_dir="${my_app}_"
if test -n "$2"; then
my_app="$2"
sh_dir="$1"
else
_require_file "$my_app"
mb_app=$(basename "$my_app")
test -d "$sh_dir" || { test -d "$mb_app" && sh_dir="$mb_app"; }
fi
test "${ARG[static]}" = "1" && rkbash_inc=$(_merge_static "$sh_dir")
_require_dir "$sh_dir"
tmp_app="$sh_dir"'_'
test -s "$my_app" && md5_old=$(_md5 "$my_app")
_msg "merge $sh_dir into $my_app ... " -n
inc_sh=$(find "$sh_dir" -name '*.inc.sh' 2>/dev/null | sort)
scheck=$(grep -E '^# shellcheck disable=' $inc_sh | sed -E 's/.+ disable=(.+)$/\1/g' | tr ',' ' ' | xargs -n1 | sort -u | xargs | tr ' ' ',')
test -z "$scheck" || RKS_HEADER_SCHECK="shellcheck disable=SC1091,$scheck"
if test -z "$rkbash_inc"; then
_rks_header "$tmp_app" 1
else
_rks_header "$tmp_app"
echo "$rkbash_inc" >> "$tmp_app"
fi
for a in $inc_sh; do
tail -n+2 "$a" | grep -E -v '^# shellcheck disable=' >> "$tmp_app"
done
_add_abort_linenum "$tmp_app"
md5_new=$(_md5 "$tmp_app")
if test "$md5_old" = "$md5_new"; then
_msg "no change"
_rm "$tmp_app" >/dev/null
else
_msg "update"
_mv "$tmp_app" "$my_app"
_chmod 755 "$my_app"
fi
test -z "$2" && exit 0
}
function _merge_static {
local a rks_inc inc_sh
inc_sh=$(find "$1" -name '*.inc.sh' 2>/dev/null | sort)
for a in $inc_sh; do
_rkbash_inc "$a"
rks_inc="$rks_inc $RKBASH_INC"
done
for a in $(_sort $rks_inc); do
tail -n +2 "$RKBASH_SRC/${a:1}.sh" | grep -E -v '^\s*#'
done
}
function _mkdir {
local flag
flag=$(($2 + 0))
test -z "$1" && _abort "Empty directory path"
if test -d "$1"; then
test $((flag & 1)) = 1 && _abort "directory $1 already exists"
test $((flag & 4)) = 4 && _msg "directory $1 already exists"
else
_msg "mkdir -p $1"
$SUDO mkdir -p "$1" || _abort "mkdir -p '$1'"
fi
test $((flag & 2)) = 2 && _chmod 777 "$1"
}
function _msg {
if test "$2" == '-n'; then
echo -n -e "\033[0;2m$1\033[0m"
else
echo -e "\033[0;2m$1\033[0m"
fi
}
function _mv {
if test -z "$1"; then
_abort "Empty source path"
fi
if test -z "$2"; then
_abort "Empty target path"
fi
local pdir
pdir=$(dirname "$2")
if ! test -d "$pdir"; then
_abort "No such directory [$pdir]"
fi
local AFTER_LAST_SLASH=${1##*/}
if test "$AFTER_LAST_SLASH" = "*"
then
_msg "mv $1 $2"
mv "$1" "$2" || _abort "mv $1 $2 failed"
else
_msg "mv '$1' '$2'"
mv "$1" "$2" || _abort "mv '$1' '$2' failed"
fi
}
function _ok {
echo -e "\033[0;32m$1\033[0m" 1>&2
}
function _os_type {
local os me
_require_program uname
me=$(uname -s)
if [ "$(uname)" = "Darwin" ]; then
os="macos"
elif [ "$OSTYPE" = "linux-gnu" ]; then
os="linux"
elif [ "${me:0:5}" = "Linux" ]; then
os="linux"
elif [ "${me:0:5}" = "MINGW" ]; then
os="cygwin"
fi
if test -z "$1"; then
echo $os
elif test "$1" != "$os"; then
_abort "$1 required (this is $os)"
fi
return 0
}
declare -A ARG
declare ARGV
function _parse_arg {
test "${#ARG[@]}" -gt 0 && return
ARGV=()
local i n key val
n=0
for (( i = 0; i <= $#; i++ )); do
ARGV[$i]="${!i}"
val="${!i}"
key=
if [[ "$val" =~ ^\-?\-?[a-zA-Z0-9_\.\-]+= ]]; then
key="${val/=*/}"
val="${val#*=}"
test "${key:0:2}" = '--' && key="${key:2}"
test "${key:0:1}" = '-' && key="${key:1}"
elif [[ "$val" =~ ^\-\-[[a-zA-Z0-9_\.\-]+$ ]]; then
key="${val:2}"
val=1
fi
if test -z "$key"; then
ARG[$n]="$val"
n=$(( n + 1 ))
elif test -z "${ARG[$key]}"; then
ARG[$key]="$val"
else
ARG[$key]="${ARG[$key]} $val"
fi
done
ARG[#]=$n
}
function _require_dir {
test -d "$1" || _abort "no such directory '$1'"
test -z "$2" || _require_owner "$1" "$2"
test -z "$3" || _require_priv "$1" "$3"
}
function _require_file {
test -f "$1" || _abort "no such file '$1'"
test -z "$2" || _require_owner "$1" "$2"
test -z "$3" || _require_priv "$1" "$3"
}
function _require_global {
local a has_hash bash_version
bash_version=$(bash --version | grep -iE '.+bash.+version [0-9\.]+' | sed -E 's/^.+version ([0-9]+)\.([0-9]+)\..+$/\1.\2/i')
for a in "$@"; do
has_hash="HAS_HASH_$a"
if (( $(echo "$bash_version >= 4.4" | bc -l) )); then
typeset -n ARR=$a
if test -z "$ARR" && test -z "${ARR[@]:1:1}"; then
_abort "no such global variable $a"
fi
elif test -z "${a}" && test -z "${has_hash}"; then
_abort "no such global variable $a - add HAS_HASH_$a if necessary"
fi
done
}
function _require_owner {
if ! test -f "$1" && ! test -d "$1"; then
_abort "no such file or directory '$1'"
fi
local arr owner group
arr=( ${2//:/ } )
owner=$(stat -c '%U' "$1" 2>/dev/null)
test -z "$owner" && _abort "stat -c '%U' '$1'"
group=$(stat -c '%G' "$1" 2>/dev/null)
test -z "$group" && _abort "stat -c '%G' '$1'"
if [[ -n "${arr[0]}" && "${arr[0]}" != "$owner" ]]; then
_abort "invalid owner - chown ${arr[0]} '$1'"
fi
if [[ -n "${arr[1]}" && "${arr[1]}" != "$group" ]]; then
_abort "invalid group - chgrp ${arr[1]} '$1'"
fi
}
function _require_priv {
test -z "$2" && _abort "empty privileges"
local priv
priv=$(stat -c '%a' "$1" 2>/dev/null)
test -z "$priv" && _abort "stat -c '%a' '$1'"
test "$2" = "$priv" || _abort "invalid privileges [$priv] - chmod -R $2 '$1'"
}
function _require_program {
local ptype
ptype=$(type -t "$1")
test "$ptype" = "function" && return 0
command -v "$1" >/dev/null 2>&1 && return 0
command -v "./$1" >/dev/null 2>&1 && return 0
if test "${2:0:4}" = "apt:"; then
_apt_install "${2:4}"
return 0
fi
[[ -n "$2" || "$NO_ABORT" = 1 ]] && return 1
local frame trace
if type -t caller >/dev/null 2>/dev/null; then
frame=0
trace=$(while caller $frame; do ((frame++)); done)
fi
echo -e "\n\033[1;31mABORT:\033[0m \033[0;31mNo such program [$1]\033[0m\n\n$trace\n" 1>&2
exit 1
}
function _rkbash_dir {
if [[ "$RKBASH_DIR" = "$HOME/.rkbash" && "$1" = 'reset' ]]; then
RKBASH_DIR="$HOME/.rkbash/$$"
return
fi
if [[ "$RKBASH_DIR" != "$HOME/.rkbash/$$" ]]; then
:
elif test -z "$1"; then
RKBASH_DIR="$HOME/.rkbash"
elif [[ "$1" != 'reset' ]]; then
RKBASH_DIR="$HOME/.rkbash/$1"
_mkdir "$RKBASH_DIR"
fi
}
function _rkbash_inc {
local _HAS_SCRIPT
declare -A _HAS_SCRIPT
if test -z "$RKBASH_SRC"; then
if test -s "src/abort.sh"; then
RKBASH_SRC='src'
else
_abort 'set RKBASH_SRC'
fi
elif ! test -s "$RKBASH_SRC/abort.sh"; then
_abort "invalid RKBASH_SRC='$RKBASH_SRC'"
fi
test -s "$1" || _abort "no such file '$1'"
_rrs_scan "$1"
RKBASH_INC=$(_sort ${!_HAS_SCRIPT[@]})
RKBASH_INC_NUM="${#_HAS_SCRIPT[@]}"
}
function _rrs_scan {
local a func_list
test -f "$1" || _abort "no such file '$1'"
func_list=$(grep -E -o -e '(_[a-z0-9\_]+)' "$1" | xargs -n1 | sort -u | xargs)
for a in $func_list; do
if [[ -z "${_HAS_SCRIPT[$a]}" && -s "$RKBASH_SRC/${a:1}.sh" ]]; then
_HAS_SCRIPT[$a]=1
_rrs_scan "$RKBASH_SRC/${a:1}.sh"
fi
done
}
function _rks_app {
_parse_arg "$@"
local me p1 p2 p3
me="$0"
p1="$1"
p2="$2"
p3="$3"
test -z "$me" && _abort 'call _rks_app "$@"'
test -z "${ARG[1]}" || p1="${ARG[1]}"
test -z "${ARG[2]}" || p2="${ARG[2]}"
test -z "${ARG[3]}" || p3="${ARG[3]}"
if test -z "$APP"; then
APP="$me"
APP_DIR=$( cd "$( dirname "$APP" )" >/dev/null 2>&1 && pwd )
CURR="$PWD"
if test -z "$APP_PID"; then
export APP_PID="$$"
elif test "$APP_PID" != "$$"; then
export APP_PID="$APP_PID $$"
fi
fi
test -z "${#SYNTAX_CMD[@]}" && _abort "SYNTAX_CMD is empty"
test -z "${#SYNTAX_HELP[@]}" && _abort "SYNTAX_HELP is empty"
[[ "$p1" = 'self_update' ]] && _merge_sh
[[ "$p1" = 'help' ]] && _syntax "*" "cmd:* help:*"
test -z "$p1" && return
test -n "${SYNTAX_HELP[$p1]}" && APP_DESC="${SYNTAX_HELP[$p1]}"
[[ -n "$p2" && -n "${SYNTAX_HELP[$p1.$p2]}" ]] && APP_DESC="${SYNTAX_HELP[$p1.$p2]}"
[[ -n "$p2" && -n "${SYNTAX_CMD[$p1.$p2]}" && ("$p3" = 'help' || "${ARG[help]}" = '1') ]] && \
_syntax "$p1.$p2" "help:"
[[ -n "${SYNTAX_CMD[$p1]}" && ("$p2" = 'help' || "${ARG[help]}" = '1') ]] && \
_syntax "$p1" "help:"
test "${ARG[help]}" = '1' && _syntax "*" "cmd:* help:*"
}
function _rks_header {
local flag header copyright
copyright=$(date +"%Y")
flag=$(($2 + 0))
[ -z "${RKS_HEADER+x}" ] || flag=$((RKS_HEADER + 0))
if test -f ".gitignore"; then
copyright=$(git log --diff-filter=A -- .gitignore | grep 'Date:' | sed -E 's/.+ ([0-9]+) \+[0-9]+/\1/')" - $copyright"
fi
test $((flag & 1)) = 1 && \
header='source /usr/local/lib/rkbash.lib.sh || { echo -e "\nERROR: source /usr/local/lib/rkbash.lib.sh\n"; exit 1; }'
printf '\x23!/usr/bin/env bash\n\x23\n\x23 Copyright (c) %s Roland Kujundzic <[email protected]>\n\x23\n\x23 %s\n\x23\n\n' \
"$copyright" "$RKS_HEADER_SCHECK" > "$1"
test -z "$header" || echo "$header" >> "$1"
}
function _rm {
test -z "$1" && _abort "Empty remove path"
if ! test -f "$1" && ! test -d "$1"; then
test -z "$2" || _abort "No such file or directory '$1'"
else
_msg "remove '$1'"
rm -rf "$1" || _abort "rm -rf '$1'"
fi
}
function _rsync {
local target="$2"
test -z "$target" && target="."
test -z "$1" && _abort "Empty rsync source"
test -d "$target" || _abort "No such directory [$target]"
local rsync="rsync -av $3 -e ssh '$1' '$2'"
local error
_log "$rsync" rsync
eval "$rsync ${LOG_CMD[rsync]}" || error=1
if test "$error" = "1"; then
test -z "$(tail -4 "${LOG_FILE[rsync]}" | grep 'speedup is ')" && _abort "$rsync"
test -z "$(tail -1 "${LOG_FILE[rsync]}" | grep "rsync error:")" || \
_warn "FIX rsync errors in ${LOG_FILE[rsync]}"
fi
}
function _run_as_root {
test "$UID" = "0" && return
if test -z "$1"; then
_abort "Please change into root and try again"
else
echo "sudo true - you might need to type in your password"
sudo true 2>/dev/null || _abort "sudo true failed - Please change into root and try again"
fi
}
function _service {
test -z "$1" && _abort "empty service name"
test -z "$2" && _abort "empty action"
local is_active
is_active=$(systemctl is-active "$1")
if [[ "$is_active" != 'active' && ! "$2" =~ start && ! "$2" =~ able ]]; then
_abort "$is_active service $1"
fi
if test "$2" = 'status'; then
_ok "$1 is active"
return
fi
_msg "systemctl $2 $1"
_sudo "systemctl $2 $1"
}
function _sort {
echo "$@" | xargs -n1 | sort -u | xargs
}
function _stop_http {
_os_type linux
if ! _is_running port:80; then
_warn "no service on port 80"
return
fi
if _is_running docker:80; then
_warn "ignore docker service on port 80"
return
fi
if _is_running nginx; then
_service nginx stop
elif _is_running apache; then
_service apache2 stop
fi
}
function _sudo {
local curr_sudo exec flag
curr_sudo="$SUDO"
exec="$1"
flag=$(($2 + 0))
if test "$USER" = "root"; then
_log "$exec" sudo
eval "$exec ${LOG_CMD[sudo]}" || _abort "$exec"
elif test $((flag & 1)) = 1 && test -z "$curr_sudo"; then
_log "$exec" sudo
eval "$exec ${LOG_CMD[sudo]}" || \
( _msg "try sudo $exec"; eval "sudo $exec ${LOG_CMD[sudo]}" || _abort "sudo $exec" )
else
SUDO=sudo
_log "sudo $exec" sudo
eval "sudo $exec ${LOG_CMD[sudo]}" || _abort "sudo $exec"
SUDO="$curr_sudo"
fi
LOG_LAST=
}
declare -A SYNTAX_CMD
declare -A SYNTAX_HELP
function _syntax {
local a msg old_msg desc base syntax
msg=$(_syntax_cmd "$1")
syntax="\n\033[1;31mSYNTAX:\033[0m"
for a in $2; do
old_msg="$msg"