Skip to content

Commit

Permalink
build: support specifying repository file path
Browse files Browse the repository at this point in the history
Add a flag `--repositories` for specifying where to find repositories.
The default value for the new flag is `test/data/repositories/` so this
change is backwards compatible.

The new option is a bit flexible.  It supports either a full path to a
file with repository configurations for a single distribution, or a path
to a directory containing at least one repository configuration.  If the
path is a directory, the distro name is appended to the path as a
filename, e.g. `fedora-40.json`.

This makes it possible to build images with repository configurations
other than the ones in the test/data/repositories/ directory.  This
change also speeds up execution slightly because it doesn't need to
initialise the repository configurations for every file in the
directory, just the one that we are building.
  • Loading branch information
achilleas-k committed Oct 28, 2024
1 parent 9f00c48 commit bdc96bd
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/ostree"
"github.com/osbuild/images/pkg/reporegistry"
"github.com/osbuild/images/pkg/rhsm/facts"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/sbom"
Expand Down Expand Up @@ -165,6 +164,28 @@ func u(s string) string {
return strings.Replace(s, "-", "_", -1)
}

// loadRepos loads the repositories defined at the path and returns just the
// ones for the specified architecture. If the path is a directory, the distro
// name is appended to the path as a filename (with .json extension).
func loadRepos(path, distro, arch string) ([]rpmmd.RepoConfig, error) {
pstat, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("failed to stat %q: %w", path, err)
}

if pstat.IsDir() {
path = filepath.Join(path, fmt.Sprintf("%s.json", distro))
}

repos, err := rpmmd.LoadRepositoriesFromFile(path)
if err != nil {
return nil, fmt.Errorf("failed to load repositories from %q: %w", path, err)
}

// NOTE: this can be empty
return repos[arch], nil
}

func filterRepos(repos []rpmmd.RepoConfig, typeName string) []rpmmd.RepoConfig {
filtered := make([]rpmmd.RepoConfig, 0)
for _, repo := range repos {
Expand All @@ -184,10 +205,11 @@ func filterRepos(repos []rpmmd.RepoConfig, typeName string) []rpmmd.RepoConfig {

func run() error {
// common args
var outputDir, osbuildStore, rpmCacheRoot string
var outputDir, osbuildStore, rpmCacheRoot, repositories string
flag.StringVar(&outputDir, "output", ".", "artifact output directory")
flag.StringVar(&osbuildStore, "store", ".osbuild", "osbuild store for intermediate pipeline trees")
flag.StringVar(&rpmCacheRoot, "rpmmd", "/tmp/rpmmd", "rpm metadata cache directory")
flag.StringVar(&repositories, "repositories", "test/data/repositories", "path to repository file or directory")

// osbuild checkpoint arg
var checkpoints cmdutil.MultiValue
Expand All @@ -206,10 +228,6 @@ func run() error {
os.Exit(1)
}

testedRepoRegistry, err := reporegistry.NewTestedDefault()
if err != nil {
return fmt.Errorf("failed to create repo registry with tested distros: %v", err)
}
distroFac := distrofactory.NewDefault()

config, err := buildconfig.New(configFile)
Expand Down Expand Up @@ -244,7 +262,7 @@ func run() error {
}

// get repositories
repos, err := testedRepoRegistry.ReposByArchName(distroName, archName, true)
repos, err := loadRepos(repositories, distroName, archName)
if err != nil {
return fmt.Errorf("failed to get repositories for %s/%s: %w", distroName, archName, err)
}
Expand Down

0 comments on commit bdc96bd

Please sign in to comment.