Publish and restore prepared session documents
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
@@ -82,10 +83,27 @@ func (publishStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
return nil, fmt.Errorf("publish: run id is required")
|
||||
}
|
||||
|
||||
sessionPaths := publishSessionPaths(env, m)
|
||||
runFiles, err := collectPublishRunFiles(runRoot, m)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: collect run files: %w", err)
|
||||
}
|
||||
preparedFiles, err := collectPublishPreparedInputs(env.ArtifactStore, sessionPaths, runRoot, m.Inputs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: collect prepared inputs: %w", err)
|
||||
}
|
||||
byPath := make(map[string]publishUploadFile, len(runFiles)+len(preparedFiles))
|
||||
for _, file := range runFiles {
|
||||
byPath[file.RelativePath] = file
|
||||
}
|
||||
for _, file := range preparedFiles {
|
||||
byPath[file.RelativePath] = file
|
||||
}
|
||||
runFiles = runFiles[:0]
|
||||
for _, file := range byPath {
|
||||
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)
|
||||
@@ -95,7 +113,6 @@ func (publishStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("publish: object store does not support verified source uploads")
|
||||
}
|
||||
sessionPaths := publishSessionPaths(env, m)
|
||||
previousFiles, err := collectPublishPreviousFiles(ctx, sessionPaths.PreviousDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("publish: collect previous files: %w", err)
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
||||
@@ -23,6 +24,46 @@ type publishUploadFile struct {
|
||||
Checksum string
|
||||
}
|
||||
|
||||
// collectPublishPreparedInputs copies the prepared documents into this run's
|
||||
// archive, retaining their manifest checksums for the ordinary upload path.
|
||||
func collectPublishPreparedInputs(store artifacts.Store, paths artifacts.SessionPaths, runRoot string, records []manifest.InputRecord) ([]publishUploadFile, error) {
|
||||
filenames := map[string]string{"session_config": "session.yml", "campaign_config": "campaign.yml", "party": "party.yml"}
|
||||
var result []publishUploadFile
|
||||
for _, record := range records {
|
||||
name, ok := filenames[record.Kind]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
rel, err := pathsafe.SlashRelativeFromRoot(paths.Root, record.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
source, err := openPublishSourceUnderRoot(paths.Root, rel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := verifyPublishSourceChecksum(source, rel, record.Checksum); err != nil {
|
||||
_ = source.Close()
|
||||
return nil, err
|
||||
}
|
||||
data, err := io.ReadAll(source)
|
||||
closeErr := source.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if closeErr != nil {
|
||||
return nil, closeErr
|
||||
}
|
||||
archive := filepath.ToSlash(filepath.Join("prepare", "inputs", name))
|
||||
destination := filepath.Join(runRoot, filepath.FromSlash(archive))
|
||||
if _, err := writeBytesIfChanged(store, destination, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, publishUploadFile{RelativePath: archive, SourceRelativePath: archive, Checksum: record.Checksum})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func collectPublishRunFiles(runRoot string, sessionManifest *manifest.Manifest) ([]publishUploadFile, error) {
|
||||
if sessionManifest == nil {
|
||||
return nil, fmt.Errorf("session manifest is required")
|
||||
|
||||
Reference in New Issue
Block a user