-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathcloudFoundryDeploy.go
708 lines (571 loc) · 21.4 KB
/
cloudFoundryDeploy.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package cmd
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"slices"
"sort"
"strings"
"time"
"github.com/SAP/jenkins-library/pkg/cloudfoundry"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/yaml"
"github.com/pkg/errors"
)
type cfFileUtil interface {
FileExists(string) (bool, error)
FileRename(string, string) error
FileRead(string) ([]byte, error)
FileWrite(path string, content []byte, perm os.FileMode) error
Getwd() (string, error)
Glob(string) ([]string, error)
Chmod(string, os.FileMode) error
Copy(string, string) (int64, error)
Stat(path string) (os.FileInfo, error)
}
var _now = time.Now
var _cfLogin = cfLogin
var _cfLogout = cfLogout
var _getManifest = getManifest
var _replaceVariables = yaml.Substitute
var _getVarsOptions = cloudfoundry.GetVarsOptions
var _getVarsFileOptions = cloudfoundry.GetVarsFileOptions
var _environ = os.Environ
var fileUtils cfFileUtil = piperutils.Files{}
// for simplify mocking. Maybe we find a more elegant way (mock for CFUtils)
func cfLogin(c command.ExecRunner, options cloudfoundry.LoginOptions) error {
cf := &cloudfoundry.CFUtils{Exec: c}
return cf.Login(options)
}
// for simplify mocking. Maybe we find a more elegant way (mock for CFUtils)
func cfLogout(c command.ExecRunner) error {
cf := &cloudfoundry.CFUtils{Exec: c}
return cf.Logout()
}
func cloudFoundryDeploy(config cloudFoundryDeployOptions, telemetryData *telemetry.CustomData, influxData *cloudFoundryDeployInflux) {
// for command execution use Command
c := command.Command{}
// reroute command output to logging framework
c.Stdout(log.Writer())
c.Stderr(log.Writer())
// for http calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
// and use a &piperhttp.Client{} in a custom system
// Example: step checkmarxExecuteScan.go
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
err := runCloudFoundryDeploy(&config, telemetryData, influxData, &c)
if err != nil {
log.Entry().WithError(err).Fatalf("step execution failed: %s", err)
}
}
func runCloudFoundryDeploy(config *cloudFoundryDeployOptions, telemetryData *telemetry.CustomData, influxData *cloudFoundryDeployInflux, command command.ExecRunner) error {
log.Entry().Infof("General parameters: deployTool='%s', deployType='%s', cfApiEndpoint='%s', cfOrg='%s', cfSpace='%s'",
config.DeployTool, config.DeployType, config.APIEndpoint, config.Org, config.Space)
err := validateAppName(config.AppName)
if err != nil {
return err
}
validateDeployTool(config)
var deployTriggered bool
if config.DeployTool == "mtaDeployPlugin" {
deployTriggered = true
err = handleMTADeployment(config, command)
} else if config.DeployTool == "cf_native" {
deployTriggered = true
err = handleCFNativeDeployment(config, command)
} else {
log.Entry().Warningf("Found unsupported deployTool ('%s'). Skipping deployment. Supported deploy tools: 'mtaDeployPlugin', 'cf_native'", config.DeployTool)
}
if deployTriggered {
prepareInflux(err == nil, config, influxData)
}
return err
}
func validateDeployTool(config *cloudFoundryDeployOptions) {
if config.DeployTool != "" || config.BuildTool == "" {
return
}
switch config.BuildTool {
case "mta":
config.DeployTool = "mtaDeployPlugin"
default:
config.DeployTool = "cf_native"
}
log.Entry().Infof("Parameter deployTool not specified - deriving from buildTool '%s': '%s'",
config.BuildTool, config.DeployTool)
}
func validateAppName(appName string) error {
// for the sake of brevity we consider the empty string as valid app name here
isValidAppName, err := regexp.MatchString("^$|^[a-zA-Z0-9]$|^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$", appName)
if err != nil {
return err
}
if isValidAppName {
return nil
}
const (
underscore = "_"
dash = "-"
docuLink = "https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#basic-settings"
)
log.Entry().Warningf("Your application name '%s' contains non-alphanumeric characters which may lead to errors in the future, "+
"as they are not supported by CloudFoundry. For more details please visit %s", appName, docuLink)
var fail bool
message := []string{fmt.Sprintf("Your application name '%s'", appName)}
if strings.Contains(appName, underscore) {
message = append(message, fmt.Sprintf("contains a '%s' (underscore) which is not allowed, only letters, dashes and numbers can be used.", underscore))
fail = true
}
if strings.HasPrefix(appName, dash) || strings.HasSuffix(appName, dash) {
message = append(message, fmt.Sprintf("starts or ends with a '%s' (dash) which is not allowed, only letters and numbers can be used.", dash))
fail = true
}
message = append(message, fmt.Sprintf("Please change the name to fit this requirement(s). For more details please visit %s.", docuLink))
if fail {
return errors.New(strings.Join(message, " "))
}
return nil
}
func prepareInflux(success bool, config *cloudFoundryDeployOptions, influxData *cloudFoundryDeployInflux) {
if influxData == nil {
return
}
result := "FAILURE"
if success {
result = "SUCCESS"
}
influxData.deployment_data.tags.artifactVersion = config.ArtifactVersion
influxData.deployment_data.tags.deployUser = config.Username
influxData.deployment_data.tags.deployResult = result
influxData.deployment_data.tags.cfAPIEndpoint = config.APIEndpoint
influxData.deployment_data.tags.cfOrg = config.Org
influxData.deployment_data.tags.cfSpace = config.Space
// n/a (literally) is also reported in groovy
influxData.deployment_data.fields.artifactURL = "n/a"
influxData.deployment_data.fields.commitHash = config.CommitHash
influxData.deployment_data.fields.deployTime = strings.ToUpper(_now().Format("Jan 02 2006 15:04:05"))
// we should discuss how we handle the job trigger
// 1.) outside Jenkins
// 2.) inside Jenkins (how to get)
influxData.deployment_data.fields.jobTrigger = "n/a"
}
func handleMTADeployment(config *cloudFoundryDeployOptions, command command.ExecRunner) error {
mtarFilePath := config.MtaPath
if len(mtarFilePath) == 0 {
var err error
mtarFilePath, err = findMtar()
if err != nil {
return err
}
log.Entry().Debugf("Using mtar file '%s' found in workspace", mtarFilePath)
} else {
exists, err := fileUtils.FileExists(mtarFilePath)
if err != nil {
return errors.Wrapf(err, "Cannot check if file path '%s' exists", mtarFilePath)
}
if !exists {
return fmt.Errorf("mtar file '%s' retrieved from configuration does not exist", mtarFilePath)
}
log.Entry().Debugf("Using mtar file '%s' from configuration", mtarFilePath)
}
return deployMta(config, mtarFilePath, command)
}
type deployConfig struct {
DeployCommand string
DeployOptions []string
AppName string
ManifestFile string
}
func handleCFNativeDeployment(config *cloudFoundryDeployOptions, command command.ExecRunner) error {
var deployCommand string
var deployOptions []string
var err error
// deploy command will be provided by the prepare functions below
if config.DeployType == "blue-green" {
return fmt.Errorf("Blue-green deployment type is deprecated for cf native builds." +
"Instead set parameter `cfNativeDeployParameters: '--strategy rolling'`. " +
"Please refer to the Cloud Foundry documentation for further information: " +
"https://docs.cloudfoundry.org/devguide/deploy-apps/rolling-deploy.html." +
"Or alternatively, switch to mta build tool. Please refer to mta build tool" +
"documentation for further information: https://sap.github.io/cloud-mta-build-tool/configuration/.")
} else if config.DeployType == "standard" {
deployCommand, deployOptions, err = prepareCfPushCfNativeDeploy(config)
if err != nil {
return errors.Wrapf(err, "Cannot prepare cf push native deployment. DeployType '%s'", config.DeployType)
}
} else {
return fmt.Errorf("Invalid deploy type received: '%s'. Supported value: standard", config.DeployType)
}
appName, err := getAppName(config)
if err != nil {
return err
}
manifestFile, err := getManifestFileName(config)
log.Entry().Infof("CF native deployment ('%s') with:", config.DeployType)
log.Entry().Infof("cfAppName='%s'", appName)
log.Entry().Infof("cfManifest='%s'", manifestFile)
log.Entry().Infof("cfManifestVariables: '%v'", config.ManifestVariables)
log.Entry().Infof("cfManifestVariablesFiles: '%v'", config.ManifestVariablesFiles)
log.Entry().Infof("cfdeployDockerImage: '%s'", config.DeployDockerImage)
var additionalEnvironment []string
if len(config.DockerPassword) > 0 {
additionalEnvironment = []string{("CF_DOCKER_PASSWORD=" + config.DockerPassword)}
}
myDeployConfig := deployConfig{
DeployCommand: deployCommand,
DeployOptions: deployOptions,
AppName: config.AppName,
ManifestFile: config.Manifest,
}
log.Entry().Infof("DeployConfig: %v", myDeployConfig)
return deployCfNative(myDeployConfig, config, additionalEnvironment, command)
}
func deployCfNative(deployConfig deployConfig, config *cloudFoundryDeployOptions, additionalEnvironment []string, cmd command.ExecRunner) error {
deployStatement := []string{
deployConfig.DeployCommand,
}
if len(deployConfig.AppName) > 0 {
deployStatement = append(deployStatement, deployConfig.AppName)
}
if len(deployConfig.DeployOptions) > 0 {
deployStatement = append(deployStatement, deployConfig.DeployOptions...)
}
if len(deployConfig.ManifestFile) > 0 {
deployStatement = append(deployStatement, "-f")
deployStatement = append(deployStatement, deployConfig.ManifestFile)
}
if len(config.DeployDockerImage) > 0 {
deployStatement = append(deployStatement, "--docker-image", config.DeployDockerImage)
}
if len(config.DockerUsername) > 0 {
deployStatement = append(deployStatement, "--docker-username", config.DockerUsername)
}
if len(config.CfNativeDeployParameters) > 0 {
deployStatement = append(deployStatement, strings.Fields(config.CfNativeDeployParameters)...)
}
return cfDeploy(config, deployStatement, additionalEnvironment, cmd)
}
func getManifest(name string) (cloudfoundry.Manifest, error) {
return cloudfoundry.ReadManifest(name)
}
func getManifestFileName(config *cloudFoundryDeployOptions) (string, error) {
manifestFileName := config.Manifest
if len(manifestFileName) == 0 {
manifestFileName = "manifest.yml"
}
return manifestFileName, nil
}
func getAppName(config *cloudFoundryDeployOptions) (string, error) {
if len(config.AppName) > 0 {
return config.AppName, nil
}
manifestFile, err := getManifestFileName(config)
fileExists, err := fileUtils.FileExists(manifestFile)
if err != nil {
return "", errors.Wrapf(err, "Cannot check if file '%s' exists", manifestFile)
}
if !fileExists {
return "", fmt.Errorf("Manifest file '%s' not found. Cannot retrieve app name", manifestFile)
}
manifest, err := _getManifest(manifestFile)
if err != nil {
return "", err
}
apps, err := manifest.GetApplications()
if err != nil {
return "", err
}
if len(apps) == 0 {
return "", fmt.Errorf("No apps declared in manifest '%s'", manifestFile)
}
namePropertyExists, err := manifest.ApplicationHasProperty(0, "name")
if err != nil {
return "", err
}
if !namePropertyExists {
return "", fmt.Errorf("No appName available in manifest '%s'", manifestFile)
}
appName, err := manifest.GetApplicationProperty(0, "name")
if err != nil {
return "", err
}
var name string
var ok bool
if name, ok = appName.(string); !ok {
return "", fmt.Errorf("appName from manifest '%s' has wrong type", manifestFile)
}
if len(name) == 0 {
return "", fmt.Errorf("appName from manifest '%s' is empty", manifestFile)
}
return name, nil
}
func prepareCfPushCfNativeDeploy(config *cloudFoundryDeployOptions) (string, []string, error) {
deployOptions := []string{}
varOptions, err := _getVarsOptions(config.ManifestVariables)
if err != nil {
return "", []string{}, errors.Wrapf(err, "Cannot prepare var-options: '%v'", config.ManifestVariables)
}
varFileOptions, err := _getVarsFileOptions(config.ManifestVariablesFiles)
if err != nil {
if e, ok := err.(*cloudfoundry.VarsFilesNotFoundError); ok {
for _, missingVarFile := range e.MissingFiles {
log.Entry().Warningf("We skip adding not-existing file '%s' as a vars-file to the cf create-service-push call", missingVarFile)
}
} else {
return "", []string{}, errors.Wrapf(err, "Cannot prepare var-file-options: '%v'", config.ManifestVariablesFiles)
}
}
deployOptions = append(deployOptions, varOptions...)
deployOptions = append(deployOptions, varFileOptions...)
return "push", deployOptions, nil
}
func deployMta(config *cloudFoundryDeployOptions, mtarFilePath string, command command.ExecRunner) error {
deployCommand := "deploy"
deployParams := []string{}
if len(config.MtaDeployParameters) > 0 {
deployParams = append(deployParams, strings.Split(config.MtaDeployParameters, " ")...)
}
if config.DeployType == "bg-deploy" || config.DeployType == "blue-green" {
deployCommand = "bg-deploy"
const noConfirmFlag = "--no-confirm"
if !slices.Contains(deployParams, noConfirmFlag) {
deployParams = append(deployParams, noConfirmFlag)
}
}
cfDeployParams := []string{
deployCommand,
mtarFilePath,
}
if len(deployParams) > 0 {
cfDeployParams = append(cfDeployParams, deployParams...)
}
extFileParams, extFiles := handleMtaExtensionDescriptors(config.MtaExtensionDescriptor)
for _, extFile := range extFiles {
_, err := fileUtils.Copy(extFile, extFile+".original")
if err != nil {
return fmt.Errorf("Cannot prepare mta extension files: %w", err)
}
_, _, err = handleMtaExtensionCredentials(extFile, config.MtaExtensionCredentials)
if err != nil {
return fmt.Errorf("Cannot handle credentials inside mta extension files: %w", err)
}
}
cfDeployParams = append(cfDeployParams, extFileParams...)
err := cfDeploy(config, cfDeployParams, nil, command)
for _, extFile := range extFiles {
renameError := fileUtils.FileRename(extFile+".original", extFile)
if err == nil && renameError != nil {
return renameError
}
}
return err
}
func handleMtaExtensionCredentials(extFile string, credentials map[string]interface{}) (updated, containsUnresolved bool, err error) {
log.Entry().Debugf("Inserting credentials into extension file '%s'", extFile)
b, err := fileUtils.FileRead(extFile)
if err != nil {
return false, false, errors.Wrapf(err, "Cannot handle credentials for mta extension file '%s'", extFile)
}
content := string(b)
env, err := toMap(_environ(), "=")
if err != nil {
return false, false, errors.Wrap(err, "Cannot handle mta extension credentials.")
}
missingCredentials := []string{}
for name, credentialKey := range credentials {
credKey, ok := credentialKey.(string)
if !ok {
return false, false, fmt.Errorf("cannot handle mta extension credentials: Cannot cast '%v' (type %T) to string", credentialKey, credentialKey)
}
const allowedVariableNamePattern = "^[-_A-Za-z0-9]+$"
alphaNumOnly := regexp.MustCompile(allowedVariableNamePattern)
if !alphaNumOnly.MatchString(name) {
return false, false, fmt.Errorf("credential key name '%s' contains unsupported character. Must contain only %s", name, allowedVariableNamePattern)
}
pattern := regexp.MustCompile("<%=\\s*" + name + "\\s*%>")
if pattern.MatchString(content) {
cred := env[toEnvVarKey(credKey)]
if len(cred) == 0 {
missingCredentials = append(missingCredentials, credKey)
continue
}
content = pattern.ReplaceAllLiteralString(content, cred)
updated = true
log.Entry().Debugf("Mta extension credentials handling: Placeholder '%s' has been replaced by credential denoted by '%s'/'%s' in file '%s'", name, credKey, toEnvVarKey(credKey), extFile)
} else {
log.Entry().Debugf("Mta extension credentials handling: Variable '%s' is not used in file '%s'", name, extFile)
}
}
if len(missingCredentials) > 0 {
missinCredsEnvVarKeyCompatible := []string{}
for _, missingKey := range missingCredentials {
missinCredsEnvVarKeyCompatible = append(missinCredsEnvVarKeyCompatible, toEnvVarKey(missingKey))
}
// ensure stable order of the entries. Needed e.g. for the tests.
sort.Strings(missingCredentials)
sort.Strings(missinCredsEnvVarKeyCompatible)
return false, false, fmt.Errorf("cannot handle mta extension credentials: No credentials found for '%s'/'%s'. Are these credentials maintained?", missingCredentials, missinCredsEnvVarKeyCompatible)
}
if !updated {
log.Entry().Debugf("Mta extension credentials handling: Extension file '%s' has not been updated. Seems to contain no credentials.", extFile)
} else {
fInfo, err := fileUtils.Stat(extFile)
fMode := fInfo.Mode()
if err != nil {
return false, false, errors.Wrap(err, "Cannot handle mta extension credentials.")
}
err = fileUtils.FileWrite(extFile, []byte(content), fMode)
if err != nil {
return false, false, errors.Wrap(err, "Cannot handle mta extension credentials.")
}
log.Entry().Debugf("Mta extension credentials handling: Extension file '%s' has been updated.", extFile)
}
re := regexp.MustCompile(`<%=.+%>`)
placeholders := re.FindAll([]byte(content), -1)
containsUnresolved = (len(placeholders) > 0)
if containsUnresolved {
log.Entry().Warningf("mta extension credential handling: Unresolved placeholders found after inserting credentials: %s", placeholders)
}
return updated, containsUnresolved, nil
}
func toEnvVarKey(key string) string {
key = regexp.MustCompile(`[^A-Za-z0-9]`).ReplaceAllString(key, "_")
return strings.ToUpper(regexp.MustCompile(`([a-z0-9])([A-Z])`).ReplaceAllString(key, "${1}_${2}"))
}
func toMap(keyValue []string, separator string) (map[string]string, error) {
result := map[string]string{}
for _, entry := range keyValue {
kv := strings.Split(entry, separator)
if len(kv) < 2 {
return map[string]string{}, fmt.Errorf("Cannot convert to map: separator '%s' not found in entry '%s'", separator, entry)
}
result[kv[0]] = strings.Join(kv[1:], separator)
}
return result, nil
}
func handleMtaExtensionDescriptors(mtaExtensionDescriptor string) ([]string, []string) {
var result = []string{}
var extFiles = []string{}
for _, part := range strings.Fields(strings.Trim(mtaExtensionDescriptor, " ")) {
if part == "-e" || part == "" {
continue
}
// REVISIT: maybe check if the extension descriptor exists
extFiles = append(extFiles, part)
}
if len(extFiles) > 0 {
result = append(result, "-e")
result = append(result, strings.Join(extFiles, ","))
}
return result, extFiles
}
func cfDeploy(
config *cloudFoundryDeployOptions,
cfDeployParams []string,
additionalEnvironment []string,
command command.ExecRunner) error {
const cfLogFile = "cf.log"
var err error
var loginPerformed bool
additionalEnvironment = append(additionalEnvironment, "CF_TRACE="+cfLogFile)
if len(config.CfHome) > 0 {
additionalEnvironment = append(additionalEnvironment, "CF_HOME="+config.CfHome)
}
if len(config.CfPluginHome) > 0 {
additionalEnvironment = append(additionalEnvironment, "CF_PLUGIN_HOME="+config.CfPluginHome)
}
log.Entry().Infof("Using additional environment variables: %s", additionalEnvironment)
// TODO set HOME to config.DockerWorkspace
command.SetEnv(additionalEnvironment)
err = command.RunExecutable("cf", "version")
if err == nil {
err = _cfLogin(command, cloudfoundry.LoginOptions{
CfAPIEndpoint: config.APIEndpoint,
CfOrg: config.Org,
CfSpace: config.Space,
Username: config.Username,
Password: config.Password,
CfLoginOpts: strings.Fields(config.LoginParameters),
})
}
if err == nil {
loginPerformed = true
err = command.RunExecutable("cf", []string{"plugins"}...)
if err != nil {
log.Entry().WithError(err).Errorf("Command '%s' failed.", []string{"plugins"})
}
}
if err == nil {
err = command.RunExecutable("cf", cfDeployParams...)
if err != nil {
log.Entry().WithError(err).Errorf("Command '%s' failed.", cfDeployParams)
}
}
if loginPerformed {
logoutErr := _cfLogout(command)
if logoutErr != nil {
log.Entry().WithError(logoutErr).Errorf("Cannot perform cf logout")
if err == nil {
err = logoutErr
}
}
}
if err != nil || GeneralConfig.Verbose {
e := handleCfCliLog(cfLogFile)
if e != nil {
log.Entry().WithError(err).Errorf("Error reading cf log file '%s'.", cfLogFile)
}
}
return err
}
func findMtar() (string, error) {
const pattern = "**/*.mtar"
mtars, err := fileUtils.Glob(pattern)
if err != nil {
return "", err
}
if len(mtars) == 0 {
return "", fmt.Errorf("No mtar file matching pattern '%s' found", pattern)
}
if len(mtars) > 1 {
sMtars := []string{}
sMtars = append(sMtars, mtars...)
return "", fmt.Errorf("Found multiple mtar files matching pattern '%s' (%s), please specify file via parameter 'mtarPath'", pattern, strings.Join(sMtars, ","))
}
return mtars[0], nil
}
func handleCfCliLog(logFile string) error {
fExists, err := fileUtils.FileExists(logFile)
if err != nil {
return err
}
log.Entry().Info("### START OF CF CLI TRACE OUTPUT ###")
if fExists {
f, err := os.Open(logFile)
if err != nil {
return err
}
defer f.Close()
bReader := bufio.NewReader(f)
for {
line, err := bReader.ReadString('\n')
if err == nil || err == io.EOF {
// maybe inappropriate to log as info. Maybe the line from the
// log indicates an error, but that is something like a project
// standard.
log.Entry().Info(strings.TrimSuffix(line, "\n"))
}
if err != nil {
break
}
}
} else {
log.Entry().Warningf("No trace file found at '%s'", logFile)
}
log.Entry().Info("### END OF CF CLI TRACE OUTPUT ###")
return err
}