-
Notifications
You must be signed in to change notification settings - Fork 0
/
runit
executable file
·268 lines (245 loc) · 7.38 KB
/
runit
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
#!/bin/bash
# ======================================================
# Copyright (c) 2017 xxxxx.com, Inc. All Rights Reserved
# ------------------------------------------------------
# @file runit
# @author [email protected]
# @date 2017/01/02
# @brief A process runner and monitor for shell good
# -coders application. run 'runit -h' for help.
# For detail, please reference to:
# http://gitlab.xxxxx.com/cmc-shell/runit.
# ======================================================
set -e -o pipefail
readonly DEFAULT_ENVFILE=.env
readonly DEFAULT_PROCFILE=Procfile
readonly EC_VERIFY_ENVFILE=1
readonly EC_VERIFY_PROCFILE=2
function usage() {
echo 'Usage: runit [-c] [-f procfile|Procfile] [-e envfile|.env]'
}
##########################################################
# @Brief : Clear the comment line.
# @Globals : None
# @Args : textline
# @Rets : 0
# @Stdin : None
# @Stdout : None for comment line, otherwise the line.
##########################################################
function clear_line() {
local line="$1"
if [[ "${line}" =~ ^[#$] ]]; then
return
fi
echo "${line}"
}
##########################################################
# @Brief : Verify the procfile.
# @Globals : ${procfile}, ${EC_VERIFY_PROCFILE}
# @Args : None
# @Rets : 0 or ${EC_VERIFY_PROCFILE}
# @Stdin : None
# @Stdout : Fail messages
##########################################################
function verify_procfile() {
local has_err=0
local text="$(<"${procfile}")"
local line
while read -r line; do
if [[ -z "$(clear_line "${line}")" ]]; then
continue
fi
line="${line%%#*}"
local name="${line%%:*}"
if [[ ! "${line}" =~ ^.+:.+$ ]]; then
has_err=${EC_VERIFY_PROCFILE}
echo 'no_colon_command'
fi
if [[ "${name}" =~ [^a-zA-Z0-9_] ]]; then
has_err=${EC_VERIFY_PROCFILE}
echo 'invalid_char'
fi
done < <(echo "${text/'\r'/'\n'}")
return $has_err
}
##########################################################
# @Brief : Verify the envfile.
# @Globals : ${envfile}, ${EC_VERIFY_ENVFILE}
# @Args : None
# @Rets : 0 or ${EC_VERIFY_ENVFILE}
# @Stdin : None
# @Stdout : Fail messages
##########################################################
function verify_envfile() {
local has_err=0
local text="$(<"${envfile}")"
local line
while read -r line; do
if [[ -z "$(clear_line "${line}")" ]]; then
continue
fi
line="${line%%#*}"
if [[ ! "${line}" =~ ^.+=.+$ ]]; then
has_err=${EC_VERIFY_ENVFILE}
echo 'no_equal_mark'
fi
if [[ "${line%%=*}" =~ [^a-zA-Z0-9_] ]]; then
has_err=${EC_VERIFY_ENVFILE}
echo 'invalid_char'
fi
if [[ "${line}" =~ \s ]]; then
has_err=${EC_VERIFY_ENVFILE}
echo 'value_have_space'
fi
done < <(echo "${text/'\r'/'\n'}")
return $has_err
}
##########################################################
# @Brief : Verify the envfile and procfile.
# @Globals : None
# @Args : None
# @Rets : 0 or EC_VERIFY_*
# @Stdin : None
# @Stdout : None
##########################################################
function verify() {
verify_envfile && verify_procfile
}
##########################################################
# @Brief : Load the environments.
# @Globals : ${envfile}
# @Args : None
# @Rets : 0
# @Stdin : None
# @Stdout : None
##########################################################
function load_env_file() {
local text="$(<"${envfile}")"
local line
while read -r line; do
if [[ -z "$(clear_line "${line}")" ]]; then
continue
fi
line="${line%%#*}"
export "${line}"
done < <(echo "${text/'\r'/'\n'}")
}
##########################################################
# @Brief : Print logs to the screen.
# @Globals : ${RANDOM}
# @Args : Log name, command to run
# @Rets : 0
# @Stdin : Pids of logged process, messages
# @Stdout : Colored formated log string
##########################################################
function log() {
local pid
read pid
echo ${pid} >> .pid
local name="$1"
shift;
local comm="$@"
local timestamp="$(date -d now '+%H:%M:%S')"
local color=$(((${pid}+${RANDOM})%8+30))
printf "\033[%dm%s\t%s\t| %s started with pid %d\033[0m\n" ${color} "${timestamp}" "${name}" "${comm}" ${pid}
while read line; do
timestamp="$(date -d now '+%H:%M:%S')"
printf "\033[%dm%s\t%s\t| %s\033[0m\n" ${color} "${timestamp}" "${name}" "${line}"
done
}
##########################################################
# @Brief : Start the processes.
# @Globals : None
# @Args : No comment textline from procfile
# @Rets : 0
# @Stdin : None
# @Stdout : None
##########################################################
function run_command() {
local line="$*"
local name="${line%%:*}"
local comm="${line#*:}"
local run="$(eval "echo \"${comm}\"")"
# When the first command exited or killed, the broken pipe will make the
# second subshell exit.
/bin/bash -c "echo \$\$ && exec ${run}" 2>&1 | log "${name}" "${run}" &
}
##########################################################
# @Brief : Wait the processes.
# @Globals : None
# @Args : None
# @Rets : 0
# @Stdin : None
# @Stdout : None
##########################################################
function wait_all() {
declare -ar pids=("$(<.pid)")
for p in ${pids[@]}; do
wait $p
done
}
##########################################################
# @Brief : Kill the processes.
# @Globals : None
# @Args : None
# @Rets : 0
# @Stdin : None
# @Stdout : None
##########################################################
function kill_all() {
declare -ar pids=("$(<.pid)")
set +e
for p in ${pids[@]}; do
echo "Killing $p"
kill -9 $p
done
set -e
}
##########################################################
# @Brief : Parse and process the procfile
# @Globals : ${procfile}
# @Args : None
# @Rets : 0
# @Stdin : None
# @Stdout : None
##########################################################
function run_procfile() {
export PORT=${PORT:-8080}
trap 'echo Caught CR-C/CR-- && kill_all' SIGINT SIGTERM
local text=$(<"${procfile}")
while read -r line; do
if [[ -z "$(clear_line "${line}")" ]]; then
continue
fi
local line="${line%%#*}"
run_command "${line}"
if [[ -n $(echo "${line}" | grep '$PORT') ]]; then
export PORT=$((${PORT}+1))
fi
done < <(echo "${text/'\r'/'\n'}")
wait_all
}
function main() {
local func=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h) shift; usage; exit 0;;
-e) shift; envfile="$1"; shift;;
-f) shift; procfile="$1"; shift;;
-c) shift; func=1;;
*) usage; exit -1;;
esac
done
envfile="${envfile:-${DEFAULT_ENVFILE}}"
touch "${envfile}"
procfile="${procfile:-${DEFAULT_PROCFILE}}"
touch "${procfile}"
verify
if [[ ${func} -ne 0 ]]; then
return 0
fi
:>.pid
load_env_file
run_procfile
}
main "$@"