github.com/xgoffin/jenkins-library@v1.154.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  	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].Package.Status == aakaas.PackageStatusReleased {
    66  			//Ensure for Packages with Status R that CommitID of package = the one from addon.yml, beware of short commitID in addon.yml
    67  			addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID)
    68  			if len(pckgWR[i].Package.CommitID) < addonYAMLcommitIDLength {
    69  				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)
    70  			}
    71  			packageCommitIDsubsting := pckgWR[i].Package.CommitID[0:addonYAMLcommitIDLength]
    72  			if pckgWR[i].Repo.CommitID != packageCommitIDsubsting {
    73  				log.Entry().Error("package " + pckgWR[i].Package.PackageName + " was already build but with commit " + pckgWR[i].Package.CommitID + ", not with " + pckgWR[i].Repo.CommitID)
    74  				log.Entry().Error("If you want to build a new package make sure to increase the dotted-version-string in addon.yml")
    75  				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")
    76  				return repos, errors.New("commit of released package does not match with addon.yml")
    77  			}
    78  		} else if pckgWR[i].Package.PredecessorCommitID != "" {
    79  			//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]
    80  			addonYAMLcommitIDLength := len(pckgWR[i].Repo.CommitID)
    81  			if len(pckgWR[i].Package.PredecessorCommitID) < addonYAMLcommitIDLength {
    82  				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)
    83  			}
    84  			packagePredecessorCommitIDsubsting := pckgWR[i].Package.PredecessorCommitID[0:addonYAMLcommitIDLength]
    85  			if pckgWR[i].Repo.CommitID == packagePredecessorCommitIDsubsting {
    86  				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")
    87  			}
    88  		}
    89  
    90  		pckgWR[i].Package.CopyFieldsToRepo(&pckgWR[i].Repo)
    91  		repos = append(repos, pckgWR[i].Repo)
    92  	}
    93  	return repos, nil
    94  }
    95  
    96  func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, utils *aakaas.AakUtils) error {
    97  	pollingInterval := (*utils).GetPollingInterval()
    98  	timeout := time.After((*utils).GetMaxRuntime())
    99  	ticker := time.Tick(pollingInterval)
   100  	for {
   101  		select {
   102  		case <-timeout:
   103  			return errors.New("Timed out")
   104  		case <-ticker:
   105  			var allFinished bool = true
   106  			for i := range pckgWR {
   107  				err := pckgWR[i].Package.GetPackageAndNamespace()
   108  				// if there is an error, reservation is not yet finished
   109  				if err != nil {
   110  					log.Entry().Infof("Reservation of %s is not yet finished, check again in %s", pckgWR[i].Package.PackageName, pollingInterval)
   111  					allFinished = false
   112  				} else {
   113  					switch pckgWR[i].Package.Status {
   114  					case aakaas.PackageStatusLocked:
   115  						return fmt.Errorf("Package %s has invalid status 'locked'", pckgWR[i].Package.PackageName)
   116  					case aakaas.PackageStatusCreationTriggered:
   117  						log.Entry().Infof("Reservation of %s is still running with status 'creation triggered', check again in %s", pckgWR[i].Package.PackageName, pollingInterval)
   118  						allFinished = false
   119  					case aakaas.PackageStatusPlanned:
   120  						log.Entry().Infof("Reservation of %s was successful with status 'planned'", pckgWR[i].Package.PackageName)
   121  					case aakaas.PackageStatusReleased:
   122  						log.Entry().Infof("Reservation of %s not needed, package is already in status 'released'", pckgWR[i].Package.PackageName)
   123  					default:
   124  						return fmt.Errorf("Package %s has unknown status '%s'", pckgWR[i].Package.PackageName, pckgWR[i].Package.Status)
   125  					}
   126  				}
   127  			}
   128  			if allFinished {
   129  				log.Entry().Infof("Reservation of package(s) was successful")
   130  				return nil
   131  			}
   132  		}
   133  	}
   134  }
   135  
   136  func reservePackages(repositories []abaputils.Repository, conn abapbuild.Connector) ([]aakaas.PackageWithRepository, error) {
   137  	var packagesWithRepos []aakaas.PackageWithRepository
   138  	for i := range repositories {
   139  		var p aakaas.Package
   140  		p.InitPackage(repositories[i], conn)
   141  		err := p.ReserveNext()
   142  		if err != nil {
   143  			return packagesWithRepos, err
   144  		}
   145  		pWR := aakaas.PackageWithRepository{
   146  			Package: p,
   147  			Repo:    repositories[i],
   148  		}
   149  		packagesWithRepos = append(packagesWithRepos, pWR)
   150  	}
   151  	return packagesWithRepos, nil
   152  }