Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
667 lines (603 loc) · 26 KB

File metadata and controls

667 lines (603 loc) · 26 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved.
* This file is licensed under the Apache Software License, v. 2 except as noted
* otherwise in the LICENSE file at the root of the repository.
*/
package main
import (
"github.com/SAP/cf-cli-java-plugin/cmd"
"github.com/SAP/cf-cli-java-plugin/uuid"
"errors"
"fmt"
"os"
"strconv"
"strings"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/trace"
"code.cloudfoundry.org/cli/plugin"
"utils"
guuid "github.com/satori/go.uuid"
"github.com/simonleung8/flags"
)
// The JavaPlugin is a cf cli plugin that supports taking heap and thread dumps on demand
type JavaPlugin struct{}
// InvalidUsageError errors mean that the arguments passed in input to the command are invalid
type InvalidUsageError struct {
message string
}
func (e InvalidUsageError) Error() string {
return e.message
}
type commandExecutorImpl struct {
cliConnection plugin.CliConnection
}
func (c commandExecutorImpl) Execute(args []string) ([]string, error) {
output, err := c.cliConnection.CliCommand(args...)
return output, err
}
type uuidGeneratorImpl struct {
}
func (u uuidGeneratorImpl) Generate() string {
return guuid.NewV4().String()
}
const (
// JavaDetectionCommand is the prologue command to detect on the Garden container if it contains a Java app. Visible for tests
JavaDetectionCommand = "if ! pgrep -x \"java\" > /dev/null; then echo \"No 'java' process found running. Are you sure this is a Java app?\" >&2; exit 1; fi"
)
// Run must be implemented by any plugin because it is part of the
// plugin interface defined by the core CLI.
//
// Run(....) is the entry point when the core CLI is invoking a command defined
// by a plugin. The first parameter, plugin.CliConnection, is a struct that can
// be used to invoke cli commands. The second paramter, args, is a slice of
// strings. args[0] will be the Name of the command, and will be followed by
// any additional arguments a cli user typed in.
//
// Any error handling should be handled with the plugin itself (this means printing
// user facing errors). The CLI will exit 0 if the plugin exits 0 and will exit
// 1 should the plugin exit nonzero.
func (c *JavaPlugin) Run(cliConnection plugin.CliConnection, args []string) {
_, err := c.DoRun(&commandExecutorImpl{cliConnection: cliConnection}, &uuidGeneratorImpl{}, utils.CfJavaPluginUtilImpl{}, args)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
// DoRun is an internal method that we use to wrap the cmd package with CommandExecutor for test purposes
func (c *JavaPlugin) DoRun(commandExecutor cmd.CommandExecutor, uuidGenerator uuid.UUIDGenerator, util utils.CfJavaPluginUtil, args []string) (string, error) {
traceLogger := trace.NewLogger(os.Stdout, true, os.Getenv("CF_TRACE"), "")
ui := terminal.NewUI(os.Stdin, os.Stdout, terminal.NewTeePrinter(os.Stdout), traceLogger)
output, err := c.execute(commandExecutor, uuidGenerator, util, args)
if err != nil {
ui.Failed(err.Error())
if _, invalidUsageErr := err.(*InvalidUsageError); invalidUsageErr {
fmt.Println()
fmt.Println()
_, err := commandExecutor.Execute([]string{"help", "java"})
if err != nil {
ui.Failed("Failed to show help")
}
}
} else if output != "" {
ui.Say(output)
}
return output, err
}
type Command struct {
Name string
Description string
OnlyOnRecentSapMachine bool
// Required tools, checked and $TOOL_COMMAND set in the remote command
// jcmd is special: it uses asprof if available
RequiredTools []string
GenerateFiles bool
NeedsFileName bool
// use $$FILENAME to get the generated file Name and $$FSPATH to get the path where the file is stored
SshCommand string
FilePattern string
FileExtension string
FileLabel string
FileNamePart string
// Run the command in a subfolder of the container
GenerateArbitraryFiles bool
GenerateArbitraryFilesFolderName string
}
// function names "HasMiscArgs" that is used on Command and checks whethere the SSHCommand contains $$ARGS
func (c *Command) HasMiscArgs() bool {
return strings.Contains(c.SshCommand, "$$ARGS")
}
var commands = []Command{
{
Name: "heap-dump",
Description: "Generate a heap dump from a running Java application",
GenerateFiles: true,
FileExtension: ".hprof",
/*
If there is not enough space on the filesystem to write the dump, jmap will create a file
with size 0, output something about not enough space left on the device, and exit with status code 0.
Because YOLO.
Also: if the heap dump file already exists, jmap will output something about the file already
existing and exit with status code 0. At least it is consistent.
OpenJDK: Wrap everything in an if statement in case jmap is available
*/
SshCommand: `if [ -f $$FILE_NAME ]; then echo >&2 'Heap dump $$FILE_NAME already exists'; exit 1; fi
JMAP_COMMAND=$(find -executable -name jmap | head -1 | tr -d [:space:])
# SAP JVM: Wrap everything in an if statement in case jvmmon is available
JVMMON_COMMAND=$(find -executable -name jvmmon | head -1 | tr -d [:space:])
if [ -n "${JMAP_COMMAND}" ]; then
OUTPUT=$( ${JMAP_COMMAND} -dump:format=b,file=$$FILE_NAME $(pidof java) ) || STATUS_CODE=$?
if [ ! -s $$FILE_NAME ]; then echo >&2 ${OUTPUT}; exit 1; fi
if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi
elif [ -n "${JVMMON_COMMAND}" ]; then
echo -e 'change command line flag flags=-XX:HeapDumpOnDemandPath=$$FSPATH\ndump heap' > setHeapDumpOnDemandPath.sh
OUTPUT=$( ${JVMMON_COMMAND} -pid $(pidof java) -cmd "setHeapDumpOnDemandPath.sh" ) || STATUS_CODE=$?
sleep 5 # Writing the heap dump is triggered asynchronously -> give the JVM some time to create the file
HEAP_DUMP_NAME=$(find $$FSPATH -name 'java_pid*.hprof' -printf '%T@ %p\0' | sort -zk 1nr | sed -z 's/^[^ ]* //' | tr '\0' '\n' | head -n 1)
SIZE=-1; OLD_SIZE=$(stat -c '%s' "${HEAP_DUMP_NAME}"); while [ ${SIZE} != ${OLD_SIZE} ]; do OLD_SIZE=${SIZE}; sleep 3; SIZE=$(stat -c '%s' "${HEAP_DUMP_NAME}"); done
if [ ! -s "${HEAP_DUMP_NAME}" ]; then echo >&2 ${OUTPUT}; exit 1; fi
if [ ${STATUS_CODE:-0} -gt 0 ]; then echo >&2 ${OUTPUT}; exit ${STATUS_CODE}; fi
fi`,
FileLabel: "heap dump",
FileNamePart: "heapdump",
},
{
Name: "thread-dump",
Description: "Generate a thread dump from a running Java application",
GenerateFiles: false,
SshCommand: "JSTACK_COMMAND=`find -executable -name jstack | head -1`; if [ -n \"${JSTACK_COMMAND}\" ]; then ${JSTACK_COMMAND} $(pidof java); exit 0; fi; " +
"JVMMON_COMMAND=`find -executable -name jvmmon | head -1`; if [ -n \"${JVMMON_COMMAND}\" ]; then ${JVMMON_COMMAND} -pid $(pidof java) -c \"print stacktrace\"; fi",
},
{
Name: "vm-info",
Description: "Print information about the Java Virtual Machine running a Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
SshCommand: `$JCMD_COMMAND $(pidof java) VM.info`,
},
{
Name: "jcmd",
Description: "Run a JCMD command on a running Java application via --args",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
SshCommand: `$JCMD_COMMAND $(pidof java) $$ARGS`,
},
{
Name: "jfr-start",
Description: "Start a Java Flight Recorder default recording on a running Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
SshCommand: `$JCMD_COMMAND $(pidof java) JFR.start settings=profile.jfc; echo "Use 'cf java jfr-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "jfr-start-profile",
Description: "Start a Java Flight Recorder profile recording on a running Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
SshCommand: `$JCMD_COMMAND $(pidof java) JFR.start settings=profile.jfc; echo "Use 'cf java jfr-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "jfr-start-gc",
Description: "Start a Java Flight Recorder GC recording on a running Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
OnlyOnRecentSapMachine: true,
SshCommand: `$JCMD_COMMAND $(pidof java) JFR.start settings=gc.jfc; echo "Use 'cf java jfr-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "jfr-start-gc-details",
Description: "Start a Java Flight Recorder detailed GC recording on a running Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
OnlyOnRecentSapMachine: true,
SshCommand: `$JCMD_COMMAND $(pidof java) JFR.start settings=gc_details.jfc; echo "Use 'cf java jfr-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "jfr-stop",
Description: "Stop a Java Flight Recorder recording on a running Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: true,
FileExtension: ".jfr",
FileLabel: "JFR recording",
FileNamePart: "jfr",
SshCommand: `$JCMD_COMMAND $(pidof java) JFR.stop filename=$$FILE_NAME; $JCMD_COMMAND $(pidof java) JFR.stop`,
},
{
Name: "jfr-dump",
Description: "Dump a Java Flight Recorder recording on a running Java application without stopping it",
RequiredTools: []string{"jcmd"},
GenerateFiles: true,
FileExtension: ".jfr",
FileLabel: "JFR recording",
FileNamePart: "jfr",
SshCommand: `$JCMD_COMMAND $(pidof java) JFR.dump filename=$$FILE_NAME; $JCMD_COMMAND $(pidof java) JFR.stop`,
},
{
Name: "jfr-status",
Description: "Check the running Java Flight Recorder recording on a running Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
SshCommand: `$JCMD_COMMAND $(pidof java) JFR.check`,
},
{
Name: "vm-version",
Description: "Print the version of the Java Virtual Machine running a Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
SshCommand: `$JCMD_COMMAND $(pidof java) VM.version`,
},
{
Name: "vm-vitals",
Description: "Print vital statistics about the Java Virtual Machine running a Java application",
RequiredTools: []string{"jcmd"},
GenerateFiles: false,
SshCommand: `$JCMD_COMMAND $(pidof java) VM.vitals`,
},
{
Name: "asprof",
Description: "Run async-profiler commands passed to asprof via --args",
OnlyOnRecentSapMachine: true,
RequiredTools: []string{"asprof"},
GenerateFiles: false,
GenerateArbitraryFiles: true,
GenerateArbitraryFilesFolderName: "asprof",
SshCommand: `$ASPROF_COMMAND $(pidof java) $$ARGS`,
},
{
Name: "asprof-start-cpu",
Description: "Start an async-profiler CPU-time profile recording on a running Java application",
OnlyOnRecentSapMachine: true,
RequiredTools: []string{"asprof"},
GenerateFiles: false,
NeedsFileName: true,
FileExtension: ".jfr",
FileNamePart: "asprof",
SshCommand: `$ASPROF_COMMAND start $(pidof java) -e cpu -f $$FILE_NAME; echo "Use 'cf java asprof-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "asprof-start-wall",
Description: "Start an async-profiler wall-clock profile recording on a running Java application",
OnlyOnRecentSapMachine: true,
RequiredTools: []string{"asprof"},
GenerateFiles: false,
NeedsFileName: true,
FileExtension: ".jfr",
FileNamePart: "asprof",
SshCommand: `$ASPROF_COMMAND start $(pidof java) -e wall -f $$FILE_NAME; echo "Use 'cf java asprof-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "asprof-start-alloc",
Description: "Start an async-profiler allocation profile recording on a running Java application",
OnlyOnRecentSapMachine: true,
RequiredTools: []string{"asprof"},
GenerateFiles: false,
NeedsFileName: true,
FileExtension: ".jfr",
FileNamePart: "asprof",
SshCommand: `$ASPROF_COMMAND start $(pidof java) -e alloc -f $$FILE_NAME; echo "Use 'cf java asprof-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "asprof-start-lock",
Description: "Start an async-profiler lock profile recording on a running Java application",
OnlyOnRecentSapMachine: true,
RequiredTools: []string{"asprof"},
GenerateFiles: false,
NeedsFileName: true,
FileExtension: ".jfr",
FileNamePart: "asprof",
SshCommand: `$ASPROF_COMMAND start $(pidof java) -e lock -f $$FILE_NAME; echo "Use 'cf java asprof-stop $$APP_NAME' to copy the file to the local folder"`,
},
{
Name: "asprof-stop",
Description: "Stop an async-profiler profile recording on a running Java application",
RequiredTools: []string{"asprof"},
OnlyOnRecentSapMachine: true,
GenerateFiles: true,
FileExtension: ".jfr",
FileLabel: "JFR recording",
FileNamePart: "asprof",
SshCommand: `$ASPROF_COMMAND stop $(pidof java)`,
},
{
Name: "asprof-dump",
Description: "Dump an async-profiler profile recording without stopping it",
RequiredTools: []string{"asprof"},
OnlyOnRecentSapMachine: true,
GenerateFiles: true,
FileExtension: ".jfr",
FileLabel: "JFR recording",
FileNamePart: "asprof",
SshCommand: `$ASPROF_COMMAND dump $(pidof java)`,
},
{
Name: "asprof-status",
Description: "Get the status of async-profiler on a running Java application",
RequiredTools: []string{"asprof"},
OnlyOnRecentSapMachine: true,
GenerateFiles: false,
SshCommand: `$ASPROF_COMMAND status $(pidof java)`,
},
}
func toSentenceCase(input string) string {
// Convert the first character to uppercase and the rest to lowercase
if len(input) == 0 {
return input
}
// Convert the first letter to uppercase
return strings.ToUpper(string(input[0])) + strings.ToLower(input[1:])
}
func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator uuid.UUIDGenerator, util utils.CfJavaPluginUtil, args []string) (string, error) {
if len(args) == 0 {
return "", &InvalidUsageError{message: "No command provided"}
}
switch args[0] {
case "CLI-MESSAGE-UNINSTALL":
// Nothing to uninstall, we keep no local state
return "", nil
case "java":
break
default:
return "", &InvalidUsageError{message: fmt.Sprintf("Unexpected command Name '%s' (expected : 'java')", args[0])}
}
if os.Getenv("CF_TRACE") == "true" {
return "", errors.New("The environment variable CF_TRACE is set to true. This prevents download of the dump from succeeding")
}
commandFlags := flags.New()
commandFlags.NewIntFlagWithDefault("app-instance-index", "i", "application `instance` to connect to", -1)
commandFlags.NewBoolFlag("keep", "k", "whether to `keep` the heap-dump/JFR/... files on the container of the application instance after having downloaded it locally")
commandFlags.NewBoolFlag("dry-run", "n", "triggers the `dry-run` mode to show only the cf-ssh command that would have been executed")
commandFlags.NewStringFlag("container-dir", "cd", "specify the folder path where the dump/JFR/... file should be stored in the container")
commandFlags.NewStringFlag("local-dir", "ld", "specify the folder where the dump/JFR/... file will be downloaded to, dump file wil not be copied to local if this parameter was not set")
commandFlags.NewStringFlag("args", "a", "Miscellaneous arguments to pass to the command in the container, be aware to end it with a space if it is a simple option")
fileFlags := []string{"container-dir", "local-dir", "keep"}
parseErr := commandFlags.Parse(args[1:]...)
if parseErr != nil {
return "", &InvalidUsageError{message: fmt.Sprintf("Error while parsing command arguments: %v", parseErr)}
}
miscArgs := ""
if commandFlags.IsSet("args") {
miscArgs = commandFlags.String("args")
}
applicationInstance := commandFlags.Int("app-instance-index")
keepAfterDownload := commandFlags.IsSet("keep")
remoteDir := commandFlags.String("container-dir")
localDir := commandFlags.String("local-dir")
if localDir == "" {
localDir = "."
}
arguments := commandFlags.Args()
argumentLen := len(arguments)
if argumentLen < 1 {
return "", &InvalidUsageError{message: "No command provided"}
}
commandName := arguments[0]
index := -1
for i, command := range commands {
if command.Name == commandName {
index = i
break
}
}
if index == -1 {
avCommands := make([]string, 0, len(commands))
for _, command := range commands {
avCommands = append(avCommands, command.Name)
}
return "", &InvalidUsageError{message: fmt.Sprintf("Unrecognized command %q: supported commands are %s' (see cf help)", commandName, strings.Join(avCommands, ", "))}
}
command := commands[index]
if !command.GenerateFiles && !command.GenerateArbitraryFiles {
for _, flag := range fileFlags {
if commandFlags.IsSet(flag) {
return "", &InvalidUsageError{message: fmt.Sprintf("The flag %q is not supported for %s", flag, command.Name)}
}
}
}
if !command.HasMiscArgs() && commandFlags.IsSet("args") {
return "", &InvalidUsageError{message: fmt.Sprintf("The flag %q is not supported for %s", "args", command.Name)}
}
if argumentLen == 1 {
return "", &InvalidUsageError{message: "No application name provided"}
} else if argumentLen > 2 {
return "", &InvalidUsageError{message: fmt.Sprintf("Too many arguments provided: %v", strings.Join(arguments[2:], ", "))}
}
applicationName := arguments[1]
cfSSHArguments := []string{"ssh", applicationName}
if applicationInstance > 0 {
cfSSHArguments = append(cfSSHArguments, "--app-instance-index", strconv.Itoa(applicationInstance))
}
supported, err := util.CheckRequiredTools(applicationName)
if err != nil || !supported {
return "required tools checking failed", err
}
var remoteCommandTokens = []string{JavaDetectionCommand}
for _, requiredTool := range command.RequiredTools {
uppercase := strings.ToUpper(requiredTool)
var toolCommand = fmt.Sprintf("%s_COMMAND=$(realpath $(find -executable -name %s | head -1 | tr -d [:space:])); if [ -z \"${%s_COMMAND}\" ]; then echo \"%s not found\"; exit 1; fi", uppercase, requiredTool, uppercase, requiredTool)
if requiredTool == "jcmd" {
// add code that first checks whether asprof is present and if so use `asprof jcmd` instead of `jcmd`
remoteCommandTokens = append(remoteCommandTokens, toolCommand, "ASPROF_COMMAND=$(realpath $(find -executable -name asprof | head -1 | tr -d [:space:])); if [ -n \"${ASPROF_COMMAND}\" ]; then JCMD_COMMAND=\"${ASPROF_COMMAND} jcmd\"; fi")
} else {
remoteCommandTokens = append(remoteCommandTokens, toolCommand)
}
}
fileName := ""
fspath := remoteDir
var replacements = map[string]string{
"$$ARGS": miscArgs,
"$$APP_NAME": applicationName,
}
if command.GenerateFiles || command.NeedsFileName || command.GenerateArbitraryFiles {
fspath, err = util.GetAvailablePath(applicationName, remoteDir)
if err != nil {
return "", err
}
if command.GenerateArbitraryFiles {
fspath = fspath + "/" + command.GenerateArbitraryFilesFolderName
}
fileName = fspath + "/" + applicationName + "-" + command.FileNamePart + "-" + uuidGenerator.Generate() + command.FileExtension
replacements["$$FILE_NAME"] = fileName
replacements["$$FSPATH"] = fspath
if command.GenerateArbitraryFiles {
// prepend 'mkdir -p $$FSPATH' to the command to create the directory if it does not exist
remoteCommandTokens = append([]string{"mkdir -p " + fspath}, remoteCommandTokens...)
remoteCommandTokens = append(remoteCommandTokens, "cd "+fspath)
}
}
var commandText = command.SshCommand
for key, value := range replacements {
commandText = strings.ReplaceAll(commandText, key, value)
}
remoteCommandTokens = append(remoteCommandTokens, commandText)
cfSSHArguments = append(cfSSHArguments, "--command")
remoteCommand := strings.Join(remoteCommandTokens, "; ")
if commandFlags.IsSet("dry-run") {
// When printing out the entire command line for separate execution, we wrap the remote command in single quotes
// to prevent the shell processing it from running it in local
cfSSHArguments = append(cfSSHArguments, "'"+remoteCommand+"'")
return "cf " + strings.Join(cfSSHArguments, " "), nil
}
fullCommand := append(cfSSHArguments, remoteCommand)
output, err := commandExecutor.Execute(fullCommand)
if command.GenerateFiles {
finalFile := ""
var err error
switch command.FileExtension {
case ".hprof":
finalFile, err = util.FindHeapDumpFile(cfSSHArguments, fileName, fspath)
case ".jfr":
finalFile, err = util.FindJFRFile(cfSSHArguments, fileName, fspath)
default:
return "", &InvalidUsageError{message: fmt.Sprintf("Unsupported file extension %q", command.FileExtension)}
}
if err == nil && finalFile != "" {
fileName = finalFile
fmt.Println("Successfully created " + command.FileLabel + " in application container at: " + fileName)
} else {
fmt.Println("Failed to find " + command.FileLabel + " in application container")
return "", err
}
localFileFullPath := localDir + "/" + applicationName + "-" + command.FileNamePart + "-" + uuidGenerator.Generate() + command.FileExtension
err = util.CopyOverCat(cfSSHArguments, fileName, localFileFullPath)
if err == nil {
fmt.Println(toSentenceCase(command.FileLabel) + " file saved to: " + localFileFullPath)
} else {
return "", err
}
if !keepAfterDownload {
err = util.DeleteRemoteFile(cfSSHArguments, fileName)
if err != nil {
return "", err
}
fmt.Println(toSentenceCase(command.FileLabel) + " file deleted in application container")
}
}
if command.GenerateArbitraryFiles {
// download all files in the generic folder
files, err := util.ListFiles(cfSSHArguments, fspath)
if err != nil {
return "", err
}
if len(files) != 0 {
fmt.Println("Files in the folder: ", files)
for _, file := range files {
localFileFullPath := localDir + "/" + file
err = util.CopyOverCat(cfSSHArguments, fspath+"/"+file, localFileFullPath)
if err == nil {
fmt.Printf("File %s saved to: %s\n", file, localFileFullPath)
} else {
return "", err
}
}
if !keepAfterDownload {
err = util.DeleteRemoteFile(cfSSHArguments, fspath)
if err != nil {
return "", err
}
fmt.Println("File folder deleted in application container")
}
}
}
// We keep this around to make the compiler happy, but commandExecutor.Execute will cause an os.Exit
return strings.Join(output, "\n"), err
}
// GetMetadata must be implemented as part of the plugin interface
// defined by the core CLI.
//
// GetMetadata() returns a PluginMetadata struct. The first field, Name,
// determines the Name of the plugin which should generally be without spaces.
// If there are spaces in the Name a user will need to properly quote the Name
// during uninstall otherwise the Name will be treated as seperate arguments.
// The second value is a slice of Command structs. Our slice only contains one
// Command Struct, but could contain any number of them. The first field Name
// defines the command `cf heapdump` once installed into the CLI. The
// second field, HelpText, is used by the core CLI to display help information
// to the user in the core commands `cf help`, `cf`, or `cf -h`.
func (c *JavaPlugin) GetMetadata() plugin.PluginMetadata {
var usageText = "cf java COMMAND APP_NAME [options]"
for _, command := range commands {
usageText += "\n\n " + command.Name
if command.OnlyOnRecentSapMachine || command.HasMiscArgs() {
usageText += " ("
if command.OnlyOnRecentSapMachine {
usageText += "recent SapMachine only"
}
if command.HasMiscArgs() {
if command.OnlyOnRecentSapMachine {
usageText += ", "
}
usageText += "supports --args"
}
usageText += ")"
}
usageText += "\n " + command.Description
}
return plugin.PluginMetadata{
Name: "java",
Version: plugin.VersionType{
Major: 3,
Minor: 0,
Build: 3,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 7,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "java",
HelpText: "Obtain a heap-dump, thread-dump or profile from a running, SSH-enabled Java application.",
// UsageDetails is optional
// It is used to show help of usage of each command
UsageDetails: plugin.Usage{
Usage: usageText,
Options: map[string]string{
"app-instance-index": "-i [index], select to which instance of the app to connect",
"keep": "-k, keep the heap dump in the container; by default the heap dump/JFR/... will be deleted from the container's filesystem after been downloaded",
"dry-run": "-n, just output to command line what would be executed",
"container-dir": "-cd, the directory path in the container that the heap dump/JFR/... file will be saved to",
"local-dir": "-ld, the local directory path that the dump/JFR/... file will be saved to, defaults to the current directory",
"args": "-a, Miscellaneous arguments to pass to the command (if supported) in the container, be aware to end it with a space if it is a simple option",
},
},
},
},
}
}
// Unlike most Go programs, the `Main()` function will not be used to run all of the
// commands provided in your plugin. Main will be used to initialize the plugin
// process, as well as any dependencies you might require for your
// plugin.
func main() {
// Any initialization for your plugin can be handled here
//
// Note: to run the plugin.Start method, we pass in a pointer to the struct
// implementing the interface defined at "code.cloudfoundry.org/cli/plugin/plugin.go"
//
// Note: The plugin's main() method is invoked at install time to collect
// metadata. The plugin will exit 0 and the Run([]string) method will not be
// invoked.
plugin.Start(new(JavaPlugin))
// Plugin code should be written in the Run([]string) method,
// ensuring the plugin environment is bootstrapped.
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.