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 Jul 26, 2023
1 parent e097583 commit 4c5d534
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 @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
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 4c5d534

Please sign in to comment.