Compare commits

...

1 Commits

Author SHA1 Message Date
dab509a234 testing 2024-05-28 03:55:52 -04:00
8 changed files with 116 additions and 23 deletions

View File

@ -61,34 +61,34 @@ define copy_build_files
sed -i 's/EULA=true/EULA=false/' "$${TEMP_DEST_FILE}"; \ sed -i 's/EULA=true/EULA=false/' "$${TEMP_DEST_FILE}"; \
fi; \ fi; \
if cmp -s "$${SRC_FILE}" "$${TEMP_DEST_FILE}"; then \ if cmp -s "$${SRC_FILE}" "$${TEMP_DEST_FILE}"; then \
echo "INFO: \"$${DEST_FILE}\" is up to date."; \ echo "[INFO]: \"$${DEST_FILE}\" is up to date."; \
rm "$${TEMP_DEST_FILE}"; \ rm "$${TEMP_DEST_FILE}"; \
else \ else \
echo "ERROR: \"$${DEST_FILE}\" is different from \"$${SRC_FILE}\""; \ echo "[ERROR]: \"$${DEST_FILE}\" is different from \"$${SRC_FILE}\""; \
diff -u "$${DEST_FILE}" "$${SRC_FILE}"; \ diff -u "$${DEST_FILE}" "$${SRC_FILE}"; \
rm "$${TEMP_DEST_FILE}"; \ rm "$${TEMP_DEST_FILE}"; \
exit 1; \ exit 1; \
fi; \ fi; \
else \ else \
cp "$${SRC_FILE}" "$${DEST_FILE}"; \ cp "$${SRC_FILE}" "$${DEST_FILE}"; \
echo "INFO: \"$${SRC_FILE}\" copied to \"$${DEST_FILE}\""; \ echo "[INFO]: \"$${SRC_FILE}\" copied to \"$${DEST_FILE}\""; \
fi; \ fi; \
else \ else \
echo "WARN: Source file \"$${SRC_FILE}\" does not exist."; \ echo "[WARN]: Source file \"$${SRC_FILE}\" does not exist."; \
fi fi
endef endef
configure: configure:
@if [ -d "./builds/$(BUILD)" ]; then \ @if [ -d "./builds/$(BUILD)" ]; then \
echo "INFO: Configuring build $(BUILD) (./builds/$(BUILD))" && \ echo "[INFO]: Configuring build $(BUILD) (./builds/$(BUILD))" && \
$(call copy_build_files,$(BUILD),.env); \ $(call copy_build_files,$(BUILD),.env); \
$(call copy_build_files,$(BUILD),plugins.json); \ $(call copy_build_files,$(BUILD),plugins.json); \
elif [ -d "./scratch/$(BUILD)" ]; then \ elif [ -d "./scratch/$(BUILD)" ]; then \
echo "INFO: Configuring build $(BUILD) (./scratch/$(BUILD))" && \ echo "[INFO]: Configuring build $(BUILD) (./scratch/$(BUILD))" && \
$(call copy_build_files,$(BUILD),.env,scratch); \ $(call copy_build_files,$(BUILD),.env,scratch); \
$(call copy_build_files,$(BUILD),plugins.json,scratch); \ $(call copy_build_files,$(BUILD),plugins.json,scratch); \
else \ else \
echo "ERROR: Build directory for \"$(BUILD)\" not found"; \ echo "[ERROR]: Build directory for \"$(BUILD)\" not found"; \
exit 1; \ exit 1; \
fi fi

View File

@ -74,7 +74,7 @@ services:
context: . context: .
dockerfile: ./dockerfiles/Dockerfile.velocity dockerfile: ./dockerfiles/Dockerfile.velocity
args: args:
VERSION: ${VERSION:-latest} VERSION: ${VELOCITY_VERSION:-latest}
image: ${VELOCITY_IMAGE:-localhost/velocity}:${VELOCITY_TAG:-latest} image: ${VELOCITY_IMAGE:-localhost/velocity}:${VELOCITY_TAG:-latest}
depends_on: depends_on:
- minecraft-jre - minecraft-jre

View File

