1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00
sap-jenkins-library/cmd/abapAddonAssemblyKitReserveNextPackages.go
tiloKo fa17611a59
AAKaaS: 7 reserve next package (#2046)
* adding my steps

* messy step

* Update abapEnvironmentAssembly.go

* clean up

* change yaml

* corrections

* Update cloudFoundryDeploy.go

* update

* delete simulation step

* remove simulate

* Update PiperGoUtils.groovy

* Update PiperGoUtils.groovy

* Update CommonStepsTest.groovy

* add docu

* Update abapEnvironmentAssembly.md

* changes due to PR

* Update .gitignore

* b

* CV list

* Update abapEnvironmentAssembly.go

* testing with simulation

* Update abapEnvironmentAssembly.go

* remove simulation

* renaming

* Update mkdocs.yml

* moving service key to yaml and fixing code climate

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* change input

* Update abapEnvironmentAssemblePackages.go

* change json tag

* fixed error handling

* documentation

* Update abapEnvironmentAssemblePackages.md

* Update abapEnvironmentAssemblePackages.md

* fixing code climate issues

* fixing code climate issues

* Update abapEnvironmentAssemblePackages.yaml

* fixing code climate issues

* Update abapEnvironmentAssemblePackages.yaml

* adding unittests

* adding unittests and improved logging

* yaml -> json

* change scope of cfServiceKeyName

* correct indentation

* Update CommonStepsTest.groovy

* maintain correct step order

* AAKaaS register package

* AAKaaS register package #2

* Update documentation/docs/steps/abapAddonAssemblyKitPublishTargetVector.md

Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com>

* AAKaaS register package #3

* AAKaaS release package

* Update abapAddonAssemblyKitReleasePackages.go

* AAKaas release package #2

* AAKaaS reserve

* AAKaaS reserve #2

Co-authored-by: rosemarieB <45030247+rosemarieB@users.noreply.github.com>
Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2020-09-18 17:27:04 +02:00

124 lines
4.8 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"time"
"github.com/SAP/jenkins-library/pkg/abap/aakaas"
abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
"github.com/SAP/jenkins-library/pkg/abaputils"
"github.com/SAP/jenkins-library/pkg/command"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
func abapAddonAssemblyKitReserveNextPackages(config abapAddonAssemblyKitReserveNextPackagesOptions, telemetryData *telemetry.CustomData, cpe *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment) {
// for command execution use Command
c := command.Command{}
// reroute command output to logging framework
c.Stdout(log.Writer())
c.Stderr(log.Writer())
client := piperhttp.Client{}
maxRuntimeInMinutes := time.Duration(5 * time.Minute)
pollIntervalsInSeconds := time.Duration(30 * time.Second)
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
err := runAbapAddonAssemblyKitReserveNextPackages(&config, telemetryData, &client, cpe, maxRuntimeInMinutes, pollIntervalsInSeconds)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runAbapAddonAssemblyKitReserveNextPackages(config *abapAddonAssemblyKitReserveNextPackagesOptions, telemetryData *telemetry.CustomData, client piperhttp.Sender,
cpe *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment, maxRuntimeInMinutes time.Duration, pollIntervalsInSeconds time.Duration) error {
conn := new(abapbuild.Connector)
conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client)
var addonDescriptor abaputils.AddonDescriptor
json.Unmarshal([]byte(config.AddonDescriptor), &addonDescriptor)
packagesWithRepos, err := reservePackages(addonDescriptor.Repositories, *conn)
if err != nil {
return err
}
err = pollReserveNextPackages(packagesWithRepos, maxRuntimeInMinutes, pollIntervalsInSeconds)
if err != nil {
return err
}
addonDescriptor.Repositories = copyFieldsToRepositories(packagesWithRepos)
log.Entry().Info("Writing package names, types, status, namespace and predecessorCommitID to CommonPipelineEnvironment")
backToCPE, _ := json.Marshal(addonDescriptor)
cpe.abap.addonDescriptor = string(backToCPE)
return nil
}
func copyFieldsToRepositories(pckgWR []aakaas.PackageWithRepository) []abaputils.Repository {
var repos []abaputils.Repository
for i := range pckgWR {
pckgWR[i].Package.CopyFieldsToRepo(&pckgWR[i].Repo)
repos = append(repos, pckgWR[i].Repo)
}
return repos
}
func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, maxRuntimeInMinutes time.Duration, pollIntervalsInSeconds time.Duration) error {
timeout := time.After(maxRuntimeInMinutes)
ticker := time.Tick(pollIntervalsInSeconds)
for {
select {
case <-timeout:
return errors.New("Timed out")
case <-ticker:
var allFinished bool = true
for i := range pckgWR {
err := pckgWR[i].Package.GetPackageAndNamespace()
// if there is an error, reservation is not yet finished
if err != nil {
log.Entry().Infof("Reservation of %s is not yet finished, check again in %s", pckgWR[i].Package.PackageName, pollIntervalsInSeconds)
allFinished = false
} else {
switch pckgWR[i].Package.Status {
case aakaas.PackageStatusLocked:
return fmt.Errorf("Package %s has invalid status 'locked'", pckgWR[i].Package.PackageName)
case aakaas.PackageStatusCreationTriggered:
log.Entry().Infof("Reservation of %s is still running with status 'creation triggered', check again in %s", pckgWR[i].Package.PackageName, pollIntervalsInSeconds)
allFinished = false
case aakaas.PackageStatusPlanned:
log.Entry().Infof("Reservation of %s was succesful with status 'planned'", pckgWR[i].Package.PackageName)
case aakaas.PackageStatusReleased:
log.Entry().Infof("Reservation of %s not needed, package is already in status 'released'", pckgWR[i].Package.PackageName)
default:
return fmt.Errorf("Package %s has unknown status '%s'", pckgWR[i].Package.PackageName, pckgWR[i].Package.Status)
}
}
}
if allFinished {
log.Entry().Infof("Reservation of package(s) was succesful")
return nil
}
}
}
}
func reservePackages(repositories []abaputils.Repository, conn abapbuild.Connector) ([]aakaas.PackageWithRepository, error) {
var packagesWithRepos []aakaas.PackageWithRepository
for i := range repositories {
var p aakaas.Package
p.InitPackage(repositories[i], conn)
err := p.ReserveNext()
if err != nil {
return packagesWithRepos, err
}
pWR := aakaas.PackageWithRepository{
Package: p,
Repo: repositories[i],
}
packagesWithRepos = append(packagesWithRepos, pWR)
}
return packagesWithRepos, nil
}