Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add a `just ps` recipe for displaying service ports (#1160)
Madison Swain-Bowden29aab84e3157
diff --git a/documentation/guides/quickstart.md b/documentation/guides/quickstart.mdindex e767ec9e6..5e3b03907 100644--- a/documentation/guides/quickstart.md+++ b/documentation/guides/quickstart.md@@ -72,9 +72,38 @@ prerequisites.The `up` recipe orchestrates the following services: `cache`, `db`,`upstream_db`, `es`, `indexer_worker`, `ingestion_server`, `web`, `proxy`,- `plausible-ch`, `plausible-db` and `plausible`.+ `webserver`, `scheduler`, `s3`, `plausible-ch`, `plausible-db` and+ `plausible`.- Now you should be able to access the following endpoints:+ The `up` recipe also prints out services that have ports exposed to the host+ (this can also be seen by running `just ps`):++ ```+ ================================================================================+ Service Ports+ ================================================================================+ webserver (catalog):+ - http://0.0.0.0:9090 (→ 8080)+ ingestion_server (ingestion_server):+ - http://0.0.0.0:50281 (→ 8001)+ plausible (plausible/analytics):+ - http://0.0.0.0:50288 (→ 8000)+ web (api):+ - http://0.0.0.0:50280 (→ 8000)+ - http://0.0.0.0:50230 (→ 3000)+ s3 (minio/minio):+ - http://0.0.0.0:5010 (→ 5000)+ - http://0.0.0.0:5011 (→ 5001)+ db (postgres):+ - http://0.0.0.0:50254 (→ 5432)+ es (docker.elastic.co/elasticsearch/elasticsearch):+ - http://0.0.0.0:50292 (→ 9200)+ cache (redis):+ - http://0.0.0.0:50263 (→ 6379)+ ================================================================================+ ```++ For example, you can access the following endpoints:- the list of ingestion jobs on[http://localhost:50281/task](http://localhost:50281/task)diff --git a/justfile b/justfileindex 55c31e7cc..5492f6a3a 100644--- a/justfile+++ b/justfile@@ -135,9 +135,13 @@ dc *args:build *args:just dc build {{ args }}+# List all services and their URLs and ports+ps:+ python3 utilities/ps.py+# Also see `up` recipe in sub-justfiles# Bring all Docker services up, in all profiles-up *flags:+up *flags: && ps#!/usr/bin/env bashset -eo pipefailwhile true; do@@ -147,6 +151,7 @@ up *flags:((c++)) && ((c==3)) && breaksleep 5done+ echo && sleep 1# Also see `wait-up` recipe in sub-justfiles# Wait for all services to be updiff --git a/utilities/ps.py b/utilities/ps.pynew file mode 100644index 000000000..919a5aea7--- /dev/null+++ b/utilities/ps.py@@ -0,0 +1,105 @@+import subprocess+from dataclasses import dataclass+++@dataclass+class Service:+ name: str+ image: str+ bindings: set[tuple[int, int]]++ def print(self):+ """+ Print the formatted output for the service. The output contains the following:+ - name (in bold)+ - each URL (with the correct protocol) and the container port to which it maps+ It specially handles the singular NGINX port 9443 which serves over ``https``.+ """++ print(f"\033[1m{self.name} ({self.image}):\033[0m")+ for host_port, container_port in self.bindings:+ proto = "http"+ if self.name == "proxy" and container_port == 9443:+ proto = "https"+ print(f"- {proto:>5}://0.0.0.0:{host_port} (→ {container_port})")+++def get_ps() -> list[str]:+ """+ Invoke Docker "ps" with a filter to get the configuration for all services. The+ info is returned as a list of plain text lines.++ :return: the output printed by the subprocess to STDOUT+ """++ proc = subprocess.run(+ [+ "docker",+ "ps",+ "--filter",+ "network=openverse_default",+ "--format",+ "{{.Names}}\t{{.Image}}\t{{.Ports}}\n",+ ],+ capture_output=True,+ text=True,+ )+ if proc.returncode != 0:+ raise RuntimeError(+ f"docker ps failed with code {proc.returncode} and output: {proc.stderr}"+ )+ return proc.stdout.split("\n")+++def parse_ps() -> list[Service]:+ """+ Convert the output lines given by Docker "ps" into a list of services and+ their port mappings.+ :return: a list of running services with their port+ """++ services: list[Service] = []++ lines = get_ps()+ lines = [stripped for line in lines if (stripped := line.strip())]+ for line in lines:+ service, image, ports = line.split("\t")+ # Services are usually of the form openverse-<name>-1 or openverse_<name>_1,+ # but we can't always tell which one is used.+ strip_char = service.removeprefix("openverse")[0]+ name = service.split(strip_char, 1)[1].rsplit(strip_char, 1)[0]+ # Image names may start with "openverse-" or "openverse_" or be an upstream+ # image name like redis:4.0.0+ if "openverse" in image:+ image_name = image[len("openverse") + 1 :]+ else:+ image_name = image.split(":", 1)[0]+ bindings = set()+ for port_config in ports.split(","):+ if "->" not in port_config:+ # No published ports+ continue+ # Ports are of the form 0.0.0.0:50230->3000/tcp or :::50230->3000/tcp+ host_port, container_port = (+ port_config.split(":")[-1].split("/")[0].split("->")+ )+ bindings.add((host_port, container_port))+ if bindings:+ services.append(Service(name, image_name, bindings))++ return services+++def print_ps():+ """Print the formatted output for each service."""++ print("=" * 80)+ print(f"{'Service Ports':^80}")+ print("=" * 80)+ for service in parse_ps():+ service.print()+ print("=" * 80)+++if __name__ == "__main__":+ print_ps()