From db610424ee2d0301ad29ec1b23d9c205efc9acd2 Mon Sep 17 00:00:00 2001 From: Christopher Obbard Date: Wed, 5 Jan 2022 12:25:06 +0000 Subject: [PATCH] actions/recipe: Only run cleanup function if needed 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: 74df488fb6b0 ("actions: Add recipe action") Signed-off-by: Christopher Obbard --- actions/recipe_action.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/actions/recipe_action.go b/actions/recipe_action.go index 54718ecb..411a82ae 100644 --- a/actions/recipe_action.go +++ b/actions/recipe_action.go @@ -44,6 +44,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 { @@ -95,6 +98,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 } @@ -105,6 +110,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 } @@ -117,6 +124,8 @@ func (recipe *RecipeAction) Run(context *debos.DebosContext) error { recipe.LogStart() for _, a := range recipe.Actions.Actions { + recipe.cleanupActions = append(recipe.cleanupActions, a) + if err := a.Run(&recipe.context); err != nil { return err } @@ -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) @@ -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)