forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_config.go
280 lines (235 loc) · 8.69 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
package pushaction
import (
"os"
"path/filepath"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
"code.cloudfoundry.org/cli/util/manifest"
log "github.com/sirupsen/logrus"
)
type ApplicationConfig struct {
CurrentApplication Application
DesiredApplication Application
CurrentRoutes []v2action.Route
DesiredRoutes []v2action.Route
CurrentServices map[string]v2action.ServiceInstance
DesiredServices map[string]v2action.ServiceInstance
AllResources []v2action.Resource
MatchedResources []v2action.Resource
UnmatchedResources []v2action.Resource
Archive bool
Path string
TargetedSpaceGUID string
}
func (config ApplicationConfig) CreatingApplication() bool {
return config.CurrentApplication.GUID == ""
}
func (config ApplicationConfig) UpdatingApplication() bool {
return !config.CreatingApplication()
}
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 {
absPath, err := filepath.EvalSymlinks(app.Path)
if err != nil {
return nil, nil, err
}
config := ApplicationConfig{
TargetedSpaceGUID: spaceGUID,
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
}
var routeWarnings Warnings
config, routeWarnings, err = actor.configureRoutes(app.Routes, orgGUID, spaceGUID, config)
warnings = append(warnings, routeWarnings...)
if err != nil {
log.Errorln("determining routes:", err)
return nil, warnings, err
}
config, err = actor.configureResources(config, app.DockerImage)
if err != nil {
log.Errorln("configuring resources", err)
return nil, warnings, err
}
configs = append(configs, config)
}
return configs, warnings, nil
}
func (actor Actor) configureRoutes(routesInManifest []string, orgGUID string, spaceGUID string, config ApplicationConfig) (ApplicationConfig, Warnings, error) {
if len(routesInManifest) > 0 {
var warnings Warnings
var err error
config.DesiredRoutes, warnings, err = actor.CalculateRoutes(routesInManifest, orgGUID, spaceGUID, config.CurrentRoutes)
return config, warnings, err
}
defaultRoute, warnings, err := actor.GetRouteWithDefaultDomain(config.DesiredApplication.Name, orgGUID, spaceGUID, config.CurrentRoutes)
if err != nil {
log.Errorln("getting default route:", err)
return config, warnings, err
}
// TODO: when working with all of routes, append to current route
config.DesiredRoutes = append(config.CurrentRoutes, defaultRoute)
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, dockerImagePath string) (ApplicationConfig, error) {
if dockerImagePath == "" {
info, err := os.Stat(config.Path)
if err != nil {
return config, err
}
var resources []v2action.Resource
if info.IsDir() {
log.WithField("path_to_resources", config.Path).Info("determine directory resources to zip")
resources, err = actor.V2Actor.GatherDirectoryResources(config.Path)
} else {
config.Archive = true
log.WithField("path_to_resources", config.Path).Info("determine archive resources to zip")
resources, err = actor.V2Actor.GatherArchiveResources(config.Path)
}
if err != nil {
return config, err
}
config.AllResources = 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.Buildpack = manifest.Buildpack
}
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.Value
}
if manifest.Memory.IsSet {
application.Memory = manifest.Memory.Value
}
if manifest.HealthCheckHTTPEndpoint != "" {
application.HealthCheckHTTPEndpoint = manifest.HealthCheckHTTPEndpoint
}
if manifest.HealthCheckTimeout != 0 {
application.HealthCheckTimeout = manifest.HealthCheckTimeout
}
if manifest.HealthCheckType != "" {
application.HealthCheckType = ccv2.ApplicationHealthCheckType(manifest.HealthCheckType)
}
if manifest.Instances.IsSet {
application.Instances = manifest.Instances
}
if noStart {
application.State = ccv2.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
}