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 6a289e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 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
34 changes: 29 additions & 5 deletions playbook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,26 @@ func CheckDelay(ctx context.Context, playbook models.Playbook, run models.Playbo
return false, nil
}

func canRunOnLocalAgent(spec v1.PlaybookSpec, action *v1.PlaybookAction) bool {
if len(spec.RunsOn) != 0 {
if !lo.Contains(spec.RunsOn, Main) {
return false
}
}

if len(action.RunsOn) != 0 {
if !lo.Contains(action.RunsOn, Main) {
return false
}
}

return true
}

// 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,18 +193,28 @@ 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)
}

if canRunOnLocalAgent(playbookSpec, action) {
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 {
eligibleAgents := action.RunsOn
if len(eligibleAgents) == 0 {
eligibleAgents = playbookSpec.RunsOn
}

if agent, err := db.FindFirstAgent(ctx, eligibleAgents...); 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, ","))
return ctx.Oops("db").Wrapf(err, "failed to find any agent (%s)", strings.Join(eligibleAgents, ","))
} 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
Expand Down

0 comments on commit 6a289e5

Please sign in to comment.