This commit implements the `manifest` command for `image-builder`.
It will generate an osbuild manifest based on the given inputs,
e.g.:
```
$ ./image-builder manifest centos-9 qcow2
{"version":"2","pipelines":[{"name":"build","runner":",...
```
Note that there is an integration test but because of the depsolve
it will be slow. It will be skipped when doing `go test -short`.
121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/osbuild/images/pkg/arch"
|
|
"github.com/osbuild/images/pkg/blueprint"
|
|
|
|
"github.com/osbuild/image-builder-cli/internal/manifestgen"
|
|
)
|
|
|
|
var (
|
|
osStdout io.Writer = os.Stdout
|
|
osStderr io.Writer = os.Stderr
|
|
)
|
|
|
|
func cmdListImages(cmd *cobra.Command, args []string) error {
|
|
filter, err := cmd.Flags().GetStringArray("filter")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
output, err := cmd.Flags().GetString("output")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dataDir, err := cmd.Flags().GetString("datadir")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return listImages(dataDir, output, filter)
|
|
}
|
|
|
|
func cmdManifest(cmd *cobra.Command, args []string) error {
|
|
dataDir, err := cmd.Flags().GetString("datadir")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
distroStr := args[0]
|
|
imgTypeStr := args[1]
|
|
var archStr string
|
|
if len(args) > 2 {
|
|
archStr = args[2]
|
|
} else {
|
|
archStr = arch.Current().String()
|
|
}
|
|
res, err := getOneImage(dataDir, distroStr, imgTypeStr, archStr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
repos, err := newRepoRegistry(dataDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// XXX: add --rpmmd/cachedir option like bib
|
|
mg, err := manifestgen.New(repos, &manifestgen.Options{
|
|
Output: osStdout,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var bp blueprint.Blueprint
|
|
return mg.Generate(&bp, res.Distro, res.ImgType, res.Arch, nil)
|
|
}
|
|
|
|
func run() error {
|
|
// images logs a bunch of stuff to Debug/Info that is distracting
|
|
// the user (at least by default, like what repos being loaded)
|
|
logrus.SetLevel(logrus.WarnLevel)
|
|
|
|
rootCmd := &cobra.Command{
|
|
Use: "image-builder",
|
|
Short: "Build operating system images from a given distro/image-type/blueprint",
|
|
Long: `Build operating system images from a given distribution,
|
|
image-type and blueprint.
|
|
|
|
Image-builder builds operating system images for a range of predefined
|
|
operating sytsems like centos and RHEL with easy customizations support.`,
|
|
SilenceErrors: true,
|
|
}
|
|
rootCmd.PersistentFlags().String("datadir", "", `Override the default data direcotry for e.g. custom repositories/*.json data`)
|
|
rootCmd.SetOut(osStdout)
|
|
rootCmd.SetErr(osStderr)
|
|
|
|
listImagesCmd := &cobra.Command{
|
|
Use: "list-images",
|
|
Short: "List buildable images, use --filter to limit further",
|
|
RunE: cmdListImages,
|
|
SilenceUsage: true,
|
|
Args: cobra.NoArgs,
|
|
}
|
|
listImagesCmd.Flags().StringArray("filter", nil, `Filter distributions by a specific criteria (e.g. "type:rhel*")`)
|
|
listImagesCmd.Flags().String("output", "", "Output in a specific format (text, json)")
|
|
rootCmd.AddCommand(listImagesCmd)
|
|
|
|
manifestCmd := &cobra.Command{
|
|
Use: "manifest <distro> <image-type> [<arch>]",
|
|
Short: "Build manifest for the given distro/image-type, e.g. centos-9 qcow2",
|
|
RunE: cmdManifest,
|
|
SilenceUsage: true,
|
|
Args: cobra.MinimumNArgs(2),
|
|
Hidden: true,
|
|
}
|
|
// XXX: add blueprint switch
|
|
rootCmd.AddCommand(manifestCmd)
|
|
|
|
return rootCmd.Execute()
|
|
}
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintf(osStderr, "error: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|