Annotation of mtctl/mtctl_default.sh, Revision 1.1.1.1
1.1 bountyht 1: #---------------------------------------------------------------------
2: # Filename: mtctl
3: # Purpose: Minetest server Control
4: # License: Copyright (C) 2021 by Miniontoby <miniontoby@ircnow.org>
5: #---------------------------------------------------------------------
6:
7: #---------------------------------------------------------------------
8: # Alias
9:
10: mtinfo() {
11: MSG="$1"
12: echo -e "mtctl: $MSG" | sed -e 's/\\n/\\nmtctl: /'
13: }
14: mthelp() {
15: echo -e "usage: mtctl start|stop|restart|status|create worldname\n mtctl list|help|version|check_updates"; exit 1
16: }
17:
18:
19: #---------------------------------------------------------------------
20: # Variables
21: if [ "x$1" == "x" ]; then mthelp; exit 1; fi
22: ACTION="$1"
23: EXITDIR=$MTDIR/tmp
24: if [ "$MTBUILD" == "yes" ]; then SERVEREXE="$MTDIR/bin/minetestserver"; else SERVEREXE="`which minetestserver`"; fi
25: WORLDBASE=$MTDIR/worlds
26: LOGDIR=$MTDIR/log
27: EXITFLAGALL=$EXITDIR/minestop.all
28: WHOAMI=`whoami`
29: OK=0
30: VERSION="1.0"
31:
32:
33: #---------------------------------------------------------------------
34: # Normal Functions
35:
36: GetPIDS() {
37: 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/ .*//"`
38: }
39:
40: SetWorld() {
41: NAME=$1; if [ "x$NAME" == "x" ]; then mthelp; exit 1; fi
42: WORLDDIR=$WORLDBASE/$NAME; if [ \! -d $WORLDDIR ]; then mtstatus "Failed"; mtinfo "Error: World not found: $NAME"; exit 1; fi
43: 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
44: }
45: mtstatus() {
46: CHECK="$1"
47: EXTRA=""
48: if [ "$CHECK" == "Ok" ]; then
49: GetPIDS "$WORLDDIR" "$NAME"
50: if [ "x$FOO$BAR" \!= "x" ]; then
51: CHECK="Online"
52: else
53: CHECK="Offline"
54: fi
55: EXTRA="\n"
56: else if [ "$CHECK" == "Failed" ]; then EXTRA=" \n"; fi; fi
57: echo -ne "\r$NAME($CHECK)$EXTRA"
58: }
59:
60:
61: #---------------------------------------------------------------------
62: # Pre start
63:
64: if [ "$ACTION" == "version" ]; then mtinfo "Version: $VERSION"; exit 0; fi
65: if [ "@$WHOAMI" \!= "@$MTUSER" ]; then mtinfo "Please switch to $MTUSER"; exit 0; fi
66: cd $MTDIR || exit 1
67: touch temp.test && OK=1; if [ "@$OK" == "@0" ]; then mtinfo "Error: Directory tree should be owned by the MT user:\n$MTDIR"; exit 1; fi
68: rm temp.test || exit 1
69: if [ \! -f $SERVEREXE ]; then mtinfo "Error: Couldn't determine SERVEREXE setting\n$SERVEREXE"; exit 1; fi
70:
71: mkdir -p $EXITDIR || exit 1
72: mkdir -p $WORLDBASE || exit 1
73: mkdir -p $LOGDIR || exit 1
74:
75:
76: #---------------------------------------------------------------------
77: # Functions for executing the actions
78:
79: startMT() {
80: SetWorld "$1"; mtstatus
81: GetPIDS "$WORLDDIR" "$NAME"
82: BAR=`echo $BAR | wc -l`
83: if [ "x$FOO" \!= "x" ]; then
84: mtstatus "Failed"
85: mtinfo "Error: World seems already to be running\nIf it is not running, run $ mctl stop $NAME to be sure"
86: exit 1
87: fi
88:
89: EXITFLAGWORLD=$EXITDIR/minestop.$NAME
90: MINETEST_SUBGAME_PATH=$MTDIR/games
91: # MAYBE Export if linux doesnt work
92:
93: rm -f $EXITFLAGALL $EXITFLAGWORLD
94: sleep 1; DIR=`pwd` || exit 1; cd $WORLDDIR || exit 1
95: 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
96: cd $DIR || exit 1
97:
98: (
99: while true; do
100: $SERVEREXE --config $WORLDDIR/world.conf --port $PORT --logfile $LOGDIR/debug-$NAME.log --map-dir $WORLDDIR >> $LOGDIR/$NAME.log 2>&1
101: if [ -f $EXITFLAGALL ]; then exit 0; fi; if [ -f $EXITFLAGWORLD ]; then exit 0; fi; sleep 10
102: done
103: ) > $LOGDIR/start-$NAME.txt &
104: mtstatus "Ok"
105: }
106:
107: stopMT() {
108: SetWorld "$1"; mtstatus
109: GetPIDS "$WORLDDIR" "$NAME"
110: if [ "x$FOO$BAR" == "x" ]; then mtstatus "Failed"; mtinfo "$NAME seems to be stopped already"; exit 0; fi
111: 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
112: if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
113:
114: mtstatus "Waiting"
115: sleep 15; GetPIDS "$WORLDDIR" "$NAME"
116:
117: if [ "x$FOO$BAR" == "x" ]; then
118: mtstatus "Ok"
119: else
120: kill -9 $FOO; kill -9 $FOO; kill -9 $FOO; kill -9 $FOO;
121: if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
122:
123: mtstatus "Waiting."
124: sleep 15; GetPIDS "$WORLDDIR" "$NAME"
125: if [ "x$FOO$BAR" == "x" ]; then
126: mtstatus "Ok"
127: else
128: mtstatus "Failed"; mtinfo "Error: Stop of $NAME failed\nTry once more, then consult a sysadmin"; exit 1
129: fi
130: fi
131: }
132:
133: restartMT() {
134: SetWorld "$1"
135: /usr/bin/mtctl stop "$1"|| exit 1
136: /usr/bin/mtctl start "$1" || exit 1
137: }
138:
139: statusMT() {
140: SetWorld "$1";
141: mtstatus "Ok"
142: }
143:
144: listMT() {
145: for WORLDNAME in `ls $WORLDBASE 2> /dev/null | grep '^[a-zA-Z0-9]*$' | sort`; do
146: SetWorld "$WORLDNAME"
147: mtinfo "World: $WORLDNAME Port: $PORT"
148: done
149: }
150:
151: createMT() {
152: NAME=$1; if [ "x$NAME" == "x" ]; then mthelp; exit 1; fi
153: 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
154: echo -ne "\nServer Description: "; read SERVER_DESCRIPTION
155: PortCheck () {
156: 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
157: }
158: 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
159: echo -ne "\nMessage Of The Day [MOTD]: "; read MOTD; echo -ne "\nSeed: "; read SEED; echo -ne "\nCreative (true/false): "; read CREATIVE_MODE
160: echo -ne "\nEnable Damage (true/false): "; read ENABLE_DAMAGE; echo -ne "\nEnable Player Versus Player [PVP] (true/false): "; read ENABLE_PVP
161: echo -ne "\nUsername: "; read USERNAME
162:
163: cd $WORLDBASE; mkdir $NAME; cd $NAME
164: 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
165: 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
166: echo -ne "\nWorld created!\nWant to start the world?(yes/no): "; read startit
167: if [[ "$startit" == "yes" ]]; then /usr/bin/mtctl start $NAME; fi
168: echo -e "\nSuccess: \033[1;32mDone \033[m\nFeel free to join your new server at port $PORT"
169: }
170:
171: #---------------------------------------------------------------------
172: # Handle the actions
173:
174: case $ACTION in
175: start)
176: startMT "$2"
177: ;;
178: stop)
179: stopMT "$2"
180: ;;
181: restart)
182: restartMT "$2"
183: ;;
184: status)
185: statusMT "$2"
186: ;;
187: list)
188: listMT
189: ;;
190: create)
191: createMT "$2"
192: ;;
193: version)
194: mtinfo "Version: $VERSION"
195: ;;
196: check_updates)
197: NEWESTVERSION=$(curl https://cvsweb.planetofnix.com/cgi-bin/cvsweb/~checkout~/mtctl/version.txt?content-type=text/plain 2> /dev/null)
198: if [ "$NEWESTVERSION" \!= "$VERSION" ]; then
199: mtinfo "Update avaible!\n\nInstalling update NOW!"
200: curl 'https://cvsweb.planetofnix.com/cgi-bin/cvsweb/~checkout~/mtctl/installer.sh?content-type=text/plain' 2> /dev/null | $SHELL
201: fi
202: help)
203: mthelp
204: ;;
205: *)
206: mthelp
207: ;;
208: esac
209:
210:
211: #---------------------------------------------------------------------
212: # Final
CVSweb