distro: inline initializeManifest

Inline the initializeManifest() function so we can start simplifying the
PackageSets() and Manifest() (the two callers) separately.
This commit is contained in:
Achilleas Koutsou 2023-04-05 20:26:48 +02:00 committed by Simon de Vlieger
parent 744eb9d337
commit 3d9dcff7f6
4 changed files with 255 additions and 188 deletions

View file

@ -631,15 +631,41 @@ func (t *imageType) PackageSets(bp blueprint.Blueprint, options distro.ImageOpti
}
}
// create a manifest object and instantiate it with the computed packageSetChains
manifest, _, err := t.initializeManifest(&bp, options, repos, packageSets, containers, 0)
_, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
// TODO: handle manifest initialization errors more gracefully, we
// refuse to initialize manifests with invalid config.
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
// TODO: let image types specify valid workloads, rather than
// always assume Custom.
w := &workload.Custom{
BaseWorkload: workload.BaseWorkload{
Repos: packageSets[blueprintPkgsKey].Repositories,
},
Packages: bp.GetPackagesEx(false),
}
if services := bp.Customizations.GetServices(); services != nil {
w.Services = services.Enabled
w.DisabledServices = services.Disabled
}
source := rand.NewSource(0)
// math/rand is good enough in this case
/* #nosec G404 */
rng := rand.New(source)
img, err := t.image(w, t, bp.Customizations, options, packageSets, containers, rng)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
manifest := manifest.New()
_, err = img.InstantiateManifest(&manifest, repos, t.arch.distro.runner, rng)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
return manifest.GetPackageSetChains()
}
@ -713,13 +739,40 @@ func (t *imageType) PartitionType() string {
return basePartitionTable.Type
}
func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string]rpmmd.PackageSet,
packageSpecs map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (*manifest.Manifest, []string, error) {
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
repos = mergedRepos
var packageSets map[string]rpmmd.PackageSet
warnings, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
return nil, nil, err
@ -752,47 +805,8 @@ func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
if err != nil {
return nil, nil, err
}
return &manifest, warnings, err
}
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
manifest, warnings, err := t.initializeManifest(bp, options, mergedRepos, nil, containers, seed)
if err != nil {
return distro.Manifest{}, nil, err
}
ret, err := manifest.Serialize(packageSets)
ret, err := manifest.Serialize(packageSpecs)
if err != nil {
return ret, nil, err
}

View file

@ -333,18 +333,45 @@ func (t *imageType) PartitionType() string {
return basePartitionTable.Type
}
func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string]rpmmd.PackageSet,
packageSpecs map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (*manifest.Manifest, []string, error) {
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
repos = mergedRepos
warnings, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
return nil, nil, err
}
var packageSets map[string]rpmmd.PackageSet
w := t.workload
if w == nil {
cw := &workload.Custom{
@ -377,47 +404,8 @@ func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
if err != nil {
return nil, nil, err
}
return &manifest, warnings, err
}
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
manifest, warnings, err := t.initializeManifest(bp, options, mergedRepos, nil, containers, seed)
if err != nil {
return distro.Manifest{}, nil, err
}
ret, err := manifest.Serialize(packageSets)
ret, err := manifest.Serialize(packageSpecs)
if err != nil {
return ret, nil, err
}
@ -461,15 +449,47 @@ func (t *imageType) PackageSets(bp blueprint.Blueprint, options distro.ImageOpti
}
}
// create a manifest object and instantiate it with the computed packageSetChains
manifest, _, err := t.initializeManifest(&bp, options, repos, packageSets, containers, 0)
_, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
// TODO: handle manifest initialization errors more gracefully, we
// refuse to initialize manifests with invalid config.
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
w := t.workload
if w == nil {
cw := &workload.Custom{
BaseWorkload: workload.BaseWorkload{
Repos: packageSets[blueprintPkgsKey].Repositories,
},
Packages: bp.GetPackagesEx(false),
}
if services := bp.Customizations.GetServices(); services != nil {
cw.Services = services.Enabled
cw.DisabledServices = services.Disabled
}
w = cw
}
source := rand.NewSource(0)
// math/rand is good enough in this case
/* #nosec G404 */
rng := rand.New(source)
if t.image == nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
img, err := t.image(w, t, bp.Customizations, options, packageSets, containers, rng)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
manifest := manifest.New()
_, err = img.InstantiateManifest(&manifest, repos, t.arch.distro.runner, rng)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
return overridePackageNamesInSets(manifest.GetPackageSetChains())
}

View file

@ -182,12 +182,38 @@ func (t *imageType) PartitionType() string {
return basePartitionTable.Type
}
func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string]rpmmd.PackageSet,
packageSpecs map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (*manifest.Manifest, []string, error) {
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
repos = mergedRepos
warnings, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
@ -195,6 +221,7 @@ func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
}
w := t.workload
var packageSets map[string]rpmmd.PackageSet
if w == nil {
cw := &workload.Custom{
BaseWorkload: workload.BaseWorkload{
@ -226,47 +253,8 @@ func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
if err != nil {
return nil, nil, err
}
return &manifest, warnings, err
}
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
manifest, warnings, err := t.initializeManifest(bp, options, mergedRepos, nil, containers, seed)
if err != nil {
return distro.Manifest{}, nil, err
}
ret, err := manifest.Serialize(packageSets)
ret, err := manifest.Serialize(packageSpecs)
if err != nil {
return ret, nil, err
}
@ -328,11 +316,40 @@ func (t *imageType) PackageSets(bp blueprint.Blueprint, options distro.ImageOpti
}
}
// create a manifest object and instantiate it with the computed packageSetChains
manifest, _, err := t.initializeManifest(&bp, options, repos, packageSets, containers, 0)
_, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
w := t.workload
if w == nil {
cw := &workload.Custom{
BaseWorkload: workload.BaseWorkload{
Repos: packageSets[blueprintPkgsKey].Repositories,
},
Packages: bp.GetPackagesEx(false),
}
if services := bp.Customizations.GetServices(); services != nil {
cw.Services = services.Enabled
cw.DisabledServices = services.Disabled
}
w = cw
}
source := rand.NewSource(0)
// math/rand is good enough in this case
/* #nosec G404 */
rng := rand.New(source)
img, err := t.image(w, t, bp.Customizations, options, packageSets, containers, rng)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
manifest := manifest.New()
_, err = img.InstantiateManifest(&manifest, repos, t.arch.distro.runner, rng)
if err != nil {
// TODO: handle manifest initialization errors more gracefully, we
// refuse to initialize manifests with invalid config.
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}

View file

@ -185,18 +185,45 @@ func (t *imageType) PartitionType() string {
return basePartitionTable.Type
}
func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string]rpmmd.PackageSet,
packageSpecs map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (*manifest.Manifest, []string, error) {
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
repos = mergedRepos
warnings, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
return nil, nil, err
}
var packageSets map[string]rpmmd.PackageSet
w := t.workload
if w == nil {
cw := &workload.Custom{
@ -226,47 +253,8 @@ func (t *imageType) initializeManifest(bp *blueprint.Blueprint,
if err != nil {
return nil, nil, err
}
return &manifest, warnings, err
}
func (t *imageType) Manifest(customizations *blueprint.Customizations,
options distro.ImageOptions,
repos []rpmmd.RepoConfig,
packageSets map[string][]rpmmd.PackageSpec,
containers []container.Spec,
seed int64) (distro.Manifest, []string, error) {
bp := &blueprint.Blueprint{Name: "empty blueprint"}
err := bp.Initialize()
if err != nil {
panic("could not initialize empty blueprint: " + err.Error())
}
bp.Customizations = customizations
// the os pipeline filters repos based on the `osPkgsKey` package set, merge the repos which
// contain a payload package set into the `osPkgsKey`, so those repos are included when
// building the rpm stage in the os pipeline
// TODO: roll this into workloads
mergedRepos := make([]rpmmd.RepoConfig, 0, len(repos))
for _, repo := range repos {
for _, pkgsKey := range t.PayloadPackageSets() {
// If the repo already contains the osPkgsKey, skip
if slices.Contains(repo.PackageSets, osPkgsKey) {
break
}
if slices.Contains(repo.PackageSets, pkgsKey) {
repo.PackageSets = append(repo.PackageSets, osPkgsKey)
}
}
mergedRepos = append(mergedRepos, repo)
}
manifest, warnings, err := t.initializeManifest(bp, options, mergedRepos, nil, containers, seed)
if err != nil {
return distro.Manifest{}, nil, err
}
ret, err := manifest.Serialize(packageSets)
ret, err := manifest.Serialize(packageSpecs)
if err != nil {
return ret, nil, err
}
@ -328,15 +316,43 @@ func (t *imageType) PackageSets(bp blueprint.Blueprint, options distro.ImageOpti
}
}
// create a manifest object and instantiate it with the computed packageSetChains
manifest, _, err := t.initializeManifest(&bp, options, repos, packageSets, containers, 0)
_, err := t.checkOptions(bp.Customizations, options, containers)
if err != nil {
// TODO: handle manifest initialization errors more gracefully, we
// refuse to initialize manifests with invalid config.
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
w := t.workload
if w == nil {
cw := &workload.Custom{
BaseWorkload: workload.BaseWorkload{
Repos: packageSets[blueprintPkgsKey].Repositories,
},
Packages: bp.GetPackagesEx(false),
}
if services := bp.Customizations.GetServices(); services != nil {
cw.Services = services.Enabled
cw.DisabledServices = services.Disabled
}
w = cw
}
source := rand.NewSource(0)
// math/rand is good enough in this case
/* #nosec G404 */
rng := rand.New(source)
img, err := t.image(w, t, bp.Customizations, options, packageSets, containers, rng)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
manifest := manifest.New()
_, err = img.InstantiateManifest(&manifest, repos, t.arch.distro.runner, rng)
if err != nil {
logrus.Errorf("Initializing the manifest failed for %s (%s/%s): %v", t.Name(), t.arch.distro.Name(), t.arch.Name(), err)
return nil
}
return manifest.GetPackageSetChains()
}