2020-10-10 16:26:37 +00:00
|
|
|
// Copyright © 2020 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_database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
2021-08-03 14:48:29 +00:00
|
|
|
"github.com/mudler/luet/cmd/util"
|
2021-04-12 17:00:36 +00:00
|
|
|
artifact "github.com/mudler/luet/pkg/compiler/types/artifact"
|
|
|
|
|
2020-10-10 16:26:37 +00:00
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
|
|
|
|
|
|
|
. "github.com/mudler/luet/pkg/config"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewDatabaseCreateCommand() *cobra.Command {
|
|
|
|
var ans = &cobra.Command{
|
|
|
|
Use: "create <artifact_metadata1.yaml> <artifact_metadata1.yaml>",
|
|
|
|
Short: "Insert a package in the system DB",
|
2020-12-02 22:15:23 +00:00
|
|
|
Long: `Inserts a package in the system database:
|
|
|
|
|
|
|
|
$ luet database create foo.yaml
|
|
|
|
|
|
|
|
"luet database create" injects a package in the system database without actually installing it, use it with caution.
|
|
|
|
|
|
|
|
This commands takes multiple yaml input file representing package artifacts, that are usually generated while building packages.
|
|
|
|
|
|
|
|
The yaml must contain the package definition, and the file list at least.
|
|
|
|
|
|
|
|
For reference, inspect a "metadata.yaml" file generated while running "luet build"`,
|
|
|
|
Args: cobra.OnlyValidArgs,
|
2020-10-10 16:26:37 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
2021-08-03 14:48:29 +00:00
|
|
|
util.BindSystemFlags(cmd)
|
2020-10-10 16:26:37 +00:00
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
2021-08-03 14:48:29 +00:00
|
|
|
util.SetSystemConfig()
|
2020-12-19 16:23:59 +00:00
|
|
|
systemDB := LuetCfg.GetSystemDB()
|
2020-10-10 16:26:37 +00:00
|
|
|
|
|
|
|
for _, a := range args {
|
|
|
|
dat, err := ioutil.ReadFile(a)
|
|
|
|
if err != nil {
|
|
|
|
Fatal("Failed reading ", a, ": ", err.Error())
|
|
|
|
}
|
2021-04-12 17:00:36 +00:00
|
|
|
art, err := artifact.NewPackageArtifactFromYaml(dat)
|
2020-10-10 16:26:37 +00:00
|
|
|
if err != nil {
|
|
|
|
Fatal("Failed reading yaml ", a, ": ", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
files := art.Files
|
2020-10-10 16:26:37 +00:00
|
|
|
|
2021-06-25 11:09:32 +00:00
|
|
|
// Check if the package is already present
|
|
|
|
if p, err := systemDB.FindPackage(art.CompileSpec.GetPackage()); err == nil && p.GetName() != "" {
|
|
|
|
Fatal("Package", art.CompileSpec.GetPackage().HumanReadableString(),
|
|
|
|
" already present.")
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
if _, err := systemDB.CreatePackage(art.CompileSpec.GetPackage()); err != nil {
|
2020-10-10 16:26:37 +00:00
|
|
|
Fatal("Failed to create ", a, ": ", err.Error())
|
|
|
|
}
|
2021-04-12 17:00:36 +00:00
|
|
|
if err := systemDB.SetPackageFiles(&pkg.PackageFile{PackageFingerprint: art.CompileSpec.GetPackage().GetFingerPrint(), Files: files}); err != nil {
|
2020-10-10 16:26:37 +00:00
|
|
|
Fatal("Failed setting package files for ", a, ": ", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
Info(art.CompileSpec.GetPackage().HumanReadableString(), " created")
|
2020-10-10 16:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-06 09:21:49 +00:00
|
|
|
ans.Flags().String("system-dbpath", "", "System db path")
|
|
|
|
ans.Flags().String("system-target", "", "System rootpath")
|
|
|
|
ans.Flags().String("system-engine", "", "System DB engine")
|
|
|
|
|
2020-10-10 16:26:37 +00:00
|
|
|
return ans
|
|
|
|
}
|