cmd: implement manifest command

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`.
This commit is contained in:
Michael Vogt 2024-11-27 09:18:02 +01:00 committed by Simon de Vlieger
parent ea61ef593f
commit 830528fa15
5 changed files with 91 additions and 16 deletions

View file

@ -10,6 +10,7 @@ import (
var (
GetOneImage = getOneImage
Run = run
)
func MockOsArgs(new []string) (restore func()) {
@ -48,7 +49,3 @@ func MockNewRepoRegistry(f func() (*reporegistry.RepoRegistry, error)) (restore
newRepoRegistry = saved
}
}
var (
Run = run
)

View file

@ -16,6 +16,7 @@ func newImageFilterDefault(dataDir string) (*imagefilter.ImageFilter, error) {
if err != nil {
return nil, err
}
return imagefilter.New(fac, repos)
}

View file

@ -4,8 +4,8 @@ import (
"github.com/osbuild/images/pkg/imagefilter"
)
func listImages(output string, filterExprs []string, opts *cmdlineOpts) error {
imageFilter, err := newImageFilterDefault(opts.dataDir)
func listImages(dataDir, output string, filterExprs []string) error {
imageFilter, err := newImageFilterDefault(dataDir)
if err != nil {
return err
}
@ -19,7 +19,7 @@ func listImages(output string, filterExprs []string, opts *cmdlineOpts) error {
if err != nil {
return err
}
if err := fmter.Output(opts.out, filteredResult); err != nil {
if err := fmter.Output(osStdout, filteredResult); err != nil {
return err
}

View file

@ -7,6 +7,11 @@ import (
"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 (
@ -14,11 +19,6 @@ var (
osStderr io.Writer = os.Stderr
)
type cmdlineOpts struct {
dataDir string
out io.Writer
}
func cmdListImages(cmd *cobra.Command, args []string) error {
filter, err := cmd.Flags().GetStringArray("filter")
if err != nil {
@ -33,11 +33,40 @@ func cmdListImages(cmd *cobra.Command, args []string) error {
return err
}
opts := &cmdlineOpts{
out: osStdout,
dataDir: dataDir,
return listImages(dataDir, output, filter)
}
func cmdManifest(cmd *cobra.Command, args []string) error {
dataDir, err := cmd.Flags().GetString("datadir")
if err != nil {
return err
}
return listImages(output, filter, opts)
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 {
@ -70,6 +99,17 @@ operating sytsems like centos and RHEL with easy customizations support.`,
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()
}

View file

@ -3,6 +3,7 @@ package main_test
import (
"bytes"
"encoding/json"
"os"
"testing"
"github.com/sirupsen/logrus"
@ -11,6 +12,7 @@ import (
testrepos "github.com/osbuild/images/test/data/repositories"
"github.com/osbuild/image-builder-cli/cmd/image-builder"
"github.com/osbuild/image-builder-cli/internal/manifesttest"
)
func init() {
@ -123,3 +125,38 @@ func TestListImagesErrorsOnExtraArgs(t *testing.T) {
err := main.Run()
assert.EqualError(t, err, `unknown command "extra-arg" for "image-builder list-images"`)
}
func hasDepsolveDnf() bool {
// XXX: expose images/pkg/depsolve:findDepsolveDnf()
_, err := os.Stat("/usr/libexec/osbuild-depsolve-dnf")
return err == nil
}
// XXX: move to pytest like bib maybe?
func TestManifestIntegrationSmoke(t *testing.T) {
if testing.Short() {
t.Skip("manifest generation takes a while")
}
if !hasDepsolveDnf() {
t.Skip("no osbuild-depsolve-dnf binary found")
}
restore := main.MockNewRepoRegistry(testrepos.New)
defer restore()
restore = main.MockOsArgs([]string{
"manifest", "centos-9", "qcow2"},
)
defer restore()
var fakeStdout bytes.Buffer
restore = main.MockOsStdout(&fakeStdout)
defer restore()
err := main.Run()
assert.NoError(t, err)
pipelineNames, err := manifesttest.PipelineNamesFrom(fakeStdout.Bytes())
assert.NoError(t, err)
assert.Contains(t, pipelineNames, "qcow2")
}