Documentation
¶
Index ¶
- Constants
- func BuildRepoAuth(repo *model.Repo, p *model.Platform) gitbackend.AuthConfig
- func ClassifyError(err error) string
- func DecryptFile(encPath, keyParam, destPath string) error
- func DecryptToTemp(encPath, keyParam string) (string, error)
- func EncryptFile(srcPath, keyParam string) (string, error)
- func EncryptFileWithSuffix(srcPath, keyParam, suffix string) (string, error)
- func IsEncryptedFile(name string) bool
- func RestoreBundle(ctx context.Context, bundlePath, destDir string) error
- func SaveManifest(m *BackupManifest) (string, error)
- func UploadBundle(ctx context.Context, cfg *S3Config, localPath, objectKey string) error
- func VerifyDrillChain(historyFile string) (ok bool, checked int, brokenAt string)
- type BackupManifest
- type BundleInfo
- type Destination
- type DestinationType
- type DrillHistoryEntry
- type DrillReport
- type Executor
- type ForcePushApprover
- type ManifestEntry
- type ManifestVerifyResult
- type Pipeline
- type PlatformProvider
- type RepoProvider
- type RunContext
- type RunManager
- type S3Config
- type Service
- type Stage
- type UploadResult
- type Uploader
Constants ¶
const ( RemoteOrigin = "origin" RemoteTarget = "target" )
Git remote name constants
const (
RepoDir = "repo"
)
Work directory subdirectory name
Variables ¶
This section is empty.
Functions ¶
func BuildRepoAuth ¶ added in v0.8.0
func BuildRepoAuth(repo *model.Repo, p *model.Platform) gitbackend.AuthConfig
BuildRepoAuth 从仓库/平台构建 git 凭证,是 executor 与 service(镜像源仓库 读取)共用的唯一规则实现,避免两份平行逻辑漂移。
安全约定(认证路径重构):
- 令牌只在内存中传给 gitbackend.AuthConfig
- native backend 落地时写入 0600 临时文件 + credential.helper / GIT_ASKPASS, **不进 git 进程 argv,也不进 environ 明文**(见 gitbackend/credhelper.go)
- 临时凭证目录随 git 命令结束删除(RAII)
- 仓库 token 优先于平台 token;均空则 AuthNone
SSH 密钥内容同样经临时 0600 文件注入 GIT_SSH_COMMAND,命令结束后删除。
p 可为 nil:此时不带平台侧的 skipTLS / SSH 指纹 / knownHosts 信息。 platform 预取回退(repo.PlatformID → 查询)属于调用方语义,留在 Executor.authConfig 里。
func ClassifyError ¶
ClassifyError categorizes an error into one of the predefined error types. 优先识别 SDK 结构化错误(provider HTTP 层),再回落到 git 操作关键词匹配。
func DecryptFile ¶ added in v0.6.0
DecryptFile 解密 <name>.enc → <name 去掉 .enc>。
func DecryptToTemp ¶ added in v0.6.0
DecryptToTemp 解密到临时目录,返回明文路径(调用方负责清理)。
func EncryptFile ¶ added in v0.6.0
EncryptFile 用 AES-256-GCM 加密文件,写出 <name>.enc。 keyParam: base64(32 字节)或任意口令(SHA-256 派生)。 返回加密文件路径。
func EncryptFileWithSuffix ¶ added in v0.6.0
EncryptFileWithSuffix 便于测试:指定输出后缀。
func IsEncryptedFile ¶ added in v0.6.0
IsEncryptedFile 判断是否 AES 加密冷备文件。
func RestoreBundle ¶ added in v0.5.1
RestoreBundle 从 bundle 恢复到目标目录。 destDir 不存在时创建;已存在且非空则拒绝(防误覆盖)。
func SaveManifest ¶ added in v0.6.0
func SaveManifest(m *BackupManifest) (string, error)
SaveManifest 写入 backupDir/backup-manifest.json。
func UploadBundle ¶ added in v0.5.0
UploadBundle 把 bundle 文件推到 S3(冷备异地)。 只支持 path-style + SigV4,覆盖 MinIO/阿里 OSS/AWS。
Types ¶
type BackupManifest ¶ added in v0.6.0
type BackupManifest struct {
Version int `json:"version"`
Generated time.Time `json:"generated"`
BackupDir string `json:"backup_dir"`
EntryCount int `json:"entry_count"`
TotalSize int64 `json:"total_size"`
MerkleRoot string `json:"merkle_root"`
Entries []ManifestEntry `json:"entries"`
}
BackupManifest 冷备目录的完整性清单(Merkle 根可证明「目录未被静默篡改」)。
func BuildManifest ¶ added in v0.6.0
func BuildManifest(backupDir string) (*BackupManifest, error)
BuildManifest 扫描 backupDir 下所有 .bundle,计算 SHA256 与 Merkle 根。
func LoadManifest ¶ added in v0.6.0
func LoadManifest(backupDir string) (*BackupManifest, error)
LoadManifest 读取已保存的清单。
type BundleInfo ¶ added in v0.5.1
type BundleInfo struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
ModTime time.Time `json:"mod_time"`
TaskKey string `json:"task_key,omitempty"`
Valid bool `json:"valid,omitempty"`
RefSpecs []string `json:"ref_specs,omitempty"`
}
BundleInfo bundle 文件信息。
func ListBundles ¶ added in v0.5.1
func ListBundles(backupDir, taskKey string) ([]BundleInfo, error)
ListBundles 列出 backupDir 下的 bundle(按任务过滤,空=全部)。
func VerifyBundle ¶ added in v0.5.1
func VerifyBundle(ctx context.Context, bundlePath string) (*BundleInfo, error)
VerifyBundle 校验 bundle 完整性(git bundle verify)。 返回列出的引用与是否有效。
type Destination ¶ added in v0.6.0
type Destination struct {
Type DestinationType `yaml:"type" json:"type"`
Name string `yaml:"name" json:"name"`
// S3(含 MinIO/阿里 OSS)
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
Region string `yaml:"region,omitempty" json:"region,omitempty"`
Bucket string `yaml:"bucket,omitempty" json:"bucket,omitempty"`
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty"`
AccessKey string `yaml:"access_key,omitempty" json:"access_key,omitempty"`
SecretKey string `yaml:"secret_key,omitempty" json:"secret_key,omitempty"`
PathStyle bool `yaml:"path_style,omitempty" json:"path_style,omitempty"`
// WebDAV
URL string `yaml:"url,omitempty" json:"url,omitempty"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
Password string `yaml:"password,omitempty" json:"password,omitempty"`
// Azure Blob
AccountName string `yaml:"account_name,omitempty" json:"account_name,omitempty"`
AccountKey string `yaml:"account_key,omitempty" json:"account_key,omitempty"`
Container string `yaml:"container,omitempty" json:"container,omitempty"`
// Enabled 默认 true;false 时跳过
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
}
Destination 一条冷备异地目的地配置(多目的地扇出,gickup 风格)。
type DestinationType ¶ added in v0.6.0
type DestinationType string
DestinationType 冷备目的地类型。
const ( DestS3 DestinationType = "s3" DestWebDAV DestinationType = "webdav" DestAzure DestinationType = "azure" DestLocal DestinationType = "local" )
type DrillHistoryEntry ¶ added in v0.6.0
type DrillHistoryEntry struct {
Report *DrillReport `json:"report"`
PrevHash string `json:"prev_hash"`
Hash string `json:"hash"`
}
DrillHistoryEntry 演练历史落盘条目。
func LoadDrillHistory ¶ added in v0.6.0
func LoadDrillHistory(historyFile string, limit int) ([]DrillHistoryEntry, error)
LoadDrillHistory 读取演练历史(最多 limit 条,0=全部),按开始时间倒序。
func SaveDrillReport ¶ added in v0.6.0
func SaveDrillReport(historyFile string, rep *DrillReport) (*DrillHistoryEntry, error)
SaveDrillReport 将演练报告追加到 historyFile(JSONL,哈希链防篡改)。
type DrillReport ¶ added in v0.6.0
type DrillReport struct {
BundleName string `json:"bundle_name"`
StartedAt time.Time `json:"started_at"`
FinishedAt time.Time `json:"finished_at"`
DurationMS int64 `json:"duration_ms"`
Success bool `json:"success"`
RefSpecs []string `json:"ref_specs"`
RestoredRefs []string `json:"restored_refs"`
MissingRefs []string `json:"missing_refs"`
ExtraRefs []string `json:"extra_refs"`
FsckOK bool `json:"fsck_ok"`
FsckOutput string `json:"fsck_output,omitempty"`
CommitCount int `json:"commit_count"`
EstRTO string `json:"est_rto"` // 人类可读,如 "12s"
BundleSize int64 `json:"bundle_size"`
Errors []string `json:"errors,omitempty"`
CheckedAt time.Time `json:"checked_at"`
Details map[string]string `json:"details,omitempty"`
}
DrillReport 一次灾备演练(DR Drill)的结果。 语义:从冷备 bundle 恢复到临时目录 → fsck → 比对 refs → 估算 RTO。
func BatchDRDrill ¶ added in v0.6.0
func BatchDRDrill(ctx context.Context, bundlePaths []string) (reports []*DrillReport, summary map[string]any)
BatchDRDrill 对多个 bundle 演练,返回逐个报告与汇总。
func RunDRDrill ¶ added in v0.6.0
func RunDRDrill(ctx context.Context, bundlePath string) (*DrillReport, error)
RunDRDrill 对单个 bundle 做灾备恢复演练。
流程:
- bundle list-heads 取期望 refs
- RestoreBundle 到临时目录
- git fsck --strict 校验对象完整性
- for-each-ref 比对恢复出的 refs
- 统计 commit 数与耗时(作为 RTO 观测值)
临时目录在返回前清理,不留残留。
type Executor ¶
type Executor struct {
// Approver 可选:force_push_policy=block 时查询是否已获人工放行。
// 由壳层注入(如 force-push-approvals 审批表);nil=始终未放行。
Approver ForcePushApprover
// contains filtered or unexported fields
}
func NewExecutor ¶
type ForcePushApprover ¶ added in v0.7.1
type ForcePushApprover interface {
IsForcePushApproved(taskKey, branch string) bool
// RequestApproval 产生一条 pending 审批(best-effort,失败不影响主流程)。
RequestApproval(taskKey, branch, reason string)
}
ForcePushApprover 查询某任务/分支的强制推送是否已获放行。
type ManifestEntry ¶ added in v0.6.0
type ManifestEntry struct {
Name string `json:"name"`
Size int64 `json:"size"`
SHA256 string `json:"sha256"`
ModTime time.Time `json:"mod_time"`
TaskKey string `json:"task_key,omitempty"`
LeafHash string `json:"leaf_hash"`
}
ManifestEntry 单个冷备文件的完整性条目。
type ManifestVerifyResult ¶ added in v0.6.0
type ManifestVerifyResult struct {
OK bool `json:"ok"`
MerkleRoot string `json:"merkle_root"`
StoredRoot string `json:"stored_root,omitempty"`
CheckedCount int `json:"checked_count"`
MissingFiles []string `json:"missing_files,omitempty"`
HashMismatch []string `json:"hash_mismatch,omitempty"`
ExtraFiles []string `json:"extra_files,omitempty"`
Message string `json:"message"`
}
ManifestVerifyResult 清单校验结果。
func VerifyManifest ¶ added in v0.6.0
func VerifyManifest(backupDir string) (*ManifestVerifyResult, error)
VerifyManifest 现场重算并与存储清单比对。 既能发现文件被删/被改,也能发现目录被塞入多余 bundle。
type Pipeline ¶ added in v0.7.0
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline 有序阶段执行器。
type PlatformProvider ¶
type PlatformProvider interface {
GetPlatformByID(ctx context.Context, id uint) (*model.Platform, error)
}
PlatformProvider provides platform lookup by ID.
type RepoProvider ¶
RepoProvider provides repository lookup by key.
type RunContext ¶ added in v0.7.0
type RunContext struct {
Task *model.SyncTask // 本地副本(分支已回退,不回写 DB)
SourceRepo *model.Repo
TargetRepo *model.Repo
Platforms map[uint]*model.Platform
WorkDir string
RepoDir string
Details *strings.Builder
Run *model.SyncRun
Exec *Executor
BundlesPath string
}
RunContext 流水线共享运行态(阶段间传参,替代超长函数局部变量)。
type RunManager ¶
type RunManager interface {
CreateRun(task *model.SyncTask, trigger string, webhookEventID *uint) (*model.SyncRun, error)
CreateRunStep(step *model.SyncRunStep) error
UpdateRunStep(step *model.SyncRunStep) error
CompleteRun(run *model.SyncRun) error
UpdateTaskLastRun(task *model.SyncTask, run *model.SyncRun) error
CompleteRunWithTaskUpdate(run *model.SyncRun, task *model.SyncTask) error
}
RunManager handles sync run lifecycle operations.
type S3Config ¶ added in v0.5.0
type S3Config struct {
Endpoint string `yaml:"endpoint"` // https://s3.amazonaws.com 或 https://oss-cn-hangzhou.aliyuncs.com
Region string `yaml:"region"` // us-east-1 等
Bucket string `yaml:"bucket"`
Prefix string `yaml:"prefix"` // 对象 key 前缀
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
// PathStyle true=MinIO/部分兼容端点
PathStyle bool `yaml:"path_style"`
}
S3Config S3 兼容对象存储(阿里云 OSS/MinIO/AWS)。
type Service ¶
type Service interface {
GetTempDir(taskKey string) string
GetConfig() *model.Config
RunManager
RepoProvider
PlatformProvider
}
Service is the interface the executor depends on. It exposes only the operations the executor needs — no DAO leakage.
type Stage ¶ added in v0.7.0
type Stage interface {
Name() string
// IsOptional 可选阶段失败不中止流水线(如 wiki/冷备)。
IsOptional() bool
Run(ctx context.Context, rc *RunContext) error
}
Stage 同步流水线的一个阶段。 职责单一:只做一件事,通过 execCtx 传递运行态,错误即中止(除 IsOptional)。
type UploadResult ¶ added in v0.6.0
type UploadResult struct {
Destination string `json:"destination"`
Type string `json:"type"`
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
ObjectKey string `json:"object_key,omitempty"`
Bytes int64 `json:"bytes,omitempty"`
}
UploadResult 单目的地上传结果。
func FanoutUpload ¶ added in v0.6.0
func FanoutUpload(ctx context.Context, localPath string, dests []Destination) []UploadResult
FanoutUpload 把本地文件扇出到多个目的地(策略注册表分发)。
type Uploader ¶ added in v0.7.0
type Uploader interface {
Type() DestinationType
// Upload 返回 object key;失败返回 error。
Upload(ctx context.Context, d Destination, objectKey string, data []byte) (string, error)
}
Uploader 单目的地上传策略(注册表驱动,新增目的地只需实现此接口)。