github.com/SAP/jenkins-library@v1.362.0/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, config.AbapAddonAssemblyKitOriginHash, config.AbapAddonAssemblyKitCertificateFile, config.AbapAddonAssemblyKitCertificatePass); 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 (and AAKaaS had due to a bug some time also short commid IDs) 104 AAKaaSCommitId := pckgWR[i].Package.CommitID 105 AddonYAMLCommitId := pckgWR[i].Repo.CommitID 106 107 var commitIdLength int 108 //determine shortes commitID length 109 if len(AAKaaSCommitId) >= len(AddonYAMLCommitId) { 110 commitIdLength = len(AddonYAMLCommitId) 111 } else { 112 commitIdLength = len(AAKaaSCommitId) 113 } 114 115 //shorten both to common length 116 AAKaaSCommitId = AAKaaSCommitId[0:commitIdLength] 117 AddonYAMLCommitId = AddonYAMLCommitId[0:commitIdLength] 118 119 if AddonYAMLCommitId != AAKaaSCommitId { 120 log.Entry().Error("package " + pckgWR[i].Package.PackageName + " was already build but with commit " + pckgWR[i].Package.CommitID + ", not with " + pckgWR[i].Repo.CommitID) 121 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) 122 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") 123 checkFailure = errors.New("commit of already released package does not match with addon.yml") 124 log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution") 125 } 126 return checkFailure 127 } 128 129 func checkCommitIDNotSameAsPrevious(pckgWR []aakaas.PackageWithRepository, i int, checkFailure error) error { 130 //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] 131 AAKaaSPreviousCommitId := pckgWR[i].Package.PredecessorCommitID 132 AddonYAMLCommitId := pckgWR[i].Repo.CommitID 133 134 var commitIdLength int 135 //determine shortes commitID length 136 if len(AAKaaSPreviousCommitId) >= len(AddonYAMLCommitId) { 137 commitIdLength = len(AddonYAMLCommitId) 138 } else { 139 commitIdLength = len(AAKaaSPreviousCommitId) 140 } 141 142 AAKaaSPreviousCommitId = AAKaaSPreviousCommitId[0:commitIdLength] 143 AddonYAMLCommitId = AddonYAMLCommitId[0:commitIdLength] 144 145 if AddonYAMLCommitId == AAKaaSPreviousCommitId { 146 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") 147 log.Entry().WithError(checkFailure).Error(" => Check failure: to be corrected in addon.yml prior next execution") 148 } 149 150 return checkFailure 151 } 152 153 func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, utils *aakaas.AakUtils) error { 154 pollingInterval := (*utils).GetPollingInterval() 155 timeout := time.After((*utils).GetMaxRuntime()) 156 ticker := time.Tick(pollingInterval) 157 for { 158 select { 159 case <-timeout: 160 return errors.New("Timed out") 161 case <-ticker: 162 var allFinished bool = true 163 for i := range pckgWR { 164 if pckgWR[i].Package.Status == aakaas.PackageStatusReleased { 165 continue 166 } 167 err := pckgWR[i].Package.GetPackageAndNamespace() 168 // if there is an error, reservation is not yet finished 169 if err != nil { 170 log.Entry().Infof("Reservation of %s is not yet finished, check again in %s", pckgWR[i].Package.PackageName, pollingInterval) 171 allFinished = false 172 } else { 173 switch pckgWR[i].Package.Status { 174 case aakaas.PackageStatusLocked: 175 return fmt.Errorf("Package %s has invalid status 'locked'", pckgWR[i].Package.PackageName) 176 case aakaas.PackageStatusCreationTriggered: 177 log.Entry().Infof("Reservation of %s is still running with status 'creation triggered', check again in %s", pckgWR[i].Package.PackageName, pollingInterval) 178 allFinished = false 179 case aakaas.PackageStatusPlanned: 180 log.Entry().Infof("Reservation of %s was successful with status 'planned'", pckgWR[i].Package.PackageName) 181 case aakaas.PackageStatusReleased: 182 log.Entry().Infof("Reservation of %s not needed, package is already in status 'released'", pckgWR[i].Package.PackageName) 183 default: 184 return fmt.Errorf("Package %s has unknown status '%s'", pckgWR[i].Package.PackageName, pckgWR[i].Package.Status) 185 } 186 } 187 } 188 if allFinished { 189 log.Entry().Infof(" => Reservations of package(s) finished successfully") 190 return nil 191 } 192 } 193 } 194 } 195 196 func reservePackages(repositories []abaputils.Repository, conn abapbuild.Connector) ([]aakaas.PackageWithRepository, error) { 197 var packagesWithRepos []aakaas.PackageWithRepository 198 for i := range repositories { 199 var p aakaas.Package 200 p.InitPackage(repositories[i], conn) 201 err := p.ReserveNext() 202 if err != nil { 203 return packagesWithRepos, err 204 } 205 pWR := aakaas.PackageWithRepository{ 206 Package: p, 207 Repo: repositories[i], 208 } 209 packagesWithRepos = append(packagesWithRepos, pWR) 210 } 211 return packagesWithRepos, nil 212 }