-
Notifications
You must be signed in to change notification settings - Fork 14
/
tshock.sh
executable file
·132 lines (121 loc) · 2.59 KB
/
tshock.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
#!/usr/bin/env bash
##########
# Default Options
##########
IMAGE="mark2dot0/tshock:latest"
CONTAINER=""
BASEDIR=""
WORLD="world0.wld"
GAMEPORT="7777"
RESTPORT="7878"
##########
# usage
# Print usage message and exit with status 1
##########
usage () {
cat << EOF
Usage:
tshock [-g PORT] [-p PORT] [-d BASEDIR] start NAME
tshock attach NAME
tshock stop NAME
Options:
NAME Name of the docker container
-d BASEDIR Directory for mounted volumes (default: $HOME/NAME)
-p PORT Port to bind terraria server to (default: 7777)
-r PORT Port to bind REST API to (default: 7878)
EOF
exit 1
}
##########
# running NAME
# Check if a container named NAME is running
# Returns status 0 if running, otherwise 1
##########
running () {
docker inspect -f {{.State.Running}} "$1" 2> /dev/null \
| grep true &> /dev/null
}
##########
# stopcontainer NAME
# Gracefully stop tshock container named NAME
##########
stopcontainer () {
expect << EOF
spawn docker attach ${CONTAINER}
send "\\off\r"
expect eof
EOF
}
##########
# Parse Options
##########
while getopts hp:r:d: FLAG; do
case "${FLAG}" in
h)
usage
;;
p)
GAMEPORT="${OPTARG}"
;;
r)
RESTPORT="${OPTARG}"
;;
d)
BASEDIR="${OPTARG}"
;;
esac
done
shift $(($OPTIND - 1))
if [[ "$#" -lt "2" ]]; then
usage
fi
ACTION="$1"
CONTAINER="$2"
if [[ -z "${BASEDIR}" ]]; then
BASEDIR="${HOME}/${CONTAINER}"
fi
##########
# Main Script
##########
case "${ACTION}" in
start)
if running ${CONTAINER}; then
echo "${CONTAINER} already running"
exit 1
fi
echo "Starting ${CONTAINER}"
mkdir -p ${BASEDIR}/{worlds,logs,config}
docker run --name="${CONTAINER}" -itd \
-p ${GAMEPORT}:7777 \
-p ${RESTPORT}:7878 \
-v ${BASEDIR}/worlds:/opt/tshock/worlds \
-v ${BASEDIR}/logs:/opt/tshock/logs \
-v ${BASEDIR}/config:/opt/tshock/config \
${IMAGE} \
-configpath /opt/tshock/config \
-logpath /opt/tshock/logs \
-worldpath /opt/tshock/worlds \
-world /opt/tshock/worlds/${WORLD} \
-autocreate 2 2> /dev/null || \
docker start ${CONTAINER}
;;
attach)
if ! running ${CONTAINER}; then
echo "${CONTAINER} not running"
exit 1
fi
echo "Attaching ${CONTAINER}, press Ctrl-p Ctrl-q to detach"
docker attach ${CONTAINER}
;;
stop)
if ! running ${CONTAINER}; then
echo "${CONTAINER} not running"
exit 1
fi
echo "Stopping ${CONTAINER}"
# Try to gracefully shutdown the server
# fallback to force kill after 10 seconds
( sleep 10 && docker stop ${CONTAINER} &> /dev/null $$ ) &
stopcontainer ${CONATINER}
;;
esac