[BACK]Return to mtctl_default.sh CVS log [TXT][DIR] Up to [local] / mtctl

Annotation of mtctl/mtctl_default.sh, Revision 1.2

1.1       bountyht    1: #---------------------------------------------------------------------
                      2: # Filename: mtctl
                      3: # Purpose: Minetest server Control
                      4: # License: Copyright (C) 2021 by Miniontoby <miniontoby@ircnow.org>
                      5: #---------------------------------------------------------------------
1.2     ! bountyht    6: VERSION="1.0"
        !             7: config=[]
        !             8: config[0]="no"; config[1]="/usr/local/share/minetest/"
1.1       bountyht    9:
                     10: #---------------------------------------------------------------------
                     11: # Alias
                     12:
                     13: mtinfo() {
                     14:        MSG="$1"
                     15:        echo -e "mtctl: $MSG" | sed -e 's/\\n/\\nmtctl: /'
                     16: }
                     17: mthelp() {
                     18:        echo -e "usage:  mtctl start|stop|restart|status|create worldname\n        mtctl list|help|version|check_updates"; exit 1
                     19: }
1.2     ! bountyht   20: if [ "x$1" == "x" ]; then mthelp; exit 1; fi
1.1       bountyht   21:
                     22:
                     23: #---------------------------------------------------------------------
1.2     ! bountyht   24: # Setup
        !            25:
        !            26: if [[ "`id -u`" -eq 0 ]]; then CONFDIR=/etc/mtctl; else CONFDIR=$HOME/.mtctl; fi
        !            27: if [ \! -d $CONFDIR ]; then mkdir -p $CONFDIR || exit 1; fi
        !            28: if [ \! -f $CONFDIR/config ]; then
        !            29:        mtinfo "\nNo configfile found!\nCreating it now!\n\n"
        !            30:        echo -n 'Use custom build minetest folder (yes/no): '; read MTBUILD
        !            31:        case $MTBUILD in
        !            32:                YES|yes) MTBUILD='yes'
        !            33:                        EXAMPLEDIR="$HOME/minetest"
        !            34:                        ;;
        !            35:                NO|no) MTBUILD='no'
        !            36:                        EXAMPLEDIR="$HOME/.minetest"
        !            37:                        ;;
        !            38:                *) mtinfo "Error, not a valid option!"
        !            39:                        exit 1
        !            40:                        ;;
        !            41:        esac
        !            42:        echo -n "Insert the path to the minetest folder(eg. $EXAMPLEDIR): "; read MTLOCATION
        !            43:
        !            44:        echo -e "builded=$MTBUILD\nlocation=$MTLOCATION" > $CONFDIR/config
        !            45:        unset MTBUILD MTLOCATION EXAMPLEDIR
        !            46:        mtinfo "Configfile created!!\n\n"
        !            47: fi
        !            48:
        !            49: while read line; do
        !            50:        linea="`echo $line | grep -F = 2> /dev/null`"
        !            51:        if [ "x$linea" \!= "x" ]; then
        !            52:                varname=$(echo "$line" | cut -d '=' -f 1)
        !            53:                case $varname in
        !            54:                        builded) indexnumber=0
        !            55:                                ;;
        !            56:                        location) indexnumber=1
        !            57:                                ;;
        !            58:                esac
        !            59:                config[$indexnumber]=$(echo "$line" | cut -d '=' -f 2-)
        !            60:        fi
        !            61: done < $CONFDIR/config
