forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply.go
175 lines (153 loc) · 4.48 KB
/
apply.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package pushaction
import (
"os"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
log "github.com/sirupsen/logrus"
)
const PushRetries = 3
// Apply use the V2 API to create/update the application settings and
// eventually uploads the application bits.
//
// If multiple buildpacks are being applied to the application, the V3 API is
// used to set those buildpacks.
func (actor Actor) Apply(config ApplicationConfig, progressBar ProgressBar) (<-chan ApplicationConfig, <-chan Event, <-chan Warnings, <-chan error) {
configStream := make(chan ApplicationConfig)
eventStream := make(chan Event)
warningsStream := make(chan Warnings)
errorStream := make(chan error)
go func() {
log.Debug("starting apply go routine")
defer close(configStream)
defer close(eventStream)
defer close(warningsStream)
defer close(errorStream)
var event Event
var warnings Warnings
var err error
eventStream <- SettingUpApplication
if config.UpdatingApplication() {
config, event, warnings, err = actor.UpdateApplication(config)
} else {
config, event, warnings, err = actor.CreateApplication(config)
}
warningsStream <- warnings
if err != nil {
errorStream <- err
return
}
eventStream <- event
log.Debugf("desired application: %#v", config.DesiredApplication)
if config.NoRoute {
if len(config.CurrentRoutes) > 0 {
eventStream <- UnmappingRoutes
config, warnings, err = actor.UnmapRoutes(config)
warningsStream <- warnings
if err != nil {
errorStream <- err
return
}
}
} else {
eventStream <- CreatingAndMappingRoutes
var createdRoutes bool
config, createdRoutes, warnings, err = actor.CreateRoutes(config)
warningsStream <- warnings
if err != nil {
errorStream <- err
return
}
if createdRoutes {
log.Debugf("updated desired routes: %#v", config.DesiredRoutes)
eventStream <- CreatedRoutes
}
var boundRoutes bool
config, boundRoutes, warnings, err = actor.MapRoutes(config)
warningsStream <- warnings
if err != nil {
errorStream <- err
return
}
if boundRoutes {
log.Debugf("updated desired routes: %#v", config.DesiredRoutes)
eventStream <- BoundRoutes
}
}
if len(config.CurrentServices) != len(config.DesiredServices) {
eventStream <- ConfiguringServices
config, _, warnings, err = actor.BindServices(config)
warningsStream <- warnings
if err != nil {
errorStream <- err
return
}
log.Debugf("bound desired services: %#v", config.DesiredServices)
eventStream <- BoundServices
}
if config.DropletPath != "" {
for count := 0; count < PushRetries; count++ {
warnings, err = actor.UploadDroplet(config, config.DropletPath, progressBar, eventStream)
warningsStream <- warnings
if _, ok := err.(ccerror.PipeSeekError); ok {
eventStream <- RetryUpload
} else {
break
}
}
if err != nil {
if e, ok := err.(ccerror.PipeSeekError); ok {
errorStream <- actionerror.UploadFailedError{Err: e.Err}
} else {
errorStream <- err
}
return
}
} else if config.DesiredApplication.DockerImage == "" {
eventStream <- ResourceMatching
config, warnings = actor.SetMatchedResources(config)
warningsStream <- warnings
if len(config.UnmatchedResources) > 0 {
var archivePath string
archivePath, err = actor.CreateArchive(config)
if err != nil {
errorStream <- err
os.RemoveAll(archivePath)
return
}
eventStream <- CreatingArchive
defer os.RemoveAll(archivePath)
for count := 0; count < PushRetries; count++ {
warnings, err = actor.UploadPackageWithArchive(config, archivePath, progressBar, eventStream)
warningsStream <- warnings
if _, ok := err.(ccerror.PipeSeekError); ok {
eventStream <- RetryUpload
} else {
break
}
}
if err != nil {
if e, ok := err.(ccerror.PipeSeekError); ok {
errorStream <- actionerror.UploadFailedError{Err: e.Err}
} else {
errorStream <- err
}
return
}
} else {
eventStream <- UploadingApplication
warnings, err = actor.UploadPackage(config)
warningsStream <- warnings
if err != nil {
errorStream <- err
return
}
}
} else {
log.WithField("docker_image", config.DesiredApplication.DockerImage).Debug("skipping file upload")
}
configStream <- config
log.Debug("completed apply")
eventStream <- Complete
}()
return configStream, eventStream, warningsStream, errorStream
}