mirror of
https://github.com/itzg/docker-minecraft-server.git
synced 2026-02-17 07:03:57 +00:00
Improve local development (#3661)
This commit is contained in:
143
scripts/auto/autopause-daemon.sh
Normal file
143
scripts/auto/autopause-daemon.sh
Normal file
@@ -0,0 +1,143 @@
|
||||
#!/bin/bash
|
||||
|
||||
. "$(dirname "$0")/autopause-fcns.sh"
|
||||
|
||||
# shellcheck source=../start-utils
|
||||
. "$(dirname "$0")/../start-utils"
|
||||
if isTrue "${DEBUG_AUTOPAUSE}"; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
autopause_error_loop() {
|
||||
logAutopause "Available interfaces within the docker container:"
|
||||
logAutopause " $(mc-image-helper network-interfaces)"
|
||||
logAutopause "Please set the environment variable AUTOPAUSE_KNOCK_INTERFACE to the interface that handles incoming connections."
|
||||
logAutopause "If unsure which interface to choose, run the ifconfig command in the container."
|
||||
logAutopause "Autopause failed to initialize. This log entry will be printed every 30 minutes."
|
||||
while :
|
||||
do
|
||||
sleep 1800
|
||||
logAutopause "Autopause failed to initialize."
|
||||
done
|
||||
}
|
||||
|
||||
# wait for java process to be started
|
||||
while :
|
||||
do
|
||||
if java_process_exists ; then
|
||||
break
|
||||
fi
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
# check for interface existence
|
||||
if [[ -z "$AUTOPAUSE_KNOCK_INTERFACE" ]] ; then
|
||||
logAutopause "AUTOPAUSE_KNOCK_INTERFACE variable must not be empty!"
|
||||
autopause_error_loop
|
||||
fi
|
||||
if ! mc-image-helper network-interfaces --check="$AUTOPAUSE_KNOCK_INTERFACE" ; then
|
||||
logAutopause "Selected interface \"$AUTOPAUSE_KNOCK_INTERFACE\" does not exist!"
|
||||
autopause_error_loop
|
||||
fi
|
||||
|
||||
knockdArgs=(-c /tmp/knockd-config.cfg -d -i "$AUTOPAUSE_KNOCK_INTERFACE")
|
||||
if isTrue "${DEBUG_AUTOPAUSE}"; then
|
||||
knockdArgs+=(-D)
|
||||
fi
|
||||
|
||||
/usr/local/sbin/knockd "${knockdArgs[@]}"
|
||||
|
||||
if [ $? -ne 0 ] ; then
|
||||
logAutopause "Failed to start knockd daemon."
|
||||
logAutopause "Probable cause: Unable to attach to interface \"$AUTOPAUSE_KNOCK_INTERFACE\"."
|
||||
autopause_error_loop
|
||||
fi
|
||||
|
||||
STATE=INIT
|
||||
|
||||
while :
|
||||
do
|
||||
isTrue "${DEBUG_AUTOPAUSE}" && log "DEBUG: autopause state = $STATE"
|
||||
case X$STATE in
|
||||
XINIT)
|
||||
# Server startup
|
||||
if mc_server_listening ; then
|
||||
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_INIT))
|
||||
|
||||
if [ -e /data/.skip-pause ] ; then
|
||||
logAutopause "'/data/.skip-pause' file is present - skipping pausing"
|
||||
else
|
||||
logAutopause "MC Server listening for connections - pausing in $AUTOPAUSE_TIMEOUT_INIT seconds"
|
||||
fi
|
||||
|
||||
STATE=K
|
||||
fi
|
||||
;;
|
||||
XK)
|
||||
# Knocked
|
||||
if java_clients_connected ; then
|
||||
logAutopause "Client connected - waiting for disconnect"
|
||||
STATE=E
|
||||
elif [ -e /data/.skip-pause ] ; then
|
||||
logAutopause "'/data/.skip-pause' file is present - skipping pausing"
|
||||
STATE=E
|
||||
else
|
||||
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
|
||||
logAutopause "No client connected since startup / knocked - pausing"
|
||||
"$(dirname "$0")/pause.sh"
|
||||
STATE=S
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
XE)
|
||||
# Established
|
||||
if ! java_clients_connected ; then
|
||||
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_EST))
|
||||
logAutopause "All clients disconnected - pausing in $AUTOPAUSE_TIMEOUT_EST seconds"
|
||||
STATE=I
|
||||
fi
|
||||
;;
|
||||
XI)
|
||||
# Idle
|
||||
if java_clients_connected ; then
|
||||
logAutopause "Client reconnected - waiting for disconnect"
|
||||
STATE=E
|
||||
elif [ -e /data/.skip-pause ] ; then
|
||||
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_EST))
|
||||
logAutopause "'/data/.skip-pause' file is present - skipping pausing"
|
||||
STATE=E
|
||||
else
|
||||
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
|
||||
logAutopause "No client reconnected - pausing"
|
||||
"$(dirname "$0")/pause.sh"
|
||||
STATE=S
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
XS)
|
||||
# Stopped
|
||||
if rcon_client_exists ; then
|
||||
"$(dirname "$0")/resume.sh"
|
||||
fi
|
||||
if java_running ; then
|
||||
if java_clients_connected ; then
|
||||
logAutopause "Client connected - waiting for disconnect"
|
||||
STATE=E
|
||||
else
|
||||
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_KN))
|
||||
logAutopause "Server was knocked - waiting for clients or timeout"
|
||||
STATE=K
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
logAutopause "Error: invalid state: $STATE"
|
||||
;;
|
||||
esac
|
||||
if [[ "$STATE" == "S" ]] ; then
|
||||
# before rcon times out
|
||||
sleep 5
|
||||
else
|
||||
sleep "$AUTOPAUSE_PERIOD"
|
||||
fi
|
||||
done
|
||||
64
scripts/auto/autopause-fcns.sh
Normal file
64
scripts/auto/autopause-fcns.sh
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/bin/bash
|
||||
|
||||
# shellcheck source=../scripts/start-utils
|
||||
. "$(dirname "$0")/../start-utils"
|
||||
current_uptime() {
|
||||
awk '{print $1}' /proc/uptime | cut -d . -f 1
|
||||
}
|
||||
|
||||
java_running() {
|
||||
[[ $( ps -ax -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^S.*$ ]]
|
||||
}
|
||||
|
||||
java_process_exists() {
|
||||
[[ -n "$(ps -ax -o comm | grep 'java')" ]]
|
||||
}
|
||||
|
||||
rcon_client_exists() {
|
||||
[[ -n "$(ps -ax -o comm | grep 'rcon-cli')" ]]
|
||||
}
|
||||
|
||||
use_proxy() {
|
||||
if isTrue "$USES_PROXY_PROTOCOL"; then
|
||||
echo "--use-proxy"
|
||||
fi
|
||||
}
|
||||
|
||||
use_server_list_ping() {
|
||||
if [[ "${VERSION^^}" == "LATEST" || "${VERSION^^}" == "SNAPSHOT" ]]; then
|
||||
# Don't use server-list ping for unknown version
|
||||
return 1
|
||||
fi
|
||||
|
||||
if versionLessThan 1.7; then
|
||||
echo "--use-server-list-ping"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
mc_server_listening() {
|
||||
mc-monitor status $(use_proxy) --host "${SERVER_HOST:-localhost}" --port "$SERVER_PORT" $(use_server_list_ping) --timeout 10s >&/dev/null
|
||||
}
|
||||
|
||||
java_clients_connections() {
|
||||
local connections
|
||||
if java_running; then
|
||||
if ! connections=$(mc-monitor status \
|
||||
--host "${SERVER_HOST:-localhost}" \
|
||||
--port "$SERVER_PORT" \
|
||||
--retry-limit "${AUTOPAUSE_STATUS_RETRY_LIMIT:-10}" --retry-interval "${AUTOPAUSE_STATUS_RETRY_INTERVAL:-2s}" \
|
||||
$(use_proxy) $(use_server_list_ping) \
|
||||
--show-player-count); then
|
||||
# consider it a non-zero player count if the ping fails
|
||||
# otherwise a laggy server with players connected could get paused
|
||||
connections=1
|
||||
fi
|
||||
else
|
||||
connections=0
|
||||
fi
|
||||
echo $connections
|
||||
}
|
||||
|
||||
java_clients_connected() {
|
||||
(( $(java_clients_connections) > 0 ))
|
||||
}
|
||||
87
scripts/auto/autostop-daemon.sh
Normal file
87
scripts/auto/autostop-daemon.sh
Normal file
@@ -0,0 +1,87 @@
|
||||
#!/bin/bash
|
||||
|
||||
# needed for the clients connected function residing in autopause
|
||||
. "$(dirname "$0")/autopause-fcns.sh"
|
||||
|
||||
# shellcheck source=../../scripts/start-utils
|
||||
. "$(dirname "$0")/../start-utils"
|
||||
if isTrue "${DEBUG_AUTOSTOP}"; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
# wait for java process to be started
|
||||
while :
|
||||
do
|
||||
if java_process_exists ; then
|
||||
break
|
||||
fi
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
STATE=INIT
|
||||
|
||||
while :
|
||||
do
|
||||
isTrue "${DEBUG_AUTOSTOP}" && log "DEBUG: autostop state = $STATE"
|
||||
case X$STATE in
|
||||
XINIT)
|
||||
# Server startup
|
||||
if mc_server_listening ; then
|
||||
TIME_THRESH=$(($(current_uptime)+AUTOSTOP_TIMEOUT_INIT))
|
||||
|
||||
if [ -e /data/.skip-stop ] ; then
|
||||
logAutostop "'/data/.skip-stop' file is present - skipping stopping"
|
||||
else
|
||||
logAutostop "MC Server listening for connections - stopping in $AUTOSTOP_TIMEOUT_INIT seconds"
|
||||
fi
|
||||
|
||||
STATE=II
|
||||
fi
|
||||
;;
|
||||
XII)
|
||||
# Initial idle
|
||||
if java_clients_connected ; then
|
||||
logAutostop "Client connected - waiting for disconnect"
|
||||
STATE=E
|
||||
elif [ -e /data/.skip-stop ] ; then
|
||||
logAutostop "'/data/.skip-stop' file is present - skipping stopping"
|
||||
STATE=E
|
||||
else
|
||||
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
|
||||
logAutostop "No client connected since startup - stopping server"
|
||||
/auto/stop.sh
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
XE)
|
||||
# Established
|
||||
if ! java_clients_connected ; then
|
||||
TIME_THRESH=$(($(current_uptime)+$AUTOSTOP_TIMEOUT_EST))
|
||||
logAutostop "All clients disconnected - stopping in $AUTOSTOP_TIMEOUT_EST seconds"
|
||||
STATE=I
|
||||
fi
|
||||
;;
|
||||
XI)
|
||||
# Idle
|
||||
if java_clients_connected ; then
|
||||
logAutostop "Client reconnected - waiting for disconnect"
|
||||
STATE=E
|
||||
elif [ -e /data/.skip-stop ] ; then
|
||||
TIME_THRESH=$(($(current_uptime)+$AUTOSTOP_TIMEOUT_EST))
|
||||
logAutostop "'/data/.skip-stop' file is present - skipping stopping"
|
||||
STATE=E
|
||||
else
|
||||
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
|
||||
logAutostop "No client reconnected - stopping"
|
||||
/auto/stop.sh
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
logAutostop "Error: invalid state: $STATE"
|
||||
;;
|
||||
esac
|
||||
sleep $AUTOSTOP_PERIOD
|
||||
done
|
||||
27
scripts/auto/pause.sh
Normal file
27
scripts/auto/pause.sh
Normal file
@@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
|
||||
. "$(dirname "$0")/../start-utils"
|
||||
if isTrue "${DEBUG_AUTOPAUSE}"; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
if [[ $( ps -ax -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^S.*$ ]] ; then
|
||||
# save world
|
||||
rcon-cli save-all >/dev/null
|
||||
|
||||
# wait until mc-monitor is no longer connected to the server
|
||||
while :
|
||||
do
|
||||
if [[ -z "$(netstat -nt | grep "127.0.0.1:$SERVER_PORT" | grep 'ESTABLISHED')" ]]; then
|
||||
break
|
||||
fi
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
# finally pause the process
|
||||
logAutopauseAction "Pausing Java process"
|
||||
pkill -STOP java
|
||||
|
||||
# create .paused file in data directory
|
||||
touch /data/.paused
|
||||
fi
|
||||
13
scripts/auto/resume.sh
Normal file
13
scripts/auto/resume.sh
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
. "$(dirname "$0")/../start-utils"
|
||||
if isTrue "${DEBUG_AUTOPAUSE}"; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
if [[ $( ps -ax -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^T.*$ ]] ; then
|
||||
pkill -CONT java
|
||||
|
||||
# remove .paused file from data directory
|
||||
rm -f /data/.paused
|
||||
fi
|
||||
10
scripts/auto/stop.sh
Normal file
10
scripts/auto/stop.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
. "$(dirname "$0")/../start-utils"
|
||||
if isTrue "${DEBUG_AUTOSTOP}"; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
logAutostopAction "Stopping Java process"
|
||||
pkill -f --signal SIGTERM mc-server-runner
|
||||
|
||||
Reference in New Issue
Block a user