forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_config.go
322 lines (273 loc) · 10.3 KB
/
application_config.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package pushaction
import (
"os"
"path/filepath"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/util/manifest"
log "github.com/sirupsen/logrus"
)
type ApplicationConfig struct {
CurrentApplication Application
DesiredApplication Application
CurrentRoutes []v2action.Route
DesiredRoutes []v2action.Route
NoRoute bool
CurrentServices map[string]v2action.ServiceInstance
DesiredServices map[string]v2action.ServiceInstance
AllResources []v2action.Resource
MatchedResources []v2action.Resource
UnmatchedResources []v2action.Resource
Archive bool
Path string
DropletPath string
}
func (config ApplicationConfig) CreatingApplication() bool {
return config.CurrentApplication.GUID == ""
}
func (config ApplicationConfig) UpdatingApplication() bool {
return !config.CreatingApplication()
}
func (config ApplicationConfig) HasMultipleBuildpacks() bool {
return len(config.DesiredApplication.Buildpacks) > 1
}
// ConvertToApplicationConfigs converts a set of application manifests into an
// application configs. These configs reflect the current and desired states of
// the application - providing details required by the Apply function to
// proceed.
//
// The V2 Actor is primarily used to determine all but multiple buildpack
// information. Only then is the V3 Actor used to gather the multiple
// buildpacks.
func (actor Actor) ConvertToApplicationConfigs(orgGUID string, spaceGUID string, noStart bool, apps []manifest.Application) ([]ApplicationConfig, Warnings, error) {
var configs []ApplicationConfig
var warnings Warnings
log.Infof("iterating through %d app configuration(s)", len(apps))
for _, app := range apps {
config := ApplicationConfig{
NoRoute: app.NoRoute,
}
if app.DropletPath != "" {
absPath, err := filepath.EvalSymlinks(app.DropletPath)
if err != nil {
return nil, nil, err
}
config.DropletPath = absPath
} else {
absPath, err := filepath.EvalSymlinks(app.Path)
if err != nil {
return nil, nil, err
}
config.Path = absPath
}
log.Infoln("searching for app", app.Name)
found, constructedApp, v2Warnings, err := actor.FindOrReturnPartialApp(app.Name, spaceGUID)
warnings = append(warnings, v2Warnings...)
if err != nil {
log.Errorln("app lookup:", err)
return nil, warnings, err
}
if found {
var configWarnings v2action.Warnings
config, configWarnings, err = actor.configureExistingApp(config, app, constructedApp)
warnings = append(warnings, configWarnings...)
if err != nil {
log.Errorln("configuring existing app:", err)
return nil, warnings, err
}
} else {
log.Debug("using empty app as base")
config.DesiredApplication = constructedApp
}
config.DesiredApplication = actor.overrideApplicationProperties(config.DesiredApplication, app, noStart)
var stackWarnings Warnings
config.DesiredApplication, stackWarnings, err = actor.overrideStack(config.DesiredApplication, app)
warnings = append(warnings, stackWarnings...)
if err != nil {
return nil, warnings, err
}
log.Debugln("post overriding config:", config.DesiredApplication)
var serviceWarnings Warnings
config.DesiredServices, serviceWarnings, err = actor.getDesiredServices(config.CurrentServices, app.Services, spaceGUID)
warnings = append(warnings, serviceWarnings...)
if err != nil {
log.Errorln("getting services:", err)
return nil, warnings, err
}
if !config.NoRoute {
var routeWarnings Warnings
config, routeWarnings, err = actor.configureRoutes(app, orgGUID, spaceGUID, config)
warnings = append(warnings, routeWarnings...)
if err != nil {
log.Errorln("determining routes:", err)
return nil, warnings, err
}
}
if app.DockerImage == "" && app.DropletPath == "" {
config, err = actor.configureResources(config)
if err != nil {
log.Errorln("configuring resources", err)
return nil, warnings, err
}
}
configs = append(configs, config)
}
return configs, warnings, nil
}
func (actor Actor) configureRoutes(manifestApp manifest.Application, orgGUID string, spaceGUID string, config ApplicationConfig) (ApplicationConfig, Warnings, error) {
switch {
case len(manifestApp.Routes) > 0: // Routes in manifest mutually exclusive with all other route related fields
routes, warnings, err := actor.CalculateRoutes(manifestApp.Routes, orgGUID, spaceGUID, config.CurrentRoutes)
config.DesiredRoutes = routes
return config, warnings, err
case len(config.CurrentRoutes) > 0 && (manifestApp.RandomRoute || manifestApp.Domain == ""):
config.DesiredRoutes = config.CurrentRoutes
return config, nil, nil
case manifestApp.RandomRoute:
// append random route to current route (becomes desired route)
randomRoute, warnings, err := actor.GenerateRandomRoute(manifestApp, spaceGUID, orgGUID)
config.DesiredRoutes = append(config.CurrentRoutes, randomRoute)
return config, warnings, err
default:
desiredRoute, warnings, err := actor.GetGeneratedRoute(manifestApp, orgGUID, spaceGUID, config.CurrentRoutes)
if err != nil {
log.Errorln("getting default route:", err)
return config, warnings, err
}
config.DesiredRoutes = append(config.CurrentRoutes, desiredRoute)
return config, warnings, nil
}
}
func (actor Actor) getDesiredServices(currentServices map[string]v2action.ServiceInstance, requestedServices []string, spaceGUID string) (map[string]v2action.ServiceInstance, Warnings, error) {
var warnings Warnings
desiredServices := map[string]v2action.ServiceInstance{}
for name, serviceInstance := range currentServices {
log.Debugln("adding bound service:", name)
desiredServices[name] = serviceInstance
}
for _, serviceName := range requestedServices {
if _, ok := desiredServices[serviceName]; !ok {
log.Debugln("adding requested service:", serviceName)
serviceInstance, serviceWarnings, err := actor.V2Actor.GetServiceInstanceByNameAndSpace(serviceName, spaceGUID)
warnings = append(warnings, serviceWarnings...)
if err != nil {
return nil, warnings, err
}
desiredServices[serviceName] = serviceInstance
}
}
return desiredServices, warnings, nil
}
func (actor Actor) configureExistingApp(config ApplicationConfig, app manifest.Application, foundApp Application) (ApplicationConfig, v2action.Warnings, error) {
log.Debugln("found app:", foundApp)
config.CurrentApplication = foundApp
config.DesiredApplication = foundApp
log.Info("looking up application routes")
routes, warnings, err := actor.V2Actor.GetApplicationRoutes(foundApp.GUID)
if err != nil {
log.Errorln("existing routes lookup:", err)
return config, warnings, err
}
serviceInstances, serviceWarnings, err := actor.V2Actor.GetServiceInstancesByApplication(foundApp.GUID)
warnings = append(warnings, serviceWarnings...)
if err != nil {
log.Errorln("existing services lookup:", err)
return config, warnings, err
}
nameToService := map[string]v2action.ServiceInstance{}
for _, serviceInstance := range serviceInstances {
nameToService[serviceInstance.Name] = serviceInstance
}
config.CurrentRoutes = routes
config.CurrentServices = nameToService
return config, warnings, nil
}
func (actor Actor) configureResources(config ApplicationConfig) (ApplicationConfig, error) {
info, err := os.Stat(config.Path)
if err != nil {
return config, err
}
var resources []sharedaction.Resource
if info.IsDir() {
log.WithField("path_to_resources", config.Path).Info("determine directory resources to zip")
resources, err = actor.SharedActor.GatherDirectoryResources(config.Path)
} else {
config.Archive = true
log.WithField("path_to_resources", config.Path).Info("determine archive resources to zip")
resources, err = actor.SharedActor.GatherArchiveResources(config.Path)
}
if err != nil {
return config, err
}
config.AllResources = actor.ConvertSharedResourcesToV2Resources(resources)
log.WithField("number_of_files", len(resources)).Debug("completed file scan")
return config, nil
}
func (Actor) overrideApplicationProperties(application Application, manifest manifest.Application, noStart bool) Application {
if manifest.Buildpack.IsSet {
application.Buildpacks = []string{}
if len(manifest.Buildpack.Value) > 0 {
application.Buildpacks = append(application.Buildpacks, manifest.Buildpack.Value)
}
}
if manifest.Buildpacks != nil {
application.Buildpacks = append([]string{}, manifest.Buildpacks...)
}
if manifest.Command.IsSet {
application.Command = manifest.Command
}
if manifest.DockerImage != "" {
application.DockerImage = manifest.DockerImage
if manifest.DockerUsername != "" {
application.DockerCredentials.Username = manifest.DockerUsername
application.DockerCredentials.Password = manifest.DockerPassword
}
}
if manifest.DiskQuota.IsSet {
application.DiskQuota = manifest.DiskQuota
}
if manifest.Memory.IsSet {
application.Memory = manifest.Memory
}
if manifest.HealthCheckTimeout != 0 {
application.HealthCheckTimeout = manifest.HealthCheckTimeout
}
if manifest.HealthCheckType != "" {
application.HealthCheckType = constant.ApplicationHealthCheckType(manifest.HealthCheckType)
application.HealthCheckHTTPEndpoint = manifest.HealthCheckHTTPEndpoint
if application.HealthCheckType == constant.ApplicationHealthCheckHTTP && application.HealthCheckHTTPEndpoint == "" {
application.HealthCheckHTTPEndpoint = "/"
}
}
if manifest.Instances.IsSet {
application.Instances = manifest.Instances
}
if noStart {
application.State = constant.ApplicationStopped
}
if len(manifest.EnvironmentVariables) > 0 {
if application.EnvironmentVariables == nil {
application.EnvironmentVariables = manifest.EnvironmentVariables
} else {
env := map[string]string{}
for key, value := range application.EnvironmentVariables {
env[key] = value
}
for key, value := range manifest.EnvironmentVariables {
env[key] = value
}
application.EnvironmentVariables = env
}
}
log.Debugln("post application override:", application)
return application
}
func (actor Actor) overrideStack(application Application, manifest manifest.Application) (Application, Warnings, error) {
if manifest.StackName == "" {
return application, nil, nil
}
stack, warnings, err := actor.V2Actor.GetStackByName(manifest.StackName)
application.SetStack(stack)
return application, Warnings(warnings), err
}