github.com/jaylevin/jenkins-library@v1.230.4/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 conn := new(abapbuild.Connector) 28 if err := conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, *utils); err != nil { 29 return err 30 } 31 32 addonDescriptor := new(abaputils.AddonDescriptor) 33 if err := addonDescriptor.InitFromJSONstring(config.AddonDescriptor); err != nil { 34 return errors.Wrap(err, "Reading AddonDescriptor failed [Make sure abapAddonAssemblyKit...CheckCVs|CheckPV steps have been run before]") 35 } 36 37 packagesWithRepos, err := reservePackages(addonDescriptor.Repositories, *conn) 38 if err != nil { 39 return err 40 } 41 42 if err = pollReserveNextPackages(packagesWithRepos, utils); err != nil { 43 return err 44 } 45 46 addonDescriptor.Repositories, err = checkAndCopyFieldsToRepositories(packagesWithRepos) 47 if err != nil { 48 return err 49 } 50 51 log.Entry().Info("Writing package names, types, status, namespace and predecessorCommitID to CommonPipelineEnvironment") 52 cpe.abap.addonDescriptor = addonDescriptor.AsJSONstring() 53 return nil 54 } 55 56 func checkAndCopyFieldsToRepositories(pckgWR []aakaas.PackageWithRepository) ([]abaputils.Repository, error) { 57 var repos []abaputils.Repository 58 59 log.Entry().Infof("%-30v | %-20v | %-6v | %-40v | %-40v", "Software Component", "Package", "Status", "CommitID (from addon.yml)", "PredecessorCommitID (from AAKaaS)") 60 61 for i := range pckgWR { 62 63 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) 64 65 if !pckgWR[i].Repo.UseClassicCTS { 66 if pckgWR[i].Package.Status == aakaas.PackageStatusReleased { 67 //Ensure for Packages with Status R that CommitID of package = the one from addon.yml, beware of short commitID in addon.yml 68 addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID) 69 if len(pckgWR[i].Package.CommitID) < addonYAMLcommitIDLength { 70 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) 71 } 72 packageCommitIDsubsting := pckgWR[i].Package.CommitID[0:addonYAMLcommitIDLength] 73 if pckgWR[i].Repo.CommitID != packageCommitIDsubsting { 74 log.Entry().Error("package " + pckgWR[i].Package.PackageName + " was already build but with commit " + pckgWR[i].Package.CommitID + ", not with " + pckgWR[i].Repo.CommitID) 75 log.Entry().Error("If you want to build a new package make sure to increase the dotted-version-string in addon.yml") 76 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") 77 return repos, errors.New("commit of released package does not match with addon.yml") 78 } 79 } else if pckgWR[i].Package.PredecessorCommitID != "" { 80 //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] 81 addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID) 82 if len(pckgWR[i].Package.PredecessorCommitID) < addonYAMLcommitIDLength { 83 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) 84 } 85 packagePredecessorCommitIDsubsting := pckgWR[i].Package.PredecessorCommitID[0:addonYAMLcommitIDLength] 86 if pckgWR[i].Repo.CommitID == packagePredecessorCommitIDsubsting { 87 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") 88 } 89 } 90 } 91 92 pckgWR[i].Package.CopyFieldsToRepo(&pckgWR[i].Repo) 93 repos = append(repos, pckgWR[i].Repo) 94 } 95 return repos, nil 96 } 97 98 func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, utils *aakaas.AakUtils) error { 99 pollingInterval := (*utils).GetPollingInterval() 100 timeout := time.After((*utils).GetMaxRuntime()) 101 ticker := time.Tick(pollingInterval) 102 for { 103 select { 104 case <-timeout: 105 return errors.New("Timed out") 106 case <-ticker: 107 var allFinished bool = true 108 for i := range pckgWR { 109 err := pckgWR[i].Package.GetPackageAndNamespace() 110 // if there is an error, reservation is not yet finished 111 if err != nil { 112 log.Entry().Infof("Reservation of %s is not yet finished, check again in %s", pckgWR[i].Package.PackageName, pollingInterval) 113 allFinished = false 114 } else { 115 switch pckgWR[i].Package.Status { 116 case aakaas.PackageStatusLocked: 117 return fmt.Errorf("Package %s has invalid status 'locked'", pckgWR[i].Package.PackageName) 118 case aakaas.PackageStatusCreationTriggered: 119 log.Entry().Infof("Reservation of %s is still running with status 'creation triggered', check again in %s", pckgWR[i].Package.PackageName, pollingInterval) 120 allFinished = false 121 case aakaas.PackageStatusPlanned: 122 log.Entry().Infof("Reservation of %s was successful with status 'planned'", pckgWR[i].Package.PackageName) 123 case aakaas.PackageStatusReleased: 124 log.Entry().Infof("Reservation of %s not needed, package is already in status 'released'", pckgWR[i].Package.PackageName) 125 default: 126 return fmt.Errorf("Package %s has unknown status '%s'", pckgWR[i].Package.PackageName, pckgWR[i].Package.Status) 127 } 128 } 129 } 130 if allFinished { 131 log.Entry().Infof("Reservation of package(s) was successful") 132 return nil 133 } 134 } 135 } 136 } 137 138 func reservePackages(repositories []abaputils.Repository, conn abapbuild.Connector) ([]aakaas.PackageWithRepository, error) { 139 var packagesWithRepos []aakaas.PackageWithRepository 140 for i := range repositories { 141 var p aakaas.Package 142 p.InitPackage(repositories[i], conn) 143 err := p.ReserveNext() 144 if err != nil { 145 return packagesWithRepos, err 146 } 147 pWR := aakaas.PackageWithRepository{ 148 Package: p, 149 Repo: repositories[i], 150 } 151 packagesWithRepos = append(packagesWithRepos, pWR) 152 } 153 return packagesWithRepos, nil 154 }