diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2020-09-09 07:11:30 -0400 |
---|---|---|
committer | Daniel J Walsh <dwalsh@redhat.com> | 2020-09-09 08:01:23 -0400 |
commit | 28e685f26efb2b4f18f29e21c2e6a1844dc04174 (patch) | |
tree | c5a54caaa55eae59436bcb1f3c6f3086a6b5e0fe /cmd/podman/images/build.go | |
parent | 6b1a1fcc5cb92a9fd5800b0d1af44f26093a8153 (diff) | |
download | podman-28e685f26efb2b4f18f29e21c2e6a1844dc04174.tar.gz podman-28e685f26efb2b4f18f29e21c2e6a1844dc04174.tar.bz2 podman-28e685f26efb2b4f18f29e21c2e6a1844dc04174.zip |
Fix podman build --logfile
Currently this command blows up because it closes the file descriptor before
doing the build.
Add tests to make sure we don't regress.
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1877188
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
Diffstat (limited to 'cmd/podman/images/build.go')
-rw-r--r-- | cmd/podman/images/build.go | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/cmd/podman/images/build.go b/cmd/podman/images/build.go index 400f960cc..923109b15 100644 --- a/cmd/podman/images/build.go +++ b/cmd/podman/images/build.go @@ -211,7 +211,16 @@ func build(cmd *cobra.Command, args []string) error { return err } - apiBuildOpts, err := buildFlagsWrapperToOptions(cmd, contextDir, &buildOpts) + var logfile *os.File + if cmd.Flag("logfile").Changed { + logfile, err = os.OpenFile(buildOpts.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) + if err != nil { + return errors.Errorf("error opening logfile %q: %v", buildOpts.Logfile, err) + } + defer logfile.Close() + } + + apiBuildOpts, err := buildFlagsWrapperToOptions(cmd, contextDir, &buildOpts, logfile) if err != nil { return err } @@ -225,7 +234,7 @@ func build(cmd *cobra.Command, args []string) error { // conversion here prevents the API from doing that (redundantly). // // TODO: this code should really be in Buildah. -func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *buildFlagsWrapper) (*entities.BuildOptions, error) { +func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *buildFlagsWrapper, logfile *os.File) (*entities.BuildOptions, error) { output := "" tags := []string{} if c.Flag("tag").Changed { @@ -284,16 +293,11 @@ func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *buil stderr = os.Stderr reporter = os.Stderr - if c.Flag("logfile").Changed { - f, err := os.OpenFile(flags.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) - if err != nil { - return nil, errors.Errorf("error opening logfile %q: %v", flags.Logfile, err) - } - defer f.Close() - logrus.SetOutput(f) - stdout = f - stderr = f - reporter = f + if logfile != nil { + logrus.SetOutput(logfile) + stdout = logfile + stderr = logfile + reporter = logfile } var memoryLimit, memorySwap int64 |