Skip to content

Commit

Permalink
fix: runsOn handling
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Sep 20, 2024
1 parent 601a024 commit 299d61b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
6 changes: 2 additions & 4 deletions cmd/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ var Run = &cobra.Command{
logger.Fatalf(err.Error())
return
}

if action == nil {
logger.Errorf("No actions to run")
return
}

for action != nil {

if delayed, err := runner.CheckDelay(ctx, *p, *run, action, step); err != nil {
ctx.Errorf("Error running action %s: %v", action.Name, err)
break
Expand All @@ -163,7 +163,6 @@ var Run = &cobra.Command{
}

runAction, err := run.StartAction(ctx.DB(), action.Name)

if err != nil {
ctx.Errorf("Error starting action %s: %v", action.Name, err)
break
Expand All @@ -175,11 +174,11 @@ var Run = &cobra.Command{
}

action, _, err = runner.GetNextActionToRun(ctx, *p, *run)

if action != nil && action.Name == runAction.Name {
ctx.Errorf("%v", ctx.Oops().Errorf("Action cycle detected for: %s", action.Name))
shutdown.ShutdownAndExit(1, "")
}

if err != nil {
ctx.Errorf("Error getting next action %s: %v", action.Name, err)
break
Expand All @@ -197,7 +196,6 @@ var Run = &cobra.Command{
if summary.Run.Status != models.PlaybookRunStatusCompleted {
shutdown.ShutdownAndExit(1, fmt.Sprintf("Playbook run status: %s ", summary.Run.Status))
}

},
}

Expand Down
38 changes: 29 additions & 9 deletions playbook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,22 @@ func CheckDelay(ctx context.Context, playbook models.Playbook, run models.Playbo
return false, nil
}

func getEligibleAgents(spec v1.PlaybookSpec, action *v1.PlaybookAction) []string {
if len(action.RunsOn) != 0 {
return action.RunsOn
}

if len(spec.RunsOn) != 0 {
return spec.RunsOn
}

return []string{Main}
}

// ScheduleRun finds the next action step that needs to run and
// creates the PlaybookActionRun in a scheduled status, with an optional agentId
func ScheduleRun(ctx context.Context, run models.PlaybookRun) error {

var playbook models.Playbook

if err := ctx.DB().First(&playbook, run.PlaybookID).Error; err != nil {
return ctx.Oops("db").Wrap(err)
}
Expand Down Expand Up @@ -179,24 +189,34 @@ func ScheduleRun(ctx context.Context, run models.PlaybookRun) error {
}, action.Name))
}

if run.AgentID == nil && (len(action.RunsOn) == 0 || lo.Contains(action.RunsOn, Main)) {
var playbookSpec v1.PlaybookSpec
if err := json.Unmarshal(playbook.Spec, &playbookSpec); err != nil {
return ctx.Oops().Wrap(err)
}

eligibleAgents := getEligibleAgents(playbookSpec, action)

agent, err := db.FindFirstAgent(ctx, eligibleAgents...)
if err != nil {
return ctx.Oops("db").Wrap(err)
} else if agent == nil {
return ctx.Oops("db").Wrapf(err, "failed to find any agent (%s)", strings.Join(eligibleAgents, ","))
}

if agent.Name == Main {
if runAction, err := run.StartAction(ctx.DB(), action.Name); err != nil {
return ctx.Oops("db").Wrap(err)
} else {
ctx.Tracef("started %s (%v) on local", action.Name, runAction.ID)
}
}

if agent, err := db.FindFirstAgent(ctx, action.RunsOn...); err != nil {
return ctx.Oops("db").Wrap(err)
} else if agent == nil {
return ctx.Oops("db").Wrapf(err, "failed to find any agent (%s)", strings.Join(action.RunsOn, ","))
} else {
// Assign the action to an agent and step the status to Waiting
// When the agent polls for new actions to run, we return and then set the status to Running
ctx.Tracef("assigning %s to agent %s", action.Name, agent.Name)
return ctx.Oops("db").Wrap(run.Assign(ctx.DB(), agent, action.Name))
}

return nil
}

func ExecuteAndSaveAction(ctx context.Context, playbookID any, action *models.PlaybookRunAction, actionSpec v1.PlaybookAction) error {
Expand Down
2 changes: 1 addition & 1 deletion playbook/testdata/agent-runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
echo "{{.config.config_class}}"
- name: Echo class on agent aws
runsOn:
- "aws"
- 'aws'
templatesOn: agent
exec:
script: |
Expand Down

0 comments on commit 299d61b

Please sign in to comment.