-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentry_point.sh
executable file
·93 lines (83 loc) · 1.7 KB
/
entry_point.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
#!/bin/sh
function false() { return 1; }
function true() { return 0; }
INITED=false
if [ -f .inited ]; then
INITED=true
fi
INIT_COMMAND="$1"
echo
echo "Workspace: $(pwd)"
echo "ENV COMMAND=$COMMAND"
echo "ENV ARGS=$ARGS"
echo
# TODO: try make a init system, or use one that already exists in apk
# function _broadcast_signal(){
# sig=$1
# kill "-$sig" "-1" # Cannot use gid=1 here, sincu some process must be killed by their parent (such as mcdreforged)
# wait "$pid"
# }
# function _broadcast_signals(){
# for sig; do
# trap "_broadcast_signal $sig"
# done
# }
# function trap_and_wait(){
# "$@" &
# pid=$!
# _forward_signals SIGHUP SIGINT SIGQUIT SIGILL SIGABRT SIGTERM SIGSTOP
# wait $pid
# }
function exec_command(){
exec $COMMAND $ARGS
}
function initer(){
if "$INITED"; then
echo "WARN: This containter already inited"
fi
echo
echo "Running init command $INIT_COMMAND"
"$INIT_COMMAND" || exit $?
if ! "$INITED"; then
echo "Creating .inited"
touch .inited
fi
}
_CMD=$2
if ! [ -n "$_CMD" ]; then
if ! "$INITED"; then
echo "Not inited, run initer"
initer || exit $?
echo "Just inited, executing command '$COMMAND $ARGS'"
fi
exec_command
fi
case "$_CMD" in
run)
if ! "$INITED"; then
echo "Not inited, please use command 'init' for init"
exit 1
fi
exec_command
;;
init)
initer
;;
sh | shell)
echo 'Enterning `/bin/sh`'
echo
echo 'Hint: you can use ctrl+p+q to detach this container'
echo
exec /bin/sh
;;
*)
echo "Unknown command '$_CMD'"
echo
echo 'Commands:'
echo ' run: run the server with env `COMMAND` and `ARGS`'
echo ' init: initialize the server with default initializer'
echo ' sh, shell: enter the shell mode'
echo
exit 2
;;
esac