Skip to content

Commit

Permalink
cmd: make cmd/build use manifestgen
Browse files Browse the repository at this point in the history
This commit makes the `cmd/build` command use the manifestgen module
to remove some code duplication.

It does not move to use the actual `manifestgen.Generator` because
the way the repositories are used is not supported by manifestgen
right now: `build` supports loading a single repository file but
manifestgen expects a reporegistry so that it can map "distroname"
to a "repository".

We could add support for loading a repo json directly in
`manifestgen.Generator` but I do feel uneasy about it because it
seems a good property that there is some connection between the
distroname and the repo in the general case.
  • Loading branch information
mvo5 committed Jan 21, 2025
1 parent ae3d079 commit 605348a
Showing 1 changed file with 4 additions and 58 deletions.
62 changes: 4 additions & 58 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ import (
"github.com/osbuild/images/internal/cmdutil"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/dnfjson"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/manifestgen"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/ostree"
"github.com/osbuild/images/pkg/rhsm/facts"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/sbom"
)

func makeManifest(
Expand Down Expand Up @@ -59,7 +56,7 @@ func makeManifest(
fmt.Fprintf(os.Stderr, "[WARNING]\n%s", strings.Join(warnings, "\n"))
}

depsolvedSets, err := depsolve(cacheDir, manifest.GetPackageSetChains(), distribution, archName)
depsolvedSets, err := manifestgen.DefaultDepsolver(cacheDir, manifest.GetPackageSetChains(), distribution, archName)
if err != nil {
return nil, fmt.Errorf("[ERROR] depsolve failed: %w", err)
}
Expand All @@ -71,12 +68,12 @@ func makeManifest(
bp = blueprint.Blueprint(*config.Blueprint)
}

containerSpecs, err := resolvePipelineContainers(manifest.GetContainerSourceSpecs(), archName)
containerSpecs, err := manifestgen.DefaultContainerResolver(manifest.GetContainerSourceSpecs(), archName)
if err != nil {
return nil, fmt.Errorf("[ERROR] container resolution failed: %w", err)
}

commitSpecs, err := resolvePipelineCommits(manifest.GetOSTreeSourceSpecs())
commitSpecs, err := manifestgen.DefaultCommitResolver(manifest.GetOSTreeSourceSpecs())
if err != nil {
return nil, fmt.Errorf("[ERROR] ostree commit resolution failed: %w", err)
}
Expand All @@ -89,57 +86,6 @@ func makeManifest(
return mf, nil
}

func resolveContainers(containers []container.SourceSpec, archName string) ([]container.Spec, error) {
resolver := container.NewResolver(archName)

for _, c := range containers {
resolver.Add(c)
}

return resolver.Finish()
}

func resolvePipelineContainers(containerSources map[string][]container.SourceSpec, archName string) (map[string][]container.Spec, error) {
containerSpecs := make(map[string][]container.Spec, len(containerSources))
for plName, sourceSpecs := range containerSources {
specs, err := resolveContainers(sourceSpecs, archName)
if err != nil {
return nil, err
}
containerSpecs[plName] = specs
}
return containerSpecs, nil
}

func resolvePipelineCommits(commitSources map[string][]ostree.SourceSpec) (map[string][]ostree.CommitSpec, error) {
commits := make(map[string][]ostree.CommitSpec, len(commitSources))
for name, commitSources := range commitSources {
commitSpecs := make([]ostree.CommitSpec, len(commitSources))
for idx, commitSource := range commitSources {
var err error
commitSpecs[idx], err = ostree.Resolve(commitSource)
if err != nil {
return nil, err
}
}
commits[name] = commitSpecs
}
return commits, nil
}

func depsolve(cacheDir string, packageSets map[string][]rpmmd.PackageSet, d distro.Distro, arch string) (map[string]dnfjson.DepsolveResult, error) {
solver := dnfjson.NewSolver(d.ModulePlatformID(), d.Releasever(), arch, d.Name(), cacheDir)
depsolvedSets := make(map[string]dnfjson.DepsolveResult)
for name, pkgSet := range packageSets {
res, err := solver.Depsolve(pkgSet, sbom.StandardTypeNone)
if err != nil {
return nil, err
}
depsolvedSets[name] = *res
}
return depsolvedSets, nil
}

func save(ms manifest.OSBuildManifest, fpath string) error {
b, err := json.MarshalIndent(ms, "", " ")
if err != nil {
Expand Down

0 comments on commit 605348a

Please sign in to comment.