-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·140 lines (112 loc) · 4.31 KB
/
docker-entrypoint.sh
File metadata and controls
executable file
·140 lines (112 loc) · 4.31 KB
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
#!/bin/bash
readonly app_instance_dir=/appdata/instance
readonly app_server_dir=/appdata/server
readonly app_instance_cfg=/appdata/instance/SpaceEngineers-Dedicated.cfg
readonly steam_app_id=298740
readonly wine_app_instance_dir=Z:\\appdata\\instance
unset IP
unset PORT
WORLD=World
usage() {
echo "Usage: $0 -h"
echo " $0 [-ip IP] [-port PORT] [-world WORLD]"
}
eargs() {
echo >&2 "$1"
echo >&2
usage >&2
exit 2
}
while [ $# != 0 ]; do
case "$1" in
-ip) [ -z "$2" ] && eargs "$0: option $1 requires argument"
IP=$2
shift 2
;;
-port) [ -z "$2" ] && eargs "$0: option $1 requires argument"
PORT=$2
shift 2
;;
-world) [ -z "$2" ] && eargs "$0: option $1 requires argument"
WORLD=$2
shift 2
;;
-h)
usage
exit 0
;;
*)
eargs "$0: unrecognized option: $1"
;;
esac
done
if [ ! -d $app_instance_dir ] || [ ! -x $app_instance_dir ] || [ ! -w $app_instance_dir ]; then
echo >&2 "Error: The directory $app_instance_dir does not exist or is not writable."
echo >&2 " It must exist and be owned and writable by uid: $(id -u)."
exit 3
fi
if [ ! -d $app_server_dir ] || [ ! -x $app_server_dir ] || [ ! -w $app_server_dir ]; then
echo >&2 "Error: The directory $app_server_dir does not exist or is not writable."
echo >&2 " It must exist and be owned and writable by uid: $(id -u)."
exit 3
fi
if [ ! -f $app_instance_cfg ] || [ ! -w $app_instance_cfg ]; then
echo >&2 "Error: The file $app_instance_cfg does not exist or is not writable."
echo >&2 " It must exist and be owned and writable by uid: $(id -u)."
exit 3
fi
_unowned_files=$(find 2>/dev/null $app_instance_dir ! \( -user "$(id -u)" -and -writable \) -printf " %p\n");
if [ -n "$_unowned_files" ]; then
echo >&2 "Error: Files were detected in $app_instance_dir that are not owned or writable."
echo >&2 " For server to function correctly, all files must be owned by uid: $(id -u)."
echo >&2 "List of not owned files:"
echo >&2 "$_unowned_files"
exit 3
fi
echo "Preparation step: Install or validate server files"
if ! /usr/games/steamcmd +force_install_dir $app_server_dir +login anonymous +@sSteamCmdForcePlatformType windows +app_update $steam_app_id validate +quit; then
echo >&2 "Error: Failed to install or validate server files; steamcmd exited with status: $?."
exit 4
fi
echo "Preparation step: Remove server log files"
if ! find $app_instance_dir -maxdepth 1 -name "*.log" -type f -printf " %p\n" -exec rm {} \; ; then
echo >&2 "Warning: Failed to remove server log files."
fi
echo "Preparation step: Update server configuration"
_wine_app_instance_world_dir=$(echo "$wine_app_instance_dir\\Saves\\$WORLD" | sed 's;\\;\\\\;g')
if ! _sed_loadworld=$(sed -Ei "s;<LoadWorld>[^<]*</LoadWorld>;<LoadWorld>$_wine_app_instance_world_dir</LoadWorld>;g w /dev/stdout" $app_instance_cfg); then
echo >&2 "Error: Updating configuration in $app_instance_cfg: $?"
exit 5
fi
if [ -z "$_sed_loadworld" ]; then
echo >&2 "Error: Failed to update <LoadWorld> value in $app_instance_cfg."
echo >&2 " This option is updated automatically, but it is required to exist in file beforehand."
echo >&2 " If in doubt, add <LoadWorld></LoadWorld> to config in front of </MyConfigDedicated>."
exit 5
else
echo " Updated: $(echo "$_sed_loadworld" | grep -Eo '<LoadWorld>[^<]*</LoadWorld>' | sed -e '1!s/^/ /')"
fi
echo "Starting server..."
if ! cd $app_server_dir/DedicatedServer64; then
echo >&2 "Error: Failed to change directory to $app_server_dir/DedicatedServer64."
exit 6
fi
# NB: When current script is executed directly, logging to stdout does not work.
# It works fine when the script is run from Dockerfile ENTRYPOINT directive.
wine SpaceEngineersDedicated.exe -noconsole -ignorelastsession -path $wine_app_instance_dir ${IP:+-ip} "$IP" ${PORT:+-port} "$PORT" &
trap '{
echo "Sending SIGINT to server process..."
pkill -INT -f SpaceEngineersDedicated.exe
}' INT TERM
# $pid is set if `wait` returns from exited process, and the $rc is that of
# a exited process. Otherwise it means that `wait` was interrupted by a signal.
# $rc=127 to correctly check if `wait` has no children to wait (why?).
until [ -n "$pid" ] || [ "$rc" = 127 ]; do
wait -n -p pid; rc=$?
done
trap - INT TERM
if [ "$rc" != 0 ]; then
echo "Server shut down with non-zero status: $rc"
exit 1
fi
echo "Server shut down successfully"