diff --git a/dev-tools/mage/dockerbuilder.go b/dev-tools/mage/dockerbuilder.go index 24baefd1eaf..44791010217 100644 --- a/dev-tools/mage/dockerbuilder.go +++ b/dev-tools/mage/dockerbuilder.go @@ -18,8 +18,12 @@ package mage import ( + "bytes" + "compress/gzip" "fmt" + "io" "os" + "os/exec" "path/filepath" "strings" @@ -141,13 +145,47 @@ func (b *dockerBuilder) dockerSave(tag string) error { // Save the container as artifact outputFile := b.OutputFile if outputFile == "" { - outputTar, err := b.Expand(defaultBinaryName + ".docker.tar") + outputTar, err := b.Expand(defaultBinaryName + ".docker.tar.gz") if err != nil { return err } outputFile = filepath.Join(distributionsDir, outputTar) } - if err := sh.Run("docker", "save", "-o", outputFile, tag); err != nil { + var stderr bytes.Buffer + cmd := exec.Command("docker", "save", tag) + cmd.Stderr = &stderr + stdout, err := cmd.StdoutPipe() + if err != nil { + return err + } + if err = cmd.Start(); err != nil { + return err + } + + err = func() error { + f, err := os.Create(outputFile) + if err != nil { + return err + } + defer f.Close() + + w := gzip.NewWriter(f) + defer w.Close() + + _, err = io.Copy(w, stdout) + if err != nil { + return err + } + return nil + }() + if err != nil { + return err + } + + if err = cmd.Wait(); err != nil { + if errmsg := strings.TrimSpace(stderr.String()); errmsg != "" { + err = errors.Wrap(errors.New(errmsg), err.Error()) + } return err } return errors.Wrap(CreateSHA512File(outputFile), "failed to create .sha512 file") diff --git a/dev-tools/packaging/package_test.go b/dev-tools/packaging/package_test.go index d447e22ade7..cea1e9e9b69 100644 --- a/dev-tools/packaging/package_test.go +++ b/dev-tools/packaging/package_test.go @@ -80,7 +80,8 @@ func TestDeb(t *testing.T) { } func TestTar(t *testing.T) { - tars := getFiles(t, regexp.MustCompile(`\.tar\.gz$`)) + // Regexp matches *-arch.tar.gz, but not *-arch.docker.tar.gz + tars := getFiles(t, regexp.MustCompile(`-\w+\.tar\.gz$`)) for _, tar := range tars { checkTar(t, tar) } @@ -94,7 +95,7 @@ func TestZip(t *testing.T) { } func TestDocker(t *testing.T) { - dockers := getFiles(t, regexp.MustCompile(`\.docker.tar$`)) + dockers := getFiles(t, regexp.MustCompile(`\.docker\.tar\.gz$`)) for _, docker := range dockers { checkDocker(t, docker) } @@ -514,7 +515,13 @@ func readDocker(dockerFile string) (*packageFile, *dockerInfo, error) { var info *dockerInfo layers := make(map[string]*packageFile) - tarReader := tar.NewReader(file) + gzipReader, err := gzip.NewReader(file) + if err != nil { + return nil, nil, err + } + defer gzipReader.Close() + + tarReader := tar.NewReader(gzipReader) for { header, err := tarReader.Next() if err != nil {