-
Notifications
You must be signed in to change notification settings - Fork 5
/
entrypoint
executable file
·132 lines (107 loc) · 2.86 KB
/
entrypoint
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
#! /bin/bash
child=1
_handle_term() {
echo "./bin/entrypoint Caught SIGTERM, forwarding to pid $child"
kill -TERM "$child" 2>/dev/null
}
_handle_usr2() {
echo "./bin/entrypoint Caught SIGUSR2, forwarding to pid $child"
kill -USR2 "$child" 2>/dev/null
}
_handle_quit() {
echo "./bin/entrypoint Caught SIGQUIT, forwarding to pid $child"
kill -QUIT "$child" 2>/dev/null
}
_handle_int() {
exec_after_terminate=bash
echo "./bin/entrypoint Caught SIGINT, forwarding to pid $child"
kill -INT "$child" 2>/dev/null
}
trap _handle_term SIGTERM
trap _handle_quit SIGQUIT
trap _handle_usr2 SIGUSR2
trap _handle_int SIGINT
wrap() {
declare desc="Proxy signals to the child process. Invoke shell on SIGINT"
echo "Starting the child process, watching for SIGINT"
echo "\$ $@ &"
$@ &
child=$!
echo "Child process started has pid $child"
wait $child
wait $child
echo "Child process $child exited with status $?"
if [ -n "$exec_after_terminate" ]
then
echo "Running exec_after_terminate: '$exec_after_terminate'"
exec $exec_after_terminate
fi
}
# Borrowed from https://github.com/gliderlabs/herokuish
yaml-esque-get() {
declare desc="Get key value from colon-separated structure"
declare key="$1"
local inputkey
local cmd
while read line || [[ -n "$line" ]]; do
[[ "$line" =~ ^#.* ]] && continue
inputkey=${line%%:*}
cmd=${line#*:}
[[ "$inputkey" == "$key" ]] && echo "$cmd"
done <<< "$(cat)"
}
procfile-parse() {
declare desc="Get command string for a process type from Procfile"
declare type="$1"
cat "Procfile" | yaml-esque-get "$type"
}
get-command() {
declare desc="Get the command mapping for a command word"
declare type="$1"
word="$(procfile-parse "$1")"
if [ -z "$word" ]
then cmd="$1"
else cmd="$word"
fi
echo "$cmd"
}
run-commands() {
declare desc="Run command words"
declare type="$1"
if [ "$(eval echo $(get-command "$1"))" = "$1" ]; then
# if the first command is the same as the first argument, then the command
# was not found (or it was identical to the command)
echo "entrypoint: Command word '$1' not found, running the whole string '$@' as a single command"
cmd="$CMD_PREFIX$(eval echo $@)"
exec $cmd
else
# loop through the command words and handle appropriately
until [ -z "$1" ]
do
cmd="$CMD_PREFIX$(eval echo $(get-command "$1"))"
if [ "$#" -gt "1" ]; then
echo "Only one command word at a time"
exit 1
else
echo "entrypoint: Command word is '"$1"', running '$cmd'"
if [ "$$" = "1" ]
then
wrap "$cmd"
else
exec $cmd
fi
fi
shift
done
fi
}
main() {
declare desc="Kick off the entrypoint script"
declare type="$@"
if [ $# -eq 0 ]; then
echo "entrypoint: No command words specified. Exiting"
else
run-commands "$@"
fi
}
main "$@"