Skip to content

Commit

Permalink
filesystem: Bubble up errors rather than panicing
Browse files Browse the repository at this point in the history
If the overlay action fails to copy a file into the target filesystem
(e.g. destination path inside the filesystem doesn't exist), the current
behaviour is to panic, which causes a debos call inside the fakemachine
to panic, which isn't detected by the outer debos call.

This causes the execution of the inner debos call to stop (i.e. the
remaining recipe actions are no longer ran which is expected), but the
postexec commands still run and the overall exection is marked as
successful!

Rework the panics to instead bubble up errors so that any errors when
overlaying files causes the recipe to error out correctly rather than
this unexpected behaviour.

Before this commit is applied, the panic causes the debos call inside the
fakemachine to fail without being trapped by the outer debos call and the
outer debos call to still run the postprocess commands and exit with no
error as if the overlay action was successful:

    $ debos tests/overlay-non-existent-destination/overlay-non-existent-destination.yaml
    2023/07/05 11:07:24 ==== Overlay file to a non-existent destination ====
    2023/07/05 11:07:24 Overlaying tests/overlay-non-existent-destination/overlay-non-existent-destination.yaml on /scratch/root/this/path/does/not/exist
    2023/07/05 11:07:24 Failed to copy file tests/overlay-non-existent-destination/overlay-non-existent-destination.yaml: open /scratch/root/this/path/does/not/3277940894: no such file or directory
    2023/07/05 12:07:24 ==== run ====
    2023/07/05 12:07:24 echo Test | Test
    2023/07/05 12:07:24 ==== Recipe done ====
    $ echo $?
    0

With this commit applied, the execution of the outer debos call stops when
the overlay action fails and the error is correctly bubbled up to the user:

    $ debos tests/overlay-non-existent-destination/overlay-non-existent-destination.yaml
    2023/07/05 11:08:15 ==== Overlay file to a non-existent destination ====
    2023/07/05 11:08:15 Overlaying tests/overlay-non-existent-destination/overlay-non-existent-destination.yaml on /scratch/root/this/path/does/not/exist
    2023/07/05 11:08:15 Action `Overlay file to a non-existent destination` failed at stage Run, error: Failed to copy file tests/overlay-non-existent-destination/overlay-non-existent-destination.yaml: open /scratch/root/this/path/does/not/1742738134: no such file or directory
    $ echo $?
    1

Fixes: #401
Signed-off-by: Christopher Obbard <[email protected]>
  • Loading branch information
obbardc authored and sjoerdsimons committed Jul 10, 2023
1 parent d08d5d1 commit 5412d8b
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -70,18 +69,18 @@ func CopyTree(sourcetree, desttree string) error {
case 0:
err := CopyFile(p, target, info.Mode())
if err != nil {
log.Panicf("Failed to copy file %s: %v", p, err)
return fmt.Errorf("Failed to copy file %s: %w", p, err)
}
case os.ModeDir:
os.Mkdir(target, info.Mode())
case os.ModeSymlink:
link, err := os.Readlink(p)
if err != nil {
log.Panicf("Failed to read symlink %s: %v", suffix, err)
return fmt.Errorf("Failed to read symlink %s: %w", suffix, err)
}
os.Symlink(link, target)
default:
log.Panicf("Not handled /%s %v", suffix, info.Mode())
return fmt.Errorf("File %s with mode %v not handled", p, info.Mode())
}

return nil
Expand Down

0 comments on commit 5412d8b

Please sign in to comment.