-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autopause functionality #531
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
2e2c883
set enviroment and packages
Oekn5w e77fae6
add dependant files in docker
Oekn5w 51d73a5
add files
Oekn5w e52ec31
set up autopause functionality
Oekn5w 6ce10c3
env variables refinement
Oekn5w 1606223
define functions and states scaffolding
Oekn5w 424651c
rename resume script
Oekn5w fb74035
compress pause script
Oekn5w 6a4807a
build state machine
Oekn5w b7f1a25
update environment variables
Oekn5w dcc6a71
move functions to separate file
Oekn5w bafd8e3
fix function call and output states to the terminal
Oekn5w 1a1eaa3
check for knockd failure
Oekn5w 6b0e663
add static max-tick check
Oekn5w 6a3cdfc
update knockd server port dynamically
Oekn5w 3193ffa
fix returns
Oekn5w 8830498
fix bash syntax
Oekn5w 6652add
add max-tick-time check
Oekn5w 5e8b5f1
rename period variable
Oekn5w 092258c
add readme entry
Oekn5w 356c103
fixes: whitespace and return values
Oekn5w a550427
exclude local connections as valid clients
Oekn5w 65c5fb2
also listen for rcon queries to resume server
Oekn5w 9bf3d42
save via rcon before suspending process
Oekn5w 801b53d
clarify network_mode restriction
Oekn5w 9ca44d0
wait for mc-monitor to return before pausing
Oekn5w df6ff76
add healthcheck wrapper script
Oekn5w 79d5e5b
typos and formatting
Oekn5w 31fc35e
adaption acc to review
Oekn5w 053b0eb
add new env var for startup timeout
Oekn5w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/bin/bash | ||
|
||
exec 1>/tmp/terminal-mc | ||
|
||
. /autopause/autopause-fcns.sh | ||
|
||
sudo /usr/sbin/knockd -c /autopause/knockd-config.cfg -d | ||
if [ $? -ne 0 ] ; then | ||
while : | ||
do | ||
if [[ -n $(ps -o comm | grep java) ]] ; then | ||
break | ||
fi | ||
sleep 0.1 | ||
done | ||
echo "[Autopause loop] Failed to start knockd daemon." | ||
echo "[Autopause loop] Possible cause: docker's host network mode." | ||
echo "[Autopause loop] Recreate without host mode or disable autopause functionality." | ||
echo "[Autopause loop] Stopping server." | ||
killall -SIGTERM java | ||
exit 1 | ||
fi | ||
|
||
STATE=INIT | ||
|
||
while : | ||
do | ||
case X$STATE in | ||
XINIT) | ||
# Server startup | ||
if mc_server_listening ; then | ||
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_INIT)) | ||
echo "[Autopause loop] MC Server listening for connections - stopping in $AUTOPAUSE_TIMEOUT_INIT seconds" | ||
STATE=K | ||
fi | ||
;; | ||
XK) | ||
# Knocked | ||
if java_clients_connected ; then | ||
echo "[Autopause loop] Client connected - waiting for disconnect" | ||
STATE=E | ||
else | ||
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then | ||
echo "[Autopause loop] No client connected since startup / knocked - stopping" | ||
/autopause/pause.sh | ||
STATE=S | ||
fi | ||
fi | ||
;; | ||
XE) | ||
# Established | ||
if ! java_clients_connected ; then | ||
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_EST)) | ||
echo "[Autopause loop] All clients disconnected - stopping in $AUTOPAUSE_TIMEOUT_EST seconds" | ||
STATE=I | ||
fi | ||
;; | ||
XI) | ||
# Idle | ||
if java_clients_connected ; then | ||
echo "[Autopause loop] Client reconnected - waiting for disconnect" | ||
STATE=E | ||
else | ||
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then | ||
echo "[Autopause loop] No client reconnected - stopping" | ||
/autopause/pause.sh | ||
STATE=S | ||
fi | ||
fi | ||
;; | ||
XS) | ||
# Stopped | ||
if rcon_client_exists ; then | ||
/autopause/resume.sh | ||
fi | ||
if java_running ; then | ||
if java_clients_connected ; then | ||
echo "[Autopause loop] Client connected - waiting for disconnect" | ||
STATE=E | ||
else | ||
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_KN)) | ||
echo "[Autopause loop] Server was knocked - waiting for clients or timeout" | ||
STATE=K | ||
fi | ||
fi | ||
;; | ||
*) | ||
echo "[Autopause loop] Error: invalid state: $STATE" | ||
;; | ||
esac | ||
if [[ "$STATE" == "S" ]] ; then | ||
# before rcon times out | ||
sleep 2 | ||
else | ||
sleep $AUTOPAUSE_PERIOD | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/bash | ||
|
||
current_uptime() { | ||
echo $(awk '{print $1}' /proc/uptime | cut -d . -f 1) | ||
} | ||
|
||
java_running() { | ||
[[ $( ps -a -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^S.*$ ]] | ||
} | ||
|
||
rcon_client_exists() { | ||
[[ -n "$(ps -a -o comm | grep 'rcon-cli')" ]] | ||
} | ||
|
||
mc_server_listening() { | ||
[[ -n $(netstat -tln | grep "0.0.0.0:$SERVER_PORT" | grep LISTEN) ]] | ||
} | ||
|
||
java_clients_connected() { | ||
local connections | ||
connections=$(netstat -tn | grep ":$SERVER_PORT" | grep ESTABLISHED) | ||
if [[ -z "$connections" ]] ; then | ||
return 1 | ||
fi | ||
IFS=$'\n' | ||
connections=($connections) | ||
unset IFS | ||
# check that at least one external address is not localhost | ||
# remember, that the host network mode does not work with autopause because of the knockd utility | ||
for (( i=0; i<${#connections[@]}; i++ )) | ||
do | ||
if [[ ! $(echo "${connections[$i]}" | awk '{print $5}') =~ ^\s*127\.0\.0\.1:.*$ ]] ; then | ||
# not localhost | ||
return 0 | ||
fi | ||
done | ||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[options] | ||
logfile = /dev/null | ||
[unpauseMCServer-server] | ||
sequence = 25565 | ||
seq_timeout = 1 | ||
command = /sbin/su-exec minecraft:minecraft /autopause/resume.sh | ||
tcpflags = syn | ||
[unpauseMCServer-rcon] | ||
sequence = 25575 | ||
seq_timeout = 1 | ||
command = /sbin/su-exec minecraft:minecraft /autopause/resume.sh | ||
tcpflags = syn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
|
||
if [[ $( ps -a -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 | ||
echo "[$(date -Iseconds)] [Autopause] Pausing Java process" >/tmp/terminal-mc | ||
killall -q -STOP java | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
if [[ $( ps -a -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^T.*$ ]] ; then | ||
echo "[$(date -Iseconds)] [Autopause] Knocked, resuming Java process" >/tmp/terminal-mc | ||
killall -q -CONT java | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
%minecraft ALL=(ALL) NOPASSWD:/usr/bin/killall | ||
%minecraft ALL=(ALL) NOPASSWD:/usr/sbin/knockd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
. /start-utils | ||
|
||
if isTrue "${ENABLE_AUTOPAUSE}" && [[ "$( ps -a -o stat,comm | grep 'java' | awk '{ print $1 }')" =~ ^T.*$ ]]; then | ||
echo "Java process suspended by Autopause function" | ||
exit 0 | ||
else | ||
mc-monitor status --host localhost --port $SERVER_PORT | ||
exit $? | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious what this is doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redirects stdout to the terminal, that is logged by docker, as this script is called detached