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