-
Notifications
You must be signed in to change notification settings - Fork 6
/
run.sh
205 lines (169 loc) · 6.21 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
#!/usr/bin/env bash
main(){
do_set_vars "$@" # is inside, unless --help flag is present
ts=$(date "+%Y%m%d_%H%M%S")
main_log_dir=~/var/log/$RUN_UNIT/; mkdir -p $main_log_dir
main_exec "$@" \
> >(tee $main_log_dir/$RUN_UNIT.$ts.out.log) \
2> >(tee $main_log_dir/$RUN_UNIT.$ts.err.log)
}
main_exec(){
do_resolve_os
do_check_install_min_req_bins
do_load_functions
test -z ${actions:-} || {
do_run_actions "$actions"
}
do_finalize
}
#------------------------------------------------------------------------------
# the "reflection" func - identify the the funcs per file
#------------------------------------------------------------------------------
get_function_list () {
env -i PATH=/bin:/usr/bin:/usr/local/bin bash --noprofile --norc -c '
source "'"$1"'"
typeset -f |
grep '\''^[^{} ].* () $'\'' |
awk "{print \$1}" |
while read -r fnc_name; do
type "$fnc_name" | head -n 1 | grep -q "is a function$" || continue
echo "$fnc_name"
done
'
}
do_read_cmd_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-a|--actions) shift && actions="${actions:-}${1:-} " && shift ;;
-h|--help) actions=' do_print_usage ' && ENV=lde && shift ;;
*) echo FATAL unknown "cmd arg: '$1' - invalid cmd arg, probably a typo !!!" && shift && exit 1
esac
done
shift $((OPTIND -1))
}
do_run_actions(){
actions=$1
cd $PRODUCT_DIR
actions="$(echo -e "${actions}"|sed -e 's/^[[:space:]]*//')" #or how-to trim leading space
run_funcs=''
while read -d ' ' arg_action ; do
while read -r fnc_file ; do
#debug func fnc_file:$fnc_file
while read -r fnc_name ; do
#debug fnc_name:$fnc_name
action_name=`echo $(basename $fnc_file)|sed -e 's/.func.sh//g'`
action_name=`echo do_$action_name|sed -e 's/-/_/g'`
# debug action_name: $action_name
test "$action_name" != "$arg_action" && continue
source $fnc_file
test "$action_name" == "$arg_action" && run_funcs="$(echo -e "${run_funcs}\n$fnc_name")"
done< <(get_function_list "$fnc_file")
done < <(find "src/bash/run/" -type f -name '*.func.sh'|sort)
done < <(echo "$actions")
run_funcs="$(echo -e "${run_funcs}"|sed -e 's/^[[:space:]]*//;/^$/d')"
while read -r run_func ; do
cd $PRODUCT_DIR
do_log "INFO START ::: running action :: $run_func"
$run_func
if [[ "${exit_code:-}" != "0" ]]; then
do_log "FATAL failed to run action: $run_func !!!"
exit $exit_code
fi
do_log "INFO STOP ::: running function :: $run_func"
done < <(echo "$run_funcs")
}
do_flush_screen(){
# printf "\033[2J";printf "\033[0;0H"
echo ""
}
#------------------------------------------------------------------------------
# echo pass params and print them to a log file and terminal
# usage:
# do_log "INFO some info message"
# do_log "DEBUG some debug message"
#------------------------------------------------------------------------------
do_log(){
type_of_msg=$(echo $*|cut -d" " -f1)
msg="$(echo $*|cut -d" " -f2-)"
log_dir="${PRODUCT_DIR:-}/dat/log/bash" ; mkdir -p $log_dir
log_file="$log_dir/${RUN_UNIT:-}.`date "+%Y%m%d"`.log"
echo " [$type_of_msg] `date "+%Y-%m-%d %H:%M:%S %Z"` [${RUN_UNIT:-}][@${host_name:-}] [$$] $msg " | \
tee -a $log_file
}
do_check_install_min_req_bins(){
which perl > /dev/null 2>&1 || {
run_os_func install_bins perl
}
which jq > /dev/null 2>&1 || {
run_os_func install_bins jq
}
which make > /dev/null 2>&1 || {
# this will not work properly - google how-to install make on <<my-operating-system>>
run_os_func install_bins make
}
}
do_set_vars(){
set -u -o pipefail
do_read_cmd_args "$@"
# test $? -eq "0" && export exit_code='0'
export exit_code=1 # assume failure for each action, enforce return code usage
export host_name="$(hostname -s)"
unit_run_dir=$(perl -e 'use File::Basename; use Cwd "abs_path"; print dirname(abs_path(@ARGV[0]));' -- "$0")
export RUN_UNIT=$(cd $unit_run_dir/../../.. ; basename `pwd`)
export PRODUCT_DIR=$(cd $unit_run_dir/../../.. ; echo `pwd`)
ENV="${ENV:=lde}" # https://stackoverflow.com/a/2013589/65706
valid_envs=("lde dev prd stg all")
[[ " ${valid_envs[*]} " =~ " ${ENV} " ]] || {
echo -e "ENV must be one of the following : \n export ENV={lde dev stg prd all}" && exit 1
}
cd $PRODUCT_DIR
# workaround for github actions running on docker
test -z ${GROUP:-} && export GROUP=$(id -gn)
test -z ${GROUP:-} && export GROUP=$(ps -o group,supgrp $$|tail -n 1|awk '{print $1}')
test -z ${USER:-} && export USER=$(id -un)
test -z ${UID:-} && export UID=$(id -u)
test -z ${GID:-} && export GID=$(id -g)
}
do_finalize(){
# do_flush_screen
cat << EOF_FIN_MSG
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
$RUN_UNIT run completed
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
EOF_FIN_MSG
}
do_load_functions(){
while read -r f; do source $f; done < <(ls -1 $PRODUCT_DIR/lib/bash/funcs/*.sh)
while read -r f; do source $f; done < <(ls -1 $PRODUCT_DIR/src/bash/run/*.func.sh)
}
run_os_func(){
func_to_run=$1 ; shift ;
if [ $(uname -s) == *"Linux"* ]; then
distro=$(cat /etc/os-release|egrep '^ID='|cut -d= -f2)
if [ $distro == "ubuntu" ] || [ $distro == "pop" ]; then
"do_ubuntu_""$func_to_run" "$@"
elif [ $distro == "alpine" ]; then
"do_alpine_""$func_to_run" "$@"
else
echo "your Linux distro is not supported !!!"
fi
elif [ $(uname -s) == *"Linux"* ]; then
echo "you are running on mac"
"do_mac_""$func_to_run" "$@"
fi
}
do_resolve_os(){
if [ $(uname -s) == *"Linux"* ]; then
distro=$(cat /etc/os-release|egrep '^ID='|cut -d= -f2)
if [ $distro == "ubuntu" ] || [ $distro == "pop" ]; then
export OS=ubuntu
elif [ $distro == "alpine" ]; then
export OS=alpine
else
echo "your Linux distro is not supported !!!"
fi
elif [ $(uname -s) == *"Darwin"* ]; then
export OS=mac
fi
}
main "$@"