diff --git a/src/main.py b/src/main.py index 5ff8547..10e17b6 100644 --- a/src/main.py +++ b/src/main.py @@ -36,12 +36,11 @@ async def list_servers(client: DockerClient, all: bool = False) -> list[dict]: (List[Dict[str, any]]) A list of results from __extract_info """ - containers: list[Container] = list( - filter( - lambda x: "itzg/minecraft-server" in x.attrs["Config"]["Image"] and "net.forgeserv.hidden" not in x.labels, - client.containers.list(all=all), - ) - ) + def __container_filter(container: Container) -> bool: + image = container.attrs.get("Config", {}).get("Image", "") + return image == "itzg/minecraft-server" and "net.forgeserv.hidden" not in container.labels + + containers: list[Container] = list(filter(__container_filter, client.containers.list(all=all))) ret = await asyncio.gather(*(Server.from_container(c) for c in containers)) ret = await asyncio.gather(*(x.asdict() for x in ret)) diff --git a/src/server.py b/src/server.py index df7c3e1..346883e 100644 --- a/src/server.py +++ b/src/server.py @@ -3,6 +3,7 @@ from enum import Enum from os import getenv from typing import Any +import docker.errors from docker.models.containers import Container from mcstatus import JavaServer from mcstatus.responses import JavaStatusPlayer @@ -108,7 +109,7 @@ class Server: server = JavaServer(getenv("HOST_IP", "localhost"), port=int(pair["HostPort"])) status = server.status() return status.players.sample or [] - except ConnectionRefusedError: # Thrown if the port isn't actually a server port + except Exception: # noqa: BLE001, S112 continue return [] @@ -201,11 +202,14 @@ class Server: str: The resulting stdout value, or an error string to point you (me) to the logs. """ - exit_code, output = container.exec_run(cmd) - if exit_code != 0: - print(output.decode()) # type: ignore - return "⚠️ Error - Check Logs! ⚠️" - return output.decode() # type: ignore + try: + exit_code, output = container.exec_run(cmd) + if exit_code != 0: + print(output.decode()) # type: ignore + return "⚠️ Error - Check Logs! ⚠️" + return output.decode() # type: ignore + except docker.errors.APIError: # Thrown when the server is down. Return an empty string - not really an error. + return "" def __parse_server_properties(props: str) -> dict[str, str | None]: """Parses the server.properties contents into a useable dict @@ -222,9 +226,12 @@ class Server: lines = list(filter(lambda x: not x.startswith("#") and len(x), lines)) ret: dict[str, str | None] = {} for line in lines: - key, val = line.split("=") - val = str(val) if val else None - ret[key] = val + try: + key, val = line.split("=") + val = str(val) if val else None + ret[key] = val + except ValueError: # Sometimes line.split() only returns one object instead of two? + continue return ret ret = DerivedInfo() @@ -232,8 +239,8 @@ class Server: server_props = __get_from_exec("cat server.properties") # Then parse it into a useable format server_props = __parse_server_properties(server_props) - ret.motd = server_props["motd"] or "" - ret.max_players = int(server_props["max-players"] or "0") + ret.motd = server_props.get("motd", "") or "" + ret.max_players = int(server_props.get("max-players", "0") or "0") # We'll do something similar for the icon ret.icon = __get_from_exec("base64 -i server-icon.png")