Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report action cleanup errors to the user & various cleanup fixes for recipe action #301

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
13 changes: 13 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package debos

import (
"bytes"
"log"

"github.com/go-debos/fakemachine"
)

Expand Down Expand Up @@ -53,6 +55,17 @@ func (c *DebosContext) Origin(o string) (string, bool) {
}
}

func HandleError(context *DebosContext, err error, a Action, stage string) bool {
if err == nil {
return false
}

context.State = Failed
log.Printf("Action `%s` failed at stage %s, error: %s", a, stage, err)
DebugShell(*context)
return true
}

type Action interface {
/* FIXME verify should probably be prepare or somesuch */
Verify(context *DebosContext) error
Expand Down
43 changes: 33 additions & 10 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 @@ -125,11 +134,18 @@ func (recipe *RecipeAction) Run(context *debos.DebosContext) error {
return nil
}

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

/* Cannot bubble multiple errors, so check for an error locally and
* return a generic error if the child recipe failed to cleanup. */
if debos.HandleError(context, cleanup_err, action, "Cleanup") {
err = errors.New("Child recipe failed")
}
}(a)
}

return nil
Expand All @@ -145,11 +161,18 @@ func (recipe *RecipeAction) PostMachine(context *debos.DebosContext) error {
return nil
}

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

/* Cannot bubble multiple errors, so check for an error locally and
* return a generic error if the child recipe failed to cleanup. */
if debos.HandleError(context, cleanup_err, action, "PostMachineCleanup") {
err = errors.New("Child recipe failed")
}
}(a)
}

return nil
Expand Down
Loading