Skip to content

Commit

Permalink
Cleanup temp dirs properly (#145)
Browse files Browse the repository at this point in the history
We create a couple of temp dirs when starting aurora, it makes sense to
remove them when we are done as they are used for several transient
things.

Plus it has been reported that leaving things around can break building
new artifacts, as we leave kernels around and they are picked up instead
of the current ones, so its jsut good behaviour to clean up.

this cleans up before creating the temp dirs (fixed names) and when
finished

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka authored Dec 23, 2024
1 parent bc46d2f commit 7d163cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions deployer/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deployer

import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/kairos-io/AuroraBoot/internal"
"os"
"path/filepath"
Expand All @@ -14,6 +15,11 @@ import (
func (d *Deployer) StepPrepNetbootDir() error {
return d.Add(opPrepareNetboot, herd.WithCallback(
func(ctx context.Context) error {
err := os.RemoveAll(d.dstNetboot())
if err != nil {
internal.Log.Logger.Error().Err(err).Msg("Failed to remove temp netboot dir")
return err
}
return os.MkdirAll(d.dstNetboot(), 0755)
},
))
Expand All @@ -22,11 +28,28 @@ func (d *Deployer) StepPrepNetbootDir() error {
func (d *Deployer) StepPrepTmpRootDir() error {
return d.Add(opPreparetmproot, herd.WithCallback(
func(ctx context.Context) error {
err := os.RemoveAll(d.tmpRootFs())
if err != nil {
internal.Log.Logger.Error().Err(err).Msg("Failed to remove temp rootfs")
return err
}
return os.MkdirAll(d.tmpRootFs(), 0755)
},
))
}

// CleanTmpDirs removes the temp rootfs and netboot directories when finished to not leave things around
func (d *Deployer) CleanTmpDirs() error {
var err *multierror.Error
err = multierror.Append(err, os.RemoveAll(d.tmpRootFs()))
if err.ErrorOrNil() != nil {
internal.Log.Logger.Error().Err(err).Msg("Failed to remove temp rootfs")
}

err = multierror.Append(err, os.RemoveAll(d.dstNetboot()))
return err.ErrorOrNil()
}

func (d *Deployer) StepPrepISODir() error {
return d.Add(opPrepareISO, herd.WithCallback(func(ctx context.Context) error {
return os.MkdirAll(d.destination(), 0755)
Expand Down
12 changes: 11 additions & 1 deletion internal/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"github.com/hashicorp/go-multierror"
"os"

"github.com/kairos-io/AuroraBoot/deployer"
Expand Down Expand Up @@ -55,7 +56,16 @@ func GetApp(version string) *cli.App {
return err
}

return d.CollectErrors()
err = d.CollectErrors()
errCleanup := d.CleanTmpDirs()
if err != nil {
internal.Log.Logger.Error().Err(err).Msg("Failed to clean up tmp root dir")
// Append the cleanup error to the main errors if any
err = multierror.Append(err, errCleanup)
}

return err

},
}
}
Expand Down

0 comments on commit 7d163cb

Please sign in to comment.