Share lazy configuration and session acquisition
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
type pipelineCampaignConfig = config.LoadedPipelineCampaign
|
||||
@@ -37,46 +38,50 @@ func retainedCommandConfig(cfg *config.Config) *commandConfig {
|
||||
return &commandConfig{Config: cfg}
|
||||
}
|
||||
|
||||
func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campaignFlag, campaignFileFlag, sessionFlag string, sessionOpts config.SessionLoadOptions, secretsDir *string, deps commandDependencies) (loaded *commandConfig, err error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campaignFlag, campaignFileFlag, sessionFlag string, sessionOpts config.SessionLoadOptions, secretsDir *string, deps commandDependencies) (*commandConfig, error) {
|
||||
selection := configurationSelection{}
|
||||
for _, field := range []struct {
|
||||
value string
|
||||
target *commandkit.Supplied[string]
|
||||
}{
|
||||
{pipelineFlag, &selection.Config}, {campaignFlag, &selection.Campaign}, {campaignFileFlag, &selection.CampaignFile},
|
||||
{sessionFlag, &selection.SessionFile}, {sessionOpts.SessionID, &selection.Session},
|
||||
} {
|
||||
if field.value != "" {
|
||||
*field.target = commandkit.Supplied[string]{Value: field.value, Set: true}
|
||||
}
|
||||
}
|
||||
if secretsDir != nil {
|
||||
selection.SecretsDir = commandkit.Supplied[string]{Value: *secretsDir, Set: true}
|
||||
}
|
||||
if sessionOpts.Overlay != nil {
|
||||
selection.Overlay = commandkit.Supplied[string]{Value: *sessionOpts.Overlay, Set: true}
|
||||
}
|
||||
acquisition := newConfigurationAcquisition(selection, deps)
|
||||
acquisition.previousSessionID = sessionOpts.PreviousSessionID
|
||||
cfg, err := acquisition.Domain(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Join(err, acquisition.Close())
|
||||
}
|
||||
return &commandConfig{Config: cfg, cleanup: acquisition.Close}, nil
|
||||
}
|
||||
|
||||
type acquiredSession struct {
|
||||
Config *config.SessionConfig
|
||||
Path string
|
||||
Source config.SessionSource
|
||||
cleanup func() error
|
||||
}
|
||||
|
||||
func loadRemoteSessionConfig(ctx context.Context, base *config.LoadedPipelineCampaign, sessionOpts config.SessionLoadOptions, deps commandDependencies) (loaded *acquiredSession, err error) {
|
||||
var cleanup func() error
|
||||
defer func() {
|
||||
if err == nil || cleanup == nil {
|
||||
return
|
||||
}
|
||||
if cleanupErr := cleanup(); cleanupErr != nil {
|
||||
err = errors.Join(err, cleanupErr)
|
||||
if err != nil && cleanup != nil {
|
||||
err = errors.Join(err, cleanup())
|
||||
}
|
||||
}()
|
||||
|
||||
base, err := loadPipelineCampaignConfigWithDependencies(pipelineFlag, campaignFlag, campaignFileFlag, config.PipelineLoadOptions{Overlay: sessionOpts.Overlay}, secretsDir, deps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sessionOpts.Campaign = base.Campaign
|
||||
sessionOpts.CampaignPath = base.CampaignPath
|
||||
|
||||
if explicitSession := strings.TrimSpace(sessionFlag); explicitSession != "" {
|
||||
cfg, err := config.LoadSessionWithPipelineCampaignOptions(*base, explicitSession, sessionOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return retainedCommandConfig(cfg), nil
|
||||
}
|
||||
|
||||
discoveredSession, discoveredPath, err := config.DiscoverSession(config.DefaultSessionConfigSearchPaths, sessionOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if discoveredSession != nil {
|
||||
cfg, err := config.ResolveLoadedPipelineCampaign(*base, discoveredPath, discoveredSession, config.SessionSource{Source: "session_config", LocalPath: discoveredPath})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return retainedCommandConfig(cfg), nil
|
||||
}
|
||||
|
||||
sessionID := strings.TrimSpace(sessionOpts.SessionID)
|
||||
if sessionID == "" {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, "remote session loading requires a session_id")
|
||||
@@ -88,24 +93,21 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
|
||||
}
|
||||
sessionPrefix := artifacts.S3SessionPrefix(rootPrefix, config.CampaignID(base.Campaign), sessionID)
|
||||
remoteKey := artifacts.S3SessionConfigKey(sessionPrefix)
|
||||
partialCfg, err := config.ResolveLoadedPipelineCampaign(*base, "", nil, config.SessionSource{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
partialCfg := &config.Config{Pipeline: base.Pipeline, PipelinePath: base.PipelinePath, Campaign: base.Campaign, CampaignPath: base.CampaignPath}
|
||||
store, err := newCommandObjectStoreWithDependencies(ctx, partialCfg, nil, deps)
|
||||
if err != nil {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
|
||||
return nil, fmt.Errorf("remote session %q unavailable: %w", remoteKey, err)
|
||||
}
|
||||
|
||||
sessionInfo, checksum, body, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey, config.CampaignID(base.Campaign), sessionID)
|
||||
if err != nil {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error())
|
||||
return nil, fmt.Errorf("discover remote session: %w", err)
|
||||
}
|
||||
defer body.Close()
|
||||
defer func() { err = errors.Join(err, body.Close()) }()
|
||||
remoteKey = sessionInfo.Key
|
||||
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))
|
||||
return nil, fmt.Errorf("remote session %q download failed: %w", remoteKey, err)
|
||||
}
|
||||
sessionTempPath := temporary.Name()
|
||||
cleanup = func() error {
|
||||
@@ -134,23 +136,12 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg, err := config.ResolveLoadedPipelineCampaign(
|
||||
*base,
|
||||
sessionTempPath,
|
||||
sessionCfg,
|
||||
config.SessionSource{
|
||||
Source: "session_config.s3",
|
||||
LocalPath: sessionTempPath,
|
||||
S3Bucket: s3BucketName(base.Pipeline),
|
||||
S3Key: remoteKey,
|
||||
S3Size: sessionInfo.Size,
|
||||
S3ETag: sessionInfo.ETag,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
source := "session_config.s3"
|
||||
if checksum != "" {
|
||||
source = "session_config.s3.committed_snapshot"
|
||||
}
|
||||
loaded = &commandConfig{Config: cfg, cleanup: cleanup}
|
||||
loaded = &acquiredSession{Config: sessionCfg, Path: sessionTempPath, cleanup: cleanup,
|
||||
Source: config.SessionSource{Source: source, LocalPath: sessionTempPath, S3Bucket: s3BucketName(base.Pipeline), S3Key: remoteKey, S3Size: sessionInfo.Size, S3ETag: sessionInfo.ETag}}
|
||||
cleanup = nil
|
||||
return loaded, nil
|
||||
}
|
||||
@@ -169,9 +160,9 @@ func loadPipelineCampaignConfigWithDependencies(pipelineFlag, campaignFlag, camp
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if selectedID := strings.TrimSpace(campaignFlag); selectedID != "" && strings.TrimSpace(campaignFileFlag) == "" {
|
||||
if selectedID := strings.TrimSpace(campaignFlag); selectedID != "" {
|
||||
if got := config.CampaignID(campaignCfg); got != selectedID {
|
||||
return nil, fmt.Errorf("campaign config %q invalid: campaign_id %q does not match selected campaign %q", resolvedCampaignPath, got, selectedID)
|
||||
return nil, &config.SessionIdentityError{Source: resolvedCampaignPath, Field: "campaign_id", Expected: selectedID, Actual: got}
|
||||
}
|
||||
}
|
||||
loaded, err := config.LoadPipelineCampaign(loadedPipelinePath, pipelineCfg, resolvedCampaignPath, campaignCfg)
|
||||
@@ -236,8 +227,7 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses
|
||||
}
|
||||
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 storage.ObjectInfo{}, "", nil, errors.Join(fmt.Errorf("remote session %q differs from committed object identity", remoteKey), body.Close())
|
||||
}
|
||||
return info, selected.SHA256, body, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user