-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
ref(ci): wait.py, add healthcheck logging #113361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #!/usr/bin/env python3 | ||
| """Wait for the background devservices process started by the setup-devservices action. | ||
|
|
||
| Usage: wait.py [timeout_seconds] | ||
|
|
||
| Reads: /tmp/ds-exit, /tmp/ds.log (written by the setup-devservices action) | ||
| Writes: $GITHUB_ENV (DJANGO_LIVE_TEST_SERVER_ADDRESS) | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| DS_EXIT = Path("/tmp/ds-exit") | ||
| DS_LOG = Path("/tmp/ds.log") | ||
| TIMEOUT = 300 | ||
|
|
||
|
|
||
| def log(msg: str) -> None: | ||
| print(msg, flush=True) | ||
|
|
||
|
|
||
| def docker(*args: str) -> subprocess.CompletedProcess[str]: | ||
| return subprocess.run(["docker", *args], capture_output=True, text=True) | ||
|
|
||
|
|
||
| def stream_log(pos: int) -> int: | ||
| """Print any new content written to DS_LOG since pos. Returns the new position.""" | ||
| if not DS_LOG.exists(): | ||
| return pos | ||
| with DS_LOG.open() as f: | ||
| f.seek(pos) | ||
| chunk = f.read() | ||
| if chunk: | ||
| sys.stdout.write(chunk) | ||
| sys.stdout.flush() | ||
| return f.tell() | ||
|
|
||
|
|
||
| def container_inspect_dump() -> None: | ||
| ids = docker("ps", "-aq").stdout.split() | ||
| if not ids: | ||
| return | ||
|
|
||
| r = docker("inspect", *ids) | ||
| if r.returncode != 0: | ||
| return | ||
|
|
||
| containers = json.loads(r.stdout) | ||
| for c in containers: | ||
| name = c["Name"].lstrip("/") | ||
| status = c["State"]["Status"] | ||
| health = c["State"].get("Health") | ||
| health_status = health["Status"] if health else "n/a" | ||
| log(f"{name} status={status} health={health_status}") | ||
|
|
||
| log("") | ||
| for c in containers: | ||
| health = c["State"].get("Health") | ||
| if not health or health["Status"] == "healthy": | ||
| continue | ||
| name = c["Name"].lstrip("/") | ||
| log(f"--- {name} last health check ---") | ||
| for entry in health.get("Log", []): | ||
| log(f" exit={entry['ExitCode']} {entry['Output'].strip()}") | ||
|
|
||
|
|
||
| def wait(timeout: int = TIMEOUT) -> None: | ||
| start = time.monotonic() | ||
| log_pos = 0 | ||
|
|
||
| while not DS_EXIT.exists(): | ||
| elapsed = time.monotonic() - start | ||
| if elapsed > timeout: | ||
| log_pos = stream_log(log_pos) | ||
| log(f"::error::Timed out waiting for devservices after {timeout}s") | ||
| log("--- container health on timeout ---") | ||
| container_inspect_dump() | ||
| sys.exit(1) | ||
| log_pos = stream_log(log_pos) | ||
| time.sleep(2) | ||
|
|
||
| # Drain any remaining log output. | ||
| stream_log(log_pos) | ||
|
|
||
| rc = int(DS_EXIT.read_text().strip()) | ||
| if rc != 0: | ||
| log(f"::error::devservices up failed (exit {rc})") | ||
| log("--- container health on failure ---") | ||
| container_inspect_dump() | ||
| sys.exit(1) | ||
|
|
||
| r = docker( | ||
|
joshuarli marked this conversation as resolved.
|
||
| "network", | ||
| "inspect", | ||
| "bridge", | ||
| "--format", | ||
| "{{(index .IPAM.Config 0).Gateway}}", | ||
| ) | ||
| if r.returncode != 0: | ||
| log(f"::error::docker network inspect bridge failed: {r.stderr.strip()}") | ||
| sys.exit(1) | ||
| gateway = r.stdout.strip() | ||
| github_env = os.environ.get("GITHUB_ENV") | ||
| if github_env: | ||
| with open(github_env, "a") as f: | ||
| f.write(f"DJANGO_LIVE_TEST_SERVER_ADDRESS={gateway}\n") | ||
|
|
||
| r2 = docker("ps", "-a") | ||
| if r2.stdout.strip(): | ||
| log(r2.stdout.strip()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| wait(int(sys.argv[1]) if len(sys.argv) > 1 else TIMEOUT) | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.