Read selected remote sessions directly without listing run history
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -93,21 +94,28 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
|
||||
}
|
||||
|
||||
sessionInfo, checksum, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey, config.CampaignID(base.Campaign), sessionID)
|
||||
sessionInfo, checksum, body, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey, config.CampaignID(base.Campaign), sessionID)
|
||||
if err != nil {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error())
|
||||
}
|
||||
defer body.Close()
|
||||
remoteKey = sessionInfo.Key
|
||||
sessionTempPath, err := deps.downloadObjectToTemp(ctx, store, remoteKey, "narratio-session-*.yml")
|
||||
temporary, err := os.CreateTemp("", "narratio-session-*.yml")
|
||||
if err != nil {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
|
||||
}
|
||||
sessionTempPath := temporary.Name()
|
||||
cleanup = func() error {
|
||||
if err := fileops.RemoveAllUnderRoot(filepath.Dir(sessionTempPath), sessionTempPath); err != nil {
|
||||
return fmt.Errorf("remove downloaded remote session config: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
_, copyErr := io.Copy(temporary, body)
|
||||
closeErr := temporary.Close()
|
||||
if err := errors.Join(copyErr, closeErr); err != nil {
|
||||
return nil, fmt.Errorf("download remote session %q: %w", remoteKey, err)
|
||||
}
|
||||
sessionBytes, err := os.ReadFile(sessionTempPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read downloaded remote session config: %w", err)
|
||||
@@ -191,10 +199,10 @@ func loadPipelineConfigWithDependencies(pipelineFlag string, opts config.Pipelin
|
||||
return resolvedPipelinePath, pipelineCfg, nil
|
||||
}
|
||||
|
||||
func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, sessionPrefix, remoteKey, campaign, sessionID string) (storage.ObjectInfo, string, error) {
|
||||
func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, sessionPrefix, remoteKey, campaign, sessionID string) (storage.ObjectInfo, string, io.ReadCloser, error) {
|
||||
authored, err := store.Exists(ctx, remoteKey)
|
||||
if err != nil {
|
||||
return storage.ObjectInfo{}, "", fmt.Errorf("check authored session %q: %w", remoteKey, err)
|
||||
return storage.ObjectInfo{}, "", nil, fmt.Errorf("check authored session %q: %w", remoteKey, err)
|
||||
}
|
||||
var selected *artifacts.RemoteArtifact
|
||||
if !authored {
|
||||
@@ -204,7 +212,7 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses
|
||||
if item.Type == artifacts.RemoteArtifactTypeRunFile && item.Source == "run:prepare/inputs/session.yml" {
|
||||
identity, err := artifacts.ParseImmutableRunObjectKey(sessionPrefix, item.DestinationKey)
|
||||
if err != nil || "run:"+identity.RelativePath != item.Source {
|
||||
return storage.ObjectInfo{}, "", fmt.Errorf("committed session source does not match its object key")
|
||||
return storage.ObjectInfo{}, "", nil, fmt.Errorf("committed session source does not match its object key")
|
||||
}
|
||||
copy := item
|
||||
selected = ©
|
||||
@@ -213,25 +221,24 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses
|
||||
}
|
||||
}
|
||||
} else if !errors.Is(err, artifacts.ErrCurrentCommitPointerMissing) {
|
||||
return storage.ObjectInfo{}, "", fmt.Errorf("load remote session selection: %w", err)
|
||||
return storage.ObjectInfo{}, "", nil, fmt.Errorf("load remote session selection: %w", err)
|
||||
}
|
||||
}
|
||||
objects, err := store.List(ctx, sessionPrefix)
|
||||
info, body, err := store.Read(ctx, remoteKey)
|
||||
if err != nil {
|
||||
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q list failed: %w", remoteKey, err)
|
||||
}
|
||||
for _, obj := range objects {
|
||||
if obj.Key == remoteKey {
|
||||
if selected != nil {
|
||||
if obj.Size != selected.Size || strings.TrimSpace(obj.ETag) != selected.Generation {
|
||||
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q differs from committed object identity", remoteKey)
|
||||
}
|
||||
return obj, selected.SHA256, nil
|
||||
}
|
||||
return obj, "", nil
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return storage.ObjectInfo{}, "", nil, fmt.Errorf("remote session %q not found: %w", remoteKey, err)
|
||||
}
|
||||
return storage.ObjectInfo{}, "", nil, fmt.Errorf("read remote session %q: %w", remoteKey, err)
|
||||
}
|
||||
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q not found", remoteKey)
|
||||
if selected != nil {
|
||||
if info.Size != selected.Size || strings.TrimSpace(info.ETag) != selected.Generation {
|
||||
_ = body.Close()
|
||||
return storage.ObjectInfo{}, "", nil, fmt.Errorf("remote session %q differs from committed object identity", remoteKey)
|
||||
}
|
||||
return info, selected.SHA256, body, nil
|
||||
}
|
||||
return info, "", body, nil
|
||||
}
|
||||
|
||||
func s3BucketName(cfg *config.PipelineConfig) string {
|
||||
|
||||
Reference in New Issue
Block a user