Skip to content

Commit

Permalink
chore: template playbook runsOn
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Oct 10, 2024
1 parent 8212b24 commit bdc782e
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions playbook/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/flanksource/gomplate/v3"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/samber/oops"
Expand Down Expand Up @@ -92,10 +93,6 @@ func Run(ctx context.Context, playbook *models.Playbook, req RunParams) (*models
AgentID: req.AgentID,
}

// The run gets its own copy of the spec and uses that throughout its lifecycle.
// Any change to the playbook spec while the run is in progress should not affect the run.
run.Spec = playbook.Spec

if ctx.User() != nil {
run.CreatedBy = &ctx.User().ID
}
Expand Down Expand Up @@ -158,6 +155,28 @@ func Run(ctx context.Context, playbook *models.Playbook, req RunParams) (*models
return nil, err
}

// The run gets its own copy of the spec and uses that throughout its lifecycle.
// Any change to the playbook spec while the run is in progress should not affect the run.
var runSpec v1.PlaybookSpec
if err := json.Unmarshal(playbook.Spec, &runSpec); err != nil {
return nil, ctx.Oops().Wrap(err)
}
// Template run's spec (runsOn)
var runsOn []string
for _, specRunOn := range runSpec.RunsOn {
output, err := ctx.RunTemplate(gomplate.Template{Template: specRunOn}, templateEnv.AsMap())
if err != nil {
return nil, ctx.Oops().Wrap(err)
}
runsOn = append(runsOn, output)
}
runSpec.RunsOn = runsOn

run.Spec, err = json.Marshal(runSpec)
if err != nil {
return nil, ctx.Oops().Wrap(err)
}

if err := savePlaybookRun(ctx, &run); err != nil {
return nil, ctx.Oops().Wrapf(err, "failed to create playbook run")
}
Expand Down

0 comments on commit bdc782e

Please sign in to comment.