From b27ddb2c63c9d1aa9d98a27f34bb49467175158c Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Fri, 26 Dec 2025 08:31:59 +0100 Subject: [PATCH] Add option to dynamically configure paper servers in proxy start script --- proxy/start.sh | 81 +++++++++++++++++++++++++++++++++++++++++++++----- start.sh | 35 +++++++++++++++------- 2 files changed, 98 insertions(+), 18 deletions(-) diff --git a/proxy/start.sh b/proxy/start.sh index f27e091..8737794 100755 --- a/proxy/start.sh +++ b/proxy/start.sh @@ -3,27 +3,48 @@ # How many seconds to sleep before starting the server again, unless it is explicitly specified DEFAULT_SLEEP_SECONDS=3 +declare -A DEV +DEV[option]="dev" +DEV[screen]="steptech" +DEV[dir]=".." + +# Which paper servers can be started with this script +SERVERS=( DEV ) + +# Which configuration to apply to the paper servers +CONFIG="proxy" # Velocity + function show_help { local cmd=$0 - # TODO accept list of servers as argument echo "Create a start loop for the proxy server and optionally start a number of paper servers with it" echo "" echo "Usage: $cmd [OPTIONS]" echo "" echo "OPTIONS:" - echo " -s --sleep How many seconds to sleep after stopping the server" - # TODO show server options - echo " -h --help Show this message" + { + local fmt=" %s\t%s\t%s\n" + + printf "$fmt" "-s" "--sleep" "How many seconds to sleep after stopping the server" + printf "$fmt" "-n" "--no-loop" "Don't restart the proxy server once it stopped" + + for server_name in "${SERVERS[@]}"; do + declare -n server="$server_name" + printf "$fmt" "" "--${server[option]}" "Start the ${server[screen]} server (${server[dir]})" + done + + printf "$fmt" "-h" "--help" "Show this message" + } | column -t -s $'\t' exit } function main { - # TODO create servers local sleep_seconds=$DEFAULT_SLEEP_SECONDS local help=false + local loop=true + local servers=() # Parse arguments local cmd=$0 @@ -34,12 +55,28 @@ function main { sleep_seconds=$2 shift 2 ;; + "--no-loop" | "-n") + loop=false + shift + ;; "--help" | "-h") help=true shift ;; *) - err "Unknown argument: $arg" + local option_found=false + for server_name in "${SERVERS[@]}"; do + declare -n server="$server_name" + if [ "$arg" = "--${server[option]}" ]; then + servers+=("$server_name") + option_found=true + fi + done + + if ! $option_found; then + err "Unknown argument: $arg" + fi + shift ;; esac done @@ -48,13 +85,41 @@ function main { show_help "$cmd" fi - while true - do + # Start servers + for server_name in "${servers[@]}"; do + declare -n server="$server_name" + # Create server screen if it doesn't exist yet + local screen="${server[screen]}" + screen -ls | grep -q "$screen" || screen -dmS "$screen" + # Start the server + screen -S "$screen" -X stuff "cd $(pwd)/${server[dir]}; ./start.sh --no-loop --config \"$CONFIG\" --tmp\n" + echo "Started paper server in screen $screen" + done + + function cleanup { + for server_name in "${servers[@]}"; do + declare -n server="$server_name" + local screen="${server[screen]}" + screen -S "$screen" -X stuff "stop\n" + echo "Stopped paper server in screen $screen" + done + + exit + } + + trap cleanup SIGINT + + while :; do echo "Starting proxy..." ./launch.sh + + $loop || break + echo "Sleeping for $sleep_seconds seconds..." sleep "$sleep_seconds" done + + cleanup } # Shows an error message and exits the program with code 1 diff --git a/start.sh b/start.sh index 5fdfe8d..93455e3 100755 --- a/start.sh +++ b/start.sh @@ -18,12 +18,18 @@ function show_help { echo "Usage $cmd [OPTIONS]" echo "" echo "OPTIONS:" - echo " -c --config How to configure the server (see ./configure.sh --help). Possible values for mode are: proxy, bungee, standalone" - echo " -t --temp --tmp Apply the configuration only temporary, while the server is running. Requires --config." - echo " -s --sleep How many seconds to sleep after the server has stopped before starting it again" - echo " -h --help Show help" - echo " --proxy Start the server behind a velocity proxy" - echo " --bungee Start the server behind a bungeecord proxy" + { + local fmt=" %s\t%s\t%s\n" + + printf "$fmt" "-c" "--config " "How to configure the server (see ./configure.sh --help). Possible values for mode are: proxy, bungee, standalone" + printf "$fmt" "-t" "--temp" "Apply the configuration only temporary, while the server is running. Requires --config." + printf "$fmt" "" "--tmp" "Alias to --temp" + printf "$fmt" "-s" "--sleep " "How many seconds to sleep after the server has stopped before starting it again" + printf "$fmt" "-n" "--no-loop" "Don't restart the server once it stopped" + printf "$fmt" "-h" "--help" "Show this message" + printf "$fmt" "" "--proxy" "Temporary configure this server for use with Velocity and start a Velocity proxy in the screen $PROXY_SCREEN while this server is running" + printf "$fmt" "" "--bungee" "Temporary configure this server for use with Bungeecord and start a Bungeecord proxy in the screen $PROXY_SCREEN while this server is running" + } | column -t -s $'\t' exit } @@ -36,6 +42,7 @@ function main { local config="" local start_proxy=false local tmp=false + local loop=true local cmd=$0 @@ -75,6 +82,10 @@ function main { sleep_seconds=$2 shift 2 ;; + "--no-loop" | "-n") + loop=false + shift + ;; "--proxy") proxy=true if $bungee; then @@ -133,7 +144,7 @@ function main { # create proxy screen if it doesn't exist yet screen -ls | grep -q "$PROXY_SCREEN" || screen -dmS "$PROXY_SCREEN" # start the proxy - local proxy_dir="$(pwd)/proxy" + local proxy_dir="" case "$config" in "velocity") proxy_dir=$VELOCITY_DIR @@ -142,7 +153,7 @@ function main { proxy_dir=$BUNGEE_DIR ;; esac - screen -S "$PROXY_SCREEN" -X stuff "cd $proxy_dir; ./launch.sh\n" + screen -S "$PROXY_SCREEN" -X stuff "cd $(pwd)/$proxy_dir; ./launch.sh\n" echo "The proxy has been started in screen $PROXY_SCREEN" fi @@ -161,13 +172,17 @@ function main { trap cleanup SIGINT - while true - do + while :; do echo "Starting server..." ./launch.sh + + $loop || break + echo "Sleeping for $sleep_seconds seconds..." sleep "$sleep_seconds" done + + cleanup } # Shows an error message and exits the program with code 1