Files
AstraDeploy/cores/bds.sh

153 lines
3.8 KiB
Bash

#!/usr/bin/env bash
core_name_bds() {
echo "Bedrock Dedicated Server"
}
core_default_port_bds() {
echo "19132"
}
core_protocol_bds() {
echo "udp"
}
core_runtime_bds() {
echo "native"
}
core_validate_bds() {
if [[ -z "${MC_VERSION:-}" ]]; then
MC_VERSION="latest"
fi
}
bds_resolve_latest_download_url() {
local page
local url
page="$(http_get "https://www.minecraft.net/en-us/download/server/bedrock")"
if [[ -z "$page" ]]; then
fail "$(t no_stable_build "Bedrock Dedicated Server" "latest")"
fi
url="$(printf '%s\n' "$page" | grep -oE "https://minecraft\\.azureedge\\.net/bin-linux/bedrock-server-[^\" ]+\\.zip" | head -n 1)"
if [[ -z "$url" ]]; then
fail "$(t no_stable_build "Bedrock Dedicated Server" "latest")"
fi
echo "$url"
}
bds_resolve_download_url() {
local requested="${1:-latest}"
if [[ "$requested" == "latest" ]]; then
bds_resolve_latest_download_url
return
fi
if [[ "$requested" == http* ]]; then
echo "$requested"
return
fi
echo "https://minecraft.azureedge.net/bin-linux/bedrock-server-${requested}.zip"
}
bds_resolve_version_from_url() {
local url="$1"
local requested="${2:-latest}"
local file
file="${url##*/}"
file="${file#bedrock-server-}"
file="${file%.zip*}"
file="${file%%\?*}"
if [[ -z "$file" || "$file" == "$requested" ]]; then
echo "$requested"
return
fi
echo "$file"
}
bds_render_start_script() {
local dir="$SERVER_DIR"
emit_step "core_render" "bds start script"
run_cmd bash -c "cat > '$dir/start.sh' <<EOF
#!/usr/bin/env bash
set -e
cd \"\$(dirname \"\$0\")\"
export LD_LIBRARY_PATH=.:\$LD_LIBRARY_PATH
exec ./bedrock_server
EOF"
run_cmd chmod +x "$dir/start.sh"
emit_step "chmod_start_script" "$dir/start.sh"
}
core_install_bds() {
info "$(t core_install "$(core_name_bds)")"
emit_step "core_install" "$(core_name_bds)"
local requested_version="${MC_VERSION:-latest}"
local download_url
local version
local archive="$SERVER_DIR/.bds-package.zip"
download_url="$(bds_resolve_download_url "$requested_version")"
version="$(bds_resolve_version_from_url "$download_url" "$requested_version")"
emit_step "core_resolve_download" "$version"
emit_step "core_fetch" "$download_url"
if [[ -f "$SERVER_DIR/.bds-version" ]]; then
local existing_version
existing_version="$(cat "$SERVER_DIR/.bds-version" 2>/dev/null || true)"
if [[ "$UPDATE_MODE" == "no" && -n "$existing_version" && "$existing_version" == "$version" && -f "$SERVER_DIR/bedrock_server" ]]; then
info "BDS already at requested version ${version}. Skipping binary download."
run_cmd chmod +x "$SERVER_DIR/bedrock_server"
bds_render_start_script
return
fi
fi
run_cmd rm -f "$archive"
download_as_user "$download_url" "$archive"
emit_step "core_fetch" "Bedrock server archive"
run_cmd unzip -o -q "$archive" -d "$SERVER_DIR"
run_cmd rm -f "$archive"
if [[ ! -f "$SERVER_DIR/bedrock_server" ]]; then
fail "bedrock_server not found after extraction"
fi
run_cmd chmod +x "$SERVER_DIR/bedrock_server"
bds_render_start_script
run_cmd bash -lc "printf '%s\n' '$version' > '$SERVER_DIR/.bds-version'"
emit_step "core_configure" "BDS version ${version}"
}
core_post_install_bds() {
emit_step "core_post" "BDS"
run_cmd bash -lc "chown -R ${SERVER_USER}:${SERVER_USER} '$SERVER_DIR'"
emit_step "core_chown" "BDS"
open_firewall_port "$SERVER_PORT" "udp"
emit_step "core_service" "BDS"
create_service "$CORE" "$SERVER_DIR" "$SERVICE_MODE" "$SERVER_DIR/start.sh"
start_service
}
core_update_bds() {
info "$(t core_update "$(core_name_bds)")"
emit_step "core_update" "$(core_name_bds)"
backup_file "$SERVER_DIR/bedrock_server"
backup_file "$SERVER_DIR/start.sh"
backup_file "$SERVER_DIR/.bds-version"
core_install_bds
emit_step "core_update_done" "BDS"
}