@ -39,7 +39,7 @@ RUN set -eux && \
false) \ false) \
BUILD_TYPE='CRAFTBUKKIT' ;; \ BUILD_TYPE='CRAFTBUKKIT' ;; \
*) \ *) \
echo "ERROR: Invalid value for SPIGOT. Set to 'true' or 'false'"; \ echo "[ERROR]: Invalid value for SPIGOT. Set to 'true' or 'false'"; \
exit 1 ;; \ exit 1 ;; \
esac && \ esac && \
# Run BuildTools to build specified version # Run BuildTools to build specified version

View File

@ -36,7 +36,7 @@ RUN set -eux && \
false) \ false) \
JAVA_TYPE='jdk' ;; \ JAVA_TYPE='jdk' ;; \
*) \ *) \
echo "ERROR: Invalid value for JAVA_RUNTIME. Set to 'true' or 'false'"; \ echo "[ERROR]: Invalid value for JAVA_RUNTIME. Set to 'true' or 'false'"; \
exit 1 ;; \ exit 1 ;; \
esac && \ esac && \
# Install the Temurin version # Install the Temurin version

View File

@ -40,7 +40,7 @@ RUN set -eux && \
| jq -r '.downloads.application.sha256' \ | jq -r '.downloads.application.sha256' \
)" && \ )" && \
if [ ! "$SHA256" = "$EXPECTED" ]; then \ if [ ! "$SHA256" = "$EXPECTED" ]; then \
echo "[ERROR] SHA256=\"$SHA256\" expected \"$EXPECTED\""; \ echo "[ERROR]: SHA256=\"$SHA256\" expected \"$EXPECTED\""; \
exit 1; \ exit 1; \
fi && \ fi && \
mv /tmp/server.jar /app/server.jar mv /tmp/server.jar /app/server.jar

View File

@ -25,7 +25,7 @@ RUN set -eux && \
SHA1="$(sha1sum server.jar | awk '{print $1}')" && \ SHA1="$(sha1sum server.jar | awk '{print $1}')" && \
EXPECTED="$(jq -r .sha1 /tmp/dl.json)"; rm /tmp/dl.json && \ EXPECTED="$(jq -r .sha1 /tmp/dl.json)"; rm /tmp/dl.json && \
if [ ! "$SHA1" = "$EXPECTED" ]; then \ if [ ! "$SHA1" = "$EXPECTED" ]; then \
echo "[ERROR] SHA1=\"$SHA1\" expected \"$EXPECTED\""; \ echo "[ERROR]: SHA1=\"$SHA1\" expected \"$EXPECTED\""; \
exit 1; \ exit 1; \
fi fi

View File

@ -46,7 +46,7 @@ RUN set -eux && \
| jq -r '.downloads.application.sha256' \ | jq -r '.downloads.application.sha256' \
)" && \ )" && \
if [ ! "$SHA256" = "$EXPECTED" ]; then \ if [ ! "$SHA256" = "$EXPECTED" ]; then \
echo "[ERROR] SHA256=\"$SHA256\" expected \"$EXPECTED\""; \ echo "[ERROR]: SHA256=\"$SHA256\" expected \"$EXPECTED\""; \
exit 1; \ exit 1; \
fi && \ fi && \
mv /tmp/server.jar /app/velocity.jar mv /tmp/server.jar /app/velocity.jar

View File

@ -25,25 +25,72 @@ set_properties() {
if ! grep -q "^${KEY}=" "$FILE"; then if ! grep -q "^${KEY}=" "$FILE"; then
echo "[WARN]: \"$KEY\" does not exist in $FILE and was not updated" echo "[WARN]: \"$KEY\" does not exist in $FILE and was not updated"
else else
[ "$DEBUG" = "true" ] && echo "[DEBUG] Updating \"$KEY\" to \"$VALUE\"" [ "$DEBUG" = "true" ] && echo "[DEBUG]: Updating \"$KEY\" to \"$VALUE\""
sed -i.bak "s/^${KEY}=.*/${KEY}=${VALUE}/" "$FILE" sed -i.bak "s/^${KEY}=.*/${KEY}=${VALUE}/" "$FILE"
diff --unified=1 "${FILE}.bak" "$FILE" diff --unified=1 "${FILE}.bak" "$FILE" || true
rm "${FILE}.bak" rm "${FILE}.bak"
fi fi
else else
if [ "$DEBUG" = "true" ]; then if [ "$DEBUG" = "true" ]; then
echo "[DEBUG] \"$ENVVAR\" doesn't have the prefix \"$PREFIX\"" echo "[DEBUG]: \"$ENVVAR\" doesn't have the prefix \"$PREFIX\""
fi fi
fi fi
done < <(env) done < <(env)
# Show server.properties in DEBUG mode # Show server.properties in DEBUG mode
if [ "$DEBUG" = "true" ]; then if [ "$DEBUG" = "true" ]; then
echo "[DEBUG] Showing ${FILE}:" echo "[DEBUG]: Showing ${FILE}:"
cat "$FILE" cat "$FILE"
fi fi
} }
# Set Velocity's forwarding.secret
set_forwarding_secret() {
local WRITE_FILE
local FILE_CONTENT
WRITE_FILE=false
# Check file is not empty
if [ -s /app/forwarding.secret ]; then
FILE_CONTENT="$(head -c 1025 /app/forwarding.secret)"
# Check that FORWARDING_SECRET is blank
if [ ! "${#FORWARDING_SECRET}" -gt 0 ]; then
# Only the file was set, so FORWARDING_SECRET becomes the file
FORWARDING_SECRET="$(head -c 1025 /app/forwarding.secret)"
else
if [ ! "$FORWARDING_SECRET" = "$FILE_CONTENT" ]; then
# You should either bind mount a file in OR set a value
echo "[ERROR]: FORWARDING_SECRET is set with an existing file"
exit 1
fi
fi
# If the file is zero, we make sure the variable isn't also zero
elif [ "${#FORWARDING_SECRET}" -eq 0 ]; then
echo "[ERROR]: You must set FORWARDING_SECRET or set a value in the file"
exit 1
else
# File is zero, so we must write the variable out to the file
WRITE_FILE=true
fi
# Check length
if [ "${#FORWARDING_SECRET}" -lt 32 ]; then
echo "[ERROR]: FORWARDING_SECRET needs to be at least 32 characters long"
exit 1
elif [ "${#FORWARDING_SECRET}" -gt 1024 ]; then
echo "[ERROR]: FORWARDING_SECRET is >1024 bytes"
exit 1
fi
# Add secret to file
if [ "$WRITE_FILE" = "true" ]; then
echo "$FORWARDING_SECRET" > /app/forwarding.secret
fi
# Unset sensitive value
unset FORWARDING_SECRET
}
# Check if the minecraft screen is still running # Check if the minecraft screen is still running
# shellcheck disable=SC2317 # shellcheck disable=SC2317
check_screen() { check_screen() {
@ -56,6 +103,33 @@ check_screen() {
fi fi
} }
# Find Java PID, strace it, and wait for it to exit
wait_on_java() {
local JAVA_PID
local JAVA_EXIT
# Debug mode
[ "$DEBUG" = "true" ] && ps aux
# Capture PID and test
JAVA_PID="$(pgrep java)"
if ! kill -0 "$SCREEN_PID" 2>/dev/null; then
echo "[ERROR]: Cannot find running Java process (PID: \"$JAVA_PID\")"
exit 1
fi
# strace Java PID and get return code
JAVA_EXIT="$(strace -e trace=exit -p "$JAVA_PID" 2>&1 \
| grep -oP '^\+\+\+ exited with \K[0-9]+')"
# Delay if Java exits non-zero
if [ ! "$JAVA_EXIT" = "0" ]; then
echo "[ERROR]: Java exited with non-zero status"
sleep "${EXIT_DELAY:-5}"
exit "$JAVA_EXIT"
fi
}
# Find screen PID, strace it, and wait for it to exit # Find screen PID, strace it, and wait for it to exit
wait_on_screen() { wait_on_screen() {
local SCREEN_PID local SCREEN_PID
@ -82,11 +156,23 @@ wait_on_screen() {
tail -f screenlog.0 & tail -f screenlog.0 &
TAIL_PID="$!" TAIL_PID="$!"
# Debug mode
[ "$DEBUG" = "true" ] && ps aux
# Wait for screen to exit # Wait for screen to exit
strace -e exit -e signal=none -p "$SCREEN_PID" 2>/dev/null & strace -e exit -e signal=none -p "$SCREEN_PID" 2>/dev/null &
STRACE_PID="$!" STRACE_PID="$!"
[ "$DEBUG" = "true" ] && ps aux
# Wait on Java PID first
wait_on_java
# Wait if screen is somehow still running
wait "$STRACE_PID" wait "$STRACE_PID"
# Kill tail PID
if kill -0 "$TAIL_PID" 2>/dev/null; then
kill "$TAIL_PID"
fi
} }
# Function to stop the server gracefully # Function to stop the server gracefully
@ -107,13 +193,13 @@ stop_server() {
# Check only this script is running (PID 1) and pgrep (2 PIDs total) # Check only this script is running (PID 1) and pgrep (2 PIDs total)
PGREP_OUTPUT="$(pgrep .)" PGREP_OUTPUT="$(pgrep .)"
if ! [ "$(echo "$PGREP_OUTPUT" | wc -l)" -eq 2 ]; then if ! [ "$(echo "$PGREP_OUTPUT" | wc -l)" -eq 2 ]; then
echo "[WARN] Some processes might not have exited:" echo "[WARN]: Some processes might not have exited:"
echo "$PGREP_OUTPUT" echo "$PGREP_OUTPUT"
exit 1 exit 1
fi fi
# Exit cleanly # Exit cleanly
echo "[INFO] Server stopped gracefully" echo "[INFO]: Server stopped gracefully"
exit 0 exit 0
else else
echo "[ERROR]: Can't find which screen to use" echo "[ERROR]: Can't find which screen to use"
@ -136,8 +222,12 @@ minecraft_server() {
# Set up a SIGTERM signal trap to stop the server # Set up a SIGTERM signal trap to stop the server
trap 'stop_server minecraft' SIGTERM trap 'stop_server minecraft' SIGTERM
# temp
ls -al /app
ls -al /app/config
# Run server in screen (without attaching) # Run server in screen (without attaching)
echo "[INFO] Starting Minecraft server" echo "[INFO]: Starting Minecraft server"
/usr/bin/screen -dmS minecraft -L \ /usr/bin/screen -dmS minecraft -L \
bash -c "/usr/bin/java $JVM_OPTS -jar server.jar --nogui" bash -c "/usr/bin/java $JVM_OPTS -jar server.jar --nogui"
@ -151,11 +241,14 @@ velocity_server() {
# Settings # Settings
JVM_OPTS="${JVM_OPTS:--Xms1G -Xmx2G}" JVM_OPTS="${JVM_OPTS:--Xms1G -Xmx2G}"
# Ensure there is a forwarding.secret
set_forwarding_secret
# Set up a SIGTERM signal trap to stop the server # Set up a SIGTERM signal trap to stop the server
trap 'stop_server velocity' SIGTERM trap 'stop_server velocity' SIGTERM
# Start server # Start server
echo "[INFO] Starting Velocity server" echo "[INFO]: Starting Velocity server"
/usr/bin/screen -dmS velocity -L \ /usr/bin/screen -dmS velocity -L \
bash -c " bash -c "
/usr/bin/java $JVM_OPTS -XX:+UseG1GC -XX:G1HeapRegionSize=4M \ /usr/bin/java $JVM_OPTS -XX:+UseG1GC -XX:G1HeapRegionSize=4M \
@ -170,7 +263,7 @@ velocity_server() {
# Enable debug mode # Enable debug mode
DEBUG="${DEBUG:-false}" DEBUG="${DEBUG:-false}"
if [ "$DEBUG" = "true" ]; then if [ "$DEBUG" = "true" ]; then
echo "[DEBUG] Running entrypoint script at $(which entrypoint.sh)" echo "[DEBUG]: Running entrypoint script at $(which entrypoint.sh)"
sleep 0.2 sleep 0.2
set -ux set -ux
fi fi