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

     1  package v3action
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  )
    12  
    13  func (actor Actor) CancelDeploymentByAppNameAndSpace(appName string, spaceGUID string) (Warnings, error) {
    14  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    15  	if err != nil {
    16  		return warnings, err
    17  	}
    18  
    19  	deploymentGUID, deploymentWarnings, err := actor.GetCurrentDeployment(app.GUID)
    20  	warnings = append(warnings, deploymentWarnings...)
    21  	if err != nil {
    22  		return warnings, err
    23  	}
    24  
    25  	apiWarnings, err := actor.CloudControllerClient.CancelDeployment(deploymentGUID)
    26  	warnings = append(warnings, apiWarnings...)
    27  
    28  	return warnings, err
    29  }
    30  
    31  func (actor Actor) CreateDeployment(appGUID string, dropletGUID string) (string, Warnings, error) {
    32  	deploymentGUID, warnings, err := actor.CloudControllerClient.CreateApplicationDeployment(appGUID, dropletGUID)
    33  
    34  	return deploymentGUID, Warnings(warnings), err
    35  }
    36  
    37  // TODO: add tests for get current deployment
    38  
    39  func (actor Actor) GetCurrentDeployment(appGUID string) (string, Warnings, error) {
    40  	var collectedWarnings Warnings
    41  	deployments, warnings, err := actor.CloudControllerClient.GetDeployments(
    42  		ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{appGUID}},
    43  		ccv3.Query{Key: ccv3.OrderBy, Values: []string{ccv3.CreatedAtDescendingOrder}},
    44  		ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}},
    45  	)
    46  	collectedWarnings = append(collectedWarnings, warnings...)
    47  	if err != nil {
    48  		return "", collectedWarnings, err
    49  	}
    50  
    51  	if len(deployments) < 1 {
    52  		return "", collectedWarnings, errors.New("failed to find a deployment for that app")
    53  	}
    54  
    55  	return deployments[0].GUID, collectedWarnings, nil
    56  }
    57  
    58  func (actor Actor) GetDeploymentState(deploymentGUID string) (constant.DeploymentState, Warnings, error) {
    59  	deployment, warnings, err := actor.CloudControllerClient.GetDeployment(deploymentGUID)
    60  	if err != nil {
    61  		return "", Warnings(warnings), err
    62  	}
    63  	return deployment.State, Warnings(warnings), nil
    64  }
    65  
    66  func (actor Actor) PollDeployment(deploymentGUID string, warningsChannel chan<- Warnings) error {
    67  	timeout := time.Now().Add(actor.Config.StartupTimeout())
    68  	for time.Now().Before(timeout) {
    69  		deploymentState, warnings, err := actor.GetDeploymentState(deploymentGUID)
    70  		warningsChannel <- Warnings(warnings)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		switch deploymentState {
    75  		case constant.DeploymentDeployed:
    76  			return nil
    77  		case constant.DeploymentCanceled:
    78  			return errors.New("Deployment has been canceled")
    79  		case constant.DeploymentDeploying:
    80  			time.Sleep(actor.Config.PollingInterval())
    81  		}
    82  	}
    83  
    84  	return actionerror.StartupTimeoutError{}
    85  
    86  }
    87  
    88  func (actor Actor) ZeroDowntimePollStart(appGUID string, warningsChannel chan<- Warnings) error {
    89  	processes, warnings, err := actor.CloudControllerClient.GetApplicationProcesses(appGUID)
    90  	warningsChannel <- Warnings(warnings)
    91  
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	timeout := time.Now().Add(actor.Config.StartupTimeout())
    97  	for time.Now().Before(timeout) {
    98  		deployingProcess := getDeployingProcess(processes)
    99  		if deployingProcess == nil {
   100  			return nil
   101  		}
   102  		ready, err := actor.processStatus(*deployingProcess, warningsChannel)
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		if ready {
   108  			return nil
   109  		}
   110  
   111  		time.Sleep(actor.Config.PollingInterval())
   112  	}
   113  
   114  	return actionerror.StartupTimeoutError{}
   115  }
   116  
   117  func getDeployingProcess(processes []ccv3.Process) *ccv3.Process {
   118  	deployingMatcher, _ := regexp.Compile("web-deployment-.*")
   119  	for _, process := range processes {
   120  		if deployingMatcher.MatchString(process.Type) {
   121  			return &process
   122  		}
   123  	}
   124  	return nil
   125  }