Files

87 lines
4.8 KiB
Python

"""Assemble an allowlisted offline delivery, excluding credentials and runtime data."""
import base64
import hashlib
import json
import shutil
import subprocess
import sys
import tarfile
import zipfile
from pathlib import Path
root = Path(__file__).resolve().parents[1]
workspace = root.parent
bundle = workspace / "hainuo-offline-arm64"
images = bundle / "images"
licenses = bundle / "licenses"
records = json.loads((images / "IMAGE_MANIFEST.json").read_text(encoding="utf-8"))
assert {x["name"] for x in records} == {"python", "postgres", "redis", "caddy"}
assert json.loads((images / "VERIFICATION.json").read_text())["status"] == "passed"
subprocess.run([sys.executable, str(root / "scripts/package.py")], check=True)
included = []
with zipfile.ZipFile(workspace / "海诺中继服务_0.1.0_源码部署包.zip") as source:
for member in source.infolist():
relative = Path(member.filename)
assert relative.parts[0] == "hainuo-relay" and ".." not in relative.parts
destination = bundle / relative
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_bytes(source.read(member))
included.append(destination)
for path in sorted((workspace / ".arm64-wheelcheck").glob("*.whl")):
destination = bundle / "hainuo-relay/wheelhouse" / path.name
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(path, destination)
included.append(destination)
for record in records:
assert Path(record["archive"]).name == record["archive"]
included.append(images / record["archive"])
included += [images / "IMAGE_MANIFEST.json", images / "VERIFICATION.json"]
caddy = json.loads((licenses / "caddy-license.json").read_text())
assert caddy["license"]["spdx_id"] == "Apache-2.0"
(licenses / "CADDY-LICENSE.txt").write_bytes(base64.b64decode(caddy["content"]))
allowed_licenses = {"redis-licensing.html", "postgresql-license.html", "python-license.html", "COPYING",
"LICENSE.txt", "AGPL-3.0.txt", "caddy-license.json", "CADDY-LICENSE.txt"}
for path in licenses.iterdir():
if path.name in allowed_licenses or (path.name.startswith("redis-8.2.") and path.name.endswith("-source.tar.gz")):
included.append(path)
wrapper = bundle / "install.sh"
wrapper.write_text('#!/usr/bin/env bash\nset -euo pipefail\ncd "$(dirname "$0")"\nexec bash hainuo-relay/scripts/install_offline.sh "$@"\n', encoding="utf-8", newline="\n")
included.append(wrapper)
readme = bundle / "README.txt"
readme.write_text("海诺中继 Linux ARM64 官方镜像离线部署包\n\n"
"宿主机需要预装Docker Engine和Compose v2。\n"
"在本目录执行:bash install.sh relay.实际公司域名\n"
"脚本先校验SHA256,再docker load四个官方镜像,离线构建本项目服务并启动。\n"
"安装阶段不访问镜像仓库/PyPI;业务仍需连通海能Work和员工PC。\n"
"Python 3.12.15 / PostgreSQL 17.11 / Redis 8.2.10 / Caddy 2.11.7,全部linux/arm64。\n"
"Redis选择AGPLv3;其他项目许可和Redis对应版本源码见licenses/。\n"
"精确上游摘要与下载来源见images/IMAGE_MANIFEST.json,校验记录见VERIFICATION.json。\n"
"本项目服务镜像由源码在目标机本地构建,不是声称来自某官方仓库的海诺镜像。\n"
"本地无Docker daemon,已验证镜像内容及摘要,未声称已在目标机运行通过。\n"
"完整部署、真实1.065适配边界和TLS配置见hainuo-relay/docs/。\n", encoding="utf-8")
included.append(readme)
hashes = []
for path in sorted(set(included)):
value = hashlib.sha256()
with path.open("rb") as source:
for data in iter(lambda: source.read(4 * 1024 * 1024), b""):
value.update(data)
hashes.append(value.hexdigest() + " " + path.relative_to(bundle).as_posix())
checksum = bundle / "SHA256SUMS"
checksum.write_text("\n".join(hashes) + "\n", encoding="utf-8", newline="\n")
included.append(checksum)
output = workspace / "hainuo-relay-offline-arm64-0.1.0.tar.gz"
print("Packaging verified images and offline dependencies...", flush=True)
with tarfile.open(output, "w:gz", compresslevel=3) as archive:
for path in sorted(set(included)):
arcname = "hainuo-offline-arm64/" + path.relative_to(bundle).as_posix()
assert "/runtime/" not in arcname and "/operator/" not in arcname and not arcname.endswith("/.env")
archive.add(path, arcname=arcname, recursive=False)
value = hashlib.sha256()
with output.open("rb") as source:
for data in iter(lambda: source.read(4 * 1024 * 1024), b""):
value.update(data)
output.with_suffix(output.suffix + ".sha256").write_text(value.hexdigest() + "\n", encoding="ascii")
print(json.dumps({"archive": str(output), "bytes": output.stat().st_size, "sha256": value.hexdigest(),
"files": len(set(included))}, ensure_ascii=False))