mirror of
https://github.com/containers/podman.git
synced 2025-06-20 17:13:43 +08:00
build: relay exitcode from imagebuildah to registry
Podman does not relay exit code from buildah instead returns a generic error code `125`. Following PR allows `podman` to relay exit code from `imagebuildah` to `registry` as it is. Signed-off-by: Aditya Rajan <arajan@redhat.com>
This commit is contained in:
@ -1,9 +1,11 @@
|
|||||||
package images
|
package images
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -289,7 +291,23 @@ func build(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
report, err := registry.ImageEngine().Build(registry.GetContext(), containerFiles, *apiBuildOpts)
|
report, err := registry.ImageEngine().Build(registry.GetContext(), containerFiles, *apiBuildOpts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
exitCode := buildahCLI.ExecErrorCodeGeneric
|
||||||
|
if registry.IsRemote() {
|
||||||
|
// errors from server does not contain ExitCode
|
||||||
|
// so parse exit code from error message
|
||||||
|
remoteExitCode, parseErr := utils.ExitCodeFromBuildError(fmt.Sprint(errors.Cause(err)))
|
||||||
|
if parseErr == nil {
|
||||||
|
exitCode = remoteExitCode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ee, ok := (errors.Cause(err)).(*exec.ExitError); ok {
|
||||||
|
exitCode = ee.ExitCode()
|
||||||
|
}
|
||||||
|
|
||||||
|
registry.SetExitCode(exitCode)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
buildahCLI "github.com/containers/buildah/pkg/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OutputErrors []error
|
type OutputErrors []error
|
||||||
@ -17,3 +22,24 @@ func (o OutputErrors) PrintErrors() (lastError error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* For remote client, server does not returns error with exit code
|
||||||
|
instead returns a message and we cast it to a new error.
|
||||||
|
|
||||||
|
Following function performs parsing on build error and returns
|
||||||
|
exit status which was exepected for this current build
|
||||||
|
*/
|
||||||
|
func ExitCodeFromBuildError(errorMsg string) (int, error) {
|
||||||
|
if strings.Contains(errorMsg, "exit status") {
|
||||||
|
errorSplit := strings.Split(errorMsg, " ")
|
||||||
|
if errorSplit[len(errorSplit)-2] == "status" {
|
||||||
|
tmpSplit := strings.Split(errorSplit[len(errorSplit)-1], "\n")
|
||||||
|
exitCodeRemote, err := strconv.Atoi(tmpSplit[0])
|
||||||
|
if err == nil {
|
||||||
|
return exitCodeRemote, nil
|
||||||
|
}
|
||||||
|
return buildahCLI.ExecErrorCodeGeneric, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buildahCLI.ExecErrorCodeGeneric, errors.New("error message does not contains a valid exit code")
|
||||||
|
}
|
||||||
|
@ -273,6 +273,23 @@ RUN printenv http_proxy`, ALPINE)
|
|||||||
os.Unsetenv("http_proxy")
|
os.Unsetenv("http_proxy")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman build relay exit code to process", func() {
|
||||||
|
if IsRemote() {
|
||||||
|
// following states true for all the remote commands
|
||||||
|
Skip("Remote server does not emits error with exit status")
|
||||||
|
}
|
||||||
|
podmanTest.AddImageToRWStore(ALPINE)
|
||||||
|
dockerfile := fmt.Sprintf(`FROM %s
|
||||||
|
RUN exit 5`, ALPINE)
|
||||||
|
|
||||||
|
dockerfilePath := filepath.Join(podmanTest.TempDir, "Dockerfile")
|
||||||
|
err := ioutil.WriteFile(dockerfilePath, []byte(dockerfile), 0755)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
session := podmanTest.Podman([]string{"build", "-t", "error-test", "--file", dockerfilePath, podmanTest.TempDir})
|
||||||
|
session.Wait(120)
|
||||||
|
Expect(session).Should(Exit(5))
|
||||||
|
})
|
||||||
|
|
||||||
It("podman build and check identity", func() {
|
It("podman build and check identity", func() {
|
||||||
session := podmanTest.Podman([]string{"build", "--pull-never", "-f", "build/basicalpine/Containerfile.path", "--no-cache", "-t", "test", "build/basicalpine"})
|
session := podmanTest.Podman([]string{"build", "--pull-never", "-f", "build/basicalpine/Containerfile.path", "--no-cache", "-t", "test", "build/basicalpine"})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
|
Reference in New Issue
Block a user