Skip to content

Commit

Permalink
Ignore some error until a better strategy to managed defer cmd is found
Browse files Browse the repository at this point in the history
  • Loading branch information
upils committed Sep 22, 2023
1 parent 7ae9034 commit 60cec13
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 39 deletions.
23 changes: 6 additions & 17 deletions internal/statemachine/classic_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,14 +687,9 @@ func (stateMachine *StateMachine) installPackages() error {
}
}
defer func(cmds []*exec.Cmd) {
if tmpErr := runAll(cmds); tmpErr != nil {
if err != nil {
err = fmt.Errorf("%s after previous error: %w", tmpErr, err)
} else {
err = tmpErr
}
}
_ = runAll(cmds)
}(umountCmds)

installPackagesCmds = append(installPackagesCmds, mountCmds...)
umounts = append(umounts, umountCmds...)
}
Expand Down Expand Up @@ -1165,7 +1160,6 @@ func (stateMachine *StateMachine) prepareClassicImage() error {

// preseedClassicImage preseeds the snaps that have already been staged in the chroot
func (stateMachine *StateMachine) preseedClassicImage() error {
var err error
classicStateMachine := stateMachine.parent.(*ClassicStateMachine)

// create some directories in the chroot that we will bind mount from the
Expand All @@ -1190,19 +1184,14 @@ func (stateMachine *StateMachine) preseedClassicImage() error {
var umountCmds []*exec.Cmd
for _, mountPoint := range mountPoints {
thisMountCmds, thisUmountCmds := mountFromHost(stateMachine.tempDirs.chroot, mountPoint)
defer func(cmds []*exec.Cmd) {
if tmpErr := runAll(cmds); tmpErr != nil {
if err != nil {
err = fmt.Errorf("%s after previous error: %w", tmpErr, err)
} else {
err = tmpErr
}
}
}(umountCmds)
mountCmds = append(mountCmds, thisMountCmds...)
umountCmds = append(umountCmds, thisUmountCmds...)
}

defer func(cmds []*exec.Cmd) {
_ = runAll(cmds)
}(umountCmds)

// assemble the commands in the correct order: mount, preseed, unmount
preseedCmds = append(preseedCmds, mountCmds...)
preseedCmds = append(preseedCmds,
Expand Down
8 changes: 1 addition & 7 deletions internal/statemachine/classic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2062,13 +2062,7 @@ func TestSuccessfulClassicRun(t *testing.T) {
mountImageCmds = append(mountImageCmds, mountCmds...)
umountImageCmds = append(umountImageCmds, umountCmds...)
defer func(cmds []*exec.Cmd) {
if tmpErr := runAll(cmds); tmpErr != nil {
if err != nil {
err = fmt.Errorf("%s after previous error: %w", tmpErr, err)
} else {
err = tmpErr
}
}
_ = runAll(cmds)
}(umountCmds)
}
// make sure to unmount the disk too
Expand Down
16 changes: 2 additions & 14 deletions internal/statemachine/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,13 +1024,7 @@ func (stateMachine *StateMachine) updateGrub(rootfsVolName string, rootfsPartNum
updateGrubCmds = append(updateGrubCmds, mountCmds...)
umounts = append(umounts, umountCmds...)
defer func(cmds []*exec.Cmd) {
if tmpErr := runAll(cmds); tmpErr != nil {
if err != nil {
err = fmt.Errorf("Unable to unmount: %s after previous error: %w", tmpErr, err)
} else {
err = tmpErr
}
}
_ = runAll(cmds)
}(umountCmds)

}
Expand Down Expand Up @@ -1082,13 +1076,7 @@ func (stateMachine *StateMachine) updateGrub(rootfsVolName string, rootfsPartNum
loopUsed,
)
defer func() {
if tmpErr := teardownCmd.Run(); tmpErr != nil {
if err != nil {
err = fmt.Errorf("Unable to execute teardown cmd: %s after previous error: %w", tmpErr, err)
} else {
err = tmpErr
}
}
_ = teardownCmd.Run()
}()
updateGrubCmds = append(updateGrubCmds, teardownCmd)

Expand Down
1 change: 0 additions & 1 deletion internal/statemachine/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,6 @@ func TestFailedUpdateGrub(t *testing.T) {
asserter.AssertErrContains(err, "Error running command")
// check defered function failed and wrapped the error
asserter.AssertErrContains(err, "Unable to execute teardown cmd")
asserter.AssertErrContains(err, "Unable to unmount")
execCommand = exec.Command
})
}

0 comments on commit 60cec13

Please sign in to comment.