Skip to content

Commit

Permalink
actions: pack/unpack: use pigz for compression
Browse files Browse the repository at this point in the history
pigz does gzip compression on multiple cores and multiple processors
and there is improvement in compression time on multi core machines.
Use pigz for gzip compression if installed on the host machine.

Fixes: go-debos#218

Signed-off-by: Vignesh Raman <[email protected]>
  • Loading branch information
vigneshraman authored and obbardc committed Jul 11, 2023
1 parent 5412d8b commit 6bcd275
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
27 changes: 23 additions & 4 deletions actions/pack_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"log"
"path"
"strings"
"os/exec"

"github.com/go-debos/debos"
)
Expand Down Expand Up @@ -68,11 +69,29 @@ func (pf *PackAction) Verify(context *debos.DebosContext) error {

func (pf *PackAction) Run(context *debos.DebosContext) error {
pf.LogStart()
usePigz := false
if pf.Compression == "gz" {
if _,err := exec.LookPath("pigz"); err == nil {
usePigz = true
}
}
outfile := path.Join(context.Artifactdir, pf.File)

var tarOpt = "cf" + tarOpts[pf.Compression]
command := []string{"tar"}
if usePigz == true {
command = append(command, "cf")
} else {
command = append(command, "cf" + tarOpts[pf.Compression])
}
command = append(command, outfile)
command = append(command, "--xattrs")
command = append(command, "--xattrs-include=*.*")
if usePigz == true {
command = append(command, "--use-compress-program=pigz")
}
command = append(command, "-C", context.Rootdir)
command = append(command, ".")

log.Printf("Compressing to %s\n", outfile)
return debos.Command{}.Run("Packing", "tar", tarOpt, outfile,
"--xattrs", "--xattrs-include=*.*",
"-C", context.Rootdir, ".")
return debos.Command{}.Run("Packing", command...)
}
13 changes: 12 additions & 1 deletion archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"os/exec"
)

type ArchiveType int
Expand Down Expand Up @@ -92,6 +93,12 @@ func tarOptions(compression string) string {

func (tar *ArchiveTar) Unpack(destination string) error {
command := []string{"tar"}
usePigz := false
if compression, ok := tar.options["tarcompression"]; ok && compression == "gz" {
if _, err := exec.LookPath("pigz"); err == nil {
usePigz = true
}
}
if options, ok := tar.options["taroptions"].([]string); ok {
for _, option := range options {
command = append(command, option)
Expand All @@ -104,7 +111,11 @@ func (tar *ArchiveTar) Unpack(destination string) error {

if compression, ok := tar.options["tarcompression"]; ok {
if unpackTarOpt := tarOptions(compression.(string)); len(unpackTarOpt) > 0 {
command = append(command, unpackTarOpt)
if usePigz == true {
command = append(command, "--use-compress-program=pigz")
} else {
command = append(command, unpackTarOpt)
}
}
}
command = append(command, "-f", tar.file)
Expand Down

0 comments on commit 6bcd275

Please sign in to comment.