-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a new `describe-image` comamnd that contains the details about the given image type. The output is yaml as it is both nicely human readable and also machine readable. The output looks like this: ```yaml $ ./image-builder describe-image rhel-9.1 tar distro: rhel-9.1 type: tar arch: x86_64 os_vesion: "9.1" bootmode: none partition_type: "" default_filename: root.tar.xz packages: include: - policycoreutils - selinux-policy-targeted - selinux-policy-targeted exclude: - rng-tools ``` Thanks to Ondrej Budai for the idea and the example.
- Loading branch information
Showing
5 changed files
with
191 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"slices" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/osbuild/images/pkg/blueprint" | ||
"github.com/osbuild/images/pkg/distro" | ||
"github.com/osbuild/images/pkg/imagefilter" | ||
) | ||
|
||
// Use yaml output by default because it is both nicely human and | ||
// machine readable and parts of our image defintions will be written | ||
// in yaml too. This means this should be a possible input a | ||
// "flattended" image definiton. | ||
type describeImgYAML struct { | ||
Distro string `yaml:"distro"` | ||
Type string `yaml:"type"` | ||
Arch string `yaml:"arch"` | ||
|
||
// XXX: think about ordering (as this is what the user will see) | ||
OsVersion string `yaml:"os_vesion"` | ||
|
||
Bootmode string `yaml:"bootmode"` | ||
PartitionType string `yaml:"partition_type"` | ||
DefaultFilename string `yaml:"default_filename"` | ||
|
||
// XXX: add pipelines here? maybe at least exports? | ||
Packages *packagesYAML `yaml:"packages"` | ||
} | ||
|
||
type packagesYAML struct { | ||
Include []string `yaml:"include"` | ||
Exclude []string `yaml:"exclude"` | ||
} | ||
|
||
func packageSetsFor(imgType distro.ImageType) (inc, exc []string, err error) { | ||
var bp blueprint.Blueprint | ||
manifest, _, err := imgType.Manifest(&bp, distro.ImageOptions{}, nil, 0) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// XXX: or should this just do what osbuild-package-sets does | ||
// and inlcude what pipeline needs the package set too? | ||
for pipelineName, pkgSets := range manifest.GetPackageSetChains() { | ||
// XXX: or shouldn't we exclude the build pipeline here? | ||
if pipelineName == "build" { | ||
continue | ||
} | ||
for _, pkgSet := range pkgSets { | ||
inc = append(inc, pkgSet.Include...) | ||
exc = append(exc, pkgSet.Exclude...) | ||
} | ||
} | ||
slices.Sort(inc) | ||
slices.Sort(exc) | ||
return inc, exc, nil | ||
} | ||
|
||
// XXX: should this live in images instead? | ||
func describeImage(img *imagefilter.Result, out io.Writer) error { | ||
// see | ||
// https://github.com/osbuild/images/pull/1019#discussion_r1832376568 | ||
// for what is available on an image (without depsolve or partitioning) | ||
inc, exc, err := packageSetsFor(img.ImgType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outYaml := &describeImgYAML{ | ||
Distro: img.Distro.Name(), | ||
OsVersion: img.Distro.OsVersion(), | ||
Arch: img.Arch.Name(), | ||
Type: img.ImgType.Name(), | ||
Bootmode: img.ImgType.BootMode().String(), | ||
PartitionType: img.ImgType.PartitionType().String(), | ||
DefaultFilename: img.ImgType.Filename(), | ||
Packages: &packagesYAML{ | ||
Include: inc, | ||
Exclude: exc, | ||
}, | ||
} | ||
enc := yaml.NewEncoder(out) | ||
enc.SetIndent(2) | ||
return enc.Encode(outYaml) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
testrepos "github.com/osbuild/images/test/data/repositories" | ||
|
||
"github.com/osbuild/image-builder-cli/cmd/image-builder" | ||
) | ||
|
||
func TestDescribeImage(t *testing.T) { | ||
restore := main.MockNewRepoRegistry(testrepos.New) | ||
defer restore() | ||
|
||
res, err := main.GetOneImage("", "centos-9", "tar", "x86_64") | ||
assert.NoError(t, err) | ||
|
||
var buf bytes.Buffer | ||
err = main.DescribeImage(res, &buf) | ||
assert.NoError(t, err) | ||
|
||
expectedOutput := `distro: centos-9 | ||
type: tar | ||
arch: x86_64 | ||
os_vesion: 9-stream | ||
bootmode: none | ||
partition_type: "" | ||
default_filename: root.tar.xz | ||
packages: | ||
include: | ||
- policycoreutils | ||
- selinux-policy-targeted | ||
- selinux-policy-targeted | ||
exclude: | ||
- rng-tools | ||
` | ||
assert.Equal(t, expectedOutput, buf.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters