-
Notifications
You must be signed in to change notification settings - Fork 6
/
factorio
executable file
·625 lines (544 loc) · 16.7 KB
/
factorio
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#!/bin/bash
if [ -z "${DEBUG}" ]; then
DEBUG=0
fi
debug(){
if [ ${DEBUG} -gt 0 ]; then
echo "DEBUG: $@" >&2
fi
}
# Determine the environment:
if [ -z "${FENV}" ]; then
FENV=default
fi
debug "Active environment: ${FENV}"
if [ -f "/etc/factorio/init.conf" ]; then
source /etc/factorio/init.conf
debug "Sourced base init.conf"
fi
if [ -f "/etc/factorio/${FENV}/init.conf" ]; then
source /etc/factorio/${FENV}/init.conf
debug "Sourced ${FENV} init.conf"
fi
if [ ${DEBUG} -gt 0 ]; then
# Enable bash tracing in DEBUG mode to help debug ;)
set -x
fi
if [ -z "${FUSERNAME}" ]; then
FUSERNAME=factorio
fi
if [ -z "${USERGROUP}" ]; then
USERGROUP=${FUSERNAME}
fi
if [ -z "${HEADLESS}" ]; then
# This is a server init script, assume headless default
HEADLESS=1
fi
if [ -z "${NONCMDPATTERN}" ]; then
# This is a server init script, assume headless default
NONCMDPATTERN='(^\s*(\s*[0-9]+\.[0-9]+|\)|\())|(Players:$)'
fi
usage(){
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 " status \t\t Displays server status"
echo -e " players-online \t Shows online players"
echo -e " players \t\t Shows all players"
echo -e " cmd [command/message] Open interactive commandline or send a single command to the server"
echo -e " log [--tail|-t] Print the current server log, optionally tail the log to follow in real time"
echo -e " chatlog [--tail|-t] Print the current chatlog, optionally tail the log to follow in real time"
echo -e " new-game name\t Stops the server and creates a new game with the specified name"
echo -e " save-game name \t Stops the server and saves game to specified save"
echo -e " load-save name \t Stops the server and loads the specified save"
echo -e " check-permissions \t Checks for file permission and existence. Creates needed directories if possible."
echo -e " fix-permissions \t Chowns all relative directories. Might be dangerous."
echo -e " invocation \t\t Outputs the invocation for debugging purpose"
echo -e " listcommands \t List all init-commands"
echo -e " listsaves \t\t List all saves"
echo -e " version \t\t Prints the binary version"
echo -e " help \t\t Shows this help message"
}
ME=`whoami`
as_user() {
if [ $ME == $FUSERNAME ]; then
bash -c "$1"
else
sudo -u "$FUSERNAME" bash -c "$1"
fi
}
# Check/load sane default config options.
if [ -z "${CMDOUT_FILENAME}" ]; then
CMDOUT_FILENAME=factorio.log
fi
if [ -z "${EXTRA_BINARGS}" ]; then
EXTRA_BINARGS="--start-server-load-latest"
fi
if [ -z "${FACTORIO_PATH}" ]; then
FACTORIO_PATH=/opt/factorio/factorio
fi
if [ -z "${FIFO_NAME}" ]; then
FIFO_NAME=server.fifo
fi
if [ -z "${PIDFILE_NAME}" ]; then
PIDFILE_NAME=server.pid
fi
if [ -z "${SAVELOG}" ]; then
SAVELOG=0
fi
if [ -z "${SERVICE_NAME}" ]; then
SERVICE_NAME="Factorio (environment: ${FENV})"
fi
debug "Service name: ${SERVICE_NAME}"
if [ -z "${SERVER_BIND}" ]; then
SERVER_BIND=""
fi
if [ -z "${SERVER_PORT}" ]; then
SERVER_PORT=34197
fi
# Generate dynamic properties using config
if [ -z "${WRITE_DIR}" ]; then
if [ -z "${WRITE_DIR_BASE}" ]; then
WRITE_DIR_BASE=/var/factorio/instances
fi
WRITE_DIR="${WRITE_DIR_BASE}/${FENV}"
fi
debug "Write dir: ${WRITE_DIR}"
if [ -z "${BINARY}" ]; then
BINARY="${FACTORIO_PATH}/bin/x64/factorio"
fi
debug "Binary in use: ${BINARY}"
if ! [ -e ${BINARY} ]; then
echo "Could not find factorio binary! ${BINARY}"
echo "(if you store your binary some place else, override BINARY='/your/path' in /etc/factorio/init.conf)"
exit 1
fi
if [ -z "${FCONF}" ]; then
FCONF="/etc/factorio/${FENV}/config.ini"
if ! [ -e "${FCONF}" ]; then
FCONF="/etc/factorio/config.ini"
fi
fi
debug "Using FCONF: ${FCONF}"
if [ -z "${SERVER_SETTINGS}" ]; then
SERVER_SETTINGS="/etc/factorio/${FENV}/server-settings.json"
if ! [ -e "${SERVER_SETTINGS}" ]; then
SERVER_SETTINGS="/etc/factorio/server-settings.json"
fi
fi
debug "Using server-settings: ${SERVER_SETTINGS}"
if [ -z "${MAPGEN_CONF}" ]; then
MAPGEN_CONF="/etc/factorio/${FENV}/map-gen-settings.json"
if ! [ -e "${MAPGEN_CONF}" ]; then
MAPGEN_CONF="/etc/factorio/map-gen-settings.json"
fi
fi
if [ -z "${CMDOUT}" ]; then
CMDOUT="${WRITE_DIR}/${CMDOUT_FILENAME}"
fi
debug "Using cmdout: ${CMDOUT}"
if [ -z "${FIFO}" ]; then
FIFO="${WRITE_DIR}/${FIFO_NAME}"
fi
debug "Using fifo: ${FIFO}"
if [ -z "${PIDFILE}" ]; then
PIDFILE="${WRITE_DIR}/${PIDFILE_NAME}"
fi
debug "Using pidfile: ${PIDFILE}"
INVOCATION_BASE="${BINARY} --config ${WRITE_DIR}/config.ini --port ${SERVER_PORT} --server-settings ${SERVER_SETTINGS} "
if ! [ -z "${MOD_DIRECTORY}" ]; then
INVOCATION_BASE+="--mod-directory ${MOD_DIRECTORY} "
fi
if ! [ -z "${SERVER_BIND}" ]; then
INVOCATION_BASE+="--bind ${SERVER_BIND} "
fi
if ! [ -z "${RCON_PORT}" ]; then
if [ -z "${RCON_PASSWORD}" ]; then
echo "If you set RCON_PORT, you should also set RCON_PASSWORD."
exit 1
fi
INVOCATION_BASE+="--rcon-port ${RCON_PORT} "
fi
if ! [ -z "${RCON_PASSWORD}" ]; then
if [ -z "${RCON_PORT}" ]; then
echo "If you set RCON_PASSWORD, you should also set RCON_PORT."
exit 1
fi
INVOCATION_BASE+="--rcon-password ${RCON_PASSWORD} "
fi
if [ -f ${MAPGEN_CONF} ]; then
INVOCATION_BASE+="--map-gen-settings ${MAPGEN_CONF} "
fi
debug "Complete invocation base: ${INVOCATION_BASE}"
is_running() {
if as_user "test -e ${PIDFILE}"; then
if as_user "kill -0 $(cat ${PIDFILE})"; then
debug "${SERVICE_NAME} is running with pid $(cat ${PIDFILE})"
return 0
else
debug "Found ${PIDFILE}, but the server is not running. It's possible that your server has crashed"
debug "Check the log for details"
as_user "rm ${PIDFILE}"
return 2
fi
fi
return 1
}
start_service() {
if [ -e ${PIDFILE} ]; then
ps -p $(cat ${PIDFILE}) > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
echo "${SERVICE_NAME} is already running!"
return 1
fi
debug "Found rogue pid file, server might have crashed"
rm ${PIDFILE} 2> /dev/null
fi
if ! check_permissions; then
echo "Error! Incorrect permissions, unable to write to ${WRITE_DIR}"
return 1
fi
if [ "${SAVELOG}" == "0" ]; then
debug "Erasing log ${CMDOUT}"
echo "" > ${CMDOUT}
fi
as_user "tail -f ${FIFO} |${INVOCATION_BASE} ${EXTRA_BINARGS} >> ${CMDOUT} 2>&1 & echo \$! > ${PIDFILE}"
ps -p $(cat ${PIDFILE}) > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "Unable to start ${SERVICE_NAME}"
return 1
else
echo "Started ${SERVICE_NAME}, please see log for details"
fi
}
stop_service() {
if [ -e ${PIDFILE} ]; then
echo -n "Stopping ${SERVICE_NAME}: "
if kill -TERM $(cat ${PIDFILE}) 2> /dev/null; then
sec=1
while [ "$sec" -le 15 ]; do
if [ -e ${PIDFILE} ]; then
if kill -0 $(cat ${PIDFILE}) 2> /dev/null; then
echo -n ". "
sleep 1
else
break
fi
else
break
fi
sec=$(($sec+1))
done
fi
if kill -0 $(cat ${PIDFILE}) 2> /dev/null; then
echo "Unable to shut down nicely, killing the process!"
kill -KILL $(cat ${PIDFILE}) 2> /dev/null
else
echo "complete!"
fi
# Write a newline to the pipe, this triggers a SIGPIPE and causes tail to exit
echo "" > ${FIFO}
rm ${PIDFILE} 2> /dev/null
return 0 # we've either shut down gracefully or killed the process
else
echo "${SERVICE_NAME} is not running (${PIDFILE} does not exist)"
return 1
fi
}
send_cmd(){
if is_running; then
if [ -p ${FIFO} ]; then
# Grab a random 16 character id for our command
cmdid=$(head -c 1000 /dev/urandom |tr -dc 'a-zA-Z0-9' |fold -w 16 |head -n 1)
# Whisper that unknown player named after our random id
as_user "echo \"/w ${cmdid}\" > ${FIFO}"
# Wait for factorio to read stdin
sleep 1
# Run the actual command
as_user "echo $@ > ${FIFO}"
# Wait again, this might need more sleep for larger command output - time will tell (sorry future self for any head aches)
sleep 1
# Read the output file backwards - capture everything between the end of file until
# factorio tells us our random command id player does not exist.
# This also attempts to filter all non chat/command lines from the output
response=$(sed "/Player ${cmdid} doesn't exist./q" <(tac ${CMDOUT}) |egrep -v "(${cmdid})|${NONCMDPATTERN}")
echo "${response}"
else
echo "${FIFO} is not a pipe!"
return 1
fi
else
echo "Unable to send cmd to a stopped server!"
return 1
fi
}
cmd_players(){
players=$(send_cmd "/p")
if [ -z "${players}" ]; then
echo "No players found!"
return 1
fi
if [ "$1" == "online" ]; then
echo "${players}" |egrep '.+ \(online\)$' |sed -e 's/ (online)//g'
else
echo "${players}"
fi
}
check_permissions(){
if [ ! -e "${BINARY}" ]; then
echo "Can't find ${BINARY}. Please check your config!"
exit 1
fi
if ! as_user "test -w ${WRITE_DIR}" ; then
if ! as_user "mkdir -p ${WRITE_DIR}" ; then
echo "Check Permissions. Cannot create ${WRITE_DIR}"
exit 1
fi
fi
if ! as_user "touch ${PIDFILE}" ; then
echo "Check Permissions. Cannot touch pidfile ${PIDFILE}"
exit 1
fi
if ! [ -p ${FIFO} ]; then
if ! as_user "mkfifo ${FIFO}"; then
echo "Failed to create pipe for stdin, if applicable, remove ${FIFO} and try again"
exit 1
fi
fi
if ! as_user "touch ${CMDOUT}" ; then
echo "Check Permissions. Cannot touch cmd output file ${CMDOUT}"
exit 1
fi
if ! as_user "cp ${FCONF} ${WRITE_DIR}/config.ini" ; then
echo "Check Permissions. Cannot create ${WRITE_DIR}/config.ini from ${FCONF}"
exit 1
fi
if ! as_user "sed -i '/write-data/c\write-data=${WRITE_DIR}' ${WRITE_DIR}/config.ini" ; then
echo "Check Permissions. Cannot edit ${WRITE_DIR}/config.ini"
exit 1
fi
if [ -z "${MOD_DIRECTORY}" ]; then
if ! as_user "test -w ${MOD_DIRECTORY}"; then
echo "Check Permissions. Mod directory cannot be accessed: ${MOD_DIRECTORY}"
fi
fi
}
fix_permissions() {
read -p "This is a very crude way of fixing the permissions, if you are not sure what this does, press control+c now, else press enter."
chown -R $FUSERNAME:$USERGROUP $FACTORIO_PATH
chown -R $FUSERNAME:$USERGROUP $WRITE_DIR_BASE
chown -R $FUSERNAME:$USERGROUP $WRITE_DIR_BASE/../mods
}
test_deps(){
return 0 # TODO: Implement ldd check on $BINARY
}
get_bin_version(){
echo `as_user "$BINARY --version |egrep '^Version: [0-9\.]+' |egrep -o '[0-9\.]+' |head -n 1"`
}
get_bin_arch(){
echo `as_user "$BINARY --version |egrep '^Binary version: ' |egrep -o '[0-9]{2}'"`
}
case "$1" in
start)
# Starts the server
if is_running; then
echo "Server already running."
exit 0
else
if [ -z "${IKNOWWHATIMDOING}" ]; then
sudo systemctl start "factorio@${FENV}"
else
if ! start_service; then
echo "Could not start $SERVICE_NAME"
exit 1
fi
fi
fi
;;
stop)
# Stops the server
if is_running; then
if [ -z "${IKNOWWHATIMDOING}" ]; then
sudo systemctl stop "factorio@${FENV}"
else
send_cmd "Server is being shut down on request"
if ! stop_service; then
echo "Could not stop $SERVICE_NAME"
exit 1
fi
fi
else
echo "No running server."
exit 0
fi
;;
restart)
# Restarts the server
if is_running; then
send_cmd "Server is being restarted on request, be right back!"
if stop_service; then
if ! start_service; then
echo "Could not start $SERVICE_NAME after restart!"
exit 1
fi
else
echo "Failed to stop $SERVICE_NAME, aborting restart!"
exit 1
fi
else
echo "No running server to restart, starting it..."
if ! start_service; then
echo "Could not start $SERVICE_NAME"
exit 1
fi
fi
;;
status)
# Shows server status
if is_running; then
sudo systemctl status "factorio@${FENV}"
echo
echo "$SERVICE_NAME is running."
else
echo "$SERVICE_NAME is not running."
exit 1
fi
;;
cmd)
if [ -z "$2" ]; then
trap 'clear' SIGTERM EXIT
clear
echo "Type any command or send chat messages"
echo "This interactive commandline adds additional commands:"
echo ""
echo -e "\texit\t\texit the commandline"
echo -e "\tclear\t\tclear the commandline screen"
echo ""
while true; do
read -e -p "server@[${SERVICE_NAME}]> " cmd
[ "${cmd}" == "exit" ] && exit 0
[ "${cmd}" == "clear" ] && clear && continue
echo ${cmd} > ${FIFO}
sleep 1
done
else
send_cmd "${@:2}"
fi
;;
log)
case $2 in
--tail|-t)
tail -F -n +0 "${WRITE_DIR}/factorio-current.log"
;;
*)
cat "${WRITE_DIR}/factorio-current.log"
;;
esac
;;
chatlog)
case $2 in
--tail|-t)
tail -F -n +0 ${CMDOUT} |egrep -v "${NONCMDPATTERN}"
;;
*)
cat ${CMDOUT} |egrep -v "${NONCMDPATTERN}"
;;
esac
;;
players)
cmd_players
;;
players-online|online)
cmd_players online
;;
new-game)
if [ -z $2 ]; then
echo "You must specify a save name for your new game"
exit 1
fi
savename="${WRITE_DIR}/saves/$2"
if ! check_permissions; then
echo "Error! Incorrect permissions, unable to write to ${WRITE_DIR}"
return 1
fi
# Stop Service
if is_running; then
send_cmd "Generating new save, please stand by"
if ! stop_service; then
echo "Failed to stop server, unable to create new save"
exit 1
fi
fi
if ! as_user "$INVOCATION_BASE --create ${savename}"; then
echo "Failed to create new game"
exit 1
else
echo "New game created: ${savename}.zip"
fi
;;
save-game)
savename="${WRITE_DIR}/saves/$2.zip"
# Stop Service
if is_running; then
send_cmd "Stopping server to save game"
if ! stop_service; then
echo "Failed to stop server, unable to save as \"$2\""
exit 1
fi
fi
lastsave=$(find "${WRITE_DIR}/saves" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
if ! as_user "cp ${lastsave} ${savename}"; then
echo "Error! Failed to save game"
exit 1
fi
;;
load-save)
# Ensure we get a new save file name
newsave=${WRITE_DIR}/saves/$2.zip
if [ ! -f "${newsave}" ]; then
echo "Save \"${newsave}\" does not exist, aborting action!"
exit 1
fi
# Since stopping the server causes a save we have to stop the server to do this
if is_running; then
send_cmd "Stopping server to load a saved game"
if ! stop_service; then
echo "Aborting, unable to stop $SERVICE_NAME"
exit 1
fi
fi
# Touch the new save file
as_user "touch ${newsave}"
;;
inv|invocation)
echo "${INVOCATION_BASE} ${EXTRA_BINARGS}"
;;
check-permissions|--check-permissions)
check_permissions
;;
fix-permissions|--fix-permissions)
fix_permissions
;;
help|--help|-h)
usage
;;
listcommands)
echo `$0 help 2> /dev/null |egrep '^ ' |awk '{ print $1 }'`
;;
listsaves)
echo `find ${WRITE_DIR} -type f |egrep -o '\w+\.zip$' |sed -e 's/.zip//'`
;;
version)
echo `get_bin_version`
;;
*)
echo "No such command!"
echo
usage
exit 1
;;
esac
exit 0