-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterraria
executable file
·399 lines (326 loc) · 8.04 KB
/
terraria
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash
### BEGIN INIT INFO
# Provides: terraria
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop the Terraria multiplayer server
# Description: TerrariaService lets you start, stop and save a terraria server without keeping the TUI open
### END INIT INFO
# Source: https://wiki.debian.org/LSBInitScripts
CONFIG_PATH=`readlink -e $0 | sed "s:[^/]*$:config.cfg:"`
## Import configuration file
if [ -f $CONFIG_PATH ]; then
source $CONFIG_PATH
else
# Could not find the config, create new one
cp $CONFIG_PATH.example $CONFIG_PATH
if ! [ -f $CONFIG_PATH ]; then
echo "Could not create new config file in: $PWD"
exit 1
else
echo "New file added: config.cfg"
echo "Edit the config.cfg file and start the script again."
exit 0
# source config.cfg
fi
fi
## Default values
init()
{
if [ -z "${DEBUGGING}" ]; then
DEBUGGING=true
fi
if [ -z "${WRITE_DIR}" ]; then
WRITE_DIR=/opt/terraria-service
fi
if [ -z "${TERRARIA_PATH}" ]; then
TERRARIA_PATH=/opt/terraria
fi
if [ -z "${TERRARIA_BINARY}" ]; then
TERRARIA_BINARY=TerrariaServer
fi
CURRENT_USER=`whoami`
FIFO="${WRITE_DIR}/server.fifo" # First in, First out file for commands
CMDOUT="${WRITE_DIR}/server.out" # Server output log
PIDFILE="${WRITE_DIR}/server.pid" # Server proces ID
ERRLOG="${WRITE_DIR}/errors.log" # Error log
TERRARIA_CONFIG="${TERRARIA_PATH}/serverconfig.txt"
ARGS="-config serverconfig.txt" # Server requires a server config to instantly boot
INVOKE="${TERRARIA_PATH}/${TERRARIA_BINARY} ${ARGS}"
}
init
## Debugger
debug()
{
echo "Debugger:"
echo -e " USERNAME:\t\t${USERNAME}"
echo -e " CURRENT_USER:\t\t${CURRENT_USER}"
echo -e " TERRARIA_PATH:\t${TERRARIA_PATH}"
echo -e " TERRARIA_BINARY:\t${TERRARIA_BINARY}"
echo -e " WRITE_DIR:\t\t${WRITE_DIR}"
echo -e " PIDFILE:\t\t${PIDFILE}"
echo -e " ERRLOG:\t\t${ERRLOG}"
echo -e " LOG:\t\t\t${CMDOUT}"
echo
}
## Usage display
usage()
{
echo "Usage: $0 <COMMAND>"
echo
echo "Commands:"
echo -e " start\t\t Starts the server"
echo -e " stop\t\t Stops the server without saving"
echo -e " save\t\t Saves the world"
echo -e " exit\t\t Saves the world and stop the server"
echo -e " save-exit\t Alias for 'exit'"
echo -e " checkfiles\t Checks the files for any issues"
echo -e " state\t\t Displays the server state"
echo -e " kill\t\t Kills the server process"
echo -e " help\t\t Show this help message"
}
# Check file status
check_files()
{
PERMISSIONS_ERR=0
MISSING_CONFIG=false
# Terraria server
if ! [ -d ${TERRARIA_PATH} ] ; then
echo "Could not find the terraria server directory at ${TERRARIA_PATH}."
echo "Make sure that you have downloaded the terraria dedicated server from https://terraria.org before starting the service."
exit 1
fi
# Service folder permissions
## Not using the compact [-w ${WRITE_DIR} so i can test the user]
if ! exec_user "test -w ${WRITE_DIR}" ; then
echo "Missing write permissions: ${WRITE_DIR}"
((PERMISSIONS_ERR++))
fi
# Server folder permissions
if ! exec_user "test -w ${TERRARIA_PATH}" ; then
echo "Missing write permissions: ${TERRARIA_PATH}"
((PERMISSIONS_ERR++))
fi
# Terraria server config
if ! [ -f ${TERRARIA_CONFIG} ] ; then
echo "Missing file: ${TERRARIA_CONFIG}"
MISSING_CONFIG=true
elif ! exec_user "test -r ${TERRARIA_CONFIG}" ; then
echo "Missing permissions: ${TERRARIA_CONFIG}"
((PERMISSIONS_ERR++))
fi
# Terraria executable permissions
if ! [ -x ${TERRARIA_PATH}/${TERRARIA_BINARY} ] ; then
echo "Missing executable permissions: ${TERRARIA_PATH}/${TERRARIA_BINARY}"
((PERMISSIONS_ERR++))
fi
# Fix permissions
if ! [ "$PERMISSIONS_ERR" -eq "0" ]; then
echo "Found ${PERMISSIONS_ERR} errors!"
fix_permissions
fi
# Create missing files
if $MISSING_CONFIG ; then
init_terraria_config
fi
}
# Attempt to fix the permissions
fix_permissions()
{
read -r -p "Attempt to auto-fix permissions? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
# Set user permissions to the whole terraria path
sudo chown -R $USERNAME:$USERNAME ${TERRARIA_PATH}
# Set user permissions to the whole service path
sudo chown -R $USERNAME:$USERNAME ${WRITE_DIR}
# Set executable permissions on the binary
sudo chmod +x ${TERRARIA_PATH}/${TERRARIA_BINARY}
# Check again
check_files
;;
*)
exit 1
;;
esac
}
# Init new server config
init_terraria_config()
{
read -r -p "Want to generate a new simple terraria config? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
# Create new file
echo "maxplayers=8" > ${TERRARIA_CONFIG}
echo "world=${TERRARIA_PATH}/Worlds/NewWorld.wld" >> ${TERRARIA_CONFIG}
echo "worldname=NewWorld" >> ${TERRARIA_CONFIG}
echo "worldpath=${TERRARIA_PATH}/Worlds/" >> ${TERRARIA_CONFIG}
echo "autocreate=2" >> ${TERRARIA_CONFIG}
echo "difficulty=0" >> ${TERRARIA_CONFIG}
echo "Created new file: ${TERRARIA_CONFIG}"
# Check again
check_files
;;
*)
exit 1
;;
esac
}
# Execute for User
## Helper function
## Executes the provided command
exec_user()
{
if [ $CURRENT_USER == $USERNAME ]; then # Are you the terraria user?
if $DEBUGGING; then
echo "Executing: ${1}"
fi
bash -c "$1"
elif [ "$(id -u)" == "0" ]; then # Are you root?
if $DEBUGGING; then
echo "Executing as ROOT: ${1}"
fi
su $USERNAME -s /bin/bash -c "$1"
else # You are neither
echo "Run this script as the $USERNAME user or edit the config.cfg file!"
exit 1
fi
}
# Start script
## Creates a new PID file
start_service()
{
# Check if file exists
if [ -f ${PIDFILE} ]; then
echo "File already exists!"
return 1
fi
# Create new PIDFILE
if ! exec_user "touch ${PIDFILE}" ; then
echo "Could not create a new PID file"
return 1
fi
# Create error log
if ! [ -f ${ERRLOG} ]; then
echo "Error log does not exist! Making a new one"
if ! exec_user "touch ${ERRLOG}"; then
echo "Could not make a error log file!"
return 1;
fi
fi
# FIFO file?
if ! [ -f ${FIFO} ]; then
exec_user "touch ${FIFO}"
fi
# Attempt to start the process along with the PID into the PIDFILE
exec_user "tail -f ${FIFO} | ${INVOKE} >> ${CMDOUT} 2>&1 & echo \$! > ${PIDFILE}"
# Check if online
if [ "$?" -ne "0" ]; then
echo "Unable to start ${SERVICE_NAME}"
return 1
else
echo "Started the server, please see log for details (${CMDOUT})"
fi
}
# Stop service
stop_service()
{
echo "Stopping server.."
terraria_command "exit"
## TODO: Cleanly close FIFO tail
}
# Kill service
kill_service()
{
if is_running ; then
echo "Killing server process.."
exec_user "kill -TERM $(cat ${PIDFILE} 2> /dev/null) 2> /dev/null"
exec_user "rm ${PIDFILE} 2> /dev/null"
fi
}
# Check if server is running
## Why is 0 true and 1 false?
is_running()
{
# Check if file does not exists
if ! [ -r ${PIDFILE} ]; then
return 1
fi
# Check if the process is still running
if ! ps --pid $(cat ${PIDFILE}) > /dev/null ; then
# Process is not running and the PIDFILE should not be here
exec_user "rm ${PIDFILE} 2> /dev/null"
return 1
fi
# Server up and running
return 0
}
# Save game
## TODO: Check if server is running
save()
{
echo "Saving game.."
terraria_command "save"
}
# Execute server command
terraria_command()
{
echo $1 >> ${FIFO} # Send command to file
sleep 1s
> ${FIFO} # Clear file
}
## Middleware
if $DEBUGGING; then
debug
fi
## Routing
case "$1" in
start)
check_files
if is_running; then
echo "Could not start the terraria server: Server is already running"
exit 1
elif ! start_service; then
echo "Could not start the terraria server"
exit 1
else
exit 0
fi
;;
stop)
## A service handles stop on its own by killing the process PID
;;
save-exit)
stop_service
;;
exit)
stop_service
;;
save)
save
;;
checkfiles)
check_files
;;
state)
if is_running ; then
echo "Server is up and running"
else
echo "Server is offline"
fi
;;
help|--help|-h)
usage
;;
kill)
kill_service
;;
*)
echo "Unknown command"
usage
exit 1
;;
esac
exit 0