forked from Bisa/factorio-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorio
executable file
·463 lines (411 loc) · 11.1 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
#!/bin/bash
# Load config file
if [ -L $0 ]; then
source `readlink -e $0 | sed "s:[^/]*$:config:"`
else
source `echo $0 | sed "s:[^/]*$:config:"`
fi
if [ -z "${SERVICE_NAME}" ]; then
echo "Couldn't load config file, please edit config.example and rename it to config"
logger -t factorio-init "Couldn't load config file, please edit config.example and rename it to config"
exit
fi
# Check/load defaults for backwards compatible config options
if [ -z "${PACKAGE_DIR_NAME}" ]; then
PACKAGE_DIR_NAME=factorio
fi
if [ -z "${USERGROUP}" ]; then
USERGROUP=${USERNAME}
fi
if [ -z "${HEADLESS}" ]; then
# This is a server init script, assume headless default
HEADLESS=1
fi
if [ -z "${PIDFILE}" ]; then
PIDFILE="${FACTORIO_PATH}/${SERVICE_NAME}.pid"
fi
if [ -z "${SCREEN_NAME}" ]; then
SCREEN_NAME="${SERVICE_NAME}"
fi
debug(){
if [ ${DEBUG} -gt 0 ]; then
echo "DEBUG LOG: $@" >&2
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 " load-save name \t Stops the server and loads the specified save"
echo -e " refresh-save \t\t Stops the server and loads the last modified save"
echo -e " screen \t\t Shows the server screen"
echo -e " install tarball \t Installs the server with specified tarball"
echo -e " update [--dry-run] \t Updates the server"
}
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ]; then
bash -c "$1"
else
su $USERNAME -s /bin/bash -c "$1"
fi
}
refresh_save(){
savedir="${FACTORIO_PATH}/saves"
# Check to see if save dir actually exists
if ! [ -e ${savedir} ]; then
echo "Error! Save directory missing: ${savedir}"
exit 1
fi
# Find the last modified save file
lastsave=$(as_user "ls -t ${savedir}/*.zip" 2> /dev/null | head -1)
# Sanity check, did we even find any save files?
if [ -z "${lastsave}" ]; then
echo "Error! Unable to find any saves in ${savedir}"
exit 1
fi
debug "last modified save: ${lastsave}"
# If the last modified save is our own, keep using it
if [ "${lastsave}" == "${savedir}/${SAVE_NAME}.zip" ]; then
debug "using existing ${SAVE_NAME}.zip"
return 0
fi
# Else we copy the latest save to our own save file
debug "using refreshed save"
if ! as_user "cp ${lastsave} ${savedir}/${SAVE_NAME}.zip"; then
echo "Error! Failed to refresh saves"
exit 1
fi
}
find_pid(){
pid=$(ps ax | grep -v grep | grep "${BINARY} --start-server ${SAVE_NAME}" | grep -v "${SCREEN_NAME}" | awk '{ print $1}')
if [ "$pid" == "" ]; then
echo "-1"
debug "could not find a pid for binary: \"${BINARY}\""
elif [ `echo ${pid} | wc -l` -gt 1 ]; then
echo "-2"
debug "found multiple pids"
else
echo ${pid}
debug "found pid: $pid"
fi
}
is_running() {
pid=$(find_pid)
if [ ${pid} -gt 0 ]; then
return 0
elif [ ${pid} == -2 ]; then
echo "Found multiple pids, aborting!"
exit 1
else
return 1
fi
}
start_service() {
if [ ! -f "$BINARY" ]; then
echo "Failed to start: Can't find the specified binary $BINARY. Please check your config!"
exit 1
fi
check_permissions
refresh_save
as_user "cd $FACTORIO_PATH && screen -dmS $SCREEN_NAME $INVOCATION"
if [ $? -eq 0 ]; then
#
# Waiting for the server to start
#
seconds=0
until is_running; do
sleep 1
seconds=$seconds+1
if [[ $seconds -eq 2 ]]; then
echo "Still not running, waiting a while longer..."
fi
if [[ $seconds -ge 10 ]]; then
echo "Failed to start, aborting."
exit 1
fi
done
echo "$SERVICE_NAME is running."
else
echo "Failed to start, ensure SCREEN is installed"
fi
}
stop_service() {
#
# Stops the server
#
pid=$(find_pid)
as_user "kill -s 2 ${pid}"
sleep 0.5
#
# Waiting for the server to shut down
#
seconds=0
while is_running; do
sleep 1
seconds=$seconds+1
if [[ $seconds -eq 2 ]]; then
echo "Still not shut down, waiting a while longer..."
fi
if [[ $seconds -ge 10 ]];
then
echo "Failed to shut down, aborting!"
exit 1
fi
done
echo "$SERVICE_NAME is now shut down."
}
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"
return 1
fi
return 0
}
test_deps(){
return 0 # TODO: Implement ldd check on $BINARY
}
install(){
# Factorio comes packaged in a directory named "factorio"
# Unless overriden in the config we will presume this is also the
# name used in FACTORIO_PATH
expected_path="`dirname ${FACTORIO_PATH}`/${PACKAGE_DIR_NAME}"
if ! [ "${FACTORIO_PATH}" == "${expected_path}" ]; then
echo "Aborting install! FACTORIO_PATH does not match expected path: ${expected_path}"
echo "See config option PACKAGE_DIR_NAME for more details"
exit 1
fi
# Prevent accitential overwrites
if [ -e "${expected_path}" ]; then
echo "Aborting install, ${FACTORIO_PATH} already exists"
exit 1
fi
tarball=$1
if ! [ -f "${tarball}" ]; then
echo "Install package does not exist! ${tarball}"
exit 1
fi
target="`dirname ${FACTORIO_PATH}`"
if ! test -w "${target}"; then
echo "Failed to write, aborting install!"
echo "Install needs to be run as a user with write permissions to ${target}"
exit 1
fi
echo "Installing ${tarball} ..."
if ! tar -xzvf "${tarball}" --directory "${target}"; then
echo "Install failed!"
exit 1
fi
echo "Applying file ownership ..."
if ! chown -R ${USERNAME}:${USERGROUP} ${FACTORIO_PATH}; then
echo "Failed to apply ownership ${USERNAME}:${USERGROUP} for ${FACTORIO_PATH}"
exit 1
fi
if test_deps; then
if ! as_user "$BINARY --create ${SAVE_NAME}"; then
echo "Installation complete..."
echo
echo "but failed to create initial save game."
echo "Ensure you use: load-save [save-name] before starting the server for the first time."
else
echo "Install successfull!"
fi
else
echo "Installation complete..."
echo
echo "but $BINARY is missing required dependencies."
echo "Install the missing deps and ensure you use: load-save [save-name] before starting the server for the first time."
fi
}
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}'"`
}
update(){
if ! [ -e "${UPDATE_SCRIPT}" ]; then
echo "Failed to find update script, blatantly refusing to continue!"
echo "Try cloning into [email protected]:narc0tiq/factorio-updater.git and set the UPDATE_SCRIPT config before you try again."
exit 1
fi
if is_running; then
stop_service
fi
# Assume the user wants a dry run? (our only argument to this function)
if ! [ -z "$1" ]; then
echo "Running updater in --dry-run mode, no patches will be applied"
dryrun=1
else
dryrun=0
fi
if [ ${HEADLESS} -gt 0 ]; then
package="core-linux_headless`get_bin_arch`"
else
package="core-linux`get_bin_arch`"
fi
version=`get_bin_version`
if [ -z "${UPDATE_TMPDIR}" ]; then
UPDATE_TMPDIR=/tmp
fi
tmpdir="${UPDATE_TMPDIR}/factorio-update"
invocation="python ${UPDATE_SCRIPT} --for-version ${version} --package ${package} --output-path ${tmpdir}"
if [ ${UPDATE_EXPERIMENTAL} -gt 0 ]; then
invocation="${invocation} --experimental"
fi
if [ ${HEADLESS} -eq 0 ]; then
#GoodGuy Wube Software allows you to download the headless for free - yay! but you still have to
#buy the game if you want to download the sound/gfx client
invocation="${invocation} --user ${UPDATE_USERNAME} --token ${UPDATE_TOKEN}"
fi
echo "Checking for updates..."
result=`as_user "${invocation} --dry-run"`
exitcode=$?
if [ ${exitcode} -gt 0 ]; then
echo "Update check failed!"
exit 1
else
newversion=`echo ${result} |egrep '^Dry run: ' |egrep -o '[0-9\.]+' |tail -n 1`
fi
if [ -z "${newversion}" ]; then
echo "No new updates for ${package} ${version}"
exit 0
else
echo "New version ${package} ${newversion}"
fi
# Go or no Go?
if [ ${dryrun} -gt 0 ]; then
echo "Dry run, not taking further actions!"
# allow scripts to read return code 0 for no updates and 2 if there are updates to apply
if ! [ -z "${newversion}" ]; then
exit 2
fi
exit 0
fi
if [ -e ${tmpdir} ]; then
echo "Aborting update! Temporary directory already exists ${tmpdir}"
echo "Remnants from a previously failed update?"
exit 1
fi
if ! as_user "mkdir -p ${tmpdir}"; then
echo "Aborting update! Unable to create tmpdir: ${tmpdir}"
exit 1
fi
# Time to download the updates
if ! as_user "${invocation}"; then
echo "Aborting update!"
rm -rf ${tmpdir}
exit 1
fi
for patch in $(find ${tmpdir} -type f -name "*.zip"); do
echo "Applying ${patch} ..."
result=`as_user "$BINARY --apply-update ${patch}"`
exitcode=$?
if [ $exitcode -gt 0 ]; then
echo "${result}"
echo
echo "Error! Failed to apply update"
echo "You can try to apply it manually with:"
echo "su ${USERNAME} -c \"${BINARY} --apply-update ${patch}\""
exit 1
fi
done
echo "Successfully updated factorio"
rm -rf ${tmpdir}
}
case "$1" in
start)
# Starts the server
if is_running; then
echo "Server already running."
else
start_service
fi
;;
stop)
# Stops the server
if is_running; then
stop_service
else
echo "No running server."
fi
;;
restart)
# Restarts the server
if is_running; then
stop_service
else
echo "No running server, starting it..."
fi
start_service
;;
status)
# Shows server status
if is_running; then
echo "$SERVICE_NAME is running."
else
echo "$SERVICE_NAME is not running."
exit 1
fi
;;
load-save)
# Ensure we get a new save file name
newsave=${FACTORIO_PATH}/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
stop_service
fi
# Touch the new save file
as_user "touch ${newsave}"
;;
refresh-save)
if is_running; then
stop_service
fi
refresh_save
;;
screen)
if is_running; then
as_user "script /dev/null -q -c \"screen -rx $SCREEN_NAME\""
else
echo -n "Server is not running. Do you want to start it? [n]: "
read START_SERVER
case "$START_SERVER" in
[Yy])
start_service
as_user "script /dev/null -q -c \"screen -rx $SCREEN_NAME\""
;;
*)
clear
echo "Aborting startup!"
exit 1
;;
esac
fi
;;
install)
install "$2"
;;
update)
update "$2"
;;
help|--help|-h)
usage
;;
*)
echo "No such command!"
echo
usage
exit 1
;;
esac
exit 0