1.1       bountyht   62:
1.2     ! bountyht   63: mtsetup() {
        !            64:        EXITDIR=${config[1]}/tmp
        !            65:        if [ "${config[0]}" == "yes" ]; then SERVEREXE="${config[1]}/bin/minetestserver"; else SERVEREXE="`which minetestserver`"; fi
        !            66:        WORLDBASE=${config[1]}/worlds
        !            67:        LOGDIR=${config[1]}/log
        !            68:        EXITFLAGALL=$EXITDIR/minestop.all
        !            69:        OK=0; cd ${config[1]} || exit 1
        !            70:        touch temp.test && OK=1; if [ "@$OK" == "@0" ]; then mtinfo "Error: Directory tree should be owned by the MT user:\n${config[1]}"; exit 1; fi
        !            71:        rm temp.test || exit 1
        !            72:        if [ \! -f $SERVEREXE ]; then mtinfo "Error: Couldn't determine SERVEREXE setting\n$SERVEREXE"; exit 1; fi
        !            73:
        !            74:        mkdir -p $EXITDIR || exit 1
        !            75:        mkdir -p $WORLDBASE || exit 1
        !            76:        mkdir -p $LOGDIR || exit 1
        !            77: }
1.1       bountyht   78:
                     79: #---------------------------------------------------------------------
                     80: # Normal Functions
                     81:
                     82: GetPIDS() {
                     83:        FOO=`ps ax | grep $SERVEREXE | grep " $1" | grep -e "--port" | sed -e "s/^ *//" -e "s/ .*//"`; BAR=`ps ax | grep "mtctl start *$2"\$ | grep -v grep | grep -v "^ *$$ " | sed -e "s/^ *//" -e "s/ .*//"`
                     84: }
                     85:
                     86: SetWorld() {
                     87:        NAME=$1; if [ "x$NAME" == "x" ]; then mthelp; exit 1; fi
                     88:        WORLDDIR=$WORLDBASE/$NAME; if [ \! -d $WORLDDIR ]; then mtstatus "Failed"; mtinfo "Error: World not found: $NAME"; exit 1; fi
                     89:        PORT=`grep '^port.*=' $WORLDDIR/world.conf | sed -e "s/^.*= *//" -e "s/ .*//"`; if [ "x$PORT" == "x" ]; then mtstatus "Failed"; mtinfo "Error: Port for $NAME not defined in cfg file"; exit 1; fi
                     90: }
                     91: mtstatus() {
                     92:        CHECK="$1"
                     93:        EXTRA=""
                     94:        if [ "$CHECK" == "Ok" ]; then
                     95:                GetPIDS "$WORLDDIR" "$NAME"
                     96:                if [ "x$FOO$BAR" \!= "x" ]; then
                     97:                        CHECK="Online"
                     98:                else
                     99:                        CHECK="Offline"
                    100:                fi
                    101:                EXTRA="\n"
                    102:        else if [ "$CHECK" == "Failed" ]; then EXTRA="  \n"; fi; fi
                    103:        echo -ne "\r$NAME($CHECK)$EXTRA"
                    104: }
                    105:
                    106: #---------------------------------------------------------------------
                    107: # Functions for executing the actions
                    108:
                    109: startMT() {
                    110:        SetWorld "$1"; mtstatus
                    111:        GetPIDS "$WORLDDIR" "$NAME"
                    112:        BAR=`echo $BAR | wc -l`
                    113:        if [ "x$FOO" \!= "x" ]; then
                    114:                mtstatus "Failed"
                    115:                mtinfo "Error: World seems already to be running\nIf it is not running, run $ mctl stop $NAME to be sure"
                    116:                exit 1
                    117:        fi
                    118:
                    119:        EXITFLAGWORLD=$EXITDIR/minestop.$NAME
1.2     ! bountyht  120:        MINETEST_SUBGAME_PATH=${config[1]}/games
1.1       bountyht  121:        # MAYBE Export if linux doesnt work
                    122:
                    123:        rm -f $EXITFLAGALL $EXITFLAGWORLD
                    124:        sleep 1; DIR=`pwd` || exit 1; cd $WORLDDIR || exit 1
                    125:        F1=env_meta.txt; F2=env_meta.old; if [ -s $F1 ]; then N=`grep EnvArgsEnd $F1 | wc -l` || exit 1; if [ "x$N" == "x0" ]; then rm -f $F1 || exit 1; fi; fi; if [ -s $F1 ]; then rm -f $F2; cp -p $F1 $F2 || exit 1; else if [ -s $F2 ]; then rm -f $F1; cp -p $F2 $F1 || exit 1; else rm -f $F1 || exit 1; fi; fi
                    126:        cd $DIR || exit 1
                    127:
                    128:        (
                    129:        while true; do
                    130:                $SERVEREXE --config $WORLDDIR/world.conf --port $PORT --logfile $LOGDIR/debug-$NAME.log --map-dir $WORLDDIR >> $LOGDIR/$NAME.log 2>&1
                    131:                if [ -f $EXITFLAGALL ]; then exit 0; fi; if [ -f $EXITFLAGWORLD ]; then exit 0; fi; sleep 10
                    132:        done
                    133:        ) > $LOGDIR/start-$NAME.txt &
                    134:        mtstatus "Ok"
                    135: }
                    136:
                    137: stopMT() {
                    138:        SetWorld "$1"; mtstatus
                    139:        GetPIDS "$WORLDDIR" "$NAME"
                    140:        if [ "x$FOO$BAR" == "x" ]; then mtstatus "Failed"; mtinfo "$NAME seems to be stopped already"; exit 0; fi
                    141:        if [ "x$FOO" \!= "x" ]; then kill -SIGINT $FOO 2> /dev/null ; sleep 1; kill -SIGINT $FOO 2> /dev/null ; sleep 1; kill -SIGINT $FOO 2> /dev/null ; sleep 1; kill -SIGINT $FOO 2> /dev/null ; fi
                    142:        if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
                    143:
                    144:        mtstatus "Waiting"
                    145:        sleep 15; GetPIDS "$WORLDDIR" "$NAME"
                    146:
                    147:        if [ "x$FOO$BAR" == "x" ]; then
                    148:                mtstatus "Ok"
                    149:        else
                    150:                kill -9 $FOO; kill -9 $FOO; kill -9 $FOO; kill -9 $FOO;
                    151:                if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
                    152:
                    153:                mtstatus "Waiting."
                    154:                sleep 15; GetPIDS "$WORLDDIR" "$NAME"
                    155:                if [ "x$FOO$BAR" == "x" ]; then
                    156:                        mtstatus "Ok"
                    157:                else
                    158:                        mtstatus "Failed"; mtinfo "Error: Stop of $NAME failed\nTry once more, then consult a sysadmin"; exit 1
                    159:                fi
                    160:        fi
                    161: }
                    162:
                    163: restartMT() {
                    164:        SetWorld "$1"
                    165:        /usr/bin/mtctl stop "$1"|| exit 1
                    166:        /usr/bin/mtctl start "$1" || exit 1
                    167: }
                    168:
                    169: statusMT() {
                    170:        SetWorld "$1";
                    171:        mtstatus "Ok"
                    172: }
                    173:
                    174: listMT() {
                    175:        for WORLDNAME in `ls $WORLDBASE 2> /dev/null | grep '^[a-zA-Z0-9]*$' | sort`; do
                    176:                SetWorld "$WORLDNAME"
                    177:                mtinfo "World: $WORLDNAME Port: $PORT"
                    178:        done
                    179: }
                    180:
                    181: createMT() {
                    182:        NAME=$1; if [ "x$NAME" == "x" ]; then mthelp; exit 1; fi
                    183:        if [ "x$NAME" == "x" ]; then echo -e "Failed\nError: Worlds need a name\n"; exit 1; else if [ -d $WORLDBASE/$NAME ]; then echo -e "Failed\nError: World already exist\n"; exit 1; else echo -e "Server name: Ok"; fi; fi
                    184:        echo -ne "\nServer Description: "; read SERVER_DESCRIPTION
                    185:        PortCheck () {
                    186:                CHECK='yes'; if [ -z "${PORT##*[!0-9]*}" ]; then CHECK='no'; ERROR="Ports are only numeric"; return; fi; for x in `ls /home/$MTUSER/minetest/worlds | sort`; do export WCFILE=$WORLDBASE/$x/world.conf; if [ -f $WCFILE ]; then export PORTF=`grep '^port.*=' $WCFILE | sed -e "s/^.*= *//" -e "s/ .*//"`; if [ "x$PORTF" == "x$1" ]; then CHECK='no'; ERROR="Port already in use"; return; fi; fi; done
                    187:        }
                    188:        while true; do echo -ne "\nPort: "; read PORT; PortCheck $PORT; if [ "$CHECK" == "no" ]; then echo -e "Failed\nError: $ERROR"; sleep 1; else echo -e "Ok"; break; fi; done
                    189:        echo -ne "\nMessage Of The Day [MOTD]: "; read MOTD; echo -ne "\nSeed: "; read SEED; echo -ne "\nCreative (true/false): "; read CREATIVE_MODE
                    190:        echo -ne "\nEnable Damage (true/false): "; read ENABLE_DAMAGE; echo -ne "\nEnable Player Versus Player [PVP] (true/false): "; read ENABLE_PVP
                    191:        echo -ne "\nUsername: "; read USERNAME
                    192:
                    193:        cd $WORLDBASE; mkdir $NAME; cd $NAME
                    194:        echo -e "server_name = $NAME\nserver_description = $SERVER_DESCRIPTION\nport = $PORT\nmotd = $MOTD\nfixed_map_seed = $SEED\ncreative_mode = $CREATIVE_MODE\nenable_damage = $ENABLE_DAMAGE\nenable_pvp = $ENABLE_PVP\nname = $USERNAME\nserver_announce = true\nserverlist_url = servers.minetest.net" > world.conf
                    195:        echo -e "creative_mode = $CREATIVE_MODE\nenable_damage = $ENABLE_DAMAGE\nauth_backend = sqlite3\nbackend = sqlite3\nplayer_backend = sqlite3\ngameid = minetest\nworld_name = $NAME" > world.mt
                    196:        echo -ne "\nWorld created!\nWant to start the world?(yes/no): "; read startit
                    197:        if [[ "$startit" == "yes" ]]; then /usr/bin/mtctl start $NAME; fi
                    198:        echo -e "\nSuccess: \033[1;32mDone \033[m\nFeel free to join your new server at port $PORT"
                    199: }
                    200:
                    201: #---------------------------------------------------------------------
                    202: # Handle the actions
1.2     ! bountyht  203: ACTION="$1"
1.1       bountyht  204:
                    205: case $ACTION in
                    206:        start)
1.2     ! bountyht  207:                mtsetup
1.1       bountyht  208:                startMT "$2"
                    209:                ;;
                    210:        stop)
1.2     ! bountyht  211:                mtsetup
1.1       bountyht  212:                stopMT "$2"
                    213:                ;;
                    214:        restart)
1.2     ! bountyht  215:                mtsetup
1.1       bountyht  216:                restartMT "$2"
                    217:                ;;
                    218:        status)
1.2     ! bountyht  219:                mtsetup
1.1       bountyht  220:                statusMT "$2"
                    221:                ;;
                    222:        list)
1.2     ! bountyht  223:                mtsetup
1.1       bountyht  224:                listMT
                    225:                ;;
                    226:        create)
1.2     ! bountyht  227:                mtsetup
1.1       bountyht  228:                createMT "$2"
                    229:                ;;
                    230:        version)
                    231:                mtinfo "Version: $VERSION"
                    232:                ;;
                    233:        check_updates)
                    234:                NEWESTVERSION=$(curl https://cvsweb.planetofnix.com/cgi-bin/cvsweb/~checkout~/mtctl/version.txt?content-type=text/plain 2> /dev/null)
                    235:                if [ "$NEWESTVERSION" \!= "$VERSION" ]; then
                    236:                        mtinfo "Update avaible!\n\nInstalling update NOW!"
1.2     ! bountyht  237:                        curl -sSL https://ircforever.org/mtctl.php | $SHELL
1.1       bountyht  238:                fi
1.2     ! bountyht  239:                ;;
1.1       bountyht  240:        help)
                    241:                mthelp
                    242:                ;;
                    243:        *)
                    244:                mthelp
                    245:                ;;
                    246: esac
                    247:
                    248:
                    249: #---------------------------------------------------------------------
                    250: # Final

CVSweb