Skip to content

Commit

Permalink
Refactor the error handling
Browse files Browse the repository at this point in the history
Currently the debos error handling sets an exitcode
during running, then exits with that code. Since we
only ever exit with a 1, unless an error occurs while
running fakemachine, let's switch around the error
handling to use the context.State and check the state
after running.

[TODO: THIS SHOULD BE A SEPERATE COMMIT]
While we are here, also rework the fakemachine error
handling to show that an error occured during run.

Signed-off-by: Christopher Obbard <[email protected]>
  • Loading branch information
obbardc committed Jan 7, 2022
1 parent f640c00 commit 4d0f172
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions cmd/debos/debos.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import (
"github.com/jessevdk/go-flags"
)

func checkError(context *debos.DebosContext, err error, a debos.Action, stage string) int {
func checkError(context *debos.DebosContext, err error, a debos.Action, stage string) bool {
if err == nil {
return 0
return true
}

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

func do_run(r actions.Recipe, context *debos.DebosContext) int {
func do_run(r actions.Recipe, context *debos.DebosContext) bool {
for _, a := range r.Actions {
err := a.Run(context)

Expand All @@ -35,12 +35,12 @@ func do_run(r actions.Recipe, context *debos.DebosContext) int {
defer a.Cleanup(context)

// Check the state of Run method
if exitcode := checkError(context, err, a, "Run"); exitcode != 0 {
return exitcode
if !checkError(context, err, a, "Run") {
return false
}
}

return 0
return true
}

func warnLocalhost(variable string, value string) {
Expand Down Expand Up @@ -88,11 +88,12 @@ func main() {
"no_proxy",
}

var exitcode int = 0
// Allow to run all deferred calls prior to os.Exit()
defer func() {
os.Exit(exitcode)
}()
defer func(context debos.DebosContext) {
if context.State == debos.Failed {
os.Exit(1)
}
}(context)

parser := flags.NewParser(&options, flags.Default)
fakemachineBackends := parser.FindOptionByLongName("fakemachine-backend")
Expand All @@ -104,20 +105,20 @@ func main() {
if ok && flagsErr.Type == flags.ErrHelp {
return
} else {
exitcode = 1
context.State = debos.Failed
return
}
}

if len(args) != 1 {
log.Println("No recipe given!")
exitcode = 1
context.State = debos.Failed
return
}

if options.DisableFakeMachine && options.Backend != "auto" {
log.Println("--disable-fakemachine and --fakemachine-backend are mutually exclusive")
exitcode = 1
context.State = debos.Failed
return
}

Expand All @@ -140,12 +141,12 @@ func main() {
r := actions.Recipe{}
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Println(err)
exitcode = 1
context.State = debos.Failed
return
}
if err := r.Parse(file, options.PrintRecipe, options.Verbose, options.TemplateVars); err != nil {
log.Println(err)
exitcode = 1
context.State = debos.Failed
return
}

Expand All @@ -162,14 +163,14 @@ func main() {
// attempt to create a fakemachine
m, err = fakemachine.NewMachineWithBackend(options.Backend)
if err != nil {
log.Printf("error creating fakemachine: %v", err)
log.Printf("Couldn't create fakemachine: %v", err)

/* fallback to running on the host unless the user has chosen
* a specific backend */
if options.Backend == "auto" {
runInFakeMachine = false
} else {
exitcode = 1
context.State = debos.Failed
return
}
}
Expand Down Expand Up @@ -233,7 +234,7 @@ func main() {

for _, a := range r.Actions {
err = a.Verify(&context)
if exitcode = checkError(&context, err, a, "Verify"); exitcode != 0 {
if !checkError(&context, err, a, "Verify") {
return
}
}
Expand All @@ -253,7 +254,7 @@ func main() {
memsize, err := units.RAMInBytes(options.Memory)
if err != nil {
fmt.Printf("Couldn't parse memory size: %v\n", err)
exitcode = 1
context.State = debos.Failed
return
}
m.SetMemory(int(memsize / 1024 / 1024))
Expand All @@ -268,7 +269,7 @@ func main() {
size, err := units.FromHumanSize(options.ScratchSize)
if err != nil {
fmt.Printf("Couldn't parse scratch size: %v\n", err)
exitcode = 1
context.State = debos.Failed
return
}
m.SetScratch(size, "")
Expand Down Expand Up @@ -310,25 +311,26 @@ func main() {
defer a.PostMachineCleanup(&context)

err = a.PreMachine(&context, m, &args)
if exitcode = checkError(&context, err, a, "PreMachine"); exitcode != 0 {
if !checkError(&context, err, a, "PreMachine") {
return
}
}

exitcode, err = m.RunInMachineWithArgs(args)
exitcode, err := m.RunInMachineWithArgs(args)
if err != nil {
fmt.Println(err)
fmt.Printf("Couldn't start fakemachine: %v\n", err)
context.State = debos.Failed
return
}

if exitcode != 0 {
fmt.Printf("fakemachine failed with non-zero exitcode: %d\n", exitcode)
context.State = debos.Failed
return
}

for _, a := range r.Actions {
err = a.PostMachine(&context)
if exitcode = checkError(&context, err, a, "Postmachine"); exitcode != 0 {
if !checkError(&context, err, a, "Postmachine") {
return
}
}
Expand All @@ -343,7 +345,7 @@ func main() {
defer a.PostMachineCleanup(&context)

err = a.PreNoMachine(&context)
if exitcode = checkError(&context, err, a, "PreNoMachine"); exitcode != 0 {
if !checkError(&context, err, a, "PreNoMachine") {
return
}
}
Expand All @@ -353,20 +355,20 @@ func main() {
if _, err = os.Stat(context.Rootdir); os.IsNotExist(err) {
err = os.Mkdir(context.Rootdir, 0755)
if err != nil && os.IsNotExist(err) {
exitcode = 1
fmt.Printf("Could not create rootdir: %v\n", err)
context.State = debos.Failed
return
}
}

exitcode = do_run(r, &context)
if exitcode != 0 {
if !do_run(r, &context) {
return
}

if !fakemachine.InMachine() {
for _, a := range r.Actions {
err = a.PostMachine(&context)
if exitcode = checkError(&context, err, a, "PostMachine"); exitcode != 0 {
if !checkError(&context, err, a, "PostMachine") {
return
}
}
Expand Down

0 comments on commit 4d0f172

Please sign in to comment.