mirror of
https://github.com/mudler/luet.git
synced 2025-09-03 08:14:46 +00:00
Move archive helpers to artifact
This allow in the future to swap and provide archive/compression methods without hijacking the code. Refers to #33
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
package compiler
|
package compiler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -148,6 +150,48 @@ func (a *PackageArtifact) SetPath(p string) {
|
|||||||
a.Path = p
|
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 {
|
type CopyJob struct {
|
||||||
Src, Dst string
|
Src, Dst string
|
||||||
Artifact string
|
Artifact string
|
||||||
|
@@ -339,13 +339,12 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage
|
|||||||
// strip from includes
|
// strip from includes
|
||||||
cs.stripIncludesFromRootfs(p.GetIncludes(), rootfs)
|
cs.stripIncludesFromRootfs(p.GetIncludes(), rootfs)
|
||||||
}
|
}
|
||||||
|
artifact = NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar"))
|
||||||
err = helpers.Tar(rootfs, p.Rel(p.GetPackage().GetFingerPrint()+".package.tar"))
|
err = artifact.Compress(rootfs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Error met while creating package archive")
|
return nil, errors.Wrap(err, "Error met while creating package archive")
|
||||||
}
|
}
|
||||||
|
|
||||||
artifact = NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar"))
|
|
||||||
artifact.SetCompileSpec(p)
|
artifact.SetCompileSpec(p)
|
||||||
} else {
|
} else {
|
||||||
Info(pkgTag, "Generating delta")
|
Info(pkgTag, "Generating delta")
|
||||||
@@ -404,8 +403,10 @@ func (cs *LuetCompiler) packageFromImage(p CompilationSpec, tag string, keepPerm
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Could not extract rootfs")
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Error met while creating package archive")
|
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")
|
Info(pkgTag, " :white_check_mark: Done")
|
||||||
artifact := NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar"))
|
|
||||||
artifact.SetCompileSpec(p)
|
|
||||||
err = artifact.WriteYaml(p.GetOutputPath())
|
err = artifact.WriteYaml(p.GetOutputPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return artifact, err
|
return artifact, err
|
||||||
|
@@ -62,6 +62,9 @@ type Artifact interface {
|
|||||||
SetCompileSpec(as CompilationSpec)
|
SetCompileSpec(as CompilationSpec)
|
||||||
GetCompileSpec() CompilationSpec
|
GetCompileSpec() CompilationSpec
|
||||||
WriteYaml(dst string) error
|
WriteYaml(dst string) error
|
||||||
|
Unpack(dst string, keepPerms bool) error
|
||||||
|
Compress(src string) error
|
||||||
|
FileList() ([]string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArtifactNode struct {
|
type ArtifactNode struct {
|
||||||
|
@@ -16,8 +16,6 @@
|
|||||||
package installer
|
package installer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"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 {
|
func (l *LuetInstaller) installPackage(a ArtifactMatch, s *System) error {
|
||||||
|
|
||||||
// FIXME: Implement
|
|
||||||
artifact, err := a.Repository.Client().DownloadArtifact(a.Artifact)
|
artifact, err := a.Repository.Client().DownloadArtifact(a.Artifact)
|
||||||
defer os.Remove(artifact.GetPath())
|
defer os.Remove(artifact.GetPath())
|
||||||
|
|
||||||
tarFile, err := os.Open(artifact.GetPath())
|
files, err := artifact.FileList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Could not open package archive")
|
return errors.Wrap(err, "Could not open package archive")
|
||||||
}
|
}
|
||||||
defer tarFile.Close()
|
|
||||||
tr := tar.NewReader(tarFile)
|
|
||||||
|
|
||||||
var files []string
|
err = artifact.Unpack(s.Target, true)
|
||||||
// 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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Error met while unpacking rootfs")
|
return errors.Wrap(err, "Error met while unpacking rootfs")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user