Files
mtgodot-poc/tools/server/rebuild.sh
T

97 lines
3.3 KiB
Bash

#!/bin/sh
# Rebuilds the 203 game/db from the 40250 source with our server patches, deploys it, or rolls it back.
# Run on the server from tools/server (all of tools/server copied there):
#
# sh rebuild.sh check which patches the source has
# sh rebuild.sh build apply missing patches, build game + db into /root/rebuild/<time>
# sh rebuild.sh deploy <dir> stop, back up share/bin/{game,db}, install <dir>/{game,db}, start
# sh rebuild.sh rollback <dir> stop, put a backup (/root/backup-bin/<time>) back, start
# sh rebuild.sh data rerun the data installers (quests, names, shops), then restart
#
# Patches, in order (each is a patch -p1 against /usr/metin2/src, saved byte-exact):
# inventory-5-pages 5-page inventory; needs its DB migration once (inventory-5-pages/README.md)
# hunt-set quest item.set_attribute, free items in extended shops
set -eu
SRC=${SRC:-/usr/metin2/src}
SERVER=${SERVER:-/usr/metin2/server}
HERE=$(cd "$(dirname "$0")" && pwd)
PATCHES="inventory-5-pages hunt-set"
TS=$(date +%Y%m%d-%H%M%S)
patch_state() {
if patch -p1 -R -C -s -f -d "$SRC" < "$HERE/$1/server.patch" > /dev/null 2>&1; then
echo applied
elif patch -p1 -C -s -f -d "$SRC" < "$HERE/$1/server.patch" > /dev/null 2>&1; then
echo missing
else
echo conflict
fi
}
stop_server() {
(cd "$SERVER" && sh close.sh) > /dev/null 2>&1 || true
sleep 8
if ps ax -o command | grep -q '^[.]/\(game\|db\|auth\)'; then echo "server still running" >&2; exit 1; fi
}
start_server() {
(cd "$SERVER" && echo 1 | sh start.sh) > /dev/null 2>&1
sleep 15
echo "running: $(ps ax -o command | grep -c '^[.]/game') game, $(ps ax -o command | grep -c '^[.]/db') db"
}
case ${1:-} in
check)
for p in $PATCHES; do echo "$p: $(patch_state $p)"; done
;;
build)
for p in $PATCHES; do
state=$(patch_state $p)
echo "$p: $state"
case $state in
missing) patch -p1 -s -d "$SRC" < "$HERE/$p/server.patch" ;;
conflict) echo "$p/server.patch does not apply" >&2; exit 1 ;;
esac
done
OUT=/root/rebuild/$TS
mkdir -p "$OUT"
# The Makefiles move the binary to GAMEDIR/DBDIR (default: the live share/bin).
(cd "$SRC/server/game/src" && gmake -j2 GAMEDIR="$OUT") > "$OUT/game.log" 2>&1 || { tail "$OUT/game.log" >&2; exit 1; }
(cd "$SRC/server/db/src" && gmake -j2 DBDIR="$OUT") > "$OUT/db.log" 2>&1 || { tail "$OUT/db.log" >&2; exit 1; }
ls -l "$OUT/game" "$OUT/db"
echo "built: $OUT (sh rebuild.sh deploy $OUT)"
;;
deploy)
NEW=${2:?usage: sh rebuild.sh deploy <dir with game and db>}
[ -x "$NEW/game" ] && [ -x "$NEW/db" ] || { echo "$NEW has no game/db" >&2; exit 1; }
stop_server
BACKUP=/root/backup-bin/$TS
mkdir -p "$BACKUP"
cp -p "$SERVER/share/bin/game" "$SERVER/share/bin/db" "$BACKUP/"
echo "backup: $BACKUP (sh rebuild.sh rollback $BACKUP)"
cp "$NEW/game" "$NEW/db" "$SERVER/share/bin/"
start_server
;;
rollback)
OLD=${2:?usage: sh rebuild.sh rollback <backup dir>}
[ -f "$OLD/game" ] && [ -f "$OLD/db" ] || { echo "$OLD has no game/db" >&2; exit 1; }
stop_server
cp "$OLD/game" "$OLD/db" "$SERVER/share/bin/"
start_server
;;
data)
stop_server
sh "$HERE/ring-warp/install.sh"
sh "$HERE/support-skills/install.sh"
sh "$HERE/hunt-set/install.sh"
sh "$HERE/npc-shops/install.sh" "$HERE/npc-shops/item-mall"
sh "$HERE/npc-shops/install.sh" "$HERE/npc-shops/mount"
start_server
;;
*)
sed -n '2,15p' "$0" >&2
exit 2
;;
esac