Plan publication using source descriptors
This commit is contained in:
@@ -104,11 +104,6 @@ func (publishStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
runFiles = append(runFiles, file)
|
||||
}
|
||||
sort.Slice(runFiles, func(i, j int) bool { return runFiles[i].RelativePath < runFiles[j].RelativePath })
|
||||
runSources, err := openPublishRunFiles(runRoot, runFiles)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: verify run files: %w", err)
|
||||
}
|
||||
defer closePublishSources(runSources)
|
||||
uploader, ok := env.ObjectStore.(storage.ReaderUploader)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("publish: object store does not support verified source uploads")
|
||||
@@ -117,11 +112,6 @@ func (publishStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: collect previous files: %w", err)
|
||||
}
|
||||
previousSources, err := openPublishRunFiles(sessionPaths.PreviousDir, previousFiles)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: verify previous files: %w", err)
|
||||
}
|
||||
defer closePublishSources(previousSources)
|
||||
runtimeCatalog, err := buildPublishRuntimeArtifactCatalog(
|
||||
sessionPaths,
|
||||
m,
|
||||
@@ -169,7 +159,7 @@ func (publishStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: %w", err)
|
||||
}
|
||||
transfers, err := buildPublishTransferPlan(runPrefix, runFiles, runSources, previousFiles, previousSources, outputPlan)
|
||||
transfers, err := buildPublishTransferPlan(runPrefix, runRoot, runFiles, sessionPaths.PreviousDir, previousFiles, sessionPaths.Root, outputPlan)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: build immutable upload mapping: %w", err)
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ type discoveredPublishOutput struct {
|
||||
Source string
|
||||
LocalPath string
|
||||
Provenance string
|
||||
Checksum string
|
||||
}
|
||||
|
||||
type publishOutputOutcome struct {
|
||||
@@ -43,6 +44,7 @@ type publishOutputOutcome struct {
|
||||
Required bool
|
||||
LocalPath string
|
||||
Provenance string
|
||||
Checksum string
|
||||
Declaration artifacts.RemoteArtifact
|
||||
}
|
||||
|
||||
@@ -125,6 +127,7 @@ func buildPublishOutputPlan(
|
||||
outcome.OriginRun = identity.RunID
|
||||
outcome.LocalPath = discoveredSource.LocalPath
|
||||
outcome.Provenance = discoveredSource.Provenance
|
||||
outcome.Checksum = discoveredSource.Checksum
|
||||
outcome.Declaration = artifacts.RemoteArtifact{
|
||||
Type: artifacts.RemoteArtifactTypePublishedOutput, Source: rule.Source, DestinationKey: declaredKey,
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ func discoverPublishOutputs(
|
||||
return nil, fmt.Errorf("resolve source %q: %w", rule.Source, err)
|
||||
}
|
||||
out = append(out, discoveredPublishOutput{
|
||||
Source: rule.Source, LocalPath: resolved.Path, Provenance: resolved.Provenance,
|
||||
Source: rule.Source, LocalPath: resolved.Path, Provenance: resolved.Provenance, Checksum: resolved.Checksum,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
|
||||
@@ -246,24 +246,6 @@ func collectPublishPreviousFiles(ctx context.Context, previousDir string) ([]pub
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func openPublishRunFiles(runRoot string, files []publishUploadFile) ([]*os.File, error) {
|
||||
sources := make([]*os.File, 0, len(files))
|
||||
for _, candidate := range files {
|
||||
file, err := openPublishSourceUnderRoot(runRoot, candidate.SourceRelativePath)
|
||||
if err != nil {
|
||||
closePublishSources(sources)
|
||||
return nil, err
|
||||
}
|
||||
if err := verifyPublishSourceChecksum(file, candidate.RelativePath, candidate.Checksum); err != nil {
|
||||
_ = file.Close()
|
||||
closePublishSources(sources)
|
||||
return nil, err
|
||||
}
|
||||
sources = append(sources, file)
|
||||
}
|
||||
return sources, nil
|
||||
}
|
||||
|
||||
func openPublishSourceUnderRoot(rootPath, relativePath string) (*os.File, error) {
|
||||
relativePath, err := pathsafe.NormalizeRelativeDestination(relativePath)
|
||||
if err != nil {
|
||||
@@ -346,12 +328,6 @@ func verifyPublishSourceChecksum(file *os.File, relativePath, expected string) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func closePublishSources(sources []*os.File) {
|
||||
for _, source := range sources {
|
||||
_ = source.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func publishUploadPaths(files []publishUploadFile) []string {
|
||||
paths := make([]string, 0, len(files))
|
||||
for _, file := range files {
|
||||
|
||||
@@ -8,79 +8,59 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
||||
)
|
||||
|
||||
type publishTransfer struct {
|
||||
Artifact artifacts.RemoteArtifact
|
||||
Source *os.File
|
||||
SourceRoot string
|
||||
SourcePath string
|
||||
Checksum string
|
||||
TemporaryPath string
|
||||
}
|
||||
|
||||
func buildPublishTransferPlan(
|
||||
runPrefix string,
|
||||
runPrefix, runRoot string,
|
||||
runFiles []publishUploadFile,
|
||||
runSources []*os.File,
|
||||
previousRoot string,
|
||||
previousFiles []publishUploadFile,
|
||||
previousSources []*os.File,
|
||||
sessionRoot string,
|
||||
outputPlan *publishOutputPlan,
|
||||
) ([]publishTransfer, error) {
|
||||
if len(runFiles) != len(runSources) || len(previousFiles) != len(previousSources) {
|
||||
return nil, fmt.Errorf("verified publish sources do not match their declared files")
|
||||
}
|
||||
if outputPlan == nil {
|
||||
return nil, fmt.Errorf("publish output plan is required")
|
||||
}
|
||||
transfers := make([]publishTransfer, 0, len(runFiles)+len(previousFiles)+len(outputPlan.Outcomes))
|
||||
add := func(artifact artifacts.RemoteArtifact, source *os.File) error {
|
||||
if source == nil {
|
||||
return fmt.Errorf("source for %q is required", artifact.DestinationKey)
|
||||
}
|
||||
transfers = append(transfers, publishTransfer{Artifact: artifact, Source: source})
|
||||
return nil
|
||||
for _, file := range runFiles {
|
||||
transfers = append(transfers, publishTransfer{
|
||||
Artifact: artifacts.RemoteArtifact{Type: artifacts.RemoteArtifactTypeRunFile, Source: "run:" + file.RelativePath, DestinationKey: artifacts.S3RunRelativeDestinationKey(runPrefix, file.RelativePath)},
|
||||
SourceRoot: runRoot, SourcePath: file.SourceRelativePath, Checksum: file.Checksum,
|
||||
})
|
||||
}
|
||||
for index, file := range runFiles {
|
||||
if err := add(artifacts.RemoteArtifact{
|
||||
Type: artifacts.RemoteArtifactTypeRunFile,
|
||||
Source: "run:" + file.RelativePath,
|
||||
DestinationKey: artifacts.S3RunRelativeDestinationKey(runPrefix, file.RelativePath),
|
||||
}, runSources[index]); err != nil {
|
||||
closePublishTransfers(transfers)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for index, file := range previousFiles {
|
||||
if err := add(artifacts.RemoteArtifact{
|
||||
Type: artifacts.RemoteArtifactTypePreviousArtifact,
|
||||
Source: "previous:" + file.SourceRelativePath,
|
||||
DestinationKey: artifacts.S3RunRelativeDestinationKey(runPrefix, file.RelativePath),
|
||||
}, previousSources[index]); err != nil {
|
||||
closePublishTransfers(transfers)
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range previousFiles {
|
||||
transfers = append(transfers, publishTransfer{
|
||||
Artifact: artifacts.RemoteArtifact{Type: artifacts.RemoteArtifactTypePreviousArtifact, Source: "previous:" + file.SourceRelativePath, DestinationKey: artifacts.S3RunRelativeDestinationKey(runPrefix, file.RelativePath)},
|
||||
SourceRoot: previousRoot, SourcePath: file.SourceRelativePath, Checksum: file.Checksum,
|
||||
})
|
||||
}
|
||||
for _, output := range outputPlan.Outcomes {
|
||||
if output.Status != publishOutputPublished {
|
||||
continue
|
||||
}
|
||||
source, err := openPublishOutputSource(output.LocalPath)
|
||||
relative, err := pathsafe.SlashRelativeFromRoot(sessionRoot, output.LocalPath)
|
||||
if err != nil {
|
||||
closePublishTransfers(transfers)
|
||||
return nil, fmt.Errorf("open output source %q: %w", output.Source, err)
|
||||
}
|
||||
if err := add(artifacts.RemoteArtifact{
|
||||
Type: artifacts.RemoteArtifactTypePublishedOutput,
|
||||
Source: output.Source,
|
||||
DestinationKey: output.DeclaredKey,
|
||||
}, source); err != nil {
|
||||
_ = source.Close()
|
||||
closePublishTransfers(transfers)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("output source %q: %w", output.Source, err)
|
||||
}
|
||||
transfers = append(transfers, publishTransfer{
|
||||
Artifact: artifacts.RemoteArtifact{Type: artifacts.RemoteArtifactTypePublishedOutput, Source: output.Source, DestinationKey: output.DeclaredKey},
|
||||
SourceRoot: sessionRoot, SourcePath: relative, Checksum: output.Checksum,
|
||||
})
|
||||
}
|
||||
sort.Slice(transfers, func(i, j int) bool {
|
||||
return transfers[i].Artifact.DestinationKey < transfers[j].Artifact.DestinationKey
|
||||
@@ -118,18 +98,13 @@ func appendCommittedSessionManifest(transfers *[]publishTransfer, localPath, des
|
||||
if transfers == nil {
|
||||
return fmt.Errorf("transfer plan is required")
|
||||
}
|
||||
source, err := os.Open(localPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*transfers = append(*transfers, publishTransfer{
|
||||
Artifact: artifacts.RemoteArtifact{
|
||||
Type: artifacts.RemoteArtifactTypeSessionManifest,
|
||||
Source: "session.manifest",
|
||||
DestinationKey: destinationKey,
|
||||
},
|
||||
Source: source,
|
||||
TemporaryPath: localPath,
|
||||
SourceRoot: filepath.Dir(localPath), SourcePath: filepath.Base(localPath), TemporaryPath: localPath,
|
||||
})
|
||||
sort.Slice(*transfers, func(i, j int) bool {
|
||||
return (*transfers)[i].Artifact.DestinationKey < (*transfers)[j].Artifact.DestinationKey
|
||||
@@ -308,9 +283,6 @@ func verifyPublishedObject(ctx context.Context, store storage.ObjectStore, key s
|
||||
|
||||
func closePublishTransfers(transfers []publishTransfer) {
|
||||
for _, transfer := range transfers {
|
||||
if transfer.Source != nil {
|
||||
_ = transfer.Source.Close()
|
||||
}
|
||||
if transfer.TemporaryPath != "" {
|
||||
_ = os.Remove(transfer.TemporaryPath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user