Skip to content

Commit

Permalink
Compress docker artifacts with gzip (#9223)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano authored Nov 26, 2018
1 parent 959343f commit 675f2b0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
42 changes: 40 additions & 2 deletions dev-tools/mage/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
package mage

import (
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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")
Expand Down
13 changes: 10 additions & 3 deletions dev-tools/packaging/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 675f2b0

Please sign in to comment.