Files
knowledge-governance/internal/libraryhandoff/handoff.go
T
姚章浩 323f9ae502
build / build (push) Successful in 1m23s
fix(handoff): 在执行器容器内读取云端归档清单
2026-10-30 22:45:25 +08:00

654 lines
24 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package libraryhandoff 把一份已封存快照在"本机控制面"与"云测试发布执行器"之间搬运,
// 并保证"字节在到达前不可用"。
//
// 为什么不是"把归档目录 rsync 过去":发布链路在装配阶段会用 Load 逐 blob 重算 sha256,
// 半拷贝的归档会让发布在维护窗口里失败——正好落在最不该失败的时刻。所以搬运单位是
// 自描述 bundle(清单先写、blob 以 sha256 命名、下载侧边收边算),并且**只有**
// 远端 `libraryctl snapshot-unpack` 成功、版本清单摘要自证、身份与请求一致之后,
// 调用方才允许把发布作业入队。
//
// 通道只用已批准的本机→云端 SSH(BatchMode、0600 配置+私钥)。远端命令是固定 argv:
// 容器名、归档根、暂存目录全部来自部署配置,调用方只能给快照 ID 与期望身份。
package libraryhandoff
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"syscall"
"time"
"knowledge-governance/internal/librarysnapshot"
)
// Identity 是一次搬运期望的快照身份。它必须来自治理库里的作业/版本记录,
// 不能从 bundle 自己读到的值"回填"——那就是让数据自己证明自己。
type Identity struct {
SnapshotID string
Scope string
TenantID uint64
KnowledgeBaseID string
}
// Config 是部署注入的搬运配置。缺项即未接线:未接线时调用方保持旧行为(本机目标)。
type Config struct {
// SSHTarget 是 ssh 配置里的 Host 别名;SSHConfig 是 `ssh -F` 用的 0600 文件。
SSHTarget string
SSHConfig string
// ExecutorContainer 是云端 release-executor 容器名(libraryctl 在 /app/libraryctl)。
ExecutorContainer string
// BundleDir 是云端暂存目录(只放传输中的 bundle,不参与保留策略)。
BundleDir string
// RemoteArchive 是云端执行器的归档根(与执行器 env 的 LIBRARY_ARCHIVE_ROOT 一致)。
RemoteArchive string
// LocalArchive 是本机归档根(只读读取;回传时写入)。
LocalArchive string
// StagingDir 是本机暂存(0600,放本次 pack 出来的 bundle)。
StagingDir string
// MaxBundleBytes 是单份 bundle 的上限;超过即拒绝(防止把整机磁盘拖垮)。
MaxBundleBytes int64
// OpTimeout 是单个远端动作的超时。
OpTimeout time.Duration
// PullInterval 是"云端版本回传本机"的扫描间隔(只有 capture 角色用)。
PullInterval time.Duration
// Transport 是 "ssh"(默认)或 "local"(仅限隔离 fixture 测试)。
Transport string
// LocalRoot 是 local transport 的"云端根目录"(fixture 用)。
LocalRoot string
}
const (
TransportSSH = "ssh"
TransportLocal = "local"
)
func (c *Config) validate() error {
missing := make([]string, 0, 6)
if c.Transport == TransportLocal {
// fixture 通道没有 ssh,但必须有显式隔离根:否则"本地目录"会被当成云端。
if c.LocalRoot == "" || !filepath.IsAbs(c.LocalRoot) {
missing = append(missing, "LOCAL_ROOT")
}
} else {
if c.SSHTarget == "" {
missing = append(missing, "SSH_TARGET")
}
if c.SSHConfig == "" || !filepath.IsAbs(c.SSHConfig) {
missing = append(missing, "SSH_CONFIG")
}
}
if c.ExecutorContainer == "" {
missing = append(missing, "EXECUTOR_CONTAINER")
}
for name, value := range map[string]string{"BUNDLE_DIR": c.BundleDir, "REMOTE_ARCHIVE": c.RemoteArchive, "LOCAL_ARCHIVE": c.LocalArchive, "STAGING_DIR": c.StagingDir} {
if value == "" || !filepath.IsAbs(value) || filepath.Clean(value) != value || strings.Contains(value, "..") {
missing = append(missing, name)
}
}
if len(missing) > 0 {
sort.Strings(missing)
return fmt.Errorf("快照搬运未接线:缺少 %s", strings.Join(missing, "、"))
}
if c.MaxBundleBytes <= 0 {
c.MaxBundleBytes = 8 << 30
}
if c.OpTimeout <= 0 {
c.OpTimeout = 10 * time.Minute
}
return nil
}
// Evidence 是一次搬运留下的可审计事实(写进作业阶段/日志,不写凭据)。
type Evidence struct {
Snapshot string `json:"snapshot"`
Scope string `json:"scope"`
TenantID uint64 `json:"tenant_id"`
KnowledgeBase string `json:"knowledge_base_id"`
Parts int `json:"parts"`
Bytes int64 `json:"bytes"`
Reused bool `json:"reused"`
Direction string `json:"direction"`
VerifiedAt time.Time `json:"verified_at"`
}
// Remote 是"云端那一侧"的动作集合。两种实现:ssh(真实通道)与 local(隔离 fixture)。
type Remote interface {
PutBundle(ctx context.Context, name string, body io.Reader) error
OpenBundle(ctx context.Context, name string) (io.ReadCloser, error)
RemoveBundle(ctx context.Context, name string) error
RunLibraryctl(ctx context.Context, args ...string) ([]byte, error)
ReadVersionManifest(ctx context.Context, id string) ([]byte, error)
// VerifySnapshot 要求远端对这份快照做**整版**校验(逐部件重算字节),
// 复用一份已有清单前必须通过它:清单存在不等于字节还在。
VerifySnapshot(ctx context.Context, id string) error
}
// Handoff 是搬运的执行者(一套实现,两个方向)。
type Handoff struct {
config Config
remote Remote
now func() time.Time
}
var snapshotIDPattern = regexp.MustCompile(`^[0-9a-f]{64}$`)
// bundleName 只接受内容寻址的快照 ID:暂存目录里不允许出现调用方可控的文件名。
func bundleName(id string) (string, error) {
if !snapshotIDPattern.MatchString(id) {
return "", errors.New("快照 ID 必须是 64 位小写十六进制")
}
return id + ".tar", nil
}
// New 装配搬运执行器。transport 选择失败一律报错,不做"降级到本机"。
func New(config Config, now func() time.Time) (*Handoff, error) {
if err := config.validate(); err != nil {
return nil, err
}
if now == nil {
now = time.Now
}
if config.Transport == "" {
config.Transport = TransportSSH
}
var remote Remote
switch config.Transport {
case TransportSSH:
remote = &sshRemote{config: config}
case TransportLocal:
// 只看配置不足以证明这是测试:必须显式给出隔离根,且不得同时配 SSH 目标,
// 否则等于把"本地目录"冒充成云端。
if config.LocalRoot == "" || !filepath.IsAbs(config.LocalRoot) {
return nil, errors.New("local transport 必须显式给出隔离根目录")
}
remote = &localRemote{config: config}
default:
return nil, fmt.Errorf("未知搬运通道 %q(只允许 ssh/local)", config.Transport)
}
return &Handoff{config: config, remote: remote, now: now}, nil
}
// Push 把本机快照送到云端执行器,并在云端**再校验一遍**后才算成功。
//
// 返回成功即代表:云端 `versions/<id>.json` 存在、摘要 == id、逐 blob 校验通过、
// 清单身份与请求一致。任何一步不满足都返回错误,调用方不得入队发布作业。
func (h *Handoff) Push(ctx context.Context, want Identity) (Evidence, error) {
if err := validateIdentity(want); err != nil {
return Evidence{}, err
}
name, err := bundleName(want.SnapshotID)
if err != nil {
return Evidence{}, err
}
// 远端已有同 ID:必须字节一致且身份一致才复用;否则拒绝(同 ID 异内容)。
if evidence, ok, err := h.remoteEvidence(ctx, want); err != nil {
return Evidence{}, err
} else if ok {
evidence.Direction = "push"
evidence.Bytes = 0
return evidence, nil
}
unlock, err := h.lock(ctx, want.SnapshotID)
if err != nil {
return Evidence{}, err
}
defer unlock()
// 拿锁后再查一次:并发调用里后到的那个直接复用先到的结果。
if evidence, ok, err := h.remoteEvidence(ctx, want); err != nil {
return Evidence{}, err
} else if ok {
evidence.Direction = "push"
return evidence, nil
}
archive, err := librarysnapshot.Open(h.config.LocalArchive, 256*1024*1024)
if err != nil {
return Evidence{}, fmt.Errorf("本机归档不可读,无法搬运:%w", err)
}
defer archive.Close()
// Load 逐 blob 重算 sha256:只搬"在这台机器上验证过"的字节。
manifest, err := archive.Load(want.SnapshotID)
if err != nil {
return Evidence{}, fmt.Errorf("本机快照不可用(可能只存在于云端,走 Fetch):%w", err)
}
if err := matchesIdentity(manifest, want); err != nil {
return Evidence{}, err
}
bundle, cleanup, err := h.packLocally(archive, want.SnapshotID, name)
if err != nil {
return Evidence{}, err
}
defer cleanup()
file, err := os.Open(bundle)
if err != nil {
return Evidence{}, errors.New("打开本机 bundle 失败")
}
defer file.Close()
if info, statErr := file.Stat(); statErr != nil || info.Size() <= 0 || info.Size() > h.config.MaxBundleBytes {
return Evidence{}, errors.New("bundle 大小超出允许范围")
}
if err := h.remote.PutBundle(ctx, name, file); err != nil {
return Evidence{}, fmt.Errorf("把快照送到执行器失败:%w", err)
}
if _, err := h.remote.RunLibraryctl(ctx, "snapshot-unpack", "-archive", h.config.RemoteArchive,
"-bundle", filepath.Join(h.config.BundleDir, name), "-snapshot", want.SnapshotID); err != nil {
return Evidence{}, fmt.Errorf("执行器侧校验/装入失败:%w", err)
}
manifestRaw, err := h.remote.ReadVersionManifest(ctx, want.SnapshotID)
if err != nil {
return Evidence{}, fmt.Errorf("执行器侧版本清单不可读:%w", err)
}
sum := sha256.Sum256(manifestRaw)
if hex.EncodeToString(sum[:]) != want.SnapshotID {
return Evidence{}, errors.New("执行器侧版本清单摘要与快照 ID 不一致")
}
remoteManifest, err := librarysnapshot.DecodeManifest(manifestRaw, want.SnapshotID)
if err != nil {
return Evidence{}, fmt.Errorf("执行器侧清单不可解析:%w", err)
}
if err := matchesIdentity(remoteManifest, want); err != nil {
return Evidence{}, err
}
// 只删本次传输的暂存文件;归档本体(versions/blobs)一律不动。
_ = h.remote.RemoveBundle(ctx, name)
return Evidence{Snapshot: want.SnapshotID, Scope: want.Scope, TenantID: want.TenantID,
KnowledgeBase: want.KnowledgeBaseID, Parts: len(remoteManifest.Parts),
Bytes: manifestBytes(remoteManifest), Direction: "push", VerifiedAt: h.now().UTC()}, nil
}
// Fetch 把云端存在的快照取回本机归档(预备份/云端产物的读回路径)。
//
// 成功即代表:本机 versions/<id>.json 已落位、逐 blob 校验通过。失败则本机不留下版本,
// 页面照旧按"没有这份版本"展示,绝不显示成可恢复。
func (h *Handoff) Fetch(ctx context.Context, id string) (Evidence, error) {
name, err := bundleName(id)
if err != nil {
return Evidence{}, err
}
archive, err := librarysnapshot.Open(h.config.LocalArchive, 256*1024*1024)
if err != nil {
return Evidence{}, fmt.Errorf("本机归档不可用:%w", err)
}
defer archive.Close()
if manifest, loadErr := archive.Load(id); loadErr == nil {
return Evidence{Snapshot: id, Scope: manifest.Scope, TenantID: manifest.TenantID,
KnowledgeBase: manifest.KnowledgeBaseID, Parts: len(manifest.Parts),
Bytes: manifestBytes(manifest), Reused: true, Direction: "fetch", VerifiedAt: h.now().UTC()}, nil
}
unlock, err := h.lock(ctx, id)
if err != nil {
return Evidence{}, err
}
defer unlock()
if manifest, loadErr := archive.Load(id); loadErr == nil {
return Evidence{Snapshot: id, Scope: manifest.Scope, TenantID: manifest.TenantID,
KnowledgeBase: manifest.KnowledgeBaseID, Parts: len(manifest.Parts),
Bytes: manifestBytes(manifest), Reused: true, Direction: "fetch", VerifiedAt: h.now().UTC()}, nil
}
remoteBundle := filepath.Join(h.config.BundleDir, name)
defer func() { _ = h.remote.RemoveBundle(context.WithoutCancel(ctx), name) }()
if _, err := h.remote.RunLibraryctl(ctx, "snapshot-pack", "-archive", h.config.RemoteArchive,
"-snapshot", id, "-bundle", remoteBundle); err != nil {
return Evidence{}, fmt.Errorf("云端快照不可打包(可能不存在或校验失败):%w", err)
}
body, err := h.remote.OpenBundle(ctx, name)
if err != nil {
return Evidence{}, fmt.Errorf("读取云端 bundle 失败:%w", err)
}
defer body.Close()
localBundle := filepath.Join(h.config.StagingDir, name)
_ = os.Remove(localBundle)
out, err := os.OpenFile(localBundle, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if err != nil {
return Evidence{}, errors.New("无法创建本机暂存 bundle")
}
written, copyErr := io.Copy(out, io.LimitReader(body, h.config.MaxBundleBytes+1))
syncErr := out.Sync()
closeErr := out.Close()
defer os.Remove(localBundle)
if copyErr != nil || syncErr != nil || closeErr != nil {
return Evidence{}, errors.New("回传快照字节失败(连接中断或被截断)")
}
if written > h.config.MaxBundleBytes {
return Evidence{}, errors.New("回传 bundle 超过上限,拒绝导入")
}
// 读回的是权威云端副本:本地同名 blob 字节不符按损坏修复(普通导入仍默认拒绝)。
manifest, err := archive.UnpackWithOptions(localBundle, id, librarysnapshot.UnpackOptions{RepairCorruptBlobs: true})
if err != nil {
return Evidence{}, fmt.Errorf("回传快照校验失败(截断/篡改/缺部件):%w", err)
}
return Evidence{Snapshot: id, Scope: manifest.Scope, TenantID: manifest.TenantID,
KnowledgeBase: manifest.KnowledgeBaseID, Parts: len(manifest.Parts),
Bytes: manifestBytes(manifest), Direction: "fetch", VerifiedAt: h.now().UTC()}, nil
}
// remoteEvidence 判断"云端已经有这份快照且身份一致"。
func (h *Handoff) remoteEvidence(ctx context.Context, want Identity) (Evidence, bool, error) {
raw, err := h.remote.ReadVersionManifest(ctx, want.SnapshotID)
if err != nil {
if errors.Is(err, os.ErrNotExist) || strings.Contains(strings.ToLower(err.Error()), "no such file") {
return Evidence{}, false, nil
}
return Evidence{}, false, fmt.Errorf("读取执行器侧版本清单失败:%w", err)
}
sum := sha256.Sum256(raw)
if hex.EncodeToString(sum[:]) != want.SnapshotID {
return Evidence{}, false, errors.New("执行器侧已有同名版本但清单摘要不符:拒绝复用")
}
manifest, err := librarysnapshot.DecodeManifest(raw, want.SnapshotID)
if err != nil {
return Evidence{}, false, err
}
if err := matchesIdentity(manifest, want); err != nil {
return Evidence{}, false, err
}
// 关键:清单存在只证明"这个名字有主",不证明 blob 还在、还对。
// 复用前让远端整版重算一遍,缺失/篡改必须在这里就失败(尚未入发布队列)。
if err := h.remote.VerifySnapshot(ctx, want.SnapshotID); err != nil {
return Evidence{}, false, fmt.Errorf("执行器侧已有清单但字节校验失败,拒绝复用:%w", err)
}
return Evidence{Snapshot: want.SnapshotID, Scope: manifest.Scope, TenantID: manifest.TenantID,
KnowledgeBase: manifest.KnowledgeBaseID, Parts: len(manifest.Parts),
Bytes: manifestBytes(manifest), Reused: true, VerifiedAt: h.now().UTC()}, true, nil
}
func (h *Handoff) packLocally(archive *librarysnapshot.Archive, id, name string) (string, func(), error) {
if err := os.MkdirAll(h.config.StagingDir, 0700); err != nil {
return "", func() {}, errors.New("本机暂存目录不可用")
}
path := filepath.Join(h.config.StagingDir, name)
_ = os.Remove(path)
if _, err := archive.Pack(id, path); err != nil {
return "", func() {}, fmt.Errorf("打包快照失败:%w", err)
}
return path, func() { _ = os.Remove(path) }, nil
}
// lock 用 flock 串行化"同一份快照"的本机搬运(并发点击/重试不会同时写暂存)。
func (h *Handoff) lock(ctx context.Context, id string) (func(), error) {
dir := filepath.Join(h.config.LocalArchive, ".handoff")
if err := os.MkdirAll(dir, 0700); err != nil {
return nil, errors.New("无法创建搬运锁目录")
}
file, err := os.OpenFile(filepath.Join(dir, id+".lock"), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, errors.New("无法创建搬运锁")
}
deadline := time.Now().Add(h.config.OpTimeout)
for {
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err == nil {
return func() {
_ = syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
_ = file.Close()
}, nil
}
if time.Now().After(deadline) {
file.Close()
return nil, errors.New("另一份搬运正在处理同一个快照,等待超时")
}
select {
case <-ctx.Done():
file.Close()
return nil, ctx.Err()
case <-time.After(100 * time.Millisecond):
}
}
}
func validateIdentity(want Identity) error {
if !snapshotIDPattern.MatchString(want.SnapshotID) {
return errors.New("快照 ID 必须是 64 位小写十六进制")
}
if want.Scope != "internal" && want.Scope != "external" {
return errors.New("快照 scope 必须是 internal 或 external")
}
if want.TenantID == 0 {
return errors.New("缺少租户身份,无法核对快照归属")
}
if strings.TrimSpace(want.KnowledgeBaseID) == "" {
return errors.New("缺少目标知识库身份,无法核对快照归属")
}
return nil
}
// matchesIdentity 用治理库里的期望值核对 bundle 自报的身份。
func matchesIdentity(manifest librarysnapshot.Manifest, want Identity) error {
if manifest.Scope != want.Scope {
return fmt.Errorf("快照 scope=%q 与请求 %q 不一致", manifest.Scope, want.Scope)
}
if manifest.TenantID != want.TenantID {
return fmt.Errorf("快照 tenant=%d 与请求 %d 不一致", manifest.TenantID, want.TenantID)
}
if manifest.KnowledgeBaseID != want.KnowledgeBaseID {
return fmt.Errorf("快照知识库 %s 与请求 %s 不一致", manifest.KnowledgeBaseID, want.KnowledgeBaseID)
}
return nil
}
func manifestBytes(manifest librarysnapshot.Manifest) int64 {
var total int64
seen := map[string]bool{}
for _, part := range manifest.Parts {
if seen[part.SHA256] {
continue
}
seen[part.SHA256] = true
total += part.Bytes
}
return total
}
// ---- ssh 传输:固定 argv,路径全部来自配置 ----
type sshRemote struct {
config Config
}
// remoteExitError 保留远端退出码:只有"文件不存在(44)"才能当成"还没有这份版本",
// 网络断、权限错、docker 不可用都必须原样上抛——否则会把"读不到"当成"没有",
// 于是继续搬一份没人验证过的快照。
type remoteExitError struct {
code int
detail string
}
func (e *remoteExitError) Error() string { return fmt.Sprintf("ssh exit %d: %s", e.code, e.detail) }
func (r *sshRemote) run(ctx context.Context, stdin io.Reader, stdout io.Writer, remote string) error {
bounded, cancel := context.WithTimeout(ctx, r.config.OpTimeout)
defer cancel()
argv := []string{"-F", r.config.SSHConfig, "-o", "BatchMode=yes", "-o", "ConnectTimeout=10", r.config.SSHTarget, remote}
command := exec.CommandContext(bounded, "ssh", argv...)
command.Stdin = stdin
command.Stdout = stdout
var stderr strings.Builder
command.Stderr = &stderr
if err := command.Run(); err != nil {
if bounded.Err() != nil {
return fmt.Errorf("ssh %s 超时", r.config.SSHTarget)
}
detail := strings.TrimSpace(stderr.String())
if len(detail) > 300 {
detail = detail[:300]
}
var exit *exec.ExitError
if errors.As(err, &exit) {
return &remoteExitError{code: exit.ExitCode(), detail: detail}
}
return fmt.Errorf("ssh %s 失败: %s", r.config.SSHTarget, detail)
}
return nil
}
func (r *sshRemote) PutBundle(ctx context.Context, name string, body io.Reader) error {
path := filepath.Join(r.config.BundleDir, name)
if err := r.run(ctx, nil, io.Discard, fmt.Sprintf("install -d -m 0700 %s", shellQuote(r.config.BundleDir))); err != nil {
return err
}
if err := r.run(ctx, body, io.Discard, fmt.Sprintf("cat > %s", shellQuote(path+".part"))); err != nil {
return err
}
return r.run(ctx, nil, io.Discard, fmt.Sprintf("mv -f %s %s", shellQuote(path+".part"), shellQuote(path)))
}
func (r *sshRemote) OpenBundle(ctx context.Context, name string) (io.ReadCloser, error) {
path := filepath.Join(r.config.BundleDir, name)
reader, writer := io.Pipe()
go func() {
writer.CloseWithError(r.run(ctx, nil, writer, fmt.Sprintf("cat %s", shellQuote(path))))
}()
return reader, nil
}
func (r *sshRemote) RemoveBundle(ctx context.Context, name string) error {
path := filepath.Join(r.config.BundleDir, name)
return r.run(ctx, nil, io.Discard, fmt.Sprintf("rm -f %s %s", shellQuote(path), shellQuote(path+".part")))
}
func (r *sshRemote) RunLibraryctl(ctx context.Context, args ...string) ([]byte, error) {
quoted := make([]string, 0, len(args)+2)
quoted = append(quoted, "docker", "exec", shellQuote(r.config.ExecutorContainer), "/app/libraryctl")
for _, arg := range args {
quoted = append(quoted, shellQuote(arg))
}
var out strings.Builder
if err := r.run(ctx, nil, &out, strings.Join(quoted, " ")); err != nil {
return nil, err
}
return []byte(out.String()), nil
}
func (r *sshRemote) VerifySnapshot(ctx context.Context, id string) error {
_, err := r.RunLibraryctl(ctx, "snapshot-verify", "-archive", r.config.RemoteArchive, "-snapshot", id)
return err
}
func (r *sshRemote) ReadVersionManifest(ctx context.Context, id string) ([]byte, error) {
path := filepath.Join(r.config.RemoteArchive, "versions", id+".json")
var out strings.Builder
// 只有 "文件不存在" 才算"还没有这份版本";其它失败必须让调用方停下来。
// RemoteArchive belongs to the executor filesystem, not the SSH host.
read := fmt.Sprintf("if [ -f %s ]; then cat %s; else exit 44; fi", shellQuote(path), shellQuote(path))
command := "docker exec " + shellQuote(r.config.ExecutorContainer) + " sh -c " + shellQuote(read)
if err := r.run(ctx, nil, &out, command); err != nil {
var exit *remoteExitError
if errors.As(err, &exit) && exit.code == 44 {
return nil, fmt.Errorf("%w", os.ErrNotExist)
}
return nil, err
}
return []byte(out.String()), nil
}
func shellQuote(value string) string {
return "'" + strings.ReplaceAll(value, "'", `'\''`) + "'"
}
// ---- local 传输:只用于隔离 fixture(跨进程执行真正的 libraryctl) ----
type localRemote struct {
config Config
}
func (r *localRemote) path(name string) (string, error) {
if _, err := bundleName(strings.TrimSuffix(name, ".tar")); err != nil {
return "", err
}
return filepath.Join(r.config.BundleDir, name), nil
}
func (r *localRemote) PutBundle(ctx context.Context, name string, body io.Reader) error {
path, err := r.path(name)
if err != nil {
return err
}
if err := os.MkdirAll(r.config.BundleDir, 0700); err != nil {
return err
}
out, err := os.OpenFile(path+".part", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
if _, err := io.Copy(out, io.LimitReader(body, r.config.MaxBundleBytes+1)); err != nil {
out.Close()
return err
}
if err := out.Sync(); err != nil {
out.Close()
return err
}
if err := out.Close(); err != nil {
return err
}
return os.Rename(path+".part", path)
}
func (r *localRemote) OpenBundle(ctx context.Context, name string) (io.ReadCloser, error) {
path, err := r.path(name)
if err != nil {
return nil, err
}
return os.Open(path)
}
func (r *localRemote) RemoveBundle(ctx context.Context, name string) error {
path, err := r.path(name)
if err != nil {
return err
}
for _, candidate := range []string{path, path + ".part"} {
if err := os.Remove(candidate); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
}
return nil
}
// RunLibraryctl 在 fixture 里跨进程执行真正的 libraryctl 二进制(不是进程内模拟)。
func (r *localRemote) RunLibraryctl(ctx context.Context, args ...string) ([]byte, error) {
binary := os.Getenv("LIBRARY_HANDOFF_LIBRARYCTL")
if binary == "" {
return nil, errors.New("fixture 缺少 LIBRARY_HANDOFF_LIBRARYCTL(libraryctl 可执行路径)")
}
bounded, cancel := context.WithTimeout(ctx, r.config.OpTimeout)
defer cancel()
command := exec.CommandContext(bounded, binary, args...)
command.Env = append(os.Environ(), "LIBRARY_HANDOFF_FIXTURE=1")
var stderr strings.Builder
command.Stderr = &stderr
out, err := command.Output()
if err != nil {
detail := strings.TrimSpace(stderr.String())
if len(detail) > 300 {
detail = detail[:300]
}
return nil, fmt.Errorf("libraryctl 失败: %s", detail)
}
return out, nil
}
func (r *localRemote) VerifySnapshot(ctx context.Context, id string) error {
_, err := r.RunLibraryctl(ctx, "snapshot-verify", "-archive", r.config.RemoteArchive, "-snapshot", id)
return err
}
func (r *localRemote) ReadVersionManifest(ctx context.Context, id string) ([]byte, error) {
path := filepath.Join(r.config.RemoteArchive, "versions", id+".json")
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return raw, nil
}