github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapAddonAssemblyKitReserveNextPackages.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/SAP/jenkins-library/pkg/abap/aakaas" 8 abapbuild "github.com/SAP/jenkins-library/pkg/abap/build" 9 "github.com/SAP/jenkins-library/pkg/abaputils" 10 11 "github.com/SAP/jenkins-library/pkg/log" 12 "github.com/SAP/jenkins-library/pkg/telemetry" 13 "github.com/pkg/errors" 14 ) 15 16 func abapAddonAssemblyKitReserveNextPackages(config abapAddonAssemblyKitReserveNextPackagesOptions, telemetryData *telemetry.CustomData, cpe *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment) { 17 utils := aakaas.NewAakBundleWithTime(time.Duration(config.MaxRuntimeInMinutes), time.Duration(config.PollingIntervalInSeconds)) 18 // error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end 19 if err := runAbapAddonAssemblyKitReserveNextPackages(&config, telemetryData, &utils, cpe); err != nil { 20 log.Entry().WithError(err).Fatal("step execution failed") 21 } 22 } 23 24 func runAbapAddonAssemblyKitReserveNextPackages(config *abapAddonAssemblyKitReserveNextPackagesOptions, telemetryData *telemetry.CustomData, utils *aakaas.AakUtils, 25 cpe *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment) error { 26 27 log.Entry().Info("╔═════════════════════════════════════════╗") 28 log.Entry().Info("║ abapAddonAssemblyKitReserveNextPackages ║") 29 log.Entry().Info("╚═════════════════════════════════════════╝") 30 31 log.Entry().Infof("... initializing connection to AAKaaS @ %v", config.AbapAddonAssemblyKitEndpoint) 32 conn := new(abapbuild.Connector) 33 if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, *utils); err != nil { 34 return err 35 } 36 37 log.Entry().Info("... reading AddonDescriptor (Software Component, Version) from CommonPipelineEnvironment") 38 addonDescriptor := new(abaputils.AddonDescriptor) 39 if err := addonDescriptor.InitFromJSONstring(config.AddonDescriptor); err != nil { 40 return errors.Wrap(err, "Reading AddonDescriptor failed [Make sure abapAddonAssemblyKit...CheckCVs|CheckPV steps have been run before]") 41 } 42 43 log.Entry().Info("╭────────────────────────────────┬──────────────────────╮") 44 log.Entry().Info("│ Software Component │ Version │") 45 log.Entry().Info("├────────────────────────────────┼──────────────────────┤") 46 for i := range addonDescriptor.Repositories { 47 log.Entry().Infof("│ %-30v │ %-20v │", addonDescriptor.Repositories[i].Name, addonDescriptor.Repositories[i].VersionYAML) 48 } 49 log.Entry().Info("╰────────────────────────────────┴──────────────────────╯") 50 51 packagesWithRepos, err := reservePackages(addonDescriptor.Repositories, *conn) 52 if err != nil { 53 return err 54 } 55 56 log.Entry().Info("... checking for ongoing Reservations") 57 if err = pollReserveNextPackages(packagesWithRepos, utils); err != nil { 58 return err 59 } 60 61 log.Entry().Info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓") 62 log.Entry().Infof("┃ %-30v ┃ %-20v ┃ %-5v ┃ %-6v ┃ %-10v ┃ %-40v ┃ %-40v ┃", "Software Component", "Package Name", "Type", "Status", "Namespace", "CommitID (from addon.yml)", "PredecessorCommitID (from AAKaaS)") 63 log.Entry().Info("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫") 64 for i := range packagesWithRepos { 65 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) 66 } 67 log.Entry().Info("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛") 68 69 log.Entry().Info("... checking and processing provided and received data") 70 if addonDescriptor.Repositories, err = checkAndCopyFieldsToRepositories(packagesWithRepos); err != nil { 71 return err 72 } 73 74 log.Entry().Info("... writing AddonDescriptor (package name, type, status, namespace and predecessorCommitID) back to CommonPipelineEnvironment") 75 cpe.abap.addonDescriptor = addonDescriptor.AsJSONstring() 76 return nil 77 } 78 79 func checkAndCopyFieldsToRepositories(pckgWR []aakaas.PackageWithRepository) ([]abaputils.Repository, error) { 80 var repos []abaputils.Repository 81 var checkFailure error = nil 82 for i := range pckgWR { 83 checkFailure = checkCommitID(pckgWR, i, checkFailure) 84 85 pckgWR[i].Package.CopyFieldsToRepo(&pckgWR[i].Repo) 86 repos = append(repos, pckgWR[i].Repo) 87 } 88 return repos, checkFailure 89 } 90 91 func checkCommitID(pckgWR []aakaas.PackageWithRepository, i int, checkFailure error) error { 92 if !pckgWR[i].Repo.UseClassicCTS { 93 if pckgWR[i].Package.Status == aakaas.PackageStatusReleased { 94 checkFailure = checkCommitIDSameAsGiven(pckgWR, i, checkFailure) 95 } else if pckgWR[i].Package.PredecessorCommitID != "" { 96 checkFailure = checkCommitIDNotSameAsPrevious(pckgWR, i, checkFailure) 97 } 98 } 99 return checkFailure 100 } 101 102 func checkCommitIDSameAsGiven(pckgWR []aakaas.PackageWithRepository, i int, checkFailure error) error { 103 //Ensure for Packages with Status R that CommitID of package = the one from addon.yml, beware of short commitID in addon.yml 104 addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID) 105 if len(pckgWR[i].Package.CommitID) < addonYAMLcommitIDLength { 106 checkFailure = errors.New("Provided CommitIDs have wrong length: " + pckgWR[i].Repo.CommitID + "(addon.yml) longer than the one from AAKaaS " + pckgWR[i].Package.CommitID) 107 log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution") 108 } else { 109 packageCommitIDsubsting := pckgWR[i].Package.CommitID[0:addonYAMLcommitIDLength] 110 if pckgWR[i].Repo.CommitID != packageCommitIDsubsting { 111 log.Entry().Error("package " + pckgWR[i].Package.PackageName + " was already build but with commit " + pckgWR[i].Package.CommitID + ", not with " + pckgWR[i].Repo.CommitID) 112 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) 113 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") 114 checkFailure = errors.New("commit of already released package does not match with addon.yml") 115 log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution") 116 } 117 } 118 return checkFailure 119 } 120 121 func checkCommitIDNotSameAsPrevious(pckgWR []aakaas.PackageWithRepository, i int, checkFailure error) error { 122 //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] 123 addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID) 124 if len(pckgWR[i].Package.PredecessorCommitID) < addonYAMLcommitIDLength { 125 checkFailure = errors.New("Provided CommitIDs have wrong length: " + pckgWR[i].Repo.CommitID + "(addon.yml) longer than the one from AAKaaS " + pckgWR[i].Package.CommitID) 126 log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution") 127 } else { 128 packagePredecessorCommitIDsubsting := pckgWR[i].Package.PredecessorCommitID[0:addonYAMLcommitIDLength] 129 if pckgWR[i].Repo.CommitID == packagePredecessorCommitIDsubsting { 130 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") 131 log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution") 132 } 133 } 134 return checkFailure 135 } 136 137 func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, utils *aakaas.AakUtils) error { 138 pollingInterval := (*utils).GetPollingInterval() 139 timeout := time.After((*utils).GetMaxRuntime()) 140 ticker := time.Tick(pollingInterval) 141 for { 142 select { 143 case <-timeout: 144 return errors.New("Timed out") 145 case <-ticker: 146 var allFinished bool = true 147 for i := range pckgWR { 148 if pckgWR[i].Package.Status == aakaas.PackageStatusReleased { 149 continue 150 } 151 err := pckgWR[i].Package.GetPackageAndNamespace() 152 // if there is an error, reservation is not yet finished 153 if err != nil { 154 log.Entry().Infof("Reservation of %s is not yet finished, check again in %s", pckgWR[i].Package.PackageName, pollingInterval) 155 allFinished = false 156 } else { 157 switch pckgWR[i].Package.Status { 158 case aakaas.PackageStatusLocked: 159 return fmt.Errorf("Package %s has invalid status 'locked'", pckgWR[i].Package.PackageName) 160 case aakaas.PackageStatusCreationTriggered: 161 log.Entry().Infof("Reservation of %s is still running with status 'creation triggered', check again in %s", pckgWR[i].Package.PackageName, pollingInterval) 162 allFinished = false 163 case aakaas.PackageStatusPlanned: 164 log.Entry().Infof("Reservation of %s was successful with status 'planned'", pckgWR[i].Package.PackageName) 165 case aakaas.PackageStatusReleased: 166 log.Entry().Infof("Reservation of %s not needed, package is already in status 'released'", pckgWR[i].Package.PackageName) 167 default: 168 return fmt.Errorf("Package %s has unknown status '%s'", pckgWR[i].Package.PackageName, pckgWR[i].Package.Status) 169 } 170 } 171 } 172 if allFinished { 173 log.Entry().Infof(" => Reservations of package(s) finished successfully") 174 return nil 175 } 176 } 177 } 178 } 179 180 func reservePackages(repositories []abaputils.Repository, conn abapbuild.Connector) ([]aakaas.PackageWithRepository, error) { 181 var packagesWithRepos []aakaas.PackageWithRepository 182 for i := range repositories { 183 var p aakaas.Package 184 p.InitPackage(repositories[i], conn) 185 err := p.ReserveNext() 186 if err != nil { 187 return packagesWithRepos, err 188 } 189 pWR := aakaas.PackageWithRepository{ 190 Package: p, 191 Repo: repositories[i], 192 } 193 packagesWithRepos = append(packagesWithRepos, pWR) 194 } 195 return packagesWithRepos, nil 196 }