From c7730c8c8ab6bbedb58c4c91589d504f05ab37f1 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sat, 20 Feb 2021 13:17:55 +0100 Subject: [PATCH] Update scripts to include multiserver --- README | 12 ++--- scripts/common.sh | 114 +++++++++++++++++++++++++++++++++------------ scripts/console.sh | 10 ++-- scripts/restart.sh | 8 ++-- scripts/start.sh | 6 ++- scripts/stop.sh | 8 ++-- 6 files changed, 110 insertions(+), 48 deletions(-) diff --git a/README b/README index c33aec8..6ca1b5f 100644 --- a/README +++ b/README @@ -20,9 +20,9 @@ Elidragon v2 requires a GNU/Linux system to run. The setup and update scripts re 2. git clone this repository into that user's home directory and rename it to .minetest 3. cd into .minetest 4. run scripts/setup.sh - this will automatically install minetest and screen. Also, it initialized the submodules - all external mods and MineClone2 are installed automatically. It will also install multiserver and its dependencies. -5. run scripts/start.sh - this will automatically start all worlds. If one world crashes / shuts down it will automatically restart. +5. run scripts/start.sh - this will automatically start all worlds and multiserver. If one world or multiserver crashes / shuts down it will automatically restart. -To update, run scripts/update.sh, to restart all worlds run scripts/restart.sh and to stop all worlds run scripts/stop.sh +To update, run scripts/update.sh, to restart all worlds and multiserver run scripts/restart.sh and to stop all worlds and multiserver run scripts/stop.sh Organisation structure ------------------- @@ -38,13 +38,13 @@ scripts/setup.sh: This will install minetest and screen using sudo apt. For mine scripts/update.sh: This will pull the repository including all submodules. Also, it will update minetest, screen and multiserver. -scripts/start.sh: You can use this to start one or all worlds from the worlds folder. If you pass an argument to it, it will start the specified world, else it will start everything. You can use this script even if some worlds are already running. The worlds will be started in a hidden screen and restarted when killed, shut down using /shutdown or crash. Any started world will have a lock file in the /tmp directory that contains the PID of the current minetest process. When the world is stopped, the lock file is deleted. +scripts/start.sh: You can use this to start one or all worlds from the worlds folder. If you pass an argument to it, it will start the specified world, else it will start everything. You can use this script even if some worlds are already running. The worlds will be started in a hidden screen and restarted when killed, shut down using /shutdown or crash. Any started world will have a lock file in the /tmp directory that contains the PID of the current minetest process. When the world is stopped, the lock file is deleted. If --multiserver is used, multiserver is started instead of any worlds, and if all worlds are started, multiserver is started as well. -scripts/stop.sh: You can use this to stop one or all worlds from the worlds folder started using the start script. If you pass an argument to it, it will stop the specified world, else it will stop everything. You can use this script even if some worlds are not running. +scripts/stop.sh: You can use this to stop one or all worlds from the worlds folder started using the start script. If you pass an argument to it, it will stop the specified world, else it will stop everything. You can use this script even if some worlds are not running. If --multiserver is used, multiserver is stopped instead of any worlds, and if all worlds are stopped, multiserver is stopped as well. -scripts/restart.sh: You can use this to restart one or all running worlds. You can use this script even if some worlds are not running. It will only restart the running worlds. +scripts/restart.sh: You can use this to restart one or all running worlds. You can use this script even if some worlds are not running. It will only restart the running worlds. If --multiserver is used, multiserver is restarted instead of any worlds (in case it is running) and if all worlds are restarted, multiserver is restarted as well (in case it is running). -scripts/console.sh: You can used this to access the console of the world specified in the argument. (minetest --terminal running in a screen) +scripts/console.sh: You can used this to access the console of the world specified in the argument (minetest --terminal running in a screen). If --multiserver is used, the multiserver screen will be shown. scripts/common.sh: This script should not be started, it contains common functions imported by other scripts. You can modify it to e.g. change the paths of lock files or screen names. diff --git a/scripts/common.sh b/scripts/common.sh index 2f65662..caa3dad 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -4,29 +4,38 @@ function world_lock { echo "/tmp/ElidragonV2_$1_lock" } +function multiserver_lock { + echo "/tmp/ElidragonV2_Multiserver_lock" +} + function world_screenname { echo "Elidragon v2 - $1" } +function multiserver_screenname { + echo "Elidragon v2 - Multiserver" +} + function kill_world { kill `cat \`world_lock $1\`` } -function is_running { - return $(test -f `world_lock $1`) +function kill_multiserver { + kill -2 `cat \`multiserver_lock\`` } -function loop_worlds { - WORLDS=`ls worlds` - for WORLD in $WORLDS; do - $1 $WORLD - done +function world_running { + return $([ -f `world_lock $1` ]) +} + +function multiserver_running { + return $([ -f `multiserver_lock` ]) } function assert_running { - if ! is_running $1; then + if ! world_running $1; then echo -e "\e[31mWorld $1 is not running\e[0m" - if [ -z "$2" ]; then + if [ -z $2 ]; then exit 1 else return 1 @@ -35,9 +44,9 @@ function assert_running { } function assert_not_running { - if is_running $1; then + if world_running $1; then echo -e "\e[31mWorld $1 is already running\e[0m" - if [ -z "$2" ]; then + if [ -z $2 ]; then exit 1 else return 1 @@ -45,21 +54,72 @@ function assert_not_running { fi } +function loop_worlds { + WORLDS=`ls worlds` + for WORLD in $WORLDS; do + $1 $WORLD + done +} + +function run_one_or_all { + if [ -z $2 ]; then + loop_worlds $1 + return 0 + else + $1 $2 + return 1 + fi +} + +function run_in_screen { + screen -dmS $1 bash -c " + touch $2 + while [ -f $2; ] do + bash -c \" + echo \\$\\$ > $2 + exec $3 + \" + done + " +} + +function start_multiserver { + echo -n "Starting Multiserver... " + if multiserver_running; then + echo -e "\e[31mMultiserver is already running\e[0m" + else + run_in_screen `multiserver_screenname` `multiserver_lock` "~/go/bin/multiserver" + echo -e "\e[32mDone\e[0m" + fi +} + +function stop_multiserver { + echo -n "Stopping Multiserver... " + if multiserver_running; then + kill_multiserver + rm `multiserver_lock` + + echo -e "\e[32mDone\e[0m" + else + echo -e "\e[31mMultiserver is not running\e[0m" + fi +} + +function restart_multiserver { + echo -n "Multiserver..." + if multiserver_running; then + kill_multiserver + + echo -e "\e[32mDone\e[0m" + else + echo -e "\e[31mMultiserver is not running\e[0m" + fi +} + function start_world { echo -n "Starting $1... " if assert_not_running $1 "true"; then - LOCK=`world_lock $1` - - screen -dmS `world_screenname $1` bash -c " - while is_running $1; do - bash -c \" - echo \\$\\$ > $LOCK - exec minetest --server --terminal --world worlds/$1 --config worlds/$1/minetest.conf --logfile worlds/$1/debug.txt - \" - done - rm $LOCK - " - + run_in_screen `world_screenname $1` `world_lock $1` "minetest --server --terminal --world worlds/$1 --config worlds/$1/minetest.conf --logfile worlds/$1/debug.txt" echo -e "\e[32mDone\e[0m" fi } @@ -82,11 +142,3 @@ function restart_world { echo -e "\e[32mDone\e[0m" fi } - -function run_one_or_all { - if [ -z "$2" ]; then - loop_worlds $1 - else - $1 $2 - fi -} diff --git a/scripts/console.sh b/scripts/console.sh index 77d876d..ef1538c 100755 --- a/scripts/console.sh +++ b/scripts/console.sh @@ -1,9 +1,13 @@ #! /bin/bash # Elidragon v2 console script -# Attach to the console of a world +# Attach to the console of a world or multiserver # Arguments: source scripts/common.sh -assert_running $1 -screen -r `world_screenname $1` +if [[ $1 == "--multiserver" ]]; then + screen -r `multiserver_sceenname` +else + assert_running $1 + screen -r `world_screenname $1` +fi diff --git a/scripts/restart.sh b/scripts/restart.sh index f1e0f3a..53a385f 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -1,8 +1,10 @@ #! /bin/bash # Elidragon v2 restart script -# Restart one or all worlds -# Arguments: [] +# Restart one or all worlds, or if --multiserver is used, the multiserver. If all worlds are restarted, multiserver is restarted as well +# Arguments: [ | --multiserver] source source scripts/common.sh -run_one_or_all restart_world $1 +if [[ $1 == "--multiserver" || run_one_or_all restart_world $1 ]]; then + restart_multiserver +fi diff --git a/scripts/start.sh b/scripts/start.sh index 254b3a7..06a96e2 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,8 +1,10 @@ #! /bin/bash # Elidragon v2 start script -# Start one or all worlds +# Start one or all worlds, or if --multiserver is used, the multiserver. If all worlds are started, multiserver is started as well # Arguments: [] source source scripts/common.sh -run_one_or_all start_world $1 +if [ $1 = "--multiserver" ] || run_one_or_all start_world $1; then + start_multiserver +fi diff --git a/scripts/stop.sh b/scripts/stop.sh index 7e95f2c..8297ddc 100755 --- a/scripts/stop.sh +++ b/scripts/stop.sh @@ -1,8 +1,10 @@ #! /bin/bash # Elidragon v2 stop script -# Stop one or all worlds +# Stop one or all worlds, or if --multiserver is used, the multiserver. If all worlds are stopped, multiserver is stopped as well # Arguments: [] -source scripts/internal.sh +source scripts/common.sh -run_one_or_all stop_world $1 +if [[ $1 == "--multiserver" || run_one_or_all stop_world $1 ]]; then + stop_multiserver +fi -- 2.44.0