2020-11-14 13:46:16 +00:00
|
|
|
// Copyright © 2020 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
// Daniele Rondina <geaaru@sabayonlinux.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/>.
|
|
|
|
|
|
|
|
package cmd_tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-18 09:40:41 +00:00
|
|
|
"os"
|
2020-11-14 13:46:16 +00:00
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
helpers "github.com/mudler/luet/cmd/helpers"
|
2021-08-09 10:57:36 +00:00
|
|
|
"github.com/mudler/luet/cmd/util"
|
2022-01-06 22:57:56 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
2020-11-14 13:46:16 +00:00
|
|
|
"github.com/mudler/luet/pkg/compiler"
|
|
|
|
"github.com/mudler/luet/pkg/compiler/backend"
|
2022-01-09 10:58:13 +00:00
|
|
|
"github.com/mudler/luet/pkg/installer"
|
2021-10-20 22:13:02 +00:00
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
pkg "github.com/mudler/luet/pkg/database"
|
2020-11-14 13:46:16 +00:00
|
|
|
tree "github.com/mudler/luet/pkg/tree"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewTreeImageCommand() *cobra.Command {
|
|
|
|
|
|
|
|
var ans = &cobra.Command{
|
2021-12-17 14:21:03 +00:00
|
|
|
Use: "images [OPTIONS]",
|
|
|
|
// Skip processing output
|
|
|
|
Annotations: map[string]string{
|
|
|
|
util.CommandProcessOutput: "",
|
|
|
|
},
|
2020-11-14 13:46:16 +00:00
|
|
|
Short: "List of the images of a package",
|
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
t, _ := cmd.Flags().GetStringArray("tree")
|
|
|
|
if len(t) == 0 {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Mandatory tree param missing.")
|
2020-11-14 13:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Expects one package as parameter")
|
2020-11-14 13:46:16 +00:00
|
|
|
}
|
2021-08-09 10:57:36 +00:00
|
|
|
util.BindValuesFlags(cmd)
|
2020-11-14 13:46:16 +00:00
|
|
|
viper.BindPFlag("image-repository", cmd.Flags().Lookup("image-repository"))
|
|
|
|
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var results TreeResults
|
|
|
|
|
|
|
|
treePath, _ := cmd.Flags().GetStringArray("tree")
|
|
|
|
imageRepository := viper.GetString("image-repository")
|
2021-04-07 13:54:38 +00:00
|
|
|
pullRepo, _ := cmd.Flags().GetStringArray("pull-repository")
|
2021-08-09 10:57:36 +00:00
|
|
|
values := util.ValuesFlags()
|
2020-11-14 13:46:16 +00:00
|
|
|
out, _ := cmd.Flags().GetString("output")
|
|
|
|
reciper := tree.NewCompilerRecipe(pkg.NewInMemoryDatabase(false))
|
|
|
|
|
|
|
|
for _, t := range treePath {
|
|
|
|
err := reciper.Load(t)
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Error on load tree ", err)
|
2020-11-14 13:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-20 22:13:02 +00:00
|
|
|
compilerBackend := backend.NewSimpleDockerBackend(util.DefaultContext)
|
2020-11-14 13:46:16 +00:00
|
|
|
|
2021-12-17 14:21:03 +00:00
|
|
|
opts := util.DefaultContext.Config.Solver
|
2022-01-06 22:57:56 +00:00
|
|
|
opts.SolverOptions = types.SolverOptions{Type: types.SolverSingleCoreSimple, Concurrency: 1}
|
2021-04-12 17:00:36 +00:00
|
|
|
luetCompiler := compiler.NewLuetCompiler(
|
|
|
|
compilerBackend,
|
|
|
|
reciper.GetDatabase(),
|
2022-04-26 17:24:31 +00:00
|
|
|
compiler.WithBuildValues(values),
|
|
|
|
compiler.WithContext(util.DefaultContext),
|
|
|
|
compiler.WithPushRepository(imageRepository),
|
|
|
|
compiler.WithPullRepositories(pullRepo),
|
|
|
|
compiler.WithTemplateFolder(util.TemplateFolders(util.DefaultContext, installer.BuildTreeResult{}, treePath)),
|
|
|
|
compiler.WithSolverOptions(opts),
|
2021-04-12 17:00:36 +00:00
|
|
|
)
|
2020-11-14 13:46:16 +00:00
|
|
|
|
|
|
|
a := args[0]
|
|
|
|
|
|
|
|
pack, err := helpers.ParsePackageStr(a)
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Invalid package string ", a, ": ", err.Error())
|
2020-11-14 13:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spec, err := luetCompiler.FromPackage(pack)
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Error: " + err.Error())
|
2020-11-14 13:46:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:43:14 +00:00
|
|
|
ht := compiler.NewHashTree(reciper.GetDatabase())
|
2021-12-15 17:38:44 +00:00
|
|
|
|
|
|
|
copy, err := compiler.CompilerFinalImages(luetCompiler)
|
|
|
|
if err != nil {
|
|
|
|
util.DefaultContext.Fatal("Error: " + err.Error())
|
|
|
|
}
|
|
|
|
hashtree, err := ht.Query(copy, spec)
|
2021-05-19 15:43:14 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Error: " + err.Error())
|
2021-05-19 15:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, assertion := range hashtree.Solution { //highly dependent on the order
|
2020-11-14 13:46:16 +00:00
|
|
|
|
|
|
|
//buildImageHash := imageRepository + ":" + assertion.Hash.BuildHash
|
|
|
|
currentPackageImageHash := imageRepository + ":" + assertion.Hash.PackageHash
|
|
|
|
|
|
|
|
results.Packages = append(results.Packages, TreePackageResult{
|
|
|
|
Name: assertion.Package.GetName(),
|
|
|
|
Version: assertion.Package.GetVersion(),
|
|
|
|
Category: assertion.Package.GetCategory(),
|
|
|
|
Image: currentPackageImageHash,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
y, err := yaml.Marshal(results)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("err: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch out {
|
|
|
|
case "yaml":
|
|
|
|
fmt.Println(string(y))
|
|
|
|
case "json":
|
|
|
|
j2, err := yaml.YAMLToJSON(y)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("err: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(string(j2))
|
|
|
|
default:
|
|
|
|
for _, p := range results.Packages {
|
|
|
|
fmt.Println(fmt.Sprintf("%s/%s-%s: %s", p.Category, p.Name, p.Version, p.Image))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-01-18 09:40:41 +00:00
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal(err)
|
2021-01-18 09:40:41 +00:00
|
|
|
}
|
2020-11-14 13:46:16 +00:00
|
|
|
ans.Flags().StringP("output", "o", "terminal", "Output format ( Defaults: terminal, available: json,yaml )")
|
2021-01-18 09:40:41 +00:00
|
|
|
ans.Flags().StringArrayP("tree", "t", []string{path}, "Path of the tree to use.")
|
2020-11-14 13:46:16 +00:00
|
|
|
ans.Flags().String("image-repository", "luet/cache", "Default base image string for generated image")
|
2021-04-07 13:54:38 +00:00
|
|
|
ans.Flags().StringArrayP("pull-repository", "p", []string{}, "A list of repositories to pull the cache from")
|
2020-11-14 13:46:16 +00:00
|
|
|
|
|
|
|
return ans
|
|
|
|
}
|