manifest: make filename optional and generalise Tree

We have three kinds of operating system trees, until we unify them to one,
hide them behind one interface. Use this to read the architecture from the
Tree rather than pass it in as a string to parent pipelines.

Also, make the filename parameter optional in a few places, there should be no
reason to set this rather than introspect it (except for backwards
compatibility).

Lastly, add another playground example sample to build a raw image.
This commit is contained in:
Tom Gundersen 2022-07-08 15:28:14 +01:00
parent d00b98c134
commit e844453c85
17 changed files with 307 additions and 143 deletions

View file

@ -49,11 +49,11 @@ func findDnfJsonBin() string {
func main() {
var distroArg string
flag.StringVar(&distroArg, "distro", "", "distro to build from")
flag.StringVar(&distroArg, "distro", "host", "distro to build from")
var archArg string
flag.StringVar(&archArg, "arch", common.CurrentArch(), "architecture to build for")
var imageTypeArg string
flag.StringVar(&imageTypeArg, "type", "my-image", "image type to build")
flag.StringVar(&imageTypeArg, "type", "my-container", "image type to build")
flag.Parse()
// Path to options or '-' for stdin
@ -83,16 +83,16 @@ func main() {
distros := distroregistry.NewDefault()
var d distro.Distro
if distroArg != "" {
d = distros.GetDistro(distroArg)
if d == nil {
panic(fmt.Sprintf("distro '%s' not supported\n", distroArg))
}
} else {
if distroArg == "host" {
d = distros.FromHost()
if d == nil {
panic("host distro not supported")
}
} else {
d = distros.GetDistro(distroArg)
if d == nil {
panic(fmt.Sprintf("distro '%s' not supported\n", distroArg))
}
}
arch, err := d.GetArch(archArg)

View file

@ -0,0 +1,71 @@
package main
import (
"github.com/osbuild/osbuild-composer/internal/manifest"
"github.com/osbuild/osbuild-composer/internal/platform"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/runner"
)
// MyContainer contains the arguments passed in as a JSON blob.
// You can replace them with whatever you want to use to
// configure your image. In the current example they are
// unused.
type MyContainer struct {
MyOption string `json:"my_option"`
}
// Name returns the name of the image type, used to select what kind
// of image to build.
func (img *MyContainer) Name() string {
return "my-container"
}
// init registeres this image type
func init() {
AddImageType(&MyContainer{})
}
// Build your manifest by attaching pipelines to it
//
// @m is the manifest you are constructing
// @options are what was passed in on the commandline
// @repos are the default repositories for the host OS/arch
// @runner is needed by any build pipelines
//
// Return nil when you are done, or an error if something
// went wrong. Your manifest will be streamed to osbuild
// for building.
func (img *MyContainer) InstantiateManifest(m *manifest.Manifest, repos []rpmmd.RepoConfig, runner runner.Runner) error {
// Let's create a simple OCI container!
// configure a build pipeline
build := manifest.NewBuild(m, runner, repos)
// create a minimal non-bootable OS tree
os := manifest.NewOS(m, build, &platform.X86{}, repos)
// create an OCI container containing the OS tree created above
manifest.NewOCIContainer(m, build, os)
return nil
}
// GetExports returns a list of the pipelines osbuild should export.
// These are the pipelines containing the artefact you want returned.
//
// TODO: Move this to be implemented in terms ofthe Manifest package.
// We should not need to know the pipeline names.
func (img *MyContainer) GetExports() []string {
return []string{"container"}
}
// GetCheckpoints returns a list of the pipelines osbuild should
// checkpoint. These are the pipelines likely to be reusable in
// future runs.
//
// TODO: Move this to be implemented in terms ofthe Manifest package.
// We should not need to know the pipeline names.
func (img *MyContainer) GetCheckpoints() []string {
return []string{"build"}
}

View file

@ -1,78 +1,62 @@
package main
import (
"math/rand"
"github.com/osbuild/osbuild-composer/internal/disk"
"github.com/osbuild/osbuild-composer/internal/manifest"
"github.com/osbuild/osbuild-composer/internal/platform"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/runner"
)
func init() {
AddImageType(&MyImage{})
}
// MyImage contains the arguments passed in as a JSON blob.
// You can replace them with whatever you want to use to
// configure your image. In the current example they are
// unused.
type MyImage struct {
MyOption string `json:"my_option"`
Filename string `json:"filename"`
}
// Name returns the name of the image type, used to select what kind
// of image to build.
func (img *MyImage) Name() string {
return "my-image"
}
// Build your manifest by attaching pipelines to it
//
// @m is the manifest you are constructing
// @options are what was passed in on the commandline
// @repos are the default repositories for the host OS/arch
// @runner is needed by any build pipelines
//
// Return nil when you are done, or an error if something
// went wrong. Your manifest will be streamed to osbuild
// for building.
func init() {
AddImageType(&MyImage{})
}
func (img *MyImage) InstantiateManifest(m *manifest.Manifest, repos []rpmmd.RepoConfig, runner runner.Runner) error {
// Let's create a simple OCI container!
// Let's create a simple raw image!
// configure a build pipeline
build := manifest.NewBuild(m, runner, repos)
// create a non-bootable OS tree containing the `core` comps group
os := manifest.NewOS(m, build, &platform.X86{}, repos)
os.ExtraBasePackages = []string{
"@core",
// create an x86_64 platform with bios boot
platform := &platform.X86{
BIOS: true,
}
filename := "my-container.tar"
if img.Filename != "" {
filename = img.Filename
// TODO: add helper
// math/rand is good enough in this case
/* #nosec G404 */
pt, err := disk.NewPartitionTable(&basePT, nil, 0, false, rand.New(rand.NewSource(0)))
if err != nil {
panic(err)
}
// create an OCI container containing the OS tree created above
manifest.NewOCIContainer(m, build, &os.Base, "x86_64", filename)
// create a minimal bootable OS tree
os := manifest.NewOS(m, build, platform, repos)
os.PartitionTable = pt // we need a partition table
os.KernelName = "kernel" // use the default fedora kernel
// create a raw image containing the OS tree created above
manifest.NewRawImage(m, build, os)
return nil
}
// GetExports returns a list of the pipelines osbuild should export.
// These are the pipelines containing the artefact you want returned.
//
// TODO: Move this to be implemented in terms ofthe Manifest package.
// We should not need to know the pipeline names.
// TODO: make internal
func (img *MyImage) GetExports() []string {
return []string{"container"}
return []string{"image"}
}
// GetCheckpoints returns a list of the pipelines osbuild should
// checkpoint. These are the pipelines likely to be reusable in
// future runs.
//
// TODO: Move this to be implemented in terms ofthe Manifest package.
// We should not need to know the pipeline names.
func (img *MyImage) GetCheckpoints() []string {
return []string{"build"}
}

View file

@ -0,0 +1,56 @@
package main
import "github.com/osbuild/osbuild-composer/internal/disk"
var basePT = disk.PartitionTable{
UUID: "D209C89E-EA5E-4FBD-B161-B461CCE297E0",
Type: "gpt",
Partitions: []disk.Partition{
{
Size: 1048576, // 1MB
Bootable: true,
Type: disk.BIOSBootPartitionGUID,
UUID: disk.BIOSBootPartitionUUID,
},
{
Size: 209715200, // 200 MB
Type: disk.EFISystemPartitionGUID,
UUID: disk.EFISystemPartitionUUID,
Payload: &disk.Filesystem{
Type: "vfat",
UUID: disk.EFIFilesystemUUID,
Mountpoint: "/boot/efi",
Label: "EFI-SYSTEM",
FSTabOptions: "defaults,uid=0,gid=0,umask=077,shortname=winnt",
FSTabFreq: 0,
FSTabPassNo: 2,
},
},
{
Size: 524288000, // 500 MB
Type: disk.FilesystemDataGUID,
UUID: disk.FilesystemDataUUID,
Payload: &disk.Filesystem{
Type: "ext4",
Mountpoint: "/boot",
Label: "boot",
FSTabOptions: "defaults",
FSTabFreq: 0,
FSTabPassNo: 0,
},
},
{
Size: 2147483648, // 2GiB
Type: disk.FilesystemDataGUID,
UUID: disk.RootPartitionUUID,
Payload: &disk.Filesystem{
Type: "ext4",
Label: "root",
Mountpoint: "/",
FSTabOptions: "defaults",
FSTabFreq: 0,
FSTabPassNo: 0,
},
},
},
}