github.com/xgoffin/jenkins-library@v1.154.0/cmd/abapEnvironmentAssembleConfirm.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
     8  	"github.com/SAP/jenkins-library/pkg/abaputils"
     9  	"github.com/SAP/jenkins-library/pkg/command"
    10  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    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 abapEnvironmentAssembleConfirm(config abapEnvironmentAssembleConfirmOptions, telemetryData *telemetry.CustomData, cpe *abapEnvironmentAssembleConfirmCommonPipelineEnvironment) {
    17  	// for command execution use Command
    18  	c := command.Command{}
    19  	// reroute command output to logging framework
    20  	c.Stdout(log.Writer())
    21  	c.Stderr(log.Writer())
    22  
    23  	var autils = abaputils.AbapUtils{
    24  		Exec: &c,
    25  	}
    26  
    27  	client := piperhttp.Client{}
    28  	err := runAbapEnvironmentAssembleConfirm(&config, telemetryData, &autils, &client, cpe)
    29  	if err != nil {
    30  		log.Entry().WithError(err).Fatal("step execution failed")
    31  	}
    32  }
    33  
    34  func runAbapEnvironmentAssembleConfirm(config *abapEnvironmentAssembleConfirmOptions, telemetryData *telemetry.CustomData, com abaputils.Communication, client abapbuild.HTTPSendLoader, cpe *abapEnvironmentAssembleConfirmCommonPipelineEnvironment) error {
    35  	conn := new(abapbuild.Connector)
    36  	var connConfig abapbuild.ConnectorConfiguration
    37  	connConfig.CfAPIEndpoint = config.CfAPIEndpoint
    38  	connConfig.CfOrg = config.CfOrg
    39  	connConfig.CfSpace = config.CfSpace
    40  	connConfig.CfServiceInstance = config.CfServiceInstance
    41  	connConfig.CfServiceKeyName = config.CfServiceKeyName
    42  	connConfig.Host = config.Host
    43  	connConfig.Username = config.Username
    44  	connConfig.Password = config.Password
    45  	connConfig.AddonDescriptor = config.AddonDescriptor
    46  	connConfig.MaxRuntimeInMinutes = config.MaxRuntimeInMinutes
    47  
    48  	err := conn.InitBuildFramework(connConfig, com, client)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	var addonDescriptor abaputils.AddonDescriptor
    53  	err = json.Unmarshal([]byte(config.AddonDescriptor), &addonDescriptor)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	delayBetweenPosts := time.Duration(3 * time.Second)
    58  	builds, err := startingConfirm(addonDescriptor.Repositories, *conn, delayBetweenPosts)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	maxRuntimeInMinutes := time.Duration(config.MaxRuntimeInMinutes) * time.Minute
    63  	pollInterval := time.Duration(60 * time.Second)
    64  	err = polling(builds, maxRuntimeInMinutes, pollInterval)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	err = checkIfFailedAndPrintLogs(builds)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	return nil
    73  }
    74  
    75  func startingConfirm(repos []abaputils.Repository, conn abapbuild.Connector, delayBetweenPosts time.Duration) ([]buildWithRepository, error) {
    76  	var confirmedBuilds []buildWithRepository
    77  	for _, repo := range repos {
    78  		assemblyBuild := abapbuild.Build{
    79  			Connector: conn,
    80  		}
    81  		buildRepo := buildWithRepository{
    82  			build: assemblyBuild,
    83  			repo:  repo,
    84  		}
    85  		if repo.InBuildScope {
    86  			err := buildRepo.startConfirm()
    87  			if err != nil {
    88  				return confirmedBuilds, err
    89  			}
    90  			confirmedBuilds = append(confirmedBuilds, buildRepo)
    91  		} else {
    92  			log.Entry().Infof("Packages %s was not assembled in this pipeline run, thus no need to confirm", repo.PackageName)
    93  		}
    94  
    95  		//as batch events in the ABAP Backend need a little time
    96  		time.Sleep(delayBetweenPosts)
    97  	}
    98  	return confirmedBuilds, nil
    99  }
   100  
   101  func polling(builds []buildWithRepository, maxRuntimeInMinutes time.Duration, pollInterval time.Duration) error {
   102  	timeout := time.After(maxRuntimeInMinutes)
   103  	ticker := time.Tick(pollInterval)
   104  	for {
   105  		select {
   106  		case <-timeout:
   107  			return errors.New("Timed out")
   108  		case <-ticker:
   109  			var allFinished bool = true
   110  			for i := range builds {
   111  				builds[i].build.Get()
   112  				if !builds[i].build.IsFinished() {
   113  					log.Entry().Infof("Assembly of %s is not yet finished, check again in %s", builds[i].repo.PackageName, pollInterval)
   114  					allFinished = false
   115  				}
   116  			}
   117  			if allFinished {
   118  				return nil
   119  			}
   120  		}
   121  	}
   122  }
   123  
   124  func (b *buildWithRepository) startConfirm() error {
   125  	if b.repo.Name == "" || b.repo.Namespace == "" || b.repo.PackageName == "" {
   126  		return errors.New("Parameters missing. Please provide software component name, namespace and packagename")
   127  	}
   128  	valuesInput := abapbuild.Values{
   129  		Values: []abapbuild.Value{
   130  			{
   131  				ValueID: "SWC",
   132  				Value:   b.repo.Name,
   133  			},
   134  			{
   135  				ValueID: "SSDC-delta",
   136  				Value:   b.repo.Namespace + b.repo.PackageName,
   137  			},
   138  		},
   139  	}
   140  	phase := "BUILD_CONFIRM"
   141  	log.Entry().Infof("Starting confirmation of package %s", b.repo.PackageName)
   142  	return b.build.Start(phase, valuesInput)
   143  }