diff --git a/scripts/gitea-actions/discover-runners.xsh b/scripts/gitea-actions/discover-runners.xsh index 137963c..5475f92 100755 --- a/scripts/gitea-actions/discover-runners.xsh +++ b/scripts/gitea-actions/discover-runners.xsh @@ -41,6 +41,17 @@ def first_existing_compose(host, root): return None +def list_runner_containers(host): + """Return actual runner container names present on a host.""" + rc, text = ssh( + host, + "docker ps -a --format '{{.Names}}' | grep '^gitea-act-runner-' || true", + ) + if rc != 0 or not text: + return [] + return [line.strip() for line in text.splitlines() if line.strip()] + + report_dir = f"/tmp/runner-discovery-{datetime.now().strftime('%Y%m%d_%H%M%S')}" mkdir -p @(report_dir) report_path = os.path.join(report_dir, "discovery-report.txt") @@ -84,31 +95,29 @@ for host, root in HOSTS: _, size_text = ssh(host, "docker ps -a --size --format 'table {{.Names}}\t{{.Size}}'") data["container_sizes"] = size_text - # Explicit runner names avoid label-filter parsing issues seen previously. - rc1, inspect_1 = ssh( - host, - "docker inspect gitea-act-runner-1 --format 'runner1 restart={{.RestartCount}} oom={{.State.OOMKilled}} status={{.State.Status}} started={{.State.StartedAt}} image={{.Config.Image}}'", - ) - rc2, inspect_2 = ssh( - host, - "docker inspect gitea-act-runner-2 --format 'runner2 restart={{.RestartCount}} oom={{.State.OOMKilled}} status={{.State.Status}} started={{.State.StartedAt}} image={{.Config.Image}}'", - ) - if rc1 != 0: - inspect_1 = "RUNNER1_NOT_FOUND" - if rc2 != 0: - inspect_2 = "RUNNER2_NOT_FOUND" - data["runner_inspect"] = (inspect_1 + "\n" + inspect_2).strip() + runner_containers = list_runner_containers(host) + data["runner_containers"] = runner_containers - log1_rc, log1 = ssh(host, "docker logs --tail=40 gitea-act-runner-1") - log2_rc, log2 = ssh(host, "docker logs --tail=40 gitea-act-runner-2") - if log1_rc != 0: - log1 = "RUNNER1_LOGS_UNAVAILABLE" - if log2_rc != 0: - log2 = "RUNNER2_LOGS_UNAVAILABLE" - data["runner_logs"] = { - "gitea-act-runner-1": log1, - "gitea-act-runner-2": log2, - } + inspect_lines = [] + runner_logs = {} + if not runner_containers: + inspect_lines.append("NO_RUNNER_CONTAINERS_FOUND") + for container in runner_containers: + rc, inspect_text = ssh( + host, + f"docker inspect {container} --format 'container={container} restart={{{{.RestartCount}}}} oom={{{{.State.OOMKilled}}}} status={{{{.State.Status}}}} started={{{{.State.StartedAt}}}} image={{{{.Config.Image}}}}'", + ) + if rc != 0: + inspect_text = f"{container}_INSPECT_UNAVAILABLE" + inspect_lines.append(inspect_text) + + log_rc, log_text = ssh(host, f"docker logs --tail=40 {container}") + if log_rc != 0: + log_text = f"{container}_LOGS_UNAVAILABLE" + runner_logs[container] = log_text + + data["runner_inspect"] = "\n".join(inspect_lines).strip() + data["runner_logs"] = runner_logs results[host] = data print("Collected") @@ -134,8 +143,10 @@ with open(report_path, "w", encoding="utf-8") as handle: write_section(handle, "7. MEMORY STATUS", data.get("memory_status", "")) write_section(handle, "8. CONTAINER SIZES", data.get("container_sizes", "")) write_section(handle, "9. RUNNER INSPECT", data.get("runner_inspect", "")) - write_section(handle, "10. RUNNER LOGS (1)", data.get("runner_logs", {}).get("gitea-act-runner-1", "")) - write_section(handle, "11. RUNNER LOGS (2)", data.get("runner_logs", {}).get("gitea-act-runner-2", "")) + for index, container in enumerate(data.get("runner_containers", []), start=10): + write_section(handle, f"{index}. RUNNER LOGS ({container})", data.get("runner_logs", {}).get(container, "")) + if not data.get("runner_containers"): + write_section(handle, "10. RUNNER LOGS", "NO_RUNNER_CONTAINERS_FOUND") with open(summary_path, "w", encoding="utf-8") as handle: handle.write("# Infrastructure Discovery Summary\n\n") @@ -144,12 +155,7 @@ with open(summary_path, "w", encoding="utf-8") as handle: handle.write("|------|------|---------|-----------------|\n") for host, data in results.items(): - visible = 0 - ps = data.get("docker_ps", "") - if "gitea-act-runner-1" in ps: - visible += 1 - if "gitea-act-runner-2" in ps: - visible += 1 + visible = len(data.get("runner_containers", [])) handle.write(f"| {host} | {data.get('path_exists', 'N/A')} | {data.get('compose_file', 'N/A')} | {visible} |\n") print("\nDiscovery complete")