github.com/loafoe/cli@v7.1.0+incompatible/actor/v3action/build.go (about)

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