From 2fa9c754ae3865bfe44cf02b6ce751f79c8c4dca Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sat, 28 Dec 2019 16:32:32 +0100 Subject: [PATCH] Move archive helpers to artifact This allow in the future to swap and provide archive/compression methods without hijacking the code. Refers to #33 --- pkg/compiler/artifact.go | 44 ++++++++++++++++++++++++++++++++++++++ pkg/compiler/compiler.go | 12 +++++------ pkg/compiler/interface.go | 3 +++ pkg/installer/installer.go | 30 ++------------------------ 4 files changed, 55 insertions(+), 34 deletions(-) diff --git a/pkg/compiler/artifact.go b/pkg/compiler/artifact.go index d15a5339..5255b7fa 100644 --- a/pkg/compiler/artifact.go +++ b/pkg/compiler/artifact.go @@ -16,6 +16,8 @@ package compiler import ( + "archive/tar" + "io" "io/ioutil" "os" "path" @@ -148,6 +150,48 @@ func (a *PackageArtifact) SetPath(p string) { a.Path = p } +// Compress Archives and compress (TODO) to the artifact path +func (a *PackageArtifact) Compress(src string) error { + return helpers.Tar(src, a.Path) +} + +// Unpack Untar and decompress (TODO) to the given path +func (a *PackageArtifact) Unpack(dst string, keepPerms bool) error { + return helpers.Untar(a.GetPath(), dst, keepPerms) +} + +func (a *PackageArtifact) FileList() ([]string, error) { + + tarFile, err := os.Open(a.GetPath()) + if err != nil { + return []string{}, errors.Wrap(err, "Could not open package archive") + } + defer tarFile.Close() + tr := tar.NewReader(tarFile) + + var files []string + // untar each segment + for { + hdr, err := tr.Next() + if err == io.EOF { + break + } + if err != nil { + return []string{}, err + } + // determine proper file path info + finfo := hdr.FileInfo() + fileName := hdr.Name + if finfo.Mode().IsDir() { + continue + } + files = append(files, fileName) + + // if a dir, create it, then go to next segment + } + return files, nil +} + type CopyJob struct { Src, Dst string Artifact string diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 616693a9..cdc93418 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -339,13 +339,12 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage // strip from includes cs.stripIncludesFromRootfs(p.GetIncludes(), rootfs) } - - err = helpers.Tar(rootfs, p.Rel(p.GetPackage().GetFingerPrint()+".package.tar")) + artifact = NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar")) + err = artifact.Compress(rootfs) if err != nil { return nil, errors.Wrap(err, "Error met while creating package archive") } - artifact = NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar")) artifact.SetCompileSpec(p) } else { Info(pkgTag, "Generating delta") @@ -404,8 +403,10 @@ func (cs *LuetCompiler) packageFromImage(p CompilationSpec, tag string, keepPerm if err != nil { return nil, errors.Wrap(err, "Could not extract rootfs") } + artifact := NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar")) + artifact.SetCompileSpec(p) - err = helpers.Tar(rootfs, p.Rel(p.GetPackage().GetFingerPrint()+".package.tar")) + err = artifact.Compress(rootfs) if err != nil { return nil, errors.Wrap(err, "Error met while creating package archive") } @@ -422,8 +423,7 @@ func (cs *LuetCompiler) packageFromImage(p CompilationSpec, tag string, keepPerm } Info(pkgTag, " :white_check_mark: Done") - artifact := NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar")) - artifact.SetCompileSpec(p) + err = artifact.WriteYaml(p.GetOutputPath()) if err != nil { return artifact, err diff --git a/pkg/compiler/interface.go b/pkg/compiler/interface.go index 9dde5145..85a31a37 100644 --- a/pkg/compiler/interface.go +++ b/pkg/compiler/interface.go @@ -62,6 +62,9 @@ type Artifact interface { SetCompileSpec(as CompilationSpec) GetCompileSpec() CompilationSpec WriteYaml(dst string) error + Unpack(dst string, keepPerms bool) error + Compress(src string) error + FileList() ([]string, error) } type ArtifactNode struct { diff --git a/pkg/installer/installer.go b/pkg/installer/installer.go index 79edc40f..f3bd6c44 100644 --- a/pkg/installer/installer.go +++ b/pkg/installer/installer.go @@ -16,8 +16,6 @@ package installer import ( - "archive/tar" - "io" "io/ioutil" "os" "os/exec" @@ -264,39 +262,15 @@ func (l *LuetInstaller) Install(p []pkg.Package, s *System) error { func (l *LuetInstaller) installPackage(a ArtifactMatch, s *System) error { - // FIXME: Implement artifact, err := a.Repository.Client().DownloadArtifact(a.Artifact) defer os.Remove(artifact.GetPath()) - tarFile, err := os.Open(artifact.GetPath()) + files, err := artifact.FileList() if err != nil { return errors.Wrap(err, "Could not open package archive") } - defer tarFile.Close() - tr := tar.NewReader(tarFile) - var files []string - // untar each segment - for { - hdr, err := tr.Next() - if err == io.EOF { - break - } - if err != nil { - return err - } - // determine proper file path info - finfo := hdr.FileInfo() - fileName := hdr.Name - if finfo.Mode().IsDir() { - continue - } - files = append(files, fileName) - - // if a dir, create it, then go to next segment - } - - err = helpers.Untar(artifact.GetPath(), s.Target, true) + err = artifact.Unpack(s.Target, true) if err != nil { return errors.Wrap(err, "Error met while unpacking rootfs") }