2019-11-05 16:36:22 +00:00
|
|
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Recipe is a builder imeplementation.
|
|
|
|
|
|
|
|
// It reads a Tree and spit it in human readable form (YAML), called recipe,
|
|
|
|
// It also loads a tree (recipe) from a YAML (to a db, e.g. BoltDB), allowing to query it
|
|
|
|
// with the solver, using the package object.
|
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
2019-11-05 16:36:22 +00:00
|
|
|
"github.com/mudler/luet/pkg/helpers"
|
2021-06-01 14:43:31 +00:00
|
|
|
fileHelper "github.com/mudler/luet/pkg/helpers/file"
|
2019-11-05 16:36:22 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CompilerDefinitionFile = "build.yaml"
|
|
|
|
)
|
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
func NewCompilerRecipe(d types.PackageDatabase) Builder {
|
2019-11-15 23:38:07 +00:00
|
|
|
return &CompilerRecipe{Recipe: Recipe{Database: d}}
|
|
|
|
}
|
2019-11-05 16:36:22 +00:00
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
func ReadDefinitionFile(path string) (types.Package, error) {
|
|
|
|
empty := types.Package{}
|
2020-03-16 07:33:51 +00:00
|
|
|
dat, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return empty, errors.Wrap(err, "Error reading file "+path)
|
|
|
|
}
|
2022-01-06 22:57:56 +00:00
|
|
|
pack, err := types.PackageFromYaml(dat)
|
2020-03-16 07:33:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return empty, errors.Wrap(err, "Error reading yaml "+path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pack, nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:36:22 +00:00
|
|
|
// Recipe is the "general" reciper for Trees
|
|
|
|
type CompilerRecipe struct {
|
|
|
|
Recipe
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:22:00 +00:00
|
|
|
// CompilerRecipes copies tree 1:1 as they contain the specs
|
|
|
|
// and the build context required for reproducible builds
|
|
|
|
func (r *CompilerRecipe) Save(path string) error {
|
|
|
|
for _, p := range r.SourcePath {
|
2021-06-01 14:43:31 +00:00
|
|
|
if err := fileHelper.CopyDir(p, filepath.Join(path, filepath.Base(p))); err != nil {
|
2021-04-08 13:22:00 +00:00
|
|
|
return errors.Wrap(err, "while copying source tree")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:36:22 +00:00
|
|
|
func (r *CompilerRecipe) Load(path string) error {
|
|
|
|
|
2020-03-15 12:31:12 +00:00
|
|
|
r.SourcePath = append(r.SourcePath, path)
|
2019-11-10 09:48:07 +00:00
|
|
|
//tmpfile, err := ioutil.TempFile("", "luet")
|
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
2021-08-04 14:16:54 +00:00
|
|
|
c, err := helpers.ChartFiles([]string{filepath.Join(path, "templates")})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-05 16:36:22 +00:00
|
|
|
|
2019-11-10 09:48:07 +00:00
|
|
|
//r.Tree().SetPackageSet(pkg.NewBoltDatabase(tmpfile.Name()))
|
2019-11-05 16:36:22 +00:00
|
|
|
// TODO: Handle cleaning after? Cleanup implemented in GetPackageSet().Clean()
|
|
|
|
// the function that handles each file or dir
|
|
|
|
var ff = func(currentpath string, info os.FileInfo, err error) error {
|
|
|
|
|
2020-03-08 14:47:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error on walk path "+currentpath)
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
if info.Name() != types.PackageDefinitionFile && info.Name() != types.PackageCollectionFile {
|
2019-11-05 16:36:22 +00:00
|
|
|
return nil // Skip with no errors
|
|
|
|
}
|
|
|
|
|
2020-11-14 23:13:46 +00:00
|
|
|
switch info.Name() {
|
2022-01-06 22:57:56 +00:00
|
|
|
case types.PackageDefinitionFile:
|
2019-11-05 16:36:22 +00:00
|
|
|
|
2020-11-14 23:13:46 +00:00
|
|
|
pack, err := ReadDefinitionFile(currentpath)
|
2019-11-05 16:36:22 +00:00
|
|
|
if err != nil {
|
2020-11-14 23:13:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Path is set only internally when tree is loaded from disk
|
|
|
|
pack.SetPath(filepath.Dir(currentpath))
|
2021-04-14 14:49:43 +00:00
|
|
|
pack.SetTreeDir(path)
|
2020-11-14 23:13:46 +00:00
|
|
|
|
|
|
|
// Instead of rdeps, have a different tree for build deps.
|
|
|
|
compileDefPath := pack.Rel(CompilerDefinitionFile)
|
2021-06-01 14:43:31 +00:00
|
|
|
if fileHelper.Exists(compileDefPath) {
|
2021-08-04 14:16:54 +00:00
|
|
|
dat, err := helpers.RenderFiles(append(c, helpers.ChartFile(compileDefPath)...), currentpath)
|
2020-11-14 23:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err,
|
|
|
|
"Error templating file "+CompilerDefinitionFile+" from "+
|
|
|
|
filepath.Dir(currentpath))
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
packbuild, err := types.PackageFromYaml([]byte(dat))
|
2020-11-14 23:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err,
|
|
|
|
"Error reading yaml "+CompilerDefinitionFile+" from "+
|
|
|
|
filepath.Dir(currentpath))
|
|
|
|
}
|
|
|
|
pack.Requires(packbuild.GetRequires())
|
|
|
|
pack.Conflicts(packbuild.GetConflicts())
|
2019-11-05 16:36:22 +00:00
|
|
|
}
|
2020-11-14 23:13:46 +00:00
|
|
|
|
|
|
|
_, err = r.Database.CreatePackage(&pack)
|
2019-11-05 16:36:22 +00:00
|
|
|
if err != nil {
|
2020-11-14 23:13:46 +00:00
|
|
|
return errors.Wrap(err, "Error creating package "+pack.GetName())
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
case types.PackageCollectionFile:
|
2021-04-14 14:49:43 +00:00
|
|
|
|
2020-11-14 23:13:46 +00:00
|
|
|
dat, err := ioutil.ReadFile(currentpath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error reading file "+currentpath)
|
|
|
|
}
|
2021-04-14 14:49:43 +00:00
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
packs, err := types.PackagesFromYAML(dat)
|
2020-11-14 23:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error reading yaml "+currentpath)
|
|
|
|
}
|
2021-04-14 14:49:43 +00:00
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
packsRaw, err := types.GetRawPackages(dat)
|
2021-04-14 14:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error reading raw packages from "+currentpath)
|
|
|
|
}
|
2020-11-14 23:13:46 +00:00
|
|
|
|
|
|
|
for _, pack := range packs {
|
|
|
|
pack.SetPath(filepath.Dir(currentpath))
|
2021-04-14 14:49:43 +00:00
|
|
|
pack.SetTreeDir(path)
|
2020-11-14 23:13:46 +00:00
|
|
|
|
|
|
|
// Instead of rdeps, have a different tree for build deps.
|
|
|
|
compileDefPath := pack.Rel(CompilerDefinitionFile)
|
2021-06-01 14:43:31 +00:00
|
|
|
if fileHelper.Exists(compileDefPath) {
|
2020-11-14 23:13:46 +00:00
|
|
|
|
|
|
|
raw := packsRaw.Find(pack.GetName(), pack.GetCategory(), pack.GetVersion())
|
|
|
|
buildyaml, err := ioutil.ReadFile(compileDefPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error reading file "+currentpath)
|
|
|
|
}
|
2021-08-04 14:16:54 +00:00
|
|
|
dat, err := helpers.RenderHelm(append(c, helpers.ChartFileB(buildyaml)...), raw, map[string]interface{}{})
|
2020-11-14 23:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err,
|
|
|
|
"Error templating file "+CompilerDefinitionFile+" from "+
|
|
|
|
filepath.Dir(currentpath))
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
packbuild, err := types.PackageFromYaml([]byte(dat))
|
2020-11-14 23:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err,
|
|
|
|
"Error reading yaml "+CompilerDefinitionFile+" from "+
|
|
|
|
filepath.Dir(currentpath))
|
|
|
|
}
|
|
|
|
pack.Requires(packbuild.GetRequires())
|
2021-11-24 20:59:43 +00:00
|
|
|
|
2020-11-14 23:13:46 +00:00
|
|
|
pack.Conflicts(packbuild.GetConflicts())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.Database.CreatePackage(&pack)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error creating package "+pack.GetName())
|
|
|
|
}
|
2019-11-05 16:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:16:54 +00:00
|
|
|
err = filepath.Walk(path, ff)
|
2019-11-05 16:36:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-11 23:13:03 +00:00
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
func (r *CompilerRecipe) GetDatabase() types.PackageDatabase { return r.Database }
|
|
|
|
func (r *CompilerRecipe) WithDatabase(d types.PackageDatabase) { r.Database = d }
|
|
|
|
func (r *CompilerRecipe) GetSourcePath() []string { return r.SourcePath }
|