-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
556 lines (504 loc) · 16 KB
/
main.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package main
import (
"bytes"
"context"
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"code.cloudfoundry.org/goshims/ioutilshim"
"github.com/benbjohnson/clock"
uuid "github.com/satori/go.uuid"
"github.com/cloudfoundry/uptimer/app"
"github.com/cloudfoundry/uptimer/appLogValidator"
"github.com/cloudfoundry/uptimer/cfCmdGenerator"
"github.com/cloudfoundry/uptimer/cfWorkflow"
"github.com/cloudfoundry/uptimer/cmdRunner"
"github.com/cloudfoundry/uptimer/cmdStartWaiter"
"github.com/cloudfoundry/uptimer/config"
"github.com/cloudfoundry/uptimer/measurement"
"github.com/cloudfoundry/uptimer/orchestrator"
"github.com/cloudfoundry/uptimer/syslogSink"
"github.com/cloudfoundry/uptimer/tcpApp"
"github.com/cloudfoundry/uptimer/version"
)
func main() {
logger := log.New(os.Stdout, "\n[UPTIMER] ", log.Ldate|log.Ltime|log.LUTC)
useBuildpackDetection := flag.Bool("useBuildpackDetection", false, "Use buildpack detection (defaults to false)")
useQuotas := flag.Bool("useQuotas", true, "Create and set quotas for orgs (defaults to true)")
configPath := flag.String("configFile", "", "Path to the config file")
resultPath := flag.String("resultFile", "", "Path to the result file")
showVersion := flag.Bool("v", false, "Prints the version of uptimer and exits")
flag.Parse()
if *showVersion {
fmt.Printf("version: %s\n", version.Version)
os.Exit(0)
}
if *configPath == "" {
logger.Println("Failed to load config: ", fmt.Errorf("'-configFile' flag required"))
os.Exit(1)
}
cfg, err := config.Load(*configPath)
if err != nil {
logger.Println("Failed to load config: ", err)
os.Exit(1)
}
err = cfg.Validate()
if err != nil {
logger.Println(err)
os.Exit(1)
}
performMeasurements := true
logger.Println("Preparing included app...")
appPath, err := prepareIncludedApp("app", app.Source)
if err != nil {
logger.Println("Failed to prepare included app: ", err)
performMeasurements = false
}
logger.Println("Finished preparing included app")
defer os.RemoveAll(appPath)
var tcpPath string
if cfg.OptionalTests.RunTcpAvailability {
logger.Println("Preparing included tcp app...")
tcpPath, err = prepareIncludedApp("tcpApp", tcpApp.Source)
if err != nil {
logger.Println("Failed to prepare included tcp app: ", err)
performMeasurements = false
}
logger.Println("Finished preparing included tcp app")
defer os.RemoveAll(tcpPath)
}
var sinkAppPath string
if cfg.OptionalTests.RunAppSyslogAvailability {
logger.Println("Preparing included syslog sink app...")
sinkAppPath, err = prepareIncludedApp("syslogSink", syslogSink.Source)
if err != nil {
logger.Println("Failed to prepare included syslog sink app: ", err)
}
logger.Println("Finished preparing included syslog sink app")
}
orcTmpDir, recentLogsTmpDir, streamingLogsTmpDir, appStatsTmpDir, pushTmpDir, tcpTmpDir, sinkTmpDir, err := createTmpDirs()
if err != nil {
logger.Println("Failed to create temp dirs:", err)
performMeasurements = false
}
bufferedRunner, runnerOutBuf, runnerErrBuf := createBufferedRunner()
pushCmdGenerator := cfCmdGenerator.New(pushTmpDir, *useBuildpackDetection)
pushWorkflow := createWorkflow(cfg.CF, appPath, *useQuotas)
logger.Printf("Setting up push workflow with org %s ...", pushWorkflow.Org())
if err := bufferedRunner.RunInSequence(pushWorkflow.Setup(pushCmdGenerator)...); err != nil {
logBufferedRunnerFailure(logger, "push workflow setup", err, runnerOutBuf, runnerErrBuf)
performMeasurements = false
} else {
logger.Println("Finished setting up push workflow")
}
pushWorkflowGeneratorFunc := func() cfWorkflow.CfWorkflow {
return cfWorkflow.New(
cfg.CF,
pushWorkflow.Org(),
pushWorkflow.Space(),
pushWorkflow.Quota(),
fmt.Sprintf("uptimer-app-%s", uuid.NewV4().String()),
appPath,
)
}
var tcpWorkflow cfWorkflow.CfWorkflow
var tcpCmdGenerator cfCmdGenerator.CfCmdGenerator
if cfg.OptionalTests.RunTcpAvailability {
tcpCmdGenerator = cfCmdGenerator.New(tcpTmpDir, *useBuildpackDetection)
tcpWorkflow = createWorkflow(cfg.CF, tcpPath, *useQuotas)
logger.Printf("Setting up tcp app workflow with org %s ...", tcpWorkflow.Org())
err = bufferedRunner.RunInSequence(
append(append(
tcpWorkflow.Setup(tcpCmdGenerator),
tcpWorkflow.PushNoRoute(tcpCmdGenerator)...),
tcpWorkflow.MapTCPRoute(tcpCmdGenerator)...)...)
if err != nil {
logBufferedRunnerFailure(logger, "tcp workflow setup", err, runnerOutBuf, runnerErrBuf)
performMeasurements = false
} else {
logger.Println("Finished setting up tcp workflow")
}
}
var sinkWorkflow cfWorkflow.CfWorkflow
var sinkCmdGenerator cfCmdGenerator.CfCmdGenerator
if cfg.OptionalTests.RunAppSyslogAvailability {
sinkCmdGenerator = cfCmdGenerator.New(sinkTmpDir, *useBuildpackDetection)
sinkWorkflow = createWorkflow(cfg.CF, sinkAppPath, *useQuotas)
logger.Printf("Setting up sink workflow with org %s ...", sinkWorkflow.Org())
err = bufferedRunner.RunInSequence(
append(append(
sinkWorkflow.Setup(sinkCmdGenerator),
sinkWorkflow.Push(sinkCmdGenerator)...),
sinkWorkflow.MapSyslogRoute(sinkCmdGenerator)...)...)
if err != nil {
logBufferedRunnerFailure(logger, "sink workflow setup", err, runnerOutBuf, runnerErrBuf)
performMeasurements = false
} else {
logger.Println("Finished setting up sink workflow")
}
}
orcCmdGenerator := cfCmdGenerator.New(orcTmpDir, *useBuildpackDetection)
orcWorkflow := createWorkflow(cfg.CF, appPath, *useQuotas)
authFailedRetryFunc := func(stdOut, stdErr string) bool {
authFailedMessage := "Authentication has expired. Please log back in to re-authenticate."
return strings.Contains(stdOut, authFailedMessage) || strings.Contains(stdErr, authFailedMessage)
}
clock := clock.New()
measurements := createMeasurements(
clock,
logger,
orcWorkflow,
pushWorkflowGeneratorFunc,
cfCmdGenerator.New(recentLogsTmpDir, *useBuildpackDetection),
cfCmdGenerator.New(streamingLogsTmpDir, *useBuildpackDetection),
cfCmdGenerator.New(appStatsTmpDir, *useBuildpackDetection),
pushCmdGenerator,
cfg.AllowedFailures,
authFailedRetryFunc,
)
if cfg.OptionalTests.RunTcpAvailability {
measurements = append(
measurements,
createTcpAvailabilityMeasurement(
clock,
logger,
tcpWorkflow,
tcpCmdGenerator,
cfg.AllowedFailures,
authFailedRetryFunc,
),
)
}
if cfg.OptionalTests.RunAppSyslogAvailability {
measurements = append(
measurements,
createAppSyslogAvailabilityMeasurement(
clock,
logger,
sinkWorkflow,
sinkCmdGenerator,
cfg.AllowedFailures,
authFailedRetryFunc,
),
)
}
logger.Printf("Setting up main workflow with org %s ...", orcWorkflow.Org())
orc := orchestrator.New(cfg.While, logger, orcWorkflow, cmdRunner.New(os.Stdout, os.Stderr, io.Copy), measurements, &ioutilshim.IoutilShim{})
if err = orc.Setup(bufferedRunner, orcCmdGenerator, cfg.OptionalTests); err != nil {
logBufferedRunnerFailure(logger, "main workflow setup", err, runnerOutBuf, runnerErrBuf)
performMeasurements = false
} else {
logger.Println("Finished setting up main workflow")
}
if !cfg.OptionalTests.RunAppSyslogAvailability {
logger.Println("*NOT* running measurement: App syslog availability")
}
exitCode, err := orc.Run(performMeasurements, *resultPath)
if err != nil {
logger.Println("Failed run:", err)
}
logger.Println("Tearing down...")
tearDown(
orc,
orcCmdGenerator,
logger,
pushWorkflow,
pushCmdGenerator,
tcpWorkflow,
tcpCmdGenerator,
sinkWorkflow,
sinkCmdGenerator,
bufferedRunner,
runnerOutBuf,
runnerErrBuf,
)
logger.Println("Finished tearing down")
os.Exit(exitCode)
}
func createTmpDirs() (string, string, string, string, string, string, string, error) {
orcTmpDir, err := os.MkdirTemp("", "uptimer")
if err != nil {
return "", "", "", "", "", "", "", err
}
recentLogsTmpDir, err := os.MkdirTemp("", "uptimer")
if err != nil {
return "", "", "", "", "", "", "", err
}
streamingLogsTmpDir, err := os.MkdirTemp("", "uptimer")
if err != nil {
return "", "", "", "", "", "", "", err
}
appsStatsTmpDir, err := os.MkdirTemp("", "uptimer")
if err != nil {
return "", "", "", "", "", "", "", err
}
pushTmpDir, err := os.MkdirTemp("", "uptimer")
if err != nil {
return "", "", "", "", "", "", "", err
}
tcpTmpDir, err := os.MkdirTemp("", "uptimer")
if err != nil {
return "", "", "", "", "", "", "", err
}
sinkTmpDir, err := os.MkdirTemp("", "uptimer")
if err != nil {
return "", "", "", "", "", "", "", err
}
return orcTmpDir, recentLogsTmpDir, streamingLogsTmpDir, appsStatsTmpDir, pushTmpDir, tcpTmpDir, sinkTmpDir, nil
}
func prepareIncludedApp(name, source string) (string, error) {
dir, err := os.MkdirTemp("", "uptimer-sample-*")
if err != nil {
return "", err
}
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(source), 0644); err != nil {
os.RemoveAll(dir)
return "", err
}
manifest := goManifest(name)
if err := os.WriteFile(filepath.Join(dir, "manifest.yml"), []byte(manifest), 0644); err != nil {
os.RemoveAll(dir)
return "", err
}
return dir, nil
}
func goManifest(appName string) string {
return fmt.Sprintf(`applications:
- name: %s
memory: 64M
disk: 16M
env:
GOPACKAGENAME: github.com/cloudfoundry/uptimer/%s`, appName, appName)
}
func createWorkflow(cfc *config.Cf, appPath string, useQuotas bool) cfWorkflow.CfWorkflow {
var quota string
if useQuotas {
quota = fmt.Sprintf("uptimer-quota-%s", uuid.NewV4().String())
}
return cfWorkflow.New(
cfc,
fmt.Sprintf("uptimer-org-%s", uuid.NewV4().String()),
fmt.Sprintf("uptimer-space-%s", uuid.NewV4().String()),
quota,
fmt.Sprintf("uptimer-app-%s", uuid.NewV4().String()),
appPath,
)
}
func createMeasurements(
clock clock.Clock,
logger *log.Logger,
orcWorkflow cfWorkflow.CfWorkflow,
pushWorkFlowGeneratorFunc func() cfWorkflow.CfWorkflow,
recentLogsCmdGenerator, streamingLogsCmdGenerator, appStatsCmdGenerator, pushCmdGenerator cfCmdGenerator.CfCmdGenerator,
allowedFailures config.AllowedFailures,
authFailedRetryFunc func(stdOut, stdErr string) bool,
) []measurement.Measurement {
recentLogsBufferRunner, recentLogsRunnerOutBuf, recentLogsRunnerErrBuf := createBufferedRunner()
recentLogsMeasurement := measurement.NewRecentLogs(
func() []cmdStartWaiter.CmdStartWaiter {
return orcWorkflow.RecentLogs(recentLogsCmdGenerator)
},
recentLogsBufferRunner,
recentLogsRunnerOutBuf,
recentLogsRunnerErrBuf,
appLogValidator.New(),
)
streamingLogsBufferRunner, streamingLogsRunnerOutBuf, streamingLogsRunnerErrBuf := createBufferedRunner()
streamingLogsMeasurement := measurement.NewStreamingLogs(
func() (context.Context, context.CancelFunc, []cmdStartWaiter.CmdStartWaiter) {
ctx, cancelFunc := context.WithTimeout(context.Background(), 15*time.Second)
return ctx, cancelFunc, orcWorkflow.StreamLogs(ctx, streamingLogsCmdGenerator)
},
streamingLogsBufferRunner,
streamingLogsRunnerOutBuf,
streamingLogsRunnerErrBuf,
appLogValidator.New(),
)
pushRunner, pushRunnerOutBuf, pushRunnerErrBuf := createBufferedRunner()
appPushabilityMeasurement := measurement.NewAppPushability(
func() []cmdStartWaiter.CmdStartWaiter {
w := pushWorkFlowGeneratorFunc()
return append(
w.Push(pushCmdGenerator),
w.Delete(pushCmdGenerator)...,
)
},
pushRunner,
pushRunnerOutBuf,
pushRunnerErrBuf,
)
httpAvailabilityMeasurement := measurement.NewHTTPAvailability(
orcWorkflow.AppUrl(),
&http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
},
},
)
appStatsRunner, appStatsRunnerOutBuf, appStatsRunnerErrBuf := createBufferedRunner()
appStatsMeasurement := measurement.NewStatsAvailability(
func() []cmdStartWaiter.CmdStartWaiter {
return orcWorkflow.AppStats(appStatsCmdGenerator)
},
appStatsRunner,
appStatsRunnerOutBuf,
appStatsRunnerErrBuf,
)
return []measurement.Measurement{
measurement.NewPeriodic(
logger,
clock,
time.Second,
httpAvailabilityMeasurement,
measurement.NewResultSet(),
allowedFailures.HttpAvailability,
func(string, string) bool { return false },
),
measurement.NewPeriodic(
logger,
clock,
time.Minute,
appPushabilityMeasurement,
measurement.NewResultSet(),
allowedFailures.AppPushability,
authFailedRetryFunc,
),
measurement.NewPeriodic(
logger,
clock,
10*time.Second,
recentLogsMeasurement,
measurement.NewResultSet(),
allowedFailures.RecentLogs,
authFailedRetryFunc,
),
measurement.NewPeriodic(
logger,
clock,
30*time.Second,
streamingLogsMeasurement,
measurement.NewResultSet(),
allowedFailures.StreamingLogs,
authFailedRetryFunc,
),
measurement.NewPeriodic(
logger,
clock,
10*time.Second,
appStatsMeasurement,
measurement.NewResultSet(),
allowedFailures.AppStats,
authFailedRetryFunc,
),
}
}
func createTcpAvailabilityMeasurement(
clock clock.Clock,
logger *log.Logger,
tcpWorkflow cfWorkflow.CfWorkflow,
tcpCmdGenerator cfCmdGenerator.CfCmdGenerator,
allowedFailures config.AllowedFailures,
authFailedRetryFunc func(stdOut, stdErr string) bool,
) measurement.Measurement {
tcpAvailabilityMeasurement := measurement.NewTCPAvailability(
tcpWorkflow.TCPDomain(),
tcpWorkflow.TCPPort())
return measurement.NewPeriodic(
logger,
clock,
time.Second,
tcpAvailabilityMeasurement,
measurement.NewResultSet(),
allowedFailures.TCPAvailability,
func(string, string) bool { return false },
)
}
func createAppSyslogAvailabilityMeasurement(
clock clock.Clock,
logger *log.Logger,
sinkWorkflow cfWorkflow.CfWorkflow,
sinkCmdGenerator cfCmdGenerator.CfCmdGenerator,
allowedFailures config.AllowedFailures,
authFailedRetryFunc func(stdOut, stdErr string) bool,
) measurement.Measurement {
syslogAvailabilityBufferRunner, syslogAvailabilityRunnerOutBuf, syslogAvailabilityRunnerErrBuf := createBufferedRunner()
syslogAvailabilityMeasurement := measurement.NewSyslogDrain(
func() []cmdStartWaiter.CmdStartWaiter {
return sinkWorkflow.RecentLogs(sinkCmdGenerator)
},
syslogAvailabilityBufferRunner,
syslogAvailabilityRunnerOutBuf,
syslogAvailabilityRunnerErrBuf,
appLogValidator.New(),
)
return measurement.NewPeriodicWithoutMeasuringImmediately(
logger,
clock,
30*time.Second,
syslogAvailabilityMeasurement,
measurement.NewResultSet(),
allowedFailures.AppSyslogAvailability,
authFailedRetryFunc,
)
}
func createBufferedRunner() (cmdRunner.CmdRunner, *bytes.Buffer, *bytes.Buffer) {
outBuf := bytes.NewBuffer([]byte{})
errBuf := bytes.NewBuffer([]byte{})
return cmdRunner.New(outBuf, errBuf, io.Copy), outBuf, errBuf
}
func logBufferedRunnerFailure(
logger *log.Logger,
whatFailed string,
err error,
outBuf, errBuf *bytes.Buffer,
) {
logger.Printf(
"Failed %s: %v\nstdout:\n%s\nstderr:\n%s\n",
whatFailed,
err,
outBuf.String(),
errBuf.String(),
)
outBuf.Reset()
errBuf.Reset()
}
func tearDown(
orc orchestrator.Orchestrator,
orcCmdGenerator cfCmdGenerator.CfCmdGenerator,
logger *log.Logger,
pushWorkflow cfWorkflow.CfWorkflow,
pushCmdGenerator cfCmdGenerator.CfCmdGenerator,
tcpWorkflow cfWorkflow.CfWorkflow,
tcpCmdGenerator cfCmdGenerator.CfCmdGenerator,
sinkWorkflow cfWorkflow.CfWorkflow,
sinkCmdGenerator cfCmdGenerator.CfCmdGenerator,
runner cmdRunner.CmdRunner,
runnerOutBuf *bytes.Buffer,
runnerErrBuf *bytes.Buffer,
) {
if err := orc.TearDown(runner, orcCmdGenerator); err != nil {
logBufferedRunnerFailure(logger, "main teardown", err, runnerOutBuf, runnerErrBuf)
}
if err := runner.RunInSequence(pushWorkflow.TearDown(pushCmdGenerator)...); err != nil {
logBufferedRunnerFailure(logger, "push workflow teardown", err, runnerOutBuf, runnerErrBuf)
}
if tcpWorkflow != nil {
if err := runner.RunInSequence(tcpWorkflow.TearDown(tcpCmdGenerator)...); err != nil {
logBufferedRunnerFailure(logger, "tcp workflow teardown", err, runnerOutBuf, runnerErrBuf)
}
}
if sinkWorkflow != nil {
if err := runner.RunInSequence(sinkWorkflow.TearDown(sinkCmdGenerator)...); err != nil {
logBufferedRunnerFailure(logger, "sink workflow teardown", err, runnerOutBuf, runnerErrBuf)
}
}
}