create-repo: annotate runtime definition in artifacts

This commit is contained in:
Ettore Di Giacinto
2021-09-17 14:28:58 +02:00
parent b73ac21004
commit fc45eae80a
7 changed files with 88 additions and 37 deletions

View File

@@ -63,6 +63,7 @@ type PackageArtifact struct {
CompressionType compression.Implementation `json:"compressiontype"` CompressionType compression.Implementation `json:"compressiontype"`
Files []string `json:"files"` Files []string `json:"files"`
PackageCacheImage string `json:"package_cacheimage"` PackageCacheImage string `json:"package_cacheimage"`
Runtime *pkg.DefaultPackage `json:"runtime,omitempty"`
} }
func (p *PackageArtifact) ShallowCopy() *PackageArtifact { func (p *PackageArtifact) ShallowCopy() *PackageArtifact {
@@ -108,12 +109,16 @@ func (a *PackageArtifact) WriteYaml(dst string) error {
return errors.Wrap(err, "Failed generating checksums for artifact") return errors.Wrap(err, "Failed generating checksums for artifact")
} }
//p := a.CompileSpec.GetPackage().GetPath() // Update runtime package information
if a.CompileSpec != nil && a.CompileSpec.Package != nil {
runtime, err := a.CompileSpec.Package.GetRuntimePackage()
if err != nil {
return errors.Wrapf(err, "getting runtime package for '%s'", a.CompileSpec.Package.HumanReadableString())
}
a.Runtime = runtime
}
//a.CompileSpec.GetPackage().SetPath("")
// for _, ass := range a.CompileSpec.GetSourceAssertion() {
// ass.Package.SetPath("")
// }
data, err := yaml.Marshal(a) data, err := yaml.Marshal(a)
if err != nil { if err != nil {
return errors.Wrap(err, "While marshalling for PackageArtifact YAML") return errors.Wrap(err, "While marshalling for PackageArtifact YAML")

View File

@@ -318,13 +318,17 @@ func GenerateRepository(p ...RepositoryOption) (*LuetSystemRepository, error) {
} }
if _, err := runtimeTree.FindPackage(art.CompileSpec.Package); err != nil && art.CompileSpec.Package.Name != "" { if _, err := runtimeTree.FindPackage(art.CompileSpec.Package); err != nil && art.CompileSpec.Package.Name != "" {
Debug("Adding", art.CompileSpec.Package.HumanReadableString(), "from metadata file", currentpath) Debug("Adding", art.CompileSpec.Package.HumanReadableString(), "from metadata file", currentpath)
// We don't have runtime at this point. So we import the package as is if art.Runtime != nil && art.Runtime.Name != "" {
r := []*pkg.DefaultPackage{} runtimeTree.CreatePackage(art.Runtime)
p := art.CompileSpec.Package.Clone() } else {
p.Requires(r) // We don't have runtime at this point. So we import the package as is
p.SetProvides(r) r := []*pkg.DefaultPackage{}
p.Conflicts(r) p := art.CompileSpec.Package.Clone()
runtimeTree.CreatePackage(p) p.Requires(r)
p.SetProvides(r)
p.Conflicts(r)
runtimeTree.CreatePackage(p)
}
} }
return nil return nil

View File

@@ -29,7 +29,7 @@ func (s *System) ExecuteFinalizers(packs []pkg.Package) error {
executedFinalizer := map[string]bool{} executedFinalizer := map[string]bool{}
for _, p := range packs { for _, p := range packs {
if fileHelper.Exists(p.Rel(tree.FinalizerFile)) { if fileHelper.Exists(p.Rel(tree.FinalizerFile)) {
out, err := helpers.RenderFiles(helpers.ChartFile(p.Rel(tree.FinalizerFile)), p.Rel(tree.DefinitionFile)) out, err := helpers.RenderFiles(helpers.ChartFile(p.Rel(tree.FinalizerFile)), p.Rel(pkg.PackageDefinitionFile))
if err != nil { if err != nil {
Warning("Failed rendering finalizer for ", p.HumanReadableString(), err.Error()) Warning("Failed rendering finalizer for ", p.HumanReadableString(), err.Error())
errs = multierror.Append(errs, err) errs = multierror.Append(errs, err)

View File

@@ -21,11 +21,14 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
fileHelper "github.com/mudler/luet/pkg/helpers/file"
"github.com/mudler/luet/pkg/helpers/docker" "github.com/mudler/luet/pkg/helpers/docker"
"github.com/mudler/luet/pkg/helpers/match" "github.com/mudler/luet/pkg/helpers/match"
version "github.com/mudler/luet/pkg/versioner" version "github.com/mudler/luet/pkg/versioner"
@@ -124,7 +127,11 @@ type Package interface {
JSON() ([]byte, error) JSON() ([]byte, error)
} }
const PackageMetaSuffix = "metadata.yaml" const (
PackageMetaSuffix = "metadata.yaml"
PackageCollectionFile = "collection.yaml"
PackageDefinitionFile = "definition.yaml"
)
type Tree interface { type Tree interface {
GetPackageSet() PackageDatabase GetPackageSet() PackageDatabase
@@ -215,11 +222,14 @@ func GetRawPackages(yml []byte) (rawPackages, error) {
return rawPackages.Packages, nil return rawPackages.Packages, nil
} }
func DefaultPackagesFromYaml(yml []byte) ([]DefaultPackage, error) {
var unescaped struct { type Collection struct {
Packages []DefaultPackage `json:"packages"` Packages []DefaultPackage `json:"packages"`
} }
func DefaultPackagesFromYAML(yml []byte) ([]DefaultPackage, error) {
var unescaped Collection
source, err := yaml.YAMLToJSON(yml) source, err := yaml.YAMLToJSON(yml)
if err != nil { if err != nil {
return []DefaultPackage{}, err return []DefaultPackage{}, err
@@ -381,6 +391,10 @@ func (p *DefaultPackage) MatchLabel(r *regexp.Regexp) bool {
return match.MapMatchRegex(&p.Labels, r) return match.MapMatchRegex(&p.Labels, r)
} }
func (p DefaultPackage) IsCollection() bool {
return fileHelper.Exists(filepath.Join(p.Path, PackageCollectionFile))
}
func (p *DefaultPackage) HasAnnotation(label string) bool { func (p *DefaultPackage) HasAnnotation(label string) bool {
return match.MapHasKey(&p.Annotations, label) return match.MapHasKey(&p.Annotations, label)
} }
@@ -703,6 +717,39 @@ func (set Packages) Unique() Packages {
return result return result
} }
func (p *DefaultPackage) GetRuntimePackage() (*DefaultPackage, error) {
var r *DefaultPackage
if p.IsCollection() {
collectionFile := filepath.Join(p.Path, PackageCollectionFile)
dat, err := ioutil.ReadFile(collectionFile)
if err != nil {
return r, errors.Wrapf(err, "failed while reading '%s'", collectionFile)
}
coll, err := DefaultPackagesFromYAML(dat)
if err != nil {
return r, errors.Wrapf(err, "failed while parsing YAML '%s'", collectionFile)
}
for _, c := range coll {
if c.Matches(p) {
r = &c
break
}
}
} else {
definitionFile := filepath.Join(p.Path, PackageDefinitionFile)
dat, err := ioutil.ReadFile(definitionFile)
if err != nil {
return r, errors.Wrapf(err, "failed while reading '%s'", definitionFile)
}
d, err := DefaultPackageFromYaml(dat)
if err != nil {
return r, errors.Wrapf(err, "failed while parsing YAML '%s'", definitionFile)
}
r = &d
}
return r, nil
}
func (pack *DefaultPackage) buildFormula(definitiondb PackageDatabase, db PackageDatabase, visited map[string]interface{}) ([]bf.Formula, error) { func (pack *DefaultPackage) buildFormula(definitiondb PackageDatabase, db PackageDatabase, visited map[string]interface{}) ([]bf.Formula, error) {
if _, ok := visited[pack.HumanReadableString()]; ok { if _, ok := visited[pack.HumanReadableString()]; ok {
return nil, nil return nil, nil

View File

@@ -90,12 +90,12 @@ func (r *CompilerRecipe) Load(path string) error {
return errors.Wrap(err, "Error on walk path "+currentpath) return errors.Wrap(err, "Error on walk path "+currentpath)
} }
if info.Name() != DefinitionFile && info.Name() != CollectionFile { if info.Name() != pkg.PackageDefinitionFile && info.Name() != pkg.PackageCollectionFile {
return nil // Skip with no errors return nil // Skip with no errors
} }
switch info.Name() { switch info.Name() {
case DefinitionFile: case pkg.PackageDefinitionFile:
pack, err := ReadDefinitionFile(currentpath) pack, err := ReadDefinitionFile(currentpath)
if err != nil { if err != nil {
@@ -130,14 +130,14 @@ func (r *CompilerRecipe) Load(path string) error {
return errors.Wrap(err, "Error creating package "+pack.GetName()) return errors.Wrap(err, "Error creating package "+pack.GetName())
} }
case CollectionFile: case pkg.PackageCollectionFile:
dat, err := ioutil.ReadFile(currentpath) dat, err := ioutil.ReadFile(currentpath)
if err != nil { if err != nil {
return errors.Wrap(err, "Error reading file "+currentpath) return errors.Wrap(err, "Error reading file "+currentpath)
} }
packs, err := pkg.DefaultPackagesFromYaml(dat) packs, err := pkg.DefaultPackagesFromYAML(dat)
if err != nil { if err != nil {
return errors.Wrap(err, "Error reading yaml "+currentpath) return errors.Wrap(err, "Error reading yaml "+currentpath)
} }

View File

@@ -56,7 +56,7 @@ func (r *InstallerRecipe) Save(path string) error {
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(filepath.Join(dir, DefinitionFile), data, 0644) err = ioutil.WriteFile(filepath.Join(dir, pkg.PackageDefinitionFile), data, 0644)
if err != nil { if err != nil {
return err return err
} }
@@ -86,7 +86,7 @@ func (r *InstallerRecipe) Load(path string) error {
// the function that handles each file or dir // the function that handles each file or dir
var ff = func(currentpath string, info os.FileInfo, err error) error { var ff = func(currentpath string, info os.FileInfo, err error) error {
if info.Name() != DefinitionFile && info.Name() != CollectionFile { if info.Name() != pkg.PackageDefinitionFile && info.Name() != pkg.PackageCollectionFile {
return nil // Skip with no errors return nil // Skip with no errors
} }
@@ -96,7 +96,7 @@ func (r *InstallerRecipe) Load(path string) error {
} }
switch info.Name() { switch info.Name() {
case DefinitionFile: case pkg.PackageDefinitionFile:
pack, err := pkg.DefaultPackageFromYaml(dat) pack, err := pkg.DefaultPackageFromYaml(dat)
if err != nil { if err != nil {
return errors.Wrap(err, "Error reading yaml "+currentpath) return errors.Wrap(err, "Error reading yaml "+currentpath)
@@ -109,8 +109,8 @@ func (r *InstallerRecipe) Load(path string) error {
return errors.Wrap(err, "Error creating package "+pack.GetName()) return errors.Wrap(err, "Error creating package "+pack.GetName())
} }
case CollectionFile: case pkg.PackageCollectionFile:
packs, err := pkg.DefaultPackagesFromYaml(dat) packs, err := pkg.DefaultPackagesFromYAML(dat)
if err != nil { if err != nil {
return errors.Wrap(err, "Error reading yaml "+currentpath) return errors.Wrap(err, "Error reading yaml "+currentpath)
} }

View File

@@ -33,11 +33,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
const (
DefinitionFile = "definition.yaml"
CollectionFile = "collection.yaml"
)
func NewGeneralRecipe(db pkg.PackageDatabase) Builder { return &Recipe{Database: db} } func NewGeneralRecipe(db pkg.PackageDatabase) Builder { return &Recipe{Database: db} }
// Recipe is the "general" reciper for Trees // Recipe is the "general" reciper for Trees
@@ -64,7 +59,7 @@ func (r *Recipe) Save(path string) error {
dir := filepath.Join(path, p.GetCategory(), p.GetName(), p.GetVersion()) dir := filepath.Join(path, p.GetCategory(), p.GetName(), p.GetVersion())
os.MkdirAll(dir, os.ModePerm) os.MkdirAll(dir, os.ModePerm)
err := WriteDefinitionFile(p, filepath.Join(dir, DefinitionFile)) err := WriteDefinitionFile(p, filepath.Join(dir, pkg.PackageDefinitionFile))
if err != nil { if err != nil {
return err return err
} }
@@ -96,7 +91,7 @@ func (r *Recipe) Load(path string) error {
// the function that handles each file or dir // the function that handles each file or dir
var ff = func(currentpath string, info os.FileInfo, err error) error { var ff = func(currentpath string, info os.FileInfo, err error) error {
if info.Name() != DefinitionFile && info.Name() != CollectionFile { if info.Name() != pkg.PackageDefinitionFile && info.Name() != pkg.PackageCollectionFile {
return nil // Skip with no errors return nil // Skip with no errors
} }
@@ -106,7 +101,7 @@ func (r *Recipe) Load(path string) error {
} }
switch info.Name() { switch info.Name() {
case DefinitionFile: case pkg.PackageDefinitionFile:
pack, err := pkg.DefaultPackageFromYaml(dat) pack, err := pkg.DefaultPackageFromYaml(dat)
if err != nil { if err != nil {
return errors.Wrap(err, "Error reading yaml "+currentpath) return errors.Wrap(err, "Error reading yaml "+currentpath)
@@ -118,8 +113,8 @@ func (r *Recipe) Load(path string) error {
if err != nil { if err != nil {
return errors.Wrap(err, "Error creating package "+pack.GetName()) return errors.Wrap(err, "Error creating package "+pack.GetName())
} }
case CollectionFile: case pkg.PackageCollectionFile:
packs, err := pkg.DefaultPackagesFromYaml(dat) packs, err := pkg.DefaultPackagesFromYAML(dat)
if err != nil { if err != nil {
return errors.Wrap(err, "Error reading yaml "+currentpath) return errors.Wrap(err, "Error reading yaml "+currentpath)
} }