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