-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
390 lines (323 loc) · 11.1 KB
/
start.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package application
import (
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/noaa/events"
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/api/app_instances"
"github.com/cloudfoundry/cli/cf/api/applications"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
const (
DefaultStagingTimeout = 15 * time.Minute
DefaultStartupTimeout = 5 * time.Minute
DefaultPingerThrottle = 5 * time.Second
)
const LogMessageTypeStaging = "STG"
type Start struct {
ui terminal.UI
config core_config.Reader
appDisplayer ApplicationDisplayer
appReq requirements.ApplicationRequirement
appRepo applications.ApplicationRepository
appInstancesRepo app_instances.AppInstancesRepository
logRepo api.LogsNoaaRepository
StartupTimeout time.Duration
StagingTimeout time.Duration
PingerThrottle time.Duration
}
type ApplicationStarter interface {
SetStartTimeoutInSeconds(timeout int)
ApplicationStart(app models.Application, orgName string, spaceName string) (updatedApp models.Application, err error)
}
type ApplicationStagingWatcher interface {
ApplicationWatchStaging(app models.Application, orgName string, spaceName string, startCommand func(app models.Application) (models.Application, error)) (updatedApp models.Application, err error)
}
func NewStart(ui terminal.UI, config core_config.Reader, appDisplayer ApplicationDisplayer, appRepo applications.ApplicationRepository, appInstancesRepo app_instances.AppInstancesRepository, logRepo api.LogsNoaaRepository) (cmd *Start) {
cmd = new(Start)
cmd.ui = ui
cmd.config = config
cmd.appDisplayer = appDisplayer
cmd.appRepo = appRepo
cmd.appInstancesRepo = appInstancesRepo
cmd.logRepo = logRepo
cmd.PingerThrottle = DefaultPingerThrottle
if os.Getenv("CF_STAGING_TIMEOUT") != "" {
duration, err := strconv.ParseInt(os.Getenv("CF_STAGING_TIMEOUT"), 10, 64)
if err != nil {
cmd.ui.Failed(T("invalid value for env var CF_STAGING_TIMEOUT\n{{.Err}}",
map[string]interface{}{"Err": err}))
}
cmd.StagingTimeout = time.Duration(duration) * time.Minute
} else {
cmd.StagingTimeout = DefaultStagingTimeout
}
if os.Getenv("CF_STARTUP_TIMEOUT") != "" {
duration, err := strconv.ParseInt(os.Getenv("CF_STARTUP_TIMEOUT"), 10, 64)
if err != nil {
cmd.ui.Failed(T("invalid value for env var CF_STARTUP_TIMEOUT\n{{.Err}}",
map[string]interface{}{"Err": err}))
}
cmd.StartupTimeout = time.Duration(duration) * time.Minute
} else {
cmd.StartupTimeout = DefaultStartupTimeout
}
return
}
func (cmd *Start) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "start",
ShortName: "st",
Description: T("Start an app"),
Usage: T("CF_NAME start APP_NAME"),
}
}
func (cmd *Start) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}
if cmd.appReq == nil {
cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
} else {
cmd.appReq.SetApplicationName(c.Args()[0])
}
reqs = []requirements.Requirement{requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(), cmd.appReq}
return
}
func (cmd *Start) Run(c *cli.Context) {
cmd.ApplicationStart(cmd.appReq.GetApplication(), cmd.config.OrganizationFields().Name, cmd.config.SpaceFields().Name)
}
func (cmd *Start) ApplicationStart(app models.Application, orgName, spaceName string) (updatedApp models.Application, err error) {
if app.State == "started" {
cmd.ui.Say(terminal.WarningColor(T("App ") + app.Name + T(" is already started")))
return
}
return cmd.ApplicationWatchStaging(app, orgName, spaceName, func(app models.Application) (models.Application, error) {
cmd.ui.Say(T("Starting app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"AppName": terminal.EntityNameColor(app.Name),
"OrgName": terminal.EntityNameColor(orgName),
"SpaceName": terminal.EntityNameColor(spaceName),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username())}))
state := "STARTED"
return cmd.appRepo.Update(app.Guid, models.AppParams{State: &state})
})
}
func (cmd *Start) ApplicationWatchStaging(app models.Application, orgName, spaceName string, start func(app models.Application) (models.Application, error)) (updatedApp models.Application, err error) {
var isConnected bool
loggingStartedChan := make(chan bool)
doneLoggingChan := make(chan bool)
go cmd.tailStagingLogs(app, loggingStartedChan, doneLoggingChan)
timeout := make(chan struct{})
go func() {
time.Sleep(20 * time.Second)
close(timeout)
}()
select {
case <-timeout:
cmd.ui.Warn("timeout connecting to log server, no log will be shown")
break
case <-loggingStartedChan: // block until we have established connection to Loggregator
isConnected = true
break
}
updatedApp, apiErr := start(app)
if apiErr != nil {
cmd.ui.Failed(apiErr.Error())
return
}
isStaged := cmd.waitForInstancesToStage(updatedApp)
if isConnected { //only close when actually connected, else CLI hangs at closing consumer connection
cmd.logRepo.Close()
}
<-doneLoggingChan
cmd.ui.Say("")
if !isStaged {
cmd.ui.Failed(fmt.Sprintf("%s failed to stage within %f minutes", app.Name, cmd.StagingTimeout.Minutes()))
}
cmd.waitForOneRunningInstance(updatedApp)
cmd.ui.Say(terminal.HeaderColor(T("\nApp started\n")))
cmd.ui.Say("")
cmd.ui.Ok()
//detectedstartcommand on first push is not present until starting completes
startedApp, apiErr := cmd.appRepo.Read(updatedApp.Name)
if err != nil {
cmd.ui.Failed(apiErr.Error())
return
}
var appStartCommand string
if app.Command == "" {
appStartCommand = startedApp.DetectedStartCommand
} else {
appStartCommand = startedApp.Command
}
cmd.ui.Say(T("\nApp {{.AppName}} was started using this command `{{.Command}}`\n",
map[string]interface{}{
"AppName": terminal.EntityNameColor(startedApp.Name),
"Command": appStartCommand,
}))
cmd.appDisplayer.ShowApp(startedApp, orgName, spaceName)
return
}
func (cmd *Start) SetStartTimeoutInSeconds(timeout int) {
cmd.StartupTimeout = time.Duration(timeout) * time.Second
}
func simpleLogMessageOutput(logMsg *events.LogMessage) (msgText string) {
msgText = string(logMsg.GetMessage())
reg, err := regexp.Compile("[\n\r]+$")
if err != nil {
return
}
msgText = reg.ReplaceAllString(msgText, "")
return
}
func (cmd Start) tailStagingLogs(app models.Application, startChan, doneChan chan bool) {
onConnect := func() {
startChan <- true
}
err := cmd.logRepo.TailNoaaLogsFor(app.Guid, onConnect, func(msg *events.LogMessage) {
if msg.GetSourceType() == LogMessageTypeStaging {
cmd.ui.Say(simpleLogMessageOutput(msg))
}
})
if err != nil {
cmd.ui.Warn(T("Warning: error tailing logs"))
cmd.ui.Say("%s", err)
startChan <- true
}
close(doneChan)
}
func isStagingError(err error) bool {
httpError, ok := err.(errors.HttpError)
return ok && httpError.ErrorCode() == errors.APP_NOT_STAGED
}
func (cmd Start) waitForInstancesToStage(app models.Application) bool {
stagingStartTime := time.Now()
_, err := cmd.appInstancesRepo.GetInstances(app.Guid)
for isStagingError(err) && time.Since(stagingStartTime) < cmd.StagingTimeout {
cmd.ui.Wait(cmd.PingerThrottle)
_, err = cmd.appInstancesRepo.GetInstances(app.Guid)
}
if err != nil && !isStagingError(err) {
cmd.ui.Say("")
cmd.ui.Failed(T("{{.Err}}\n\nTIP: use '{{.Command}}' for more information",
map[string]interface{}{
"Err": err.Error(),
"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))}))
}
if time.Since(stagingStartTime) >= cmd.StagingTimeout {
return false
}
return true
}
func (cmd Start) waitForOneRunningInstance(app models.Application) {
startupStartTime := time.Now()
for {
if time.Since(startupStartTime) > cmd.StartupTimeout {
cmd.ui.Failed(fmt.Sprintf(T("Start app timeout\n\nTIP: use '{{.Command}}' for more information",
map[string]interface{}{
"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))})))
return
}
count, err := cmd.fetchInstanceCount(app.Guid)
if err != nil {
cmd.ui.Wait(cmd.PingerThrottle)
continue
}
cmd.ui.Say(instancesDetails(count))
if count.running > 0 {
return
}
if count.flapping > 0 || count.crashed > 0 {
cmd.ui.Failed(fmt.Sprintf(T("Start unsuccessful\n\nTIP: use '{{.Command}}' for more information",
map[string]interface{}{"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))})))
return
}
cmd.ui.Wait(cmd.PingerThrottle)
}
}
type instanceCount struct {
running int
starting int
startingDetails map[string]struct{}
flapping int
down int
crashed int
total int
}
func (cmd Start) fetchInstanceCount(appGuid string) (instanceCount, error) {
count := instanceCount{
startingDetails: make(map[string]struct{}),
}
instances, apiErr := cmd.appInstancesRepo.GetInstances(appGuid)
if apiErr != nil {
return instanceCount{}, apiErr
}
count.total = len(instances)
for _, inst := range instances {
switch inst.State {
case models.InstanceRunning:
count.running++
case models.InstanceStarting:
count.starting++
if inst.Details != "" {
count.startingDetails[inst.Details] = struct{}{}
}
case models.InstanceFlapping:
count.flapping++
case models.InstanceDown:
count.down++
case models.InstanceCrashed:
count.crashed++
}
}
return count, nil
}
func instancesDetails(count instanceCount) string {
details := []string{fmt.Sprintf(T("{{.RunningCount}} of {{.TotalCount}} instances running",
map[string]interface{}{"RunningCount": count.running, "TotalCount": count.total}))}
if count.starting > 0 {
if len(count.startingDetails) == 0 {
details = append(details, fmt.Sprintf(T("{{.StartingCount}} starting",
map[string]interface{}{"StartingCount": count.starting})))
} else {
info := []string{}
for d, _ := range count.startingDetails {
info = append(info, d)
}
sort.Strings(info)
details = append(details, fmt.Sprintf(T("{{.StartingCount}} starting ({{.Details}})",
map[string]interface{}{
"StartingCount": count.starting,
"Details": strings.Join(info, ", "),
})))
}
}
if count.down > 0 {
details = append(details, fmt.Sprintf(T("{{.DownCount}} down",
map[string]interface{}{"DownCount": count.down})))
}
if count.flapping > 0 {
details = append(details, fmt.Sprintf(T("{{.FlappingCount}} failing",
map[string]interface{}{"FlappingCount": count.flapping})))
}
if count.crashed > 0 {
details = append(details, fmt.Sprintf(T("{{.CrashedCount}} crashed",
map[string]interface{}{"CrashedCount": count.crashed})))
}
return strings.Join(details, ", ")
}