forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2_push_command.go
374 lines (328 loc) · 14.6 KB
/
v2_push_command.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package v2
import (
"os"
"path/filepath"
"code.cloudfoundry.org/cli/actor/pushaction"
"code.cloudfoundry.org/cli/actor/pushaction/manifest"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v2/shared"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/cli/util/progressbar"
"github.com/cloudfoundry/noaa/consumer"
log "github.com/sirupsen/logrus"
)
//go:generate counterfeiter . ProgressBar
type ProgressBar interface {
pushaction.ProgressBar
Complete()
Ready()
}
//go:generate counterfeiter . V2PushActor
type V2PushActor interface {
Apply(config pushaction.ApplicationConfig, progressBar pushaction.ProgressBar) (<-chan pushaction.ApplicationConfig, <-chan pushaction.Event, <-chan pushaction.Warnings, <-chan error)
ConvertToApplicationConfigs(orgGUID string, spaceGUID string, noStart bool, apps []manifest.Application) ([]pushaction.ApplicationConfig, pushaction.Warnings, error)
MergeAndValidateSettingsAndManifests(cmdSettings pushaction.CommandLineSettings, apps []manifest.Application) ([]manifest.Application, error)
ReadManifest(pathToManifest string) ([]manifest.Application, error)
}
type V2PushCommand struct {
OptionalArgs flag.OptionalAppName `positional-args:"yes"`
Buildpack flag.Buildpack `short:"b" description:"Custom buildpack by name (e.g. my-buildpack) or Git URL (e.g. 'https://github.com/cloudfoundry/java-buildpack.git') or Git URL with a branch or tag (e.g. 'https://github.com/cloudfoundry/java-buildpack.git#v3.3.0' for 'v3.3.0' tag). To use built-in buildpacks only, specify 'default' or 'null'"`
Command flag.Command `short:"c" description:"Startup command, set to null to reset to default start command"`
// Domain string `short:"d" description:"Domain (e.g. example.com)"`
DockerImage flag.DockerImage `long:"docker-image" short:"o" description:"Docker-image to be used (e.g. user/docker-image-name)"`
DockerUsername string `long:"docker-username" description:"Repository username; used with password from environment variable CF_DOCKER_PASSWORD"`
PathToManifest flag.PathWithExistenceCheck `short:"f" description:"Path to manifest"`
HealthCheckType flag.HealthCheckType `long:"health-check-type" short:"u" description:"Application health check type (Default: 'port', 'none' accepted for 'process', 'http' implies endpoint '/')"`
// Hostname string `long:"hostname" short:"n" description:"Hostname (e.g. my-subdomain)"`
Instances flag.Instances `short:"i" description:"Number of instances"`
DiskQuota flag.Megabytes `short:"k" description:"Disk limit (e.g. 256M, 1024M, 1G)"`
Memory flag.Megabytes `short:"m" description:"Memory limit (e.g. 256M, 1024M, 1G)"`
// NoHostname bool `long:"no-hostname" description:"Map the root domain to this app"`
NoManifest bool `long:"no-manifest" description:"Ignore manifest file"`
// NoRoute bool `long:"no-route" description:"Do not map a route to this app and remove routes from previous pushes of this app"`
NoStart bool `long:"no-start" description:"Do not start an app after pushing"`
AppPath flag.PathWithExistenceCheck `short:"p" description:"Path to app directory or to a zip file of the contents of the app directory"`
// RandomRoute bool `long:"random-route" description:"Create a random route for this app"`
// RoutePath string `long:"route-path" description:"Path for the route"`
StackName string `short:"s" description:"Stack to use (a stack is a pre-built file system, including an operating system, that can run apps)"`
HealthCheckTimeout int `short:"t" description:"Time (in seconds) allowed to elapse between starting up an app and the first healthy response from the app"`
envCFStagingTimeout interface{} `environmentName:"CF_STAGING_TIMEOUT" environmentDescription:"Max wait time for buildpack staging, in minutes" environmentDefault:"15"`
envCFStartupTimeout interface{} `environmentName:"CF_STARTUP_TIMEOUT" environmentDescription:"Max wait time for app instance startup, in minutes" environmentDefault:"5"`
dockerPassword interface{} `environmentName:"CF_DOCKER_PASSWORD" environmentDescription:"Password used for private docker repository"`
usage interface{} `usage:"cf v2-push APP_NAME [-b BUILDPACK_NAME] [-c COMMAND] [-f MANIFEST_PATH | --no-manifest] [--no-start]\n [-i NUM_INSTANCES] [-k DISK] [-m MEMORY] [-p PATH] [-s STACK] [-t HEALTH_TIMEOUT] [-u (process | port | http)]\n [--no-route | --random-route | --hostname HOST | --no-hostname] [-d DOMAIN] [--route-path ROUTE_PATH]\n\n cf v2-push APP_NAME --docker-image [REGISTRY_HOST:PORT/]IMAGE[:TAG] [--docker-username USERNAME]\n [-c COMMAND] [-f MANIFEST_PATH | --no-manifest] [--no-start]\n [-i NUM_INSTANCES] [-k DISK] [-m MEMORY] [-t HEALTH_TIMEOUT] [-u (process | port | http)]\n [--no-route | --random-route | --hostname HOST | --no-hostname] [-d DOMAIN] [--route-path ROUTE_PATH]\n\n cf v2-push -f MANIFEST_WITH_MULTIPLE_APPS_PATH [APP_NAME] [--no-start]"`
relatedCommands interface{} `related_commands:"apps, create-app-manifest, logs, ssh, start"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor V2PushActor
ProgressBar ProgressBar
RestartActor RestartActor
NOAAClient *consumer.Consumer
}
func (cmd *V2PushCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor()
ccClient, uaaClient, err := shared.NewClients(config, ui, true)
if err != nil {
return err
}
v2Actor := v2action.NewActor(ccClient, uaaClient, config)
cmd.RestartActor = v2Actor
cmd.Actor = pushaction.NewActor(v2Actor)
cmd.NOAAClient = shared.NewNOAAClient(ccClient.DopplerEndpoint(), config, uaaClient, ui)
cmd.ProgressBar = progressbar.NewProgressBar()
return nil
}
func (cmd V2PushCommand) Execute(args []string) error {
cmd.UI.DisplayWarning(command.ExperimentalWarning)
err := cmd.SharedActor.CheckTarget(cmd.Config, true, true)
if err != nil {
return shared.HandleError(err)
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return shared.HandleError(err)
}
log.Info("collating flags")
cliSettings, err := cmd.GetCommandLineSettings()
if err != nil {
log.Errorln("reading flags:", err)
return shared.HandleError(err)
}
log.Info("checking manifest")
rawApps, err := cmd.findAndReadManifest(cliSettings)
if err != nil {
log.Errorln("reading manifest:", err)
return shared.HandleError(err)
}
log.Info("merging manifest and command flags")
manifestApplications, err := cmd.Actor.MergeAndValidateSettingsAndManifests(cliSettings, rawApps)
if err != nil {
log.Errorln("merging manifest:", err)
return shared.HandleError(err)
}
cmd.UI.DisplayText("Getting app info...")
log.Info("converting manifests to ApplicationConfigs")
appConfigs, warnings, err := cmd.Actor.ConvertToApplicationConfigs(
cmd.Config.TargetedOrganization().GUID,
cmd.Config.TargetedSpace().GUID,
cmd.NoStart,
manifestApplications,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
log.Errorln("converting manifest:", err)
return shared.HandleError(err)
}
for _, appConfig := range appConfigs {
if appConfig.CreatingApplication() {
cmd.UI.DisplayText("Creating app with these attributes...")
} else {
cmd.UI.DisplayText("Updating app with these attributes...")
}
log.Infoln("starting create/update:", appConfig.DesiredApplication.Name)
changes := shared.GetApplicationChanges(appConfig)
err := cmd.UI.DisplayChangesForPush(changes)
if err != nil {
log.Errorln("display changes:", err)
return shared.HandleError(err)
}
cmd.UI.DisplayNewline()
}
for appNumber, appConfig := range appConfigs {
if appConfig.CreatingApplication() {
cmd.UI.DisplayTextWithFlavor("Creating app {{.AppName}}...", map[string]interface{}{
"AppName": appConfig.DesiredApplication.Name,
})
} else {
cmd.UI.DisplayTextWithFlavor("Updating app {{.AppName}}...", map[string]interface{}{
"AppName": appConfig.DesiredApplication.Name,
})
}
configStream, eventStream, warningsStream, errorStream := cmd.Actor.Apply(appConfig, cmd.ProgressBar)
updatedConfig, err := cmd.processApplyStreams(user, appConfig, configStream, eventStream, warningsStream, errorStream)
if err != nil {
log.Errorln("process apply stream:", err)
return shared.HandleError(err)
}
if !cmd.NoStart {
messages, logErrs, appState, apiWarnings, errs := cmd.RestartActor.RestartApplication(updatedConfig.CurrentApplication.Application, cmd.NOAAClient, cmd.Config)
err = shared.PollStart(cmd.UI, cmd.Config, messages, logErrs, appState, apiWarnings, errs)
if err != nil {
return err
}
}
cmd.UI.DisplayNewline()
appSummary, warnings, err := cmd.RestartActor.GetApplicationSummaryByNameAndSpace(appConfig.DesiredApplication.Name, cmd.Config.TargetedSpace().GUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return shared.HandleError(err)
}
shared.DisplayAppSummary(cmd.UI, appSummary, true)
if appNumber+1 <= len(appConfigs) {
cmd.UI.DisplayNewline()
}
}
return nil
}
func (cmd V2PushCommand) GetCommandLineSettings() (pushaction.CommandLineSettings, error) {
err := cmd.validateArgs()
if err != nil {
return pushaction.CommandLineSettings{}, shared.HandleError(err)
}
pwd, err := os.Getwd()
if err != nil {
return pushaction.CommandLineSettings{}, err
}
config := pushaction.CommandLineSettings{
Buildpack: cmd.Buildpack.FilteredString,
Command: cmd.Command.FilteredString,
CurrentDirectory: pwd,
DiskQuota: cmd.DiskQuota.Value,
DockerImage: cmd.DockerImage.Path,
DockerUsername: cmd.DockerUsername,
DockerPassword: cmd.Config.DockerPassword(),
HealthCheckTimeout: cmd.HealthCheckTimeout,
HealthCheckType: cmd.HealthCheckType.Type,
Instances: cmd.Instances.NullInt,
Memory: cmd.Memory.Value,
Name: cmd.OptionalArgs.AppName,
ProvidedAppPath: string(cmd.AppPath),
StackName: cmd.StackName,
}
log.Debugln("Command Line Settings:", config)
return config, nil
}
func (cmd V2PushCommand) findAndReadManifest(settings pushaction.CommandLineSettings) ([]manifest.Application, error) {
var pathToManifest string
switch {
case cmd.NoManifest:
log.Debug("skipping reading of manifest")
return nil, nil
case cmd.PathToManifest != "":
log.Debug("using specified manifest file")
pathToManifest = string(cmd.PathToManifest)
default:
log.Debug("searching for manifest file")
pathToManifest = filepath.Join(settings.CurrentDirectory, "manifest.yml")
if _, err := os.Stat(pathToManifest); os.IsNotExist(err) {
log.WithField("pathToManifest", pathToManifest).Debug("could not find")
// While this is unlikely to be used, it is kept for backwards
// compatibility.
pathToManifest = filepath.Join(settings.CurrentDirectory, "manifest.yaml")
if _, err := os.Stat(pathToManifest); os.IsNotExist(err) {
log.WithField("pathToManifest", pathToManifest).Debug("could not find")
return nil, nil
}
}
}
log.WithField("pathToManifest", pathToManifest).Info("reading manifest")
cmd.UI.DisplayText("Using manifest file {{.Path}}", map[string]interface{}{
"Path": pathToManifest,
})
return cmd.Actor.ReadManifest(pathToManifest)
}
func (cmd V2PushCommand) processApplyStreams(
user configv3.User,
appConfig pushaction.ApplicationConfig,
configStream <-chan pushaction.ApplicationConfig,
eventStream <-chan pushaction.Event,
warningsStream <-chan pushaction.Warnings,
errorStream <-chan error,
) (pushaction.ApplicationConfig, error) {
var configClosed, eventClosed, warningsClosed, complete bool
var updatedConfig pushaction.ApplicationConfig
for {
select {
case config, ok := <-configStream:
if !ok {
log.Debug("processing config stream closed")
configClosed = true
break
}
updatedConfig = config
log.Debugf("updated config received: %#v", updatedConfig)
case event, ok := <-eventStream:
if !ok {
log.Debug("processing event stream closed")
eventClosed = true
break
}
complete = cmd.processEvent(user, appConfig, event)
case warnings, ok := <-warningsStream:
if !ok {
log.Debug("processing warnings stream closed")
warningsClosed = true
break
}
cmd.UI.DisplayWarnings(warnings)
case err, ok := <-errorStream:
if !ok {
log.Debug("processing error stream closed")
warningsClosed = true
break
}
return pushaction.ApplicationConfig{}, err
}
if configClosed && eventClosed && warningsClosed && complete {
log.Debug("breaking apply display loop")
break
}
}
return updatedConfig, nil
}
func (cmd V2PushCommand) processEvent(user configv3.User, appConfig pushaction.ApplicationConfig, event pushaction.Event) bool {
log.Infoln("received apply event:", event)
switch event {
case pushaction.ConfiguringRoutes:
cmd.UI.DisplayText("Mapping routes...")
case pushaction.ConfiguringServices:
cmd.UI.DisplayText("Binding services...")
case pushaction.ResourceMatching:
cmd.UI.DisplayText("Comparing local files to remote cache...")
case pushaction.CreatingArchive:
cmd.UI.DisplayText("Packaging files to upload...")
case pushaction.UploadingApplication:
cmd.UI.DisplayText("Uploading files...")
log.Debug("starting progress bar")
cmd.ProgressBar.Ready()
case pushaction.RetryUpload:
cmd.UI.DisplayText("Retrying upload due to an error...")
case pushaction.UploadComplete:
cmd.ProgressBar.Complete()
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("Waiting for API to complete processing files...")
case pushaction.Complete:
return true
default:
log.WithField("event", event).Debug("ignoring event")
}
return false
}
func (cmd V2PushCommand) validateArgs() error {
switch {
case cmd.DockerImage.Path != "" && cmd.AppPath != "":
return translatableerror.ArgumentCombinationError{
Arg1: "--docker-image, -o",
Arg2: "-p",
}
case cmd.DockerUsername != "" && cmd.DockerImage.Path == "":
return translatableerror.RequiredFlagsError{
Arg1: "--docker-image, -o",
Arg2: "--docker-username",
}
case cmd.DockerUsername != "" && cmd.Config.DockerPassword() == "":
return translatableerror.DockerPasswordNotSetError{}
case cmd.PathToManifest != "" && cmd.NoManifest:
return translatableerror.ArgumentCombinationError{
Arg1: "-f",
Arg2: "--no-manifest",
}
}
return nil
}