N7: Dedicated server packaging, health endpoint and graceful shutdown
- packaging/server/tidewall-server: a wrapper that turns SIGTERM and SIGINT into a clean stop through the server's new --stop-file, since the engine can't catch signals. The server tells everyone it shut down, logs its statistics and exits 0; the wrapper kills it only after 15 s. - --config=PATH reads options from a file (packaging/server/server.cfg); options after it on the command line win. - --health-port answers GET /health with JSON (phase, players, tick, uptime) on loopback by default. HealthServer is non-blocking and bounded. - packaging/docker: a debian:13 image that checks the engine against tools/engine.sha512, imports the game at build time, runs as an unprivileged user, and works with a read-only root and a small /tmp. compose.yaml is project and container tidewall-server, read-only, no capabilities. - packaging/systemd: a hardened unit with a dynamic user. - make server-package-check: compose up, healthy, non-root, read-only, a real headless client joins over UDP into the drop, then SIGTERM stops it cleanly. This also covers H7 (graceful shutdown on SIGTERM). Closes #38 Closes #70 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue N7: the dedicated server's health endpoint answers GET /health with JSON,
|
||||
## refuses everything else, and drops requests that never finish.
|
||||
|
||||
var health: HealthServer
|
||||
|
||||
|
||||
func before_each() -> void:
|
||||
health = HealthServer.new()
|
||||
health.report = func() -> Dictionary: return {"phase": "lobby", "players": 2}
|
||||
assert_eq(health.listen(0), OK)
|
||||
|
||||
|
||||
func after_each() -> void:
|
||||
health.close()
|
||||
|
||||
|
||||
## Sends `request` and returns everything the server says before it closes.
|
||||
func _ask(request: String, limit_msec: int = 3000) -> String:
|
||||
var peer: StreamPeerTCP = StreamPeerTCP.new()
|
||||
assert_eq(peer.connect_to_host("127.0.0.1", health.port()), OK)
|
||||
var reply: PackedByteArray = PackedByteArray()
|
||||
var sent: bool = false
|
||||
var started: int = Time.get_ticks_msec()
|
||||
while Time.get_ticks_msec() - started < limit_msec:
|
||||
health.poll()
|
||||
peer.poll()
|
||||
var status: int = peer.get_status()
|
||||
if status == StreamPeerTCP.STATUS_CONNECTED:
|
||||
if not sent and not request.is_empty():
|
||||
peer.put_data(request.to_ascii_buffer())
|
||||
sent = true
|
||||
var available: int = peer.get_available_bytes()
|
||||
if available > 0:
|
||||
reply.append_array(peer.get_data(available)[1])
|
||||
elif status != StreamPeerTCP.STATUS_CONNECTING:
|
||||
break
|
||||
OS.delay_msec(5)
|
||||
peer.disconnect_from_host()
|
||||
return reply.get_string_from_utf8()
|
||||
|
||||
|
||||
func test_health_answers_with_json() -> void:
|
||||
var reply: String = _ask("GET /health HTTP/1.1\r\nHost: x\r\n\r\n")
|
||||
assert_true(reply.begins_with("HTTP/1.0 200 OK\r\n"), reply)
|
||||
var body: Variant = JSON.parse_string(reply.get_slice("\r\n\r\n", 1))
|
||||
assert_eq(body, {"status": "ok", "phase": "lobby", "players": 2.0})
|
||||
assert_eq(health.answered.get(200, 0), 1)
|
||||
|
||||
|
||||
func test_other_paths_and_methods_are_not_found() -> void:
|
||||
assert_true(_ask("GET /admin HTTP/1.0\r\n\r\n").begins_with("HTTP/1.0 404"))
|
||||
assert_true(_ask("POST /health HTTP/1.0\r\n\r\n").begins_with("HTTP/1.0 404"))
|
||||
assert_eq(health.answered.get(404, 0), 2)
|
||||
|
||||
|
||||
func test_unfinished_and_oversized_requests_are_dropped() -> void:
|
||||
assert_eq(_ask("GET /health HTTP/1.0\r\n", 2600), "", "never finished: closed without an answer")
|
||||
assert_eq(_ask("GET /" + "a".repeat(HealthServer.MAX_REQUEST_BYTES) + " HTTP/1.0\r\n\r\n"), "")
|
||||
assert_eq(health.answered.size(), 0)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dty6o6q6x6j4t
|
||||
@@ -119,3 +119,37 @@ func test_soak_options() -> void:
|
||||
assert_false(_parse(["--server", "--world-hash-every=300"]).is_valid(), "hashes are only logged when listening")
|
||||
assert_false(_parse(["--server", "--matches=-1"]).is_valid())
|
||||
|
||||
|
||||
func test_config_file_and_command_line_order() -> void:
|
||||
var path: String = "user://test_server.cfg"
|
||||
var file: FileAccess = FileAccess.open(path, FileAccess.WRITE)
|
||||
file.store_string("# a comment\n\nlisten\nport = 24700\nname = Harbour Light\nhealth-port = 24700\nno-lan\n")
|
||||
file.close()
|
||||
var config: ServerConfig = _parse(["--server", "--config=" + path, "--port=24702"])
|
||||
assert_true(config.is_valid(), str(config.errors))
|
||||
assert_true(config.listen)
|
||||
assert_false(config.lan)
|
||||
assert_eq(config.port, 24702, "the command line after --config wins")
|
||||
assert_eq(config.server_name, "Harbour Light")
|
||||
assert_eq(config.health_port, 24700)
|
||||
assert_eq(config.health_bind, "127.0.0.1")
|
||||
DirAccess.remove_absolute(ProjectSettings.globalize_path(path))
|
||||
|
||||
|
||||
func test_config_file_problems_are_errors() -> void:
|
||||
assert_false(_parse(["--server", "--config=user://no_such_file.cfg"]).is_valid())
|
||||
var path: String = "user://test_bad_server.cfg"
|
||||
var file: FileAccess = FileAccess.open(path, FileAccess.WRITE)
|
||||
file.store_string("listen\nprot = 5\nconfig = other.cfg\n")
|
||||
file.close()
|
||||
var config: ServerConfig = _parse(["--server", "--config=" + path])
|
||||
assert_eq(config.errors.size(), 2, str(config.errors))
|
||||
DirAccess.remove_absolute(ProjectSettings.globalize_path(path))
|
||||
|
||||
|
||||
func test_health_and_stop_options() -> void:
|
||||
var config: ServerConfig = _parse(["--server", "--listen", "--health-port=8080", "--health-bind=0.0.0.0", "--stop-file=/tmp/x"])
|
||||
assert_true(config.is_valid(), str(config.errors))
|
||||
assert_eq([config.health_port, config.health_bind, config.stop_file], [8080, "0.0.0.0", "/tmp/x"])
|
||||
assert_false(_parse(["--server", "--health-bind=localhost"]).is_valid())
|
||||
assert_false(_parse(["--server", "--health-port=70000"]).is_valid())
|
||||
|
||||
Reference in New Issue
Block a user