|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Re-host jefzda/sweap-images from Docker Hub to GHCR for Daytona compatibility. |
| 3 | +
|
| 4 | +Daytona's remote builder cannot pull from Docker Hub (auth error on public images). |
| 5 | +This script re-hosts all sweap-images used by benchmark tasks to ghcr.io/sg-evals/sweap-images. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + python3 scripts/rehost_sweap_images.py --dry-run # list what would be done |
| 9 | + python3 scripts/rehost_sweap_images.py --pull-push # pull+tag+push to GHCR |
| 10 | + python3 scripts/rehost_sweap_images.py --update-dockerfiles # update FROM lines |
| 11 | + python3 scripts/rehost_sweap_images.py --all # do everything |
| 12 | +""" |
| 13 | + |
| 14 | +import re |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +SOURCE_REGISTRY = "jefzda/sweap-images" |
| 20 | +TARGET_REGISTRY = "ghcr.io/sg-evals/sweap-images" |
| 21 | + |
| 22 | +BENCHMARKS = Path("benchmarks") |
| 23 | + |
| 24 | + |
| 25 | +def find_sweap_references() -> dict[str, list[Path]]: |
| 26 | + """Find all Dockerfiles referencing sweap-images, grouped by tag.""" |
| 27 | + tags: dict[str, list[Path]] = {} |
| 28 | + for df in sorted(BENCHMARKS.glob("*/*/environment/Dockerfile*")): |
| 29 | + content = df.read_text() |
| 30 | + for m in re.finditer(r"FROM jefzda/sweap-images:(\S+)", content): |
| 31 | + tag = m.group(1) |
| 32 | + tags.setdefault(tag, []).append(df) |
| 33 | + return tags |
| 34 | + |
| 35 | + |
| 36 | +def pull_and_push(tags: dict[str, list[Path]]) -> list[str]: |
| 37 | + """Pull from Docker Hub, tag for GHCR, push to GHCR.""" |
| 38 | + failed = [] |
| 39 | + for i, tag in enumerate(sorted(tags.keys()), 1): |
| 40 | + source = f"{SOURCE_REGISTRY}:{tag}" |
| 41 | + target = f"{TARGET_REGISTRY}:{tag}" |
| 42 | + print(f"[{i}/{len(tags)}] {tag}") |
| 43 | + |
| 44 | + # Pull |
| 45 | + r = subprocess.run(["docker", "pull", source], capture_output=True, text=True) |
| 46 | + if r.returncode != 0: |
| 47 | + print(f" PULL FAILED: {r.stderr.strip()}") |
| 48 | + failed.append(tag) |
| 49 | + continue |
| 50 | + print(f" Pulled {source}") |
| 51 | + |
| 52 | + # Tag |
| 53 | + subprocess.run(["docker", "tag", source, target], check=True) |
| 54 | + print(f" Tagged -> {target}") |
| 55 | + |
| 56 | + # Push |
| 57 | + r = subprocess.run(["docker", "push", target], capture_output=True, text=True) |
| 58 | + if r.returncode != 0: |
| 59 | + print(f" PUSH FAILED: {r.stderr.strip()}") |
| 60 | + failed.append(tag) |
| 61 | + continue |
| 62 | + print(f" Pushed {target}") |
| 63 | + |
| 64 | + return failed |
| 65 | + |
| 66 | + |
| 67 | +def update_dockerfiles(tags: dict[str, list[Path]]) -> int: |
| 68 | + """Update FROM lines in all affected Dockerfiles.""" |
| 69 | + count = 0 |
| 70 | + for tag, files in sorted(tags.items()): |
| 71 | + old = f"FROM {SOURCE_REGISTRY}:{tag}" |
| 72 | + new = f"FROM {TARGET_REGISTRY}:{tag}" |
| 73 | + for df in files: |
| 74 | + content = df.read_text() |
| 75 | + if old in content: |
| 76 | + df.write_text(content.replace(old, new)) |
| 77 | + count += 1 |
| 78 | + return count |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + dry_run = "--dry-run" in sys.argv |
| 83 | + pull_push = "--pull-push" in sys.argv |
| 84 | + update = "--update-dockerfiles" in sys.argv |
| 85 | + do_all = "--all" in sys.argv |
| 86 | + |
| 87 | + if not any([dry_run, pull_push, update, do_all]): |
| 88 | + print("Usage: --dry-run | --pull-push | --update-dockerfiles | --all") |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + tags = find_sweap_references() |
| 92 | + total_files = sum(len(f) for f in tags.values()) |
| 93 | + print(f"Found {len(tags)} unique sweap-images tags across {total_files} Dockerfiles\n") |
| 94 | + |
| 95 | + if dry_run: |
| 96 | + for tag, files in sorted(tags.items()): |
| 97 | + print(f" {SOURCE_REGISTRY}:{tag}") |
| 98 | + print(f" -> {TARGET_REGISTRY}:{tag}") |
| 99 | + for f in files: |
| 100 | + print(f" {f}") |
| 101 | + return |
| 102 | + |
| 103 | + if pull_push or do_all: |
| 104 | + print("=== Pull + Push to GHCR ===") |
| 105 | + failed = pull_and_push(tags) |
| 106 | + if failed: |
| 107 | + print(f"\nFailed tags: {failed}") |
| 108 | + else: |
| 109 | + print(f"\nAll {len(tags)} images re-hosted successfully") |
| 110 | + |
| 111 | + if update or do_all: |
| 112 | + print("\n=== Updating Dockerfiles ===") |
| 113 | + count = update_dockerfiles(tags) |
| 114 | + print(f"Updated {count} Dockerfiles") |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments