Skip to content

Commit

Permalink
cli: implement messy/untested "image-builder build"
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Oct 30, 2024
1 parent edc3d8a commit e130485
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 20 deletions.
36 changes: 36 additions & 0 deletions cmd/image-builder/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"

"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/osbuild"
)

func buildImage(out io.Writer, distroName, imgTypeStr string) error {
// cross arch building is not possible, we would have to download
// a pre-populated buildroot (tar,container) with rpm for that
archStr := arch.Current().String()
filterResult, err := getOneImage(distroName, imgTypeStr, archStr)
if err != nil {
return err
}
imgType := filterResult.ImgType

var mf bytes.Buffer
if err := outputManifest(&mf, distroName, imgTypeStr, archStr); err != nil {
return err
}

osbuildStoreDir := ".store"
outputDir := "."
buildName := fmt.Sprintf("%s-%s-%s", distroName, imgTypeStr, archStr)
jobOutputDir := filepath.Join(outputDir, buildName)
// XXX: support stremaing via statusWriter
_, err = osbuild.RunOSBuild(mf.Bytes(), osbuildStoreDir, jobOutputDir, imgType.Exports(), nil, nil, false, os.Stderr)
return err
}
21 changes: 21 additions & 0 deletions cmd/image-builder/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ type FilterResult struct {
ImgType distro.ImageType
}

func getOneImage(distroName, imgTypeStr, archStr string) (*FilterResult, error) {
filterExprs := []string{
fmt.Sprintf("name:%s", distroName),
fmt.Sprintf("arch:%s", archStr),
fmt.Sprintf("type:%s", imgTypeStr),
}
filteredResults, err := getFilteredImages(filterExprs)
if err != nil {
return nil, err
}
switch len(filteredResults) {
case 0:
return nil, fmt.Errorf("cannot find image for %s %s %s", distroName, imgTypeStr, archStr)
case 1:
return &filteredResults[0], nil
default:
return nil, fmt.Errorf("internal error: found %v results for %s %s %s", len(filteredResults), distroName, imgTypeStr, archStr)
}
}

// XXX: rename FilterDistros to FilterImages(?)
func FilterDistros(fac *distrofactory.Factory, distroNames []string, filters Filters) ([]FilterResult, error) {
var res []FilterResult

Expand Down
17 changes: 17 additions & 0 deletions cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func cmdManifest(cmd *cobra.Command, args []string) error {
return outputManifest(osStdout, distroName, imgType, archStr)
}

func cmdBuild(cmd *cobra.Command, args []string) error {
distroName := args[0]
imgType := args[1]

return buildImage(osStdout, distroName, imgType)
}

func run() error {
// images logs a bunch of stuff to Debug/Info that we we do not
// want to show
Expand Down Expand Up @@ -76,6 +83,16 @@ operating sytsems like centos and RHEL with easy customizations support.`,
}
rootCmd.AddCommand(manifestCmd)

buildCmd := &cobra.Command{
Use: "build <distro> <image-type>",
Short: "Build the given distro/image-type, e.g. centos-9 qcow2",
RunE: cmdBuild,
SilenceUsage: true,
Args: cobra.ExactArgs(2),
}
rootCmd.AddCommand(buildCmd)
// XXX: add --output=text,json and streaming

return rootCmd.Execute()
}

Expand Down
28 changes: 8 additions & 20 deletions cmd/image-builder/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,15 @@ func depsolve(cacheDir string, packageSets map[string][]rpmmd.PackageSet, d dist
}

func outputManifest(out io.Writer, distroName, imgTypeStr, archStr string) error {
filterExprs := []string{
fmt.Sprintf("name:%s", distroName),
fmt.Sprintf("arch:%s", archStr),
fmt.Sprintf("type:%s", imgTypeStr),
}
filteredResults, err := getFilteredImages(filterExprs)
// XXX: what/how much do we expose here?
var options distro.ImageOptions

filterResult, err := getOneImage(distroName, imgTypeStr, archStr)
if err != nil {
return err
}
switch len(filteredResults) {
case 0:
return fmt.Errorf("cannot find image for %s %s %s", distroName, imgTypeStr, archStr)
case 1:
// nothing
default:
return fmt.Errorf("internal error: found %v results for %s %s %s", len(filteredResults), distroName, imgTypeStr, archStr)
}

var bp blueprint.Blueprint
// XXX: what/how much do we expose here?
options := distro.ImageOptions{}
distro := filteredResults[0].Distro
imgType := filteredResults[0].ImgType
distro := filterResult.Distro
imgType := filterResult.ImgType

reporeg, err := newRepoRegistry()
if err != nil {
Expand All @@ -62,6 +48,8 @@ func outputManifest(out io.Writer, distroName, imgTypeStr, archStr string) error
if err != nil {
return err
}

var bp blueprint.Blueprint
preManifest, warnings, err := imgType.Manifest(&bp, options, repos, 0)
if err != nil {
return err
Expand Down

0 comments on commit e130485

Please sign in to comment.