2019-11-10 21:40:31 +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/>.
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-12-24 14:34:02 +00:00
|
|
|
"fmt"
|
2019-11-16 10:51:30 +00:00
|
|
|
"io/ioutil"
|
2019-11-15 17:12:50 +00:00
|
|
|
"os"
|
|
|
|
|
2019-11-10 21:40:31 +00:00
|
|
|
"github.com/mudler/luet/pkg/compiler"
|
|
|
|
"github.com/mudler/luet/pkg/compiler/backend"
|
2019-12-29 15:33:10 +00:00
|
|
|
. "github.com/mudler/luet/pkg/config"
|
2019-11-10 21:40:31 +00:00
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
|
|
|
tree "github.com/mudler/luet/pkg/tree"
|
|
|
|
|
2019-12-29 15:33:10 +00:00
|
|
|
_gentoo "github.com/Sabayon/pkgs-checker/pkg/gentoo"
|
2019-11-10 21:40:31 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var buildCmd = &cobra.Command{
|
2019-11-12 21:17:23 +00:00
|
|
|
Use: "build <package name> <package name> <package name> ...",
|
2019-11-10 21:40:31 +00:00
|
|
|
Short: "build a package or a tree",
|
2019-11-12 21:17:23 +00:00
|
|
|
Long: `build packages or trees from luet tree definitions. Packages are in [category]/[name]-[version] form`,
|
2019-11-25 18:55:30 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
viper.BindPFlag("tree", cmd.Flags().Lookup("tree"))
|
|
|
|
viper.BindPFlag("destination", cmd.Flags().Lookup("destination"))
|
|
|
|
viper.BindPFlag("backend", cmd.Flags().Lookup("backend"))
|
|
|
|
viper.BindPFlag("privileged", cmd.Flags().Lookup("privileged"))
|
|
|
|
viper.BindPFlag("database", cmd.Flags().Lookup("database"))
|
|
|
|
viper.BindPFlag("revdeps", cmd.Flags().Lookup("revdeps"))
|
|
|
|
viper.BindPFlag("all", cmd.Flags().Lookup("all"))
|
2019-12-30 13:52:34 +00:00
|
|
|
viper.BindPFlag("compression", cmd.Flags().Lookup("compression"))
|
2019-11-25 18:55:30 +00:00
|
|
|
},
|
2019-11-10 21:40:31 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
src := viper.GetString("tree")
|
2019-11-25 18:55:30 +00:00
|
|
|
dst := viper.GetString("destination")
|
2019-12-29 15:33:10 +00:00
|
|
|
concurrency := LuetCfg.GetGeneral().Concurrency
|
2019-11-10 21:40:31 +00:00
|
|
|
backendType := viper.GetString("backend")
|
|
|
|
privileged := viper.GetBool("privileged")
|
2019-11-15 17:12:50 +00:00
|
|
|
revdeps := viper.GetBool("revdeps")
|
|
|
|
all := viper.GetBool("all")
|
2019-11-16 10:51:30 +00:00
|
|
|
databaseType := viper.GetString("database")
|
2019-12-30 13:52:34 +00:00
|
|
|
compressionType := viper.GetString("compression")
|
2019-11-10 21:40:31 +00:00
|
|
|
|
2019-11-15 18:31:56 +00:00
|
|
|
compilerSpecs := compiler.NewLuetCompilationspecs()
|
2019-11-10 21:40:31 +00:00
|
|
|
var compilerBackend compiler.CompilerBackend
|
2019-11-16 10:51:30 +00:00
|
|
|
var db pkg.PackageDatabase
|
2019-11-10 21:40:31 +00:00
|
|
|
switch backendType {
|
|
|
|
case "img":
|
|
|
|
compilerBackend = backend.NewSimpleImgBackend()
|
|
|
|
case "docker":
|
|
|
|
compilerBackend = backend.NewSimpleDockerBackend()
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:51:30 +00:00
|
|
|
switch databaseType {
|
|
|
|
case "memory":
|
|
|
|
db = pkg.NewInMemoryDatabase(false)
|
2019-11-29 18:01:53 +00:00
|
|
|
|
2019-11-16 10:51:30 +00:00
|
|
|
case "boltdb":
|
|
|
|
tmpdir, err := ioutil.TempDir("", "package")
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
db = pkg.NewBoltDatabase(tmpdir)
|
2019-11-29 18:01:53 +00:00
|
|
|
|
2019-11-16 10:51:30 +00:00
|
|
|
}
|
|
|
|
defer db.Clean()
|
|
|
|
|
|
|
|
generalRecipe := tree.NewCompilerRecipe(db)
|
2019-11-10 21:40:31 +00:00
|
|
|
|
|
|
|
Info("Loading", src)
|
2019-11-25 18:55:30 +00:00
|
|
|
Info("Building in", dst)
|
|
|
|
|
2019-11-10 21:40:31 +00:00
|
|
|
err := generalRecipe.Load(src)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("Error: " + err.Error())
|
|
|
|
}
|
2019-11-29 18:01:53 +00:00
|
|
|
luetCompiler := compiler.NewLuetCompiler(compilerBackend, generalRecipe.GetDatabase())
|
2019-12-30 13:52:34 +00:00
|
|
|
luetCompiler.SetConcurrency(concurrency)
|
|
|
|
luetCompiler.SetCompressionType(compiler.CompressionImplementation(compressionType))
|
2019-11-15 17:12:50 +00:00
|
|
|
if !all {
|
|
|
|
for _, a := range args {
|
2019-12-24 14:34:02 +00:00
|
|
|
gp, err := _gentoo.ParsePackageStr(a)
|
2019-11-15 17:12:50 +00:00
|
|
|
if err != nil {
|
2019-12-24 14:34:02 +00:00
|
|
|
Fatal("Invalid package string ", a, ": ", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-12-27 11:19:03 +00:00
|
|
|
if gp.Version == "" {
|
|
|
|
gp.Version = "0"
|
|
|
|
gp.Condition = _gentoo.PkgCondGreaterEqual
|
|
|
|
}
|
|
|
|
|
2019-12-24 14:34:02 +00:00
|
|
|
pack := &pkg.DefaultPackage{
|
|
|
|
Name: gp.Name,
|
2019-12-25 23:32:04 +00:00
|
|
|
Version: fmt.Sprintf("%s%s%s", gp.Condition.String(), gp.Version, gp.VersionSuffix),
|
2019-12-24 14:34:02 +00:00
|
|
|
Category: gp.Category,
|
|
|
|
Uri: make([]string, 0),
|
2019-11-15 17:12:50 +00:00
|
|
|
}
|
2019-12-24 14:34:02 +00:00
|
|
|
spec, err := luetCompiler.FromPackage(pack)
|
2019-11-15 17:12:50 +00:00
|
|
|
if err != nil {
|
|
|
|
Fatal("Error: " + err.Error())
|
|
|
|
}
|
2019-11-12 21:17:23 +00:00
|
|
|
|
2019-11-15 17:12:50 +00:00
|
|
|
spec.SetOutputPath(dst)
|
|
|
|
compilerSpecs.Add(spec)
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-29 18:01:53 +00:00
|
|
|
w := generalRecipe.GetDatabase().World()
|
|
|
|
|
2019-11-15 17:12:50 +00:00
|
|
|
for _, p := range w {
|
|
|
|
spec, err := luetCompiler.FromPackage(p)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("Error: " + err.Error())
|
|
|
|
}
|
2019-11-16 13:16:44 +00:00
|
|
|
Info(":package: Selecting ", p.GetName(), p.GetVersion())
|
2019-11-25 18:55:30 +00:00
|
|
|
spec.SetOutputPath(dst)
|
2019-11-15 17:12:50 +00:00
|
|
|
compilerSpecs.Add(spec)
|
|
|
|
}
|
2019-11-10 21:40:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 17:12:50 +00:00
|
|
|
var artifact []compiler.Artifact
|
|
|
|
var errs []error
|
|
|
|
if revdeps {
|
2019-12-30 13:52:34 +00:00
|
|
|
artifact, errs = luetCompiler.CompileWithReverseDeps(privileged, compilerSpecs)
|
2019-11-15 17:12:50 +00:00
|
|
|
|
|
|
|
} else {
|
2019-12-30 13:52:34 +00:00
|
|
|
artifact, errs = luetCompiler.CompileParallel(privileged, compilerSpecs)
|
2019-11-15 17:12:50 +00:00
|
|
|
|
|
|
|
}
|
2019-11-12 21:17:23 +00:00
|
|
|
if len(errs) != 0 {
|
|
|
|
for _, e := range errs {
|
|
|
|
Error("Error: " + e.Error())
|
|
|
|
}
|
|
|
|
Fatal("Bailing out")
|
|
|
|
}
|
|
|
|
for _, a := range artifact {
|
|
|
|
Info("Artifact generated:", a.GetPath())
|
|
|
|
}
|
2019-11-10 21:40:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
buildCmd.Flags().String("tree", path, "Source luet tree")
|
|
|
|
buildCmd.Flags().String("backend", "docker", "backend used (docker,img)")
|
|
|
|
buildCmd.Flags().Bool("privileged", false, "Privileged (Keep permissions)")
|
2019-11-16 10:51:30 +00:00
|
|
|
buildCmd.Flags().String("database", "memory", "database used for solving (memory,boltdb)")
|
2019-11-15 17:12:50 +00:00
|
|
|
buildCmd.Flags().Bool("revdeps", false, "Build with revdeps")
|
|
|
|
buildCmd.Flags().Bool("all", false, "Build all packages in the tree")
|
2019-11-25 18:55:30 +00:00
|
|
|
buildCmd.Flags().String("destination", path, "Destination folder")
|
2019-12-30 13:52:34 +00:00
|
|
|
buildCmd.Flags().String("compression", "none", "Compression alg: none, gzip")
|
2019-11-25 18:55:30 +00:00
|
|
|
|
2019-11-10 21:40:31 +00:00
|
|
|
RootCmd.AddCommand(buildCmd)
|
|
|
|
}
|