github.com/arunkumar7540/cli@v6.45.0+incompatible/actor/v7action/build.go (about)

     1  package v7action
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  
    11  	log "github.com/sirupsen/logrus"
    12  )
    13  
    14  type Build struct {
    15  	GUID string
    16  }
    17  
    18  func (actor Actor) StagePackage(packageGUID string, appName string) (<-chan Droplet, <-chan Warnings, <-chan error) {
    19  	dropletStream := make(chan Droplet)
    20  	warningsStream := make(chan Warnings)
    21  	errorStream := make(chan error)
    22  	go func() {
    23  		defer close(dropletStream)
    24  		defer close(warningsStream)
    25  		defer close(errorStream)
    26  
    27  		build := ccv3.Build{PackageGUID: packageGUID}
    28  		build, allWarnings, err := actor.CloudControllerClient.CreateBuild(build)
    29  		warningsStream <- Warnings(allWarnings)
    30  
    31  		if err != nil {
    32  			errorStream <- err
    33  			return
    34  		}
    35  
    36  		timeout := time.Now().Add(actor.Config.StagingTimeout())
    37  
    38  		for time.Now().Before(timeout) {
    39  			var warnings ccv3.Warnings
    40  			build, warnings, err = actor.CloudControllerClient.GetBuild(build.GUID)
    41  			warningsStream <- Warnings(warnings)
    42  			if err != nil {
    43  				errorStream <- err
    44  				return
    45  			}
    46  
    47  			switch build.State {
    48  			case constant.BuildFailed:
    49  				errorStream <- errors.New(build.Error)
    50  				return
    51  			case constant.BuildStaging:
    52  				time.Sleep(actor.Config.PollingInterval())
    53  			default:
    54  
    55  				//TODO: uncomment after #150569020
    56  				// ccv3Droplet, warnings, err := actor.CloudControllerClient.GetDroplet(build.DropletGUID)
    57  				// warningsStream <- Warnings(warnings)
    58  				// if err != nil {
    59  				// 	errorStream <- err
    60  				// 	return
    61  				// }
    62  
    63  				ccv3Droplet := ccv3.Droplet{
    64  					GUID:      build.DropletGUID,
    65  					State:     constant.DropletState(build.State),
    66  					CreatedAt: build.CreatedAt,
    67  				}
    68  
    69  				dropletStream <- actor.convertCCToActorDroplet(ccv3Droplet)
    70  				return
    71  			}
    72  		}
    73  
    74  		errorStream <- actionerror.StagingTimeoutError{AppName: appName, Timeout: actor.Config.StagingTimeout()}
    75  	}()
    76  
    77  	return dropletStream, warningsStream, errorStream
    78  }
    79  
    80  func (actor Actor) StageApplicationPackage(packageGUID string) (Build, Warnings, error) {
    81  	var allWarnings Warnings
    82  
    83  	build := ccv3.Build{PackageGUID: packageGUID}
    84  	build, warnings, err := actor.CloudControllerClient.CreateBuild(build)
    85  	log.Debug("created build")
    86  	allWarnings = append(allWarnings, warnings...)
    87  	if err != nil {
    88  		return Build{}, allWarnings, err
    89  	}
    90  
    91  	log.Debug("no errors creating build")
    92  	return Build{GUID: build.GUID}, allWarnings, nil
    93  }
    94  
    95  func (actor Actor) PollBuild(buildGUID string, appName string) (Droplet, Warnings, error) {
    96  	var allWarnings Warnings
    97  
    98  	timeout := time.After(actor.Config.StagingTimeout())
    99  	interval := time.NewTimer(0)
   100  
   101  	for {
   102  		select {
   103  		case <-interval.C:
   104  			build, warnings, err := actor.CloudControllerClient.GetBuild(buildGUID)
   105  			allWarnings = append(allWarnings, warnings...)
   106  			if err != nil {
   107  				return Droplet{}, allWarnings, err
   108  			}
   109  
   110  			switch build.State {
   111  			case constant.BuildFailed:
   112  				return Droplet{}, allWarnings, errors.New(build.Error)
   113  
   114  			case constant.BuildStaged:
   115  				droplet, warnings, err := actor.CloudControllerClient.GetDroplet(build.DropletGUID)
   116  				allWarnings = append(allWarnings, warnings...)
   117  				if err != nil {
   118  					return Droplet{}, allWarnings, err
   119  				}
   120  
   121  				return Droplet{
   122  					GUID:      droplet.GUID,
   123  					State:     droplet.State,
   124  					CreatedAt: droplet.CreatedAt,
   125  				}, allWarnings, nil
   126  			}
   127  
   128  			interval.Reset(actor.Config.PollingInterval())
   129  
   130  		case <-timeout:
   131  			return Droplet{}, allWarnings, actionerror.StagingTimeoutError{AppName: appName, Timeout: actor.Config.StagingTimeout()}
   132  		}
   133  	}
   134  }