1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Reserve package log output (#3981)

* beautify log output
This commit is contained in:
tiloKo 2022-09-01 08:53:41 +02:00 committed by GitHub
parent 721994fac5
commit 0ef399cd1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 45 deletions

View File

@ -24,75 +24,114 @@ func abapAddonAssemblyKitReserveNextPackages(config abapAddonAssemblyKitReserveN
func runAbapAddonAssemblyKitReserveNextPackages(config *abapAddonAssemblyKitReserveNextPackagesOptions, telemetryData *telemetry.CustomData, utils *aakaas.AakUtils,
cpe *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment) error {
log.Entry().Info("╔═════════════════════════════════════════╗")
log.Entry().Info("║ abapAddonAssemblyKitReserveNextPackages ║")
log.Entry().Info("╚═════════════════════════════════════════╝")
log.Entry().Infof("... initializing connection to AAKaaS @ %v", config.AbapAddonAssemblyKitEndpoint)
conn := new(abapbuild.Connector)
if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, *utils); err != nil {
return err
}
log.Entry().Info("... reading AddonDescriptor (Software Component, Version) from CommonPipelineEnvironment")
addonDescriptor := new(abaputils.AddonDescriptor)
if err := addonDescriptor.InitFromJSONstring(config.AddonDescriptor); err != nil {
return errors.Wrap(err, "Reading AddonDescriptor failed [Make sure abapAddonAssemblyKit...CheckCVs|CheckPV steps have been run before]")
}
log.Entry().Info("╭────────────────────────────────┬──────────────────────╮")
log.Entry().Info("│ Software Component │ Version │")
log.Entry().Info("├────────────────────────────────┼──────────────────────┤")
for i := range addonDescriptor.Repositories {
log.Entry().Infof("│ %-30v │ %-20v │", addonDescriptor.Repositories[i].Name, addonDescriptor.Repositories[i].VersionYAML)
}
log.Entry().Info("╰────────────────────────────────┴──────────────────────╯")
packagesWithRepos, err := reservePackages(addonDescriptor.Repositories, *conn)
if err != nil {
return err
}
log.Entry().Info("... checking for ongoing Reservations")
if err = pollReserveNextPackages(packagesWithRepos, utils); err != nil {
return err
}
addonDescriptor.Repositories, err = checkAndCopyFieldsToRepositories(packagesWithRepos)
if err != nil {
log.Entry().Info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
log.Entry().Infof("┃ %-30v ┃ %-20v ┃ %-5v ┃ %-6v ┃ %-10v ┃ %-40v ┃ %-40v ┃", "Software Component", "Package Name", "Type", "Status", "Namespace", "CommitID (from addon.yml)", "PredecessorCommitID (from AAKaaS)")
log.Entry().Info("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫")
for i := range packagesWithRepos {
log.Entry().Infof("┃ %-30v ┃ %-20v ┃ %-5v ┃ %-6v ┃ %-10v ┃ %-40v ┃ %-40v ┃", packagesWithRepos[i].Repo.Name, packagesWithRepos[i].Package.PackageName, packagesWithRepos[i].Package.Type, packagesWithRepos[i].Package.Status, packagesWithRepos[i].Package.Namespace, packagesWithRepos[i].Repo.CommitID, packagesWithRepos[i].Package.PredecessorCommitID)
}
log.Entry().Info("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
log.Entry().Info("... checking and processing provided and received data")
if addonDescriptor.Repositories, err = checkAndCopyFieldsToRepositories(packagesWithRepos); err != nil {
return err
}
log.Entry().Info("Writing package names, types, status, namespace and predecessorCommitID to CommonPipelineEnvironment")
log.Entry().Info("... writing AddonDescriptor (package name, type, status, namespace and predecessorCommitID) back to CommonPipelineEnvironment")
cpe.abap.addonDescriptor = addonDescriptor.AsJSONstring()
return nil
}
func checkAndCopyFieldsToRepositories(pckgWR []aakaas.PackageWithRepository) ([]abaputils.Repository, error) {
var repos []abaputils.Repository
log.Entry().Infof("%-30v | %-20v | %-6v | %-40v | %-40v", "Software Component", "Package", "Status", "CommitID (from addon.yml)", "PredecessorCommitID (from AAKaaS)")
var checkFailure error = nil
for i := range pckgWR {
log.Entry().Infof("%-30v | %-20v | %-6v | %-40v | %-40v", pckgWR[i].Repo.Name, pckgWR[i].Package.PackageName, pckgWR[i].Package.Status, pckgWR[i].Repo.CommitID, pckgWR[i].Package.PredecessorCommitID)
if !pckgWR[i].Repo.UseClassicCTS {
if pckgWR[i].Package.Status == aakaas.PackageStatusReleased {
//Ensure for Packages with Status R that CommitID of package = the one from addon.yml, beware of short commitID in addon.yml
addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID)
if len(pckgWR[i].Package.CommitID) < addonYAMLcommitIDLength {
return repos, errors.New("Provided CommitIDs have wrong length: " + pckgWR[i].Repo.CommitID + "(addon.yml) longer than the one from AAKaaS " + pckgWR[i].Package.CommitID)
}
packageCommitIDsubsting := pckgWR[i].Package.CommitID[0:addonYAMLcommitIDLength]
if pckgWR[i].Repo.CommitID != packageCommitIDsubsting {
log.Entry().Error("package " + pckgWR[i].Package.PackageName + " was already build but with commit " + pckgWR[i].Package.CommitID + ", not with " + pckgWR[i].Repo.CommitID)
log.Entry().Error("If you want to build a new package make sure to increase the dotted-version-string in addon.yml")
log.Entry().Error("If you do NOT want to build a new package enter the commitID " + pckgWR[i].Package.CommitID + " for software component " + pckgWR[i].Repo.Name + " in addon.yml")
return repos, errors.New("commit of released package does not match with addon.yml")
}
} else if pckgWR[i].Package.PredecessorCommitID != "" {
//Check for newly reserved packages which are to be build that CommitID from addon.yml != PreviousCommitID [this will result in an error as no delta can be calculated]
addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID)
if len(pckgWR[i].Package.PredecessorCommitID) < addonYAMLcommitIDLength {
return repos, errors.New("Provided CommitIDs have wrong length: " + pckgWR[i].Repo.CommitID + "(addon.yml) longer than the one from AAKaaS " + pckgWR[i].Package.CommitID)
}
packagePredecessorCommitIDsubsting := pckgWR[i].Package.PredecessorCommitID[0:addonYAMLcommitIDLength]
if pckgWR[i].Repo.CommitID == packagePredecessorCommitIDsubsting {
return repos, errors.New("CommitID of package " + pckgWR[i].Package.PackageName + " is the same as the one of the predecessor package. Make sure to change both the dotted-version-string AND the commitID in addon.yml")
}
}
}
checkFailure = checkCommitID(pckgWR, i, checkFailure)
pckgWR[i].Package.CopyFieldsToRepo(&pckgWR[i].Repo)
repos = append(repos, pckgWR[i].Repo)
}
return repos, nil
return repos, checkFailure
}
func checkCommitID(pckgWR []aakaas.PackageWithRepository, i int, checkFailure error) error {
if !pckgWR[i].Repo.UseClassicCTS {
if pckgWR[i].Package.Status == aakaas.PackageStatusReleased {
checkFailure = checkCommitIDSameAsGiven(pckgWR, i, checkFailure)
} else if pckgWR[i].Package.PredecessorCommitID != "" {
checkFailure = checkCommitIDNotSameAsPrevious(pckgWR, i, checkFailure)
}
}
return checkFailure
}
func checkCommitIDSameAsGiven(pckgWR []aakaas.PackageWithRepository, i int, checkFailure error) error {
//Ensure for Packages with Status R that CommitID of package = the one from addon.yml, beware of short commitID in addon.yml
addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID)
if len(pckgWR[i].Package.CommitID) < addonYAMLcommitIDLength {
checkFailure = errors.New("Provided CommitIDs have wrong length: " + pckgWR[i].Repo.CommitID + "(addon.yml) longer than the one from AAKaaS " + pckgWR[i].Package.CommitID)
log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution")
} else {
packageCommitIDsubsting := pckgWR[i].Package.CommitID[0:addonYAMLcommitIDLength]
if pckgWR[i].Repo.CommitID != packageCommitIDsubsting {
log.Entry().Error("package " + pckgWR[i].Package.PackageName + " was already build but with commit " + pckgWR[i].Package.CommitID + ", not with " + pckgWR[i].Repo.CommitID)
log.Entry().Error("If you want to build a new package make sure to increase the dotted-version-string in addon.yml - current value: " + pckgWR[i].Package.VersionYAML)
log.Entry().Error("If you do NOT want to build a new package enter the commitID " + pckgWR[i].Package.CommitID + " for software component " + pckgWR[i].Repo.Name + " in addon.yml")
checkFailure = errors.New("commit of already released package does not match with addon.yml")
log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution")
}
}
return checkFailure
}
func checkCommitIDNotSameAsPrevious(pckgWR []aakaas.PackageWithRepository, i int, checkFailure error) error {
//Check for newly reserved packages which are to be build that CommitID from addon.yml != PreviousCommitID [this will result in an error as no delta can be calculated]
addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID)
if len(pckgWR[i].Package.PredecessorCommitID) < addonYAMLcommitIDLength {
checkFailure = errors.New("Provided CommitIDs have wrong length: " + pckgWR[i].Repo.CommitID + "(addon.yml) longer than the one from AAKaaS " + pckgWR[i].Package.CommitID)
log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution")
} else {
packagePredecessorCommitIDsubsting := pckgWR[i].Package.PredecessorCommitID[0:addonYAMLcommitIDLength]
if pckgWR[i].Repo.CommitID == packagePredecessorCommitIDsubsting {
checkFailure = errors.New("CommitID of package " + pckgWR[i].Package.PackageName + " is the same as the one of the predecessor package. Make sure to change both the dotted-version-string AND the commitID in addon.yml")
log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution")
}
}
return checkFailure
}
func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, utils *aakaas.AakUtils) error {
@ -106,6 +145,9 @@ func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, utils *aakaa
case <-ticker:
var allFinished bool = true
for i := range pckgWR {
if pckgWR[i].Package.Status == aakaas.PackageStatusReleased {
continue
}
err := pckgWR[i].Package.GetPackageAndNamespace()
// if there is an error, reservation is not yet finished
if err != nil {
@ -128,7 +170,7 @@ func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, utils *aakaa
}
}
if allFinished {
log.Entry().Infof("Reservation of package(s) was successful")
log.Entry().Infof(" => Reservations of package(s) finished successfully")
return nil
}
}

View File

@ -69,7 +69,6 @@ func (p *Package) CopyFieldsToRepo(initialRepo *abaputils.Repository) {
initialRepo.PredecessorCommitID = p.PredecessorCommitID
initialRepo.Status = string(p.Status)
initialRepo.Namespace = p.Namespace
log.Entry().Infof("Package name %s, type %s, status %s, namespace %s, predecessorCommitID %s", p.PackageName, p.Type, p.Status, p.Namespace, p.PredecessorCommitID)
}
// ReserveNext : reserve next delivery package for this software component version
@ -77,7 +76,7 @@ func (p *Package) ReserveNext() error {
if p.ComponentName == "" || p.VersionYAML == "" {
return errors.New("Parameters missing. Please provide the name and version of the component")
}
log.Entry().Infof("Reserve package for %s version %s", p.ComponentName, p.VersionYAML)
log.Entry().Infof("... determining package name and attributes for software component %s version %s", p.ComponentName, p.VersionYAML)
p.Connector.GetToken("/odata/aas_ocs_package")
appendum := "/odata/aas_ocs_package/DeterminePackageForScv?Name='" + url.QueryEscape(p.ComponentName) + "'&Version='" + url.QueryEscape(p.VersionYAML) + "'"
body, err := p.Connector.Post(appendum, "")
@ -92,9 +91,13 @@ func (p *Package) ReserveNext() error {
p.Type = jPck.DeterminePackage.Package.Type
p.PredecessorCommitID = jPck.DeterminePackage.Package.PredecessorCommitID
p.Status = jPck.DeterminePackage.Package.Status
p.Namespace = jPck.DeterminePackage.Package.Namespace
p.setNamespace(jPck.DeterminePackage.Package.Namespace)
p.CommitID = jPck.DeterminePackage.Package.CommitID
log.Entry().Infof("Reservation of package %s started", p.PackageName)
if p.Status == PackageStatusReleased {
log.Entry().Infof(" => Reservation of package %s not needed as status is already 'released'", p.PackageName)
} else {
log.Entry().Infof(" => Reservation of package %s started", p.PackageName)
}
return nil
}
@ -105,15 +108,15 @@ func (p *Package) GetPackageAndNamespace() error {
if err != nil {
return err
}
var jPck jsonPackage
if err := json.Unmarshal(body, &jPck); err != nil {
return errors.Wrap(err, "Unexpected AAKaaS response for check of package status: "+string(body))
}
p.Status = jPck.Package.Status
p.Namespace = jPck.Package.Namespace
if p.Namespace == "//" {
p.Namespace = ""
}
p.setNamespace(jPck.Package.Namespace)
return nil
}
@ -162,3 +165,12 @@ func (p *Package) Release() error {
p.Status = jPck.Package.Status
return nil
}
// setNamespace
func (p *Package) setNamespace(namespace string) {
if namespace == "//" {
p.Namespace = ""
} else {
p.Namespace = namespace
}
}