Skip to content

Commit be87971

Browse files
IronMinesevenrats
authored andcommitted
New logger with color and specific types. Code cleanup (itzg#3108)
1 parent 79c62a7 commit be87971

38 files changed

+420
-418
lines changed

bin/mc-send-to-console

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@
33
: "${CONSOLE_IN_NAMED_PIPE:=/tmp/minecraft-console-in}"
44

55
if isFalse "${CREATE_CONSOLE_IN_PIPE:-false}"; then
6-
echo "ERROR: console pipe needs to be enabled by setting CREATE_CONSOLE_IN_PIPE to true"
6+
error "Console pipe needs to be enabled by setting CREATE_CONSOLE_IN_PIPE to true"
77
fi
88

99

1010
if [ $# = 0 ]; then
11-
echo "ERROR: pass console commands as arguments"
11+
error "Pass console commands as arguments"
1212
exit 1
1313
fi
1414

1515
if [ ! -p "${CONSOLE_IN_NAMED_PIPE}" ]; then
16-
echo "ERROR: named pipe ${CONSOLE_IN_NAMED_PIPE} is missing"
16+
error "Named pipe ${CONSOLE_IN_NAMED_PIPE} is missing"
1717
exit 1
1818
fi
1919

2020
if [ "$(id -u)" = 0 -a $UID != 0 ]; then
21-
if [[ $(getDistro) == alpine ]]; then
22-
exec su-exec minecraft bash -c "echo '$*' > '${CONSOLE_IN_NAMED_PIPE:-/tmp/minecraft-console-in}'"
23-
else
24-
exec gosu minecraft bash -c "echo '$*' > '${CONSOLE_IN_NAMED_PIPE:-/tmp/minecraft-console-in}'"
25-
fi
21+
exec $(getSudoFromDistro) minecraft bash -c "echo '$*' > '${CONSOLE_IN_NAMED_PIPE}'"
2622
else
27-
echo "$@" >"${CONSOLE_IN_NAMED_PIPE:-/tmp/minecraft-console-in}"
23+
echo "$@" >"${CONSOLE_IN_NAMED_PIPE}"
2824
fi

bin/mcstatus

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
echo "WARNING: mcstatus is deprecated; calling mc-monitor instead"
3+
warning "mcstatus is deprecated; calling mc-monitor instead"
44

55
##### mcstatus shim for mc-monitor
66
# handles translating calls to
@@ -11,8 +11,8 @@ addr="$1"
1111

1212
IFS=':'
1313
read -a parts <<< "${addr}"
14+
args=(--host ${parts[0]})
1415
if [[ ${#parts[*]} -gt 1 ]]; then
15-
exec mc-monitor status --host ${parts[0]} --port ${parts[1]}
16-
else
17-
exec mc-monitor status --host ${parts[0]}
16+
args+=(--port ${parts[1]})
1817
fi
18+
exec mc-monitor ${args[@]}

scripts/start

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if ! isTrue "${SKIP_SUDO:-false}" && [ "$(id -u)" = 0 ]; then
5555
echo "Starting DNS update..."
5656
exec gosu ${runAsUser}:${runAsGroup} "${SCRIPTS:-/}start-dns-update" "$@"
5757
fi
58+
exec $(getSudoFromDistro) ${runAsUser}:${runAsGroup} "${SCRIPTS:-/}start-configuration" "$@"
5859
else
5960
exec "${SCRIPTS:-/}start-configuration" "$@"
6061
fi

scripts/start-autopause

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,60 +26,40 @@ isDebugging && set -x
2626

2727
cp /auto/knockd-config.cfg /tmp/knockd-config.cfg
2828

29+
function updatePort() {
30+
regseq="^\s*sequence\s*=\s*$1\s*$"
31+
linenum=$(grep -nm${2} sequence /tmp/knockd-config.cfg | cut -d : -f 1 | tail -n1)
32+
if ! [[ $(awk "NR==$linenum" /tmp/knockd-config.cfg) =~ $regseq ]]; then
33+
sed -i "${linenum}s/sequence.*/sequence = $1/" /tmp/knockd-config.cfg
34+
log "Updated $3 port in knockd config"
35+
fi
36+
}
37+
2938
# update server port to listen to
30-
regseq="^\s*sequence\s*=\s*$SERVER_PORT\s*$"
31-
linenum=$(grep -nm1 sequence /tmp/knockd-config.cfg | cut -d : -f 1 | tail -n1)
32-
if ! [[ $(awk "NR==$linenum" /tmp/knockd-config.cfg) =~ $regseq ]]; then
33-
sed -i "${linenum}s/sequence.*/sequence = $SERVER_PORT/" /tmp/knockd-config.cfg
34-
log "Updated server port in knockd config"
35-
fi
39+
updatePort $SERVER_PORT 1 "server"
40+
3641
# update rcon port to listen to
37-
regseq="^\s*sequence\s*=\s*$RCON_PORT\s*$"
38-
linenum=$(grep -nm2 sequence /tmp/knockd-config.cfg | cut -d : -f 1 | tail -n1)
39-
if ! [[ $(awk "NR==$linenum" /tmp/knockd-config.cfg) =~ $regseq ]]; then
40-
sed -i "${linenum}s/sequence.*/sequence = $RCON_PORT/" /tmp/knockd-config.cfg
41-
log "Updated rcon port in knockd config"
42-
fi
42+
updatePort $RCON_PORT 2 "rcon"
43+
44+
isNumericElseSetToDefault "AUTOPAUSE_PERIOD" 10
45+
checkIfNotZeroElseSetToDefault "AUTOPAUSE_PERIOD" 10
46+
isNumericElseSetToDefault "AUTOPAUSE_TIMEOUT_KN" 120
47+
isNumericElseSetToDefault "AUTOPAUSE_TIMEOUT_EST" 3600
48+
isNumericElseSetToDefault "AUTOPAUSE_TIMEOUT_INIT" 600
4349

44-
if ! [[ $AUTOPAUSE_PERIOD =~ ^[0-9]+$ ]]; then
45-
AUTOPAUSE_PERIOD=10
46-
export AUTOPAUSE_PERIOD
47-
log "Warning: AUTOPAUSE_PERIOD is not numeric, set to 10 (seconds)"
48-
fi
49-
if [ "$AUTOPAUSE_PERIOD" -eq "0" ] ; then
50-
AUTOPAUSE_PERIOD=10
51-
export AUTOPAUSE_PERIOD
52-
log "Warning: AUTOPAUSE_PERIOD must not be 0, set to 10 (seconds)"
53-
fi
54-
if ! [[ $AUTOPAUSE_TIMEOUT_KN =~ ^[0-9]+$ ]] ; then
55-
AUTOPAUSE_TIMEOUT_KN=120
56-
export AUTOPAUSE_TIMEOUT_KN
57-
log "Warning: AUTOPAUSE_TIMEOUT_KN is not numeric, set to 120 (seconds)"
58-
fi
59-
if ! [[ $AUTOPAUSE_TIMEOUT_EST =~ ^[0-9]+$ ]] ; then
60-
AUTOPAUSE_TIMEOUT_EST=3600
61-
export AUTOPAUSE_TIMEOUT_EST
62-
log "Warning: AUTOPAUSE_TIMEOUT_EST is not numeric, set to 3600 (seconds)"
63-
fi
64-
if ! [[ $AUTOPAUSE_TIMEOUT_INIT =~ ^[0-9]+$ ]] ; then
65-
AUTOPAUSE_TIMEOUT_INIT=600
66-
export AUTOPAUSE_TIMEOUT_INIT
67-
log "Warning: AUTOPAUSE_TIMEOUT_INIT is not numeric, set to 600 (seconds)"
68-
fi
6950
if [[ "$AUTOPAUSE_KNOCK_INTERFACE" == "lo" ]] ; then
70-
log "Warning: AUTOPAUSE_KNOCK_INTERFACE is set to the local loopback interface."
71-
log " This is not advisable, as incoming connections are likely not picked up there."
72-
log " Continuing with this setting."
51+
logWarning "AUTOPAUSE_KNOCK_INTERFACE is set to the local loopback interface."
52+
logWarning " This is not advisable, as incoming connections are likely not picked up there."
53+
logWarning " Continuing with this setting."
7354
fi
7455

7556
if [[ -n "$MAX_TICK_TIME" && "$MAX_TICK_TIME" != "-1" ]] ; then
76-
log "Warning: MAX_TICK_TIME is non-default, for autopause to work properly, this check should be disabled (-1 for versions >= 1.8.1)"
57+
logWarning "MAX_TICK_TIME is non-default, for autopause to work properly, this check should be disabled (-1 for versions >= 1.8.1)"
7758
elif [[ -z "$MAX_TICK_TIME" ]] ; then
59+
MAX_TICK_TIME=-1
7860
if versionLessThan 1.8.1; then
7961
# 10 years
8062
MAX_TICK_TIME=315360000000
81-
else
82-
MAX_TICK_TIME=-1
8363
fi
8464
export MAX_TICK_TIME
8565
fi

scripts/start-autostop

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,9 @@ log "Autostop functionality enabled"
2020

2121
isDebugging && set -x
2222

23-
if ! [[ $AUTOSTOP_PERIOD =~ ^[0-9]+$ ]]; then
24-
AUTOSTOP_PERIOD=10
25-
export AUTOSTOP_PERIOD
26-
log "Warning: AUTOSTOP_PERIOD is not numeric, set to 10 (seconds)"
27-
fi
28-
if [ "$AUTOSTOP_PERIOD" -eq "0" ] ; then
29-
AUTOSTOP_PERIOD=10
30-
export AUTOSTOP_PERIOD
31-
log "Warning: AUTOSTOP_PERIOD must not be 0, set to 10 (seconds)"
32-
fi
33-
if ! [[ $AUTOSTOP_TIMEOUT_EST =~ ^[0-9]+$ ]] ; then
34-
AUTOSTOP_TIMEOUT_EST=3600
35-
export AUTOSTOP_TIMEOUT_EST
36-
log "Warning: AUTOSTOP_TIMEOUT_EST is not numeric, set to 3600 (seconds)"
37-
fi
38-
if ! [[ $AUTOSTOP_TIMEOUT_INIT =~ ^[0-9]+$ ]] ; then
39-
AUTOSTOP_TIMEOUT_INIT=1800
40-
export AUTOSTOP_TIMEOUT_INIT
41-
log "Warning: AUTOSTOP_TIMEOUT_INIT is not numeric, set to 1800 (seconds)"
42-
fi
23+
isNumericElseSetToDefault "AUTOSTOP_PERIOD" 10
24+
checkIfNotZeroElseSetToDefault "AUTOSTOP_PERIOD" 10
25+
isNumericElseSetToDefault "AUTOSTOP_TIMEOUT_EST" 3600
26+
isNumericElseSetToDefault "AUTOSTOP_TIMEOUT_INIT" 1800
4327

4428
/auto/autostop-daemon.sh &

scripts/start-configuration

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ log "Running as uid=$(id -u) gid=$(id -g) with /data as '$(ls -lnd /data)'"
3535
if [ ! -e /data/eula.txt ]; then
3636
if ! isTrue "$EULA"; then
3737
log ""
38-
log "Please accept the Minecraft EULA at"
39-
log " https://account.mojang.com/documents/minecraft_eula"
40-
log "by adding the following immediately after 'docker run':"
41-
log " -e EULA=TRUE"
38+
logError "Please accept the Minecraft EULA at"
39+
logError " https://account.mojang.com/documents/minecraft_eula"
40+
logError "by adding the following immediately after 'docker run':"
41+
logError " -e EULA=TRUE"
4242
log ""
4343
exit 1
4444
fi
@@ -62,10 +62,10 @@ if isTrue "${ENABLE_RCON:-true}"; then
6262
if [[ -v RCON_PASSWORD_FILE ]]; then
6363
if [ ! -e "${RCON_PASSWORD_FILE}" ]; then
6464
log ""
65-
log "Initial RCON password file ${RCON_PASSWORD_FILE} does not seems to exist."
66-
log "Please ensure your configuration."
67-
log "If you are using Docker Secrets feature, please check this for further information: "
68-
log " https://docs.docker.com/engine/swarm/secrets"
65+
logError "Initial RCON password file ${RCON_PASSWORD_FILE} does not seems to exist."
66+
logError "Please ensure your configuration."
67+
logError "If you are using Docker Secrets feature, please check this for further information: "
68+
logError " https://docs.docker.com/engine/swarm/secrets"
6969
log ""
7070
exit 1
7171
else
@@ -90,12 +90,12 @@ fi
9090
# Auto-pause/stop
9191

9292
if isTrue "${ENABLE_AUTOPAUSE}" && isTrue "${EXEC_DIRECTLY:-false}"; then
93-
log "EXEC_DIRECTLY=true is incompatible with ENABLE_AUTOPAUSE=true"
93+
logError "EXEC_DIRECTLY=true is incompatible with ENABLE_AUTOPAUSE=true"
9494
exit 1
9595
fi
9696

9797
if isTrue "${ENABLE_AUTOPAUSE}" && isTrue "${ENABLE_AUTOSTOP}"; then
98-
log "ENABLE_AUTOPAUSE=true is incompatible with ENABLE_AUTOSTOP=true"
98+
logError "ENABLE_AUTOPAUSE=true is incompatible with ENABLE_AUTOSTOP=true"
9999
exit 1
100100
fi
101101

@@ -111,9 +111,9 @@ function fixJavaPath() {
111111
# Some Docker management UIs grab all the image declared variables and present them for configuration.
112112
# When upgrading images across Java versions, that creates a mismatch in PATH's expected by base image.
113113
if ! which java > /dev/null; then
114-
log "ERROR: your Docker provider has an annoying flaw where it"
115-
log " tries to set PATH even though the container establishes"
116-
log " a very specific value."
114+
logError " Your Docker provider has an annoying flaw where it"
115+
logError " tries to set PATH even though the container establishes"
116+
logError " a very specific value."
117117
sleep 2
118118
# now find where java might be
119119
for d in /opt/java/openjdk/bin /usr/bin; do
@@ -128,7 +128,7 @@ function fixJavaPath() {
128128

129129

130130
if ! fixJavaPath; then
131-
log "ERROR: could not locate path that contains java"
131+
logError "could not locate path that contains java"
132132
exit 1
133133
fi
134134

@@ -183,7 +183,7 @@ if [[ $MOD_PLATFORM ]]; then
183183
;;
184184

185185
*)
186-
log "ERROR; Invalid MOD_PLATFORM: '$MOD_PLATFORM'"
186+
logError "Invalid MOD_PLATFORM: '$MOD_PLATFORM'"
187187
exit 1
188188
;;
189189
esac
@@ -272,11 +272,11 @@ case "${TYPE^^}" in
272272
;;
273273

274274
*)
275-
log "ERROR: Invalid TYPE: '$TYPE'"
276-
log "Must be: VANILLA, FORGE, BUKKIT, SPIGOT, PAPER, FOLIA, PURPUR, FABRIC, QUILT,"
277-
log " SPONGEVANILLA, CUSTOM, MAGMA, MOHIST, CATSERVER, AIRPLANE, PUFFERFISH,"
278-
log " CANYON, LIMBO, CRUCIBLE"
275+
logError "Invalid TYPE: '$TYPE'"
276+
logError "Must be: VANILLA, FORGE, BUKKIT, SPIGOT, PAPER, FOLIA, PURPUR, FABRIC, QUILT,"
277+
logError " SPONGEVANILLA, CUSTOM, MAGMA, MOHIST, CATSERVER, AIRPLANE, PUFFERFISH,"
278+
logError " CANYON, LIMBO, CRUCIBLE"
279279
exit 1
280280
;;
281281

282-
esac
282+
esac

scripts/start-deployAutoCF

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,11 @@ setArg --exclude-include-file CF_EXCLUDE_INCLUDE_FILE
5757
setArg --downloads-repo CF_DOWNLOADS_REPO
5858

5959
if ! mc-image-helper install-curseforge "${args[@]}"; then
60-
log "ERROR failed to auto-install CurseForge modpack"
60+
logError "Failed to auto-install CurseForge modpack"
6161
exit 1
6262
fi
6363

64-
# grab SERVER, TYPE, VERSION and export it
65-
set -a
66-
# shellcheck disable=SC1090
67-
source "${resultsFile}"
68-
set +a
64+
applyResultsFile ${resultsFile}
6965
resolveFamily
7066

7167
exec "${SCRIPTS:-/}start-setupWorld" "$@"

scripts/start-deployBukkitSpigot

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,56 @@ isDebugging && set -x
66

77
set -eo pipefail
88

9+
spigotBuildLog="/data/spigot_build.log"
10+
11+
function handleFailedSpigotBuild {
12+
logError "Failed to build Spigot"
13+
cat ${spigotBuildLog}
14+
exit 1
15+
}
16+
17+
918
function buildSpigotFromSource {
19+
local tempDir="/data/temp"
20+
1021
if [[ ${TYPE^^} = *BUKKIT ]] && ! versionLessThan "1.14"; then
11-
log "ERR craftbukkit build is only supported for versions less than 1.14"
22+
logError "Craftbukkit build is only supported for versions less than 1.14"
1223
exit 1
1324
fi
1425

1526
log "Building Spigot $VANILLA_VERSION from source, might take a while, get some coffee"
16-
rm -rf /data/temp
17-
mkdir /data/temp
18-
cd /data/temp
27+
rm -rf ${tempDir}
28+
mkdir ${tempDir}
29+
cd ${tempDir}
1930

2031
jvmOpts="-Xms${INIT_MEMORY:-$MEMORY} -Xmx${MAX_MEMORY:-$MEMORY}"
2132

2233
logn ''
23-
curl -sSL -o /data/temp/BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar && \
24-
java $jvmOpts -jar /data/temp/BuildTools.jar --rev "$VERSION" 2>&1 |tee /data/spigot_build.log| while read l; do echo -n .; done; log "done"
34+
curl -sSL -o ${tempDir}/BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar && \
35+
java $jvmOpts -jar ${tempDir}/BuildTools.jar --rev "$VERSION" 2>&1 |tee ${spigotBuildLog}| while read l; do echo -n .; done; log "done"
2536

2637
case ${TYPE^^} in
2738
SPIGOT)
2839
if ! mv spigot-*.jar "/data/${SERVER}"; then
29-
log "ERR failed to build Spigot"
30-
cat /data/spigot_build.log
31-
exit 1
40+
handleFailedSpigotBuild
3241
fi
3342
;;
3443
*BUKKIT)
3544
if ! mv craftbukkit-*.jar "/data/${SERVER}"; then
36-
log "ERR failed to build Spigot"
37-
cat /data/spigot_build.log
38-
exit 1
45+
handleFailedSpigotBuild
3946
fi
4047
;;
4148
esac
4249

4350
log "Cleaning up"
44-
rm -rf /data/temp
51+
rm -rf ${tempDir}
4552
cd /data
4653
}
4754

4855
function downloadSpigot {
4956
local match
57+
local getBukkitBaseUrl="https://getbukkit.org/download/"
58+
local getBukkitSpigotUrl="${getBukkitBaseUrl}spigot"
5059
case "$TYPE" in
5160
*BUKKIT|*bukkit)
5261
match="CraftBukkit"
@@ -61,18 +70,21 @@ function downloadSpigot {
6170
esac
6271

6372
if [[ ${VERSION^^} = LATEST ]]; then
64-
if ! VERSION=$(restify https://getbukkit.org/download/spigot --attribute='property=og:title' | jq -r '.[0] | .attributes | select(.property == "og:title") | .content | split(" ") | .[-1]'); then
65-
log "ERROR: failed to retrieve latest version from https://getbukkit.org/download/spigot -- site might be down"
73+
if ! VERSION=$(restify ${getBukkitSpigotUrl} --attribute='property=og:title' | jq -r '.[0] | .attributes | select(.property == "og:title") | .content | split(" ") | .[-1]'); then
74+
logError "Failed to retrieve latest version from ${getBukkitSpigotUrl} -- site might be down"
6675
exit 1
6776
fi
6877
fi
6978

7079
if [[ -z $downloadUrl ]]; then
80+
downloadBaseUrl="https://"
81+
downloadSuffixUrl=".getbukkit.org/${getbukkitFlavor}/${getbukkitFlavor}-${VERSION}.jar"
7182
if versionLessThan 1.16.5 || { [[ ${getbukkitFlavor} = "craftbukkit" ]] && [[ ${VERSION} = "1.16.5" ]] ; }; then
72-
downloadUrl="https://cdn.getbukkit.org/${getbukkitFlavor}/${getbukkitFlavor}-${VERSION}.jar"
83+
downloadBaseUrl+="cdn"
7384
else
74-
downloadUrl="https://download.getbukkit.org/${getbukkitFlavor}/${getbukkitFlavor}-${VERSION}.jar"
85+
downloadBaseUrl+="download"
7586
fi
87+
downloadUrl="${downloadBaseUrl}${downloadSuffixUrl}"
7688
fi
7789

7890
setServerVar
@@ -94,7 +106,7 @@ function downloadSpigot {
94106
cat <<EOF
95107
96108
ERROR: failed to download from $downloadUrl
97-
Visit https://getbukkit.org/download/${getbukkitFlavor} to lookup the
109+
Visit ${getBukkitBaseUrl}${getbukkitFlavor} to lookup the
98110
exact version or see if download site is unavailable.
99111
Click into the version entry to find the **exact** version.
100112

0 commit comments

Comments
 (0)