#!/usr/bin/env python3 """拉取教育平台和 Dify 要用的 linux/amd64 镜像。 打成独立的 -amd64 标签,不改本机正在使用的 arm64 标签。 """ import json import socket import subprocess import sys import urllib.parse SOCK = "/Users/zlwu/.docker/run/docker.sock" # 每个目标先试前面的镜像源。拉到的是清单时,后面再用 buildx 展开成真实层。 IMAGES = [ ( [("docker.1ms.run/langgenius/dify-api", "1.19.0")], "langgenius/dify-api:1.19.0-amd64", ), ( [("docker.1ms.run/langgenius/dify-web", "1.19.0")], "langgenius/dify-web:1.19.0-amd64", ), ( [ ("docker.1ms.run/langgenius/dify-plugin-daemon", "0.6.14"), ("docker.1ms.run/langgenius/dify-plugin-daemon", "0.6.14-local"), ], "langgenius/dify-plugin-daemon:0.6.14-amd64", ), ( [("docker.1ms.run/langgenius/dify-sandbox", "0.2.15")], "langgenius/dify-sandbox:0.2.15-amd64", ), ( [("docker.1ms.run/langgenius/dify-agent-backend", "1.19.0")], "langgenius/dify-agent-backend:1.19.0-amd64", ), ( [("docker.1ms.run/langgenius/dify-agent-local-sandbox", "1.19.0")], "langgenius/dify-agent-local-sandbox:1.19.0-amd64", ), ( [("docker.1ms.run/library/postgres", "15-alpine")], "postgres:15-alpine-amd64", ), ( [("docker.1ms.run/library/redis", "6-alpine")], "redis:6-alpine-amd64", ), ( [("docker.1ms.run/library/nginx", "1.27")], "nginx:1.27-amd64", ), ( [("docker.1ms.run/library/busybox", "1.36")], "busybox:1.36-amd64", ), ( [ ("docker.1ms.run/ubuntu/squid", "latest"), ("docker.1panel.live/ubuntu/squid", "latest"), ], "ubuntu/squid:latest-amd64", ), ( [ ("docker.1ms.run/semitechnologies/weaviate", "1.39.13"), ("docker.1panel.live/semitechnologies/weaviate", "1.39.13"), ], "weaviate:1.39.13-amd64", ), ( [("docker.1ms.run/library/eclipse-temurin", "17-jre-jammy")], "eclipse-temurin:17-jre-jammy-amd64", ), ] def request(method: str, path: str) -> bytes: header = ( f"{method} {path} HTTP/1.1\r\n" "Host: localhost\r\n" "Connection: close\r\n" "Content-Length: 0\r\n" "\r\n" ).encode() sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.settimeout(3600) sock.connect(SOCK) sock.sendall(header) chunks = [] while True: try: data = sock.recv(65536) except socket.timeout: raise SystemExit(f"引擎没有在时限内完成 {path}") if not data: break chunks.append(data) sock.close() raw = b"".join(chunks) head, _, payload = raw.partition(b"\r\n\r\n") status = head.split(b" ", 2)[1] if status not in (b"200", b"201"): raise SystemExit(f"{path} -> {status.decode()} {payload[:400]!r}") return payload def stream(path: str) -> None: header = ( f"POST {path} HTTP/1.1\r\n" "Host: localhost\r\n" "Connection: close\r\n" "Content-Length: 0\r\n" "\r\n" ).encode() sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.settimeout(3600) sock.connect(SOCK) sock.sendall(header) buffer = b"" header_done = False error = None last = "" while True: try: data = sock.recv(65536) except socket.timeout: sock.close() raise SystemExit(f"引擎没有在时限内完成 {path}") if not data: break buffer += data if not header_done: if b"\r\n\r\n" not in buffer: continue head, buffer = buffer.split(b"\r\n\r\n", 1) status = head.split(b" ", 2)[1] if status not in (b"200", b"201"): sock.close() raise SystemExit(f"{path} -> {status.decode()} {buffer[:400]!r}") header_done = True while b"\n" in buffer: line, buffer = buffer.split(b"\n", 1) line = line.strip() if not line.startswith(b"{"): continue try: item = json.loads(line) except json.JSONDecodeError: continue if "error" in item: error = item["error"] status_text = item.get("status") or "" if status_text and status_text != last: last = status_text print(f" {status_text}", flush=True) sock.close() if error: raise SystemExit(error) def pull_one(source: str, tag: str, official: str) -> None: query = urllib.parse.urlencode( {"fromImage": source, "tag": tag, "platform": "linux/amd64"} ) print(f"拉取 {source}:{tag}", flush=True) stream(f"/v1.47/images/create?{query}") source_ref = f"{source}:{tag}" repo, official_tag = official.rsplit(":", 1) tag_query = urllib.parse.urlencode({"repo": repo, "tag": official_tag}) encoded = urllib.parse.quote(source_ref, safe="") request("POST", f"/v1.47/images/{encoded}/tag?{tag_query}") print(f"已标记 {official}", flush=True) def already_amd64(official: str) -> bool: try: data = json.loads( subprocess.check_output( ["docker", "image", "inspect", official], stderr=subprocess.DEVNULL ) )[0] except (subprocess.CalledProcessError, IndexError, json.JSONDecodeError): return False return data.get("Architecture") == "amd64" and int(data.get("Size") or 0) > 1_000_000 def main() -> None: for candidates, official in IMAGES: if already_amd64(official): print(f"已有 amd64 镜像,跳过 {official}", flush=True) continue last_error = None for source, tag in candidates: try: pull_one(source, tag, official) last_error = None break except SystemExit as exc: last_error = exc print(f" 失败,换下一个源: {exc}", flush=True) if last_error is not None: raise last_error print("EXTRA_IMAGES_OK", flush=True) if __name__ == "__main__": try: main() except BrokenPipeError: sys.exit(1)