-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuwo
394 lines (364 loc) · 9.96 KB
/
cuwo
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
#!/bin/bash
# /etc/init.d/cuwo
### BEGIN INIT INFO
# Provides: cuwo
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Cube World server
# Description: Init script for cuwo server.
### END INIT INFO
# Based upon minecraft-init by Ahtenus
# Based on http://www.minecraftwiki.net/wiki/Server_startup_script
# Modified to support the cuwo Cube World open-source server by alfonsojon
# OS X doesn't support init.d scripts, so let's prevent running on OS X.
# This assumes that everyone using Darwin is on OS X. Sorry, Darwin users!
if [ `uname` == "Darwin" ]; then
echo "OS X is not currently supported."
exit 1
fi
# Loads config file
if [ -L $0 ]
then
source `readlink -e $0 | sed "s:[^/]*$:config:"`
else
source `echo $0 | sed "s:[^/]*$:config:"`
fi
if [ "$SCREEN" == "" ]
then
echo "Couldn't load config file, please edit config.example and rename it to config"
logger -t cuwo-init "Couldn't load config file, please edit config.example and rename it to config"
exit
fi
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ]; then
bash -c "$1"
else
su $USERNAME -s /bin/bash -c "$1"
fi
}
is_running() {
# Checks for the cuwo server screen session
# returns true if it exists.
pidfile=${CUWOPATH}/${SCREEN}.pid
if [ -f "$pidfile" ]
then
pid=$(head -1 $pidfile)
if ps ax | grep -v grep | grep ${pid} | grep "${SCREEN}" > /dev/null
then
return 0
else
if [ -z "$isInStop" ]
then
if [ -z "$roguePrinted" ]
then
roguePrinted=1
echo "Rogue pidfile found!"
fi
fi
return 1
fi
else
if ps ax | grep -v grep | grep "${SCREEN} ./run_server.sh" > /dev/null
then
echo "No pidfile found, but server's running."
echo "Re-creating the pidfile."
pid=$(ps ax | grep -v grep | grep "${SCREEN} ./run_server.sh" | cut -f1 -d' ')
check_permissions
as_user "echo $pid > $pidfile"
return 0
else
return 1
fi
fi
}
datepath() {
# datepath path filending-to-check returned-filending
# Returns an file path with added date between the filename and file ending.
# $1 filepath (not including file ending)
# $2 file ending to check for uniqueness
# $3 file ending to return
if [ -e $1`date +%F`$2 ]
then
echo $1`date +%FT%T`$3
else
echo $1`date +%F`$3
fi
}
cuwo_start() {
pidfile=${CUWOPATH}/${SCREEN}.pid
check_permissions
as_user "cd $CUWOPATH && screen -dmS $SCREEN $CUWOPATH/run_server.sh"
as_user "screen -list | grep "\.$SCREEN" | cut -f1 -d'.' | head -n 1 | tr -d -c 0-9 > $pidfile"
#
# Waiting for the server to start
#
seconds=0
until is_running
do
sleep 1
seconds=$seconds+1
if [[ $seconds -eq 5 ]]
then
echo "Still not running, waiting a while longer..."
fi
if [[ $seconds -ge 120 ]]
then
echo "Failed to start, aborting."
exit 1
fi
done
echo "Server is running."
}
cuwo_command() {
if is_running
then
as_user "screen -p 0 -S $SCREEN -X eval '/stuff \"$(eval echo $FORMAT)\"\015'"
else
echo "$SERVICE was not running. Not able to run command."
fi
}
cuwo_say() {
if is_running
then
echo "Said: $1"
cuwo_command "[SERVER] $1"
else
echo "$SERVICE was not running. Not able to say anything."
fi
}
cuwo_stop() {
pidfile=${CUWOPATH}/${SCREEN}.pid
#
# Stops the server
#
sleep 10
echo "Stopping server..."
cuwo_command stop
sleep 0.5
#
# Waiting for the server to shut down
#
seconds=0
isInStop=1
while is_running
do
sleep 1
seconds=$seconds+1
if [[ $seconds -eq 3 ]]
then
echo "Still not shut down, waiting a while longer..."
fi
if [[ $seconds -ge 30 ]]
then
logger -t cuwo-init "Failed to shut down server, aborting."
echo "Failed to shut down, aborting."
exit 1
fi
done
as_user "rm $pidfile"
unset isInStop
is_running
echo "Server is now shut down."
}
check_backup_settings() {
case "$BACKUPFORMAT" in
tar)
COMPRESSCMD="tar -hcjf"
STORECMD="tar -cpf"
ARCHIVEENDING=".tar.bz2"
STOREDENDING=".tar"
EXCLUDEARG="-X "
;;
zip)
COMPRESSCMD="zip -rq"
STORECMD="zip -rq -0"
ARCHIVEENDING=".zip"
STOREDENDING=".zip"
EXCLUDEARG="-x@"
;;
*)
echo "$BACKUPFORMAT is not a supported backup format"
exit 1
;;
esac
}
cuwo_whole_backup() {
check_backup_settings
echo "backing up entire setup into $WHOLEBACKUP"
path=`datepath $WHOLEBACKUP/cuwo_`
locationOfScript=$(dirname "$(readlink -e "$0")")
as_user "mkdir -p $path"
if [ -r "$locationOfScript/exclude.list" ]
then
echo "...except the following files and/or dirs:"
cat $locationOfScript/exclude.list
exclude="$EXCLUDEARG$locationOfScript/exclude.list"
fi
if [ "$COMPRESS_WHOLEBACKUP" ]
then
as_user "$COMPRESSCMD $path/whole-backup$ARCHIVEENDING $CUWOPATH $exclude"
else
as_user "$STORECMD $path/whole-backup$STOREDENDING $CUWOPATH $exclude"
fi
}
cuwo_world_backup() {
#
# Backup the worlds and puts them in a folder for each day (unless $BACKUPSCRIPTCOMPATIBLE is set)
#
check_backup_settings
get_worlds
today="`date +%F`"
as_user "mkdir -p $BACKUPPATH"
echo "Backing up the Cube World world.}"
as_user "mkdir -p $BACKUPPATH/${today}"
path=`datepath $BACKUPPATH/${today}/data}_ $ARCHIVEENDING $ARCHIVEENDING`
as_user "$COMPRESSCMD $path $CUWOPATH/data}"
}
get_script_location() {
echo $(dirname "$(readlink -e "$0")")
}
check_permissions() {
as_user "touch $pidfile"
if ! as_user "test -w '$pidfile'" ; then
echo "Check Permissions. Cannot write to $pidfile. Correct the permissions and then excute: $0 status"
fi
}
case "$1" in
start)
# Starts the server
if is_running; then
echo "Server already running."
else
cuwo_start
fi
;;
stop)
# Stops the server
if is_running; then
cuwo_say "Server shutting down."
cuwo_stop
to_disk
else
echo "No running server."
fi
;;
restart)
# Restarts the server
if is_running; then
cuwo_say "Server restarting in 10 seconds."
cuwo_stop
else
echo "No running server, starting it..."
fi
cuwo_start
;;
backup)
# Backups world
if is_running; then
cuwo_say "Complete backup begins in 10 seconds."
cuwo_say "Note: Server will restart for backup."
cuwo_stop
cuwo_world_backup
cuwo_start
else
cuwo_world_backup
fi
;;
whole-backup)
# Backup everything
if is_running; then
cuwo_say "Complete backup begins in 10 seconds."
cuwo_say "Note: Server will restart for backup."
cuwo_stop
cuwo_whole_backup
cuwo_start
else
cuwo_whole_backup
fi
;;
say)
# Says something to the ingame chat
if is_running; then
shift 1
cuwo_say "$*"
else
echo "No running server to say anything."
fi
;;
command)
if is_running; then
shift 1
cuwo_command "$*"
echo "Sent command: $*"
else
echo "No running server to send a command to."
fi
;;
log)
# Display server log using 'cat'.
cat ${CUWOPATH}/logs/log.txt
;;
status)
# Shows server status
if is_running
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
screen)
if is_running; then
as_user "script /dev/null -q -c \"screen -rx $SCREEN\""
else
echo "Server is not running. Do you want to start it?"
echo "Please put \"Yes\", or \"No\": "
read START_SERVER
case "$START_SERVER" in
[Yy]|[Yy][Ee][Ss])
cuwo_start
as_user "script /dev/null -q -c \"screen -rx $SCREEN\""
;;
[Nn]|[Nn][Oo])
clear
echo "Aborting startup!"
sleep 1
clear
exit 1
;;
*)
clear
echo "Invalid input"
sleep 1
clear
exit 1
;;
esac
fi
;;
help|--help|-h)
echo "Usage: $0 COMMAND"
echo
echo "Available commands:"
echo -e " start \t\t Starts the server"
echo -e " stop \t\t Stops the server"
echo -e " restart \t\t Restarts the server"
echo -e " reload \t\t Reloads the server plugins"
echo -e " backup \t\t Backups the worlds defined in the script"
echo -e " whole-backup \t Backups the entire server folder"
echo -e " log \t\t\t Prints the server log"
echo -e " say \t\t\t Prints the given string to the ingame chat"
echo -e " status \t\t Displays server status"
echo -e " screen \t\t Shows the server screen"
;;
*)
echo "No such command, see $0 help"
exit 1
;;
esac
exit 0