Skip to content

Commit

Permalink
actions/recipe: Only run cleanup function if needed
Browse files Browse the repository at this point in the history
When running nested recipes using the recipe action,
if an action in the child recipe exits early (e.g. on
failure) the remaining actions do not run but the
cleanup functions for these remaining actions still
run even though there is nothing to cleanup.

This doesn't follow how Debos handles running standalone
recipes, so modify the recipe action to only run action
cleanup functions when the associated action needs to be
cleaned up.

Fixes: 74df488 ("actions: Add recipe action")
Signed-off-by: Christopher Obbard <[email protected]>
  • Loading branch information
obbardc committed Jan 13, 2024
1 parent 2f9f75e commit a9919aa
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions actions/recipe_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type RecipeAction struct {
Actions Recipe `yaml:"-"`
templateVars map[string]string
context debos.DebosContext

cleanupActions []YamlAction
postMachineCleanupActions []YamlAction
}

func (recipe *RecipeAction) Verify(context *debos.DebosContext) error {
Expand Down Expand Up @@ -96,6 +99,8 @@ func (recipe *RecipeAction) PreMachine(context *debos.DebosContext, m *fakemachi
m.AddVolume(recipe.context.RecipeDir)

for _, a := range recipe.Actions.Actions {
recipe.postMachineCleanupActions = append(recipe.postMachineCleanupActions, a)

if err := a.PreMachine(&recipe.context, m, args); err != nil {
return err
}
Expand All @@ -106,6 +111,8 @@ func (recipe *RecipeAction) PreMachine(context *debos.DebosContext, m *fakemachi

func (recipe *RecipeAction) PreNoMachine(context *debos.DebosContext) error {
for _, a := range recipe.Actions.Actions {
recipe.postMachineCleanupActions = append(recipe.postMachineCleanupActions, a)

if err := a.PreNoMachine(&recipe.context); err != nil {
return err
}
Expand All @@ -116,6 +123,8 @@ func (recipe *RecipeAction) PreNoMachine(context *debos.DebosContext) error {

func (recipe *RecipeAction) Run(context *debos.DebosContext) error {
for _, a := range recipe.Actions.Actions {
recipe.cleanupActions = append(recipe.cleanupActions, a)

log.Printf("==== %s ====\n", a)
if err := a.Run(&recipe.context); err != nil {
return err
Expand All @@ -126,7 +135,8 @@ func (recipe *RecipeAction) Run(context *debos.DebosContext) error {
}

func (recipe *RecipeAction) Cleanup(context *debos.DebosContext) (err error) {
for _, a := range recipe.Actions.Actions {
/* only run Cleanup if Run was attempted */
for _, a := range recipe.cleanupActions {
defer func(action debos.Action) {
cleanup_err := action.Cleanup(context)

Expand All @@ -152,7 +162,8 @@ func (recipe *RecipeAction) PostMachine(context *debos.DebosContext) error {
}

func (recipe *RecipeAction) PostMachineCleanup(context *debos.DebosContext) (err error) {
for _, a := range recipe.Actions.Actions {
/* only run PostMachineCleanup if PreNoMachine OR PreMachine was attempted */
for _, a := range recipe.postMachineCleanupActions {
defer func(action debos.Action) {
cleanup_err := action.PostMachineCleanup(context)

Expand Down

0 comments on commit a9919aa

Please sign in to comment.