-
Notifications
You must be signed in to change notification settings - Fork 4
/
run
executable file
·151 lines (124 loc) · 2.85 KB
/
run
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
#!/bin/bash
set -e
set -u
function usage() {
echo "usage: $(basename "$0") [-v] <target> [<target>...]"
exit 1
}
function root_path() {
if [[ -z "${APPLY_ROOT:-}" ]]; then
cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd
else
echo "$APPLY_ROOT"
fi
}
function read_group() {
local target="$*"
while read -r item; do
if [[ ! "$item" == '#'* ]]; then
echo "$item"
fi
done < "$(root_path)/$target"
}
function run_group() {
local target="$*"
for item in $(read_group "$target"); do
if [[ ! "$item" == '#'* ]]; then
run_pretty "$item"
fi
done
}
function run_unit() {
/bin/bash -l -c "set -e; set -u; set -o pipefail; source lib; source '$(root_path)/$*'"
}
function log_file() {
echo -n "$LOG_DIR/"
echo "${1/\//_}.log"
}
function check_target() {
local target="$*"
if [[ ! -f "$(root_path)/$target" ]]; then
echo "missing $target"
return 1
fi
if [[ $target == groups/* ]]; then
for item in $(read_group "$target"); do
check_target "$item"
done
fi
}
function run_pretty() {
local target="$*"
case "$target" in
groups/*)
echo -e "\033[33m** \033[34m$target \033[33mprocessing...\033[0m"
run_group "$target"
echo -e "\033[33m** \033[34m$target \033[32mOK\033[0m"
;;
units/*)
run_pretty_unit "$target"
;;
*)
echo "unsupported command: $target"
;;
esac
}
function run_pretty_unit() {
local rc
local log_file
local target="$*"
echo -e -n "\033[33m** \033[34m$target\033[0m"
[[ "$VERBOSE" == "1" ]] && echo -e ": \033[33mstarting...\033[0m"
log_file=$(log_file "$@")
if [[ "$VERBOSE" == "1" ]]; then
set +e
run_unit "$@"
rc=$?
set -e
else
set +e
run_unit "$@" >"$log_file" 2>&1
rc=$?
set -e
fi
if [[ "$VERBOSE" == "1" ]]; then
echo -e -n "\033[33m** \033[34m$target\033[0m: "
else
echo -n " "
fi
if [[ $rc -eq 0 ]]; then
echo -e "\033[32mOK\033[0m"
else
echo -e "\033[31mFAILED\033[0m"
if [[ "$VERBOSE" == "1" ]]; then
echo "For details, see output above" 1>&2
else
echo "Here are the last lines of output:" 1>&2
tail -n20 "$log_file" 1>&2
echo "For details, see $log_file" 1>&2
fi
exit $rc
fi
}
if [[ $# -lt 1 ]]; then
usage
fi
if [[ "$1" == "-v" ]]; then
VERBOSE="1"
shift
else
VERBOSE="0"
fi
if [[ $# -lt 1 ]]; then
usage
fi
targets=( "$@" )
# pre-flight checks
for target in "${targets[@]}"; do
check_target "$target"
done
# fly!
LOG_DIR=$(mktemp -d)
for target in "${targets[@]}"; do
run_pretty "$target"
done