refactor: use packwiz installer for server setup

This commit is contained in:
2026-03-26 12:55:58 +08:00
parent 30444ffac2
commit 6557d2bc89
2 changed files with 105 additions and 125 deletions

165
start.sh
View File

@ -6,19 +6,23 @@ INSTALL_ROOT="${PT_INSTALL_ROOT:-$SCRIPT_DIR}"
SERVER_NAME="${PT_SERVER:-${1:-}}"
RUNTIME_BASE_DIR="${PT_RUNTIME_DIR:-$INSTALL_ROOT/.pt-panel-runtime}"
DOWNLOAD_DIR="$RUNTIME_BASE_DIR/downloads"
BOOTSTRAP_JAR="$DOWNLOAD_DIR/packwiz-installer-bootstrap.jar"
PACKWIZ_META_FILE="packwiz-installer.json"
JAVA_BIN="${JAVA_BIN:-java}"
JAVA_ARGS="${PT_JAVA_ARGS:--Xms1G -Xmx2G}"
JAVA_ARGS="${PT_JAVA_ARGS:--Xms1G -Xmx1G}"
SERVER_JAR_OVERRIDE="${PT_SERVER_JAR:-}"
FORCE_UPDATE="${PT_FORCE_UPDATE:-0}"
DRY_RUN="${PT_DRY_RUN:-0}"
SKIP_HASH_CHECK="${PT_SKIP_HASH_CHECK:-0}"
AUTO_EULA="${PT_AUTO_EULA:-TRUE}"
PACKWIZ_BOOTSTRAP_NO_UPDATE="${PT_PACKWIZ_BOOTSTRAP_NO_UPDATE:-0}"
FABRIC_INSTALLER_VERSION="${PT_FABRIC_INSTALLER_VERSION:-}"
PACKWIZ_BOOTSTRAP_URL="${PT_PACKWIZ_BOOTSTRAP_URL:-https://github.com/packwiz/packwiz-installer-bootstrap/releases/latest/download/packwiz-installer-bootstrap.jar}"
PACK_DIR=""
MODS_DIR=""
PACK_URL=""
PACK_SLUG=""
STAMP_FILE=""
MOD_STATE_FILE=""
log() {
printf '[pt-panel] %s\n' "$*"
@ -77,32 +81,6 @@ toml_value() {
trim_quotes "$line"
}
hash_cmd_for() {
case "$1" in
sha512) echo sha512sum ;;
sha256) echo sha256sum ;;
sha1) echo sha1sum ;;
md5) echo md5sum ;;
*) return 1 ;;
esac
}
verify_hash() {
local file="$1"
local expected="$2"
local format="$3"
local cmd actual
[ "$SKIP_HASH_CHECK" = "1" ] && return 0
cmd="$(hash_cmd_for "$format" || true)"
[ -n "$cmd" ] || return 0
command -v "$cmd" >/dev/null 2>&1 || return 0
actual="$($cmd "$file" | awk '{print $1}')"
[ "$actual" = "$expected" ]
}
list_available_servers() {
find "$SCRIPT_DIR" -mindepth 1 -maxdepth 1 -type d -name 'server-*' -exec basename {} \; | sort
}
@ -136,15 +114,11 @@ select_server() {
ensure_dirs() {
mkdir -p "$INSTALL_ROOT" "$RUNTIME_BASE_DIR" "$DOWNLOAD_DIR"
MODS_DIR="${PT_MODS_DIR:-$INSTALL_ROOT/mods}"
mkdir -p "$MODS_DIR"
}
prepare_server_paths() {
local safe_server_name
safe_server_name="$(printf '%s' "$SERVER_NAME" | tr '/' '_')"
STAMP_FILE="$RUNTIME_BASE_DIR/fabric-install-${safe_server_name}.stamp"
MOD_STATE_FILE="$RUNTIME_BASE_DIR/mods-${safe_server_name}.txt"
PACK_SLUG="$(printf '%s' "$SERVER_NAME" | tr '/' '_')"
STAMP_FILE="$RUNTIME_BASE_DIR/fabric-install-${PACK_SLUG}.stamp"
}
load_pack_versions() {
@ -157,7 +131,26 @@ load_pack_versions() {
[ -n "$FABRIC_LOADER_VERSION" ] || fail "无法从 $PACK_DIR/pack.toml 读取 fabric 版本"
}
compute_pack_url() {
if [ -n "${PT_PACK_URL:-}" ]; then
PACK_URL="$PT_PACK_URL"
return 0
fi
if [ -n "${PT_REPO_URL:-}" ] && [ -n "${PT_REPO_REF:-}" ]; then
PACK_URL="${PT_REPO_URL%/}/raw/branch/${PT_REPO_REF}/${SERVER_NAME}/pack.toml"
return 0
fi
PACK_URL="$PACK_DIR/pack.toml"
}
load_latest_fabric_installer_version() {
if [ -n "$FABRIC_INSTALLER_VERSION" ]; then
printf '%s' "$FABRIC_INSTALLER_VERSION"
return 0
fi
local meta version
meta="$(fetch_to_stdout 'https://meta.fabricmc.net/v2/versions/installer')"
version="$(printf '%s\n' "$meta" | grep '"version"' | head -n 1 | sed 's/.*"version": *"\([^"]*\)".*/\1/')"
@ -165,6 +158,15 @@ load_latest_fabric_installer_version() {
printf '%s' "$version"
}
ensure_packwiz_bootstrap() {
if [ -f "$BOOTSTRAP_JAR" ] && [ "$FORCE_UPDATE" != "1" ]; then
return 0
fi
log "下载 packwiz-installer-bootstrap"
fetch "$PACKWIZ_BOOTSTRAP_URL" "$BOOTSTRAP_JAR"
}
install_fabric_server() {
local installer_version installer_jar wanted_stamp current_stamp
@ -193,80 +195,24 @@ install_fabric_server() {
printf '%s\n' "$wanted_stamp" > "$STAMP_FILE"
}
cleanup_stale_mods() {
local current_file stale_name
run_packwiz_installer() {
local args=( -jar "$BOOTSTRAP_JAR" -g --side server --pack-folder "$INSTALL_ROOT" --meta-file "$PACKWIZ_META_FILE" "$PACK_URL" )
[ -f "$MOD_STATE_FILE" ] || return 0
if [ "$PACKWIZ_BOOTSTRAP_NO_UPDATE" = "1" ]; then
args=( -jar "$BOOTSTRAP_JAR" --bootstrap-no-update -g --side server --pack-folder "$INSTALL_ROOT" --meta-file "$PACKWIZ_META_FILE" "$PACK_URL" )
fi
while IFS= read -r stale_name; do
[ -n "$stale_name" ] || continue
current_file="$MODS_DIR/$stale_name"
if [ ! -f "$RUNTIME_BASE_DIR/current-mods.txt" ] || ! grep -Fxq "$stale_name" "$RUNTIME_BASE_DIR/current-mods.txt"; then
if [ -f "$current_file" ]; then
log "移除旧模组: $stale_name"
[ "$DRY_RUN" = "1" ] || rm -f "$current_file"
fi
fi
done < "$MOD_STATE_FILE"
}
if [ "$DRY_RUN" = "1" ]; then
log "DRY RUN: 跳过执行 packwiz-installer"
log "pack.toml URL: $PACK_URL"
return 0
fi
sync_mods() {
local meta file_name url hash_format hash dest tmp
local tmp_state_file="$RUNTIME_BASE_DIR/current-mods.txt"
: > "$tmp_state_file"
shopt -s nullglob
local files=("$PACK_DIR"/mods/*.pw.toml)
shopt -u nullglob
[ "${#files[@]}" -gt 0 ] || fail "$PACK_DIR/mods 下没有找到任何 .pw.toml 模组定义"
for meta in "${files[@]}"; do
file_name="$(toml_value 'filename' "$meta")"
url="$(toml_value 'url' "$meta")"
hash_format="$(toml_value 'hash-format' "$meta" || true)"
hash="$(toml_value 'hash' "$meta" || true)"
[ -n "$file_name" ] || fail "无法读取 $meta 中的 filename"
[ -n "$url" ] || fail "无法读取 $meta 中的 download.url"
printf '%s\n' "$file_name" >> "$tmp_state_file"
dest="$MODS_DIR/$file_name"
if [ "$FORCE_UPDATE" != "1" ] && [ -f "$dest" ]; then
if [ -n "$hash_format" ] && [ -n "$hash" ]; then
if verify_hash "$dest" "$hash" "$hash_format"; then
log "模组已存在且校验通过: $file_name"
continue
fi
log "模组校验失败,重新下载: $file_name"
else
log "模组已存在,跳过下载: $file_name"
continue
fi
else
log "下载模组: $file_name"
fi
if [ "$DRY_RUN" = "1" ]; then
log "DRY RUN: 跳过下载 $url"
continue
fi
tmp="$dest.part"
fetch "$url" "$tmp"
if [ -n "$hash_format" ] && [ -n "$hash" ] && ! verify_hash "$tmp" "$hash" "$hash_format"; then
rm -f "$tmp"
fail "模组校验失败: $file_name"
fi
mv "$tmp" "$dest"
done
cleanup_stale_mods
[ "$DRY_RUN" = "1" ] || mv "$tmp_state_file" "$MOD_STATE_FILE"
log "使用 packwiz-installer 同步服务端模组"
(
cd "$INSTALL_ROOT"
"$JAVA_BIN" "${args[@]}"
)
}
write_eula() {
@ -333,16 +279,19 @@ main() {
ensure_dirs
prepare_server_paths
load_pack_versions
compute_pack_url
ensure_packwiz_bootstrap
log "脚本目录: $SCRIPT_DIR"
log "安装目录: $INSTALL_ROOT"
log "选择服务端: $SERVER_NAME"
log "Pack 目录: $PACK_DIR"
log "Pack URL: $PACK_URL"
log "Minecraft 版本: $MINECRAFT_VERSION"
log "Fabric Loader 版本: $FABRIC_LOADER_VERSION"
install_fabric_server
sync_mods
run_packwiz_installer
write_eula
start_server
}