Skip to content

Commit bef7b47

Browse files
authored
New logger with color and specific types. Code cleanup (#3108)
1 parent a356c68 commit bef7b47

38 files changed

+421
-425
lines changed

bin/mc-send-to-console

+5-9
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

+4-4
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

+1-6
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ if ! isTrue "${SKIP_SUDO:-false}" && [ "$(id -u)" = 0 ]; then
4848
echo 'hosts: files dns' > /etc/nsswitch.conf
4949
fi
5050

51-
distro=$(getDistro)
52-
if [[ $distro == alpine ]]; then
53-
exec su-exec ${runAsUser}:${runAsGroup} "${SCRIPTS:-/}start-configuration" "$@"
54-
else
55-
exec gosu ${runAsUser}:${runAsGroup} "${SCRIPTS:-/}start-configuration" "$@"
56-
fi
51+
exec $(getSudoFromDistro) ${runAsUser}:${runAsGroup} "${SCRIPTS:-/}start-configuration" "$@"
5752
else
5853
exec "${SCRIPTS:-/}start-configuration" "$@"
5954
fi

scripts/start-autopause

+23-43
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

+4-20
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

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

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

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

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

130130

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

@@ -184,7 +184,7 @@ if [[ $MOD_PLATFORM ]]; then
184184
;;
185185

186186
*)
187-
log "ERROR; Invalid MOD_PLATFORM: '$MOD_PLATFORM'"
187+
logError "Invalid MOD_PLATFORM: '$MOD_PLATFORM'"
188188
exit 1
189189
;;
190190
esac
@@ -219,7 +219,7 @@ case "${TYPE^^}" in
219219
QUILT)
220220
exec "${SCRIPTS:-/}start-deployQuilt" "$@"
221221
;;
222-
222+
223223
VANILLA)
224224
exec "${SCRIPTS:-/}start-deployVanilla" "$@"
225225
;;
@@ -273,11 +273,11 @@ case "${TYPE^^}" in
273273
;;
274274

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

283-
esac
283+
esac

scripts/start-deployAutoCF

+2-6
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" "$@"

0 commit comments

Comments
 (0)