Annotate artifact metadata after compiling

This commit is contained in:
Ettore Di Giacinto
2019-11-22 21:01:29 +01:00
parent d134690560
commit df78308e98
7 changed files with 69 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ package compiler
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
@@ -26,23 +27,64 @@ import (
. "github.com/mudler/luet/pkg/logger"
"github.com/mudler/luet/pkg/solver"
yaml "gopkg.in/yaml.v2"
"github.com/mudler/luet/pkg/helpers"
"github.com/pkg/errors"
)
type PackageArtifact struct {
Path string
Dependencies []Artifact
CompileSpec CompilationSpec
type ArtifactIndex []Artifact
SourceAssertion solver.PackagesAssertions
func (i ArtifactIndex) CleanPath() ArtifactIndex {
var newIndex []Artifact
copy(newIndex, i)
for _, n := range newIndex {
n.SetPath(path.Base(n.GetPath()))
}
return newIndex
//Update if exists, otherwise just create
}
// When compiling, we write also a fingerprint.metadata.yaml file with PackageArtifact. In this way we can have another command to create the repository
// which will consist in just of an repository.yaml which is just the repository structure with the list of package artifact.
// In this way a generic client can fetch the packages and, after unpacking the tree, performing queries to install packages.
type PackageArtifact struct {
Path string `json:"path"`
Dependencies []Artifact `json:"dependencies"`
CompileSpec CompilationSpec `json:"compilationspec"`
SourceAssertion solver.PackagesAssertions `json:"-"`
}
func NewPackageArtifact(path string) Artifact {
return &PackageArtifact{Path: path, Dependencies: []Artifact{}}
}
func NewPackageArtifactFromYaml(data []byte) (Artifact, error) {
var p PackageArtifact
err := yaml.Unmarshal(data, &p)
if err != nil {
return &p, err
}
return &p, err
}
func (a *PackageArtifact) WriteYaml(dst string) error {
a.CompileSpec.GetPackage().SetPath("")
for _, ass := range a.CompileSpec.GetSourceAssertion() {
ass.Package.SetPath("")
}
data, err := yaml.Marshal(a)
if err != nil {
return errors.Wrap(err, "While marshalling for PackageArtifact YAML")
}
err = ioutil.WriteFile(filepath.Join(dst, a.GetCompileSpec().GetPackage().GetFingerPrint()+".metadata.yaml"), data, os.ModePerm)
if err != nil {
return errors.Wrap(err, "While writing PackageArtifact YAML")
}
return nil
}
func (a *PackageArtifact) GetSourceAssertion() solver.PackagesAssertions {
return a.SourceAssertion
}

View File

@@ -276,6 +276,10 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage
artifact.SetCompileSpec(p)
}
err = artifact.WriteYaml(p.GetOutputPath())
if err != nil {
return artifact, err
}
return artifact, nil
}
@@ -332,6 +336,10 @@ 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
}
return artifact, nil
}

View File

@@ -292,6 +292,8 @@ var _ = Describe("Compiler", func() {
Expect(helpers.Exists(spec.Rel("usr/bin/pkgs-checker"))).To(BeTrue())
Expect(helpers.Exists(spec.Rel("base-layer-0.1.package.tar"))).To(BeTrue())
Expect(helpers.Exists(spec.Rel("base-layer-0.1.metadata.yaml"))).To(BeTrue())
Expect(helpers.Exists(spec.Rel("extra-layer-0.1.metadata.yaml"))).To(BeTrue())
Expect(helpers.Exists(spec.Rel("extra-layer-0.1.package.tar"))).To(BeTrue())
})

View File

@@ -62,6 +62,7 @@ type Artifact interface {
SetCompileSpec(as CompilationSpec)
GetCompileSpec() CompilationSpec
WriteYaml(dst string) error
}
type ArtifactNode struct {

View File

@@ -25,4 +25,6 @@ type Builder interface {
Load(string) error // A tree might be loaded from a db (e.g. bolt) and written to folder
Tree() pkg.Tree // generates world
WithTree(pkg.Tree)
GetSourcePath() string
}

View File

@@ -48,7 +48,7 @@ func (r *CompilerRecipe) Load(path string) error {
if r.Tree() == nil {
r.PackageTree = NewDefaultTree()
}
r.SourcePath = path
//tmpfile, err := ioutil.TempFile("", "luet")
//if err != nil {
// return err
@@ -108,3 +108,4 @@ func (r *CompilerRecipe) Load(path string) error {
func (r *CompilerRecipe) Tree() pkg.Tree { return r.PackageTree }
func (r *CompilerRecipe) WithTree(t pkg.Tree) { r.PackageTree = t }
func (r *CompilerRecipe) GetSourcePath() string { return r.SourcePath }

View File

@@ -38,6 +38,7 @@ func NewGeneralRecipe(db pkg.PackageDatabase) Builder { return &Recipe{Database:
// Recipe is the "general" reciper for Trees
type Recipe struct {
PackageTree pkg.Tree
SourcePath string
Database pkg.PackageDatabase
}
@@ -75,6 +76,7 @@ func (r *Recipe) Load(path string) error {
// return err
// }
r.Tree().SetPackageSet(r.Database)
r.SourcePath = path
//r.Tree().SetPackageSet(pkg.NewBoltDatabase(tmpfile.Name()))
// TODO: Handle cleaning after? Cleanup implemented in GetPackageSet().Clean()
@@ -114,3 +116,4 @@ func (r *Recipe) Load(path string) error {
func (r *Recipe) Tree() pkg.Tree { return r.PackageTree }
func (r *Recipe) WithTree(t pkg.Tree) { r.PackageTree = t }
func (r *Recipe) GetSourcePath() string { return r.SourcePath }