Skip to content

Commit 61a3a97

Browse files
committed
Support generating arbitrary files
1 parent e159ee5 commit 61a3a97

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

cf_cli_java_plugin.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ type Command struct {
119119
FileExtension string
120120
FileLabel string
121121
FileNamePart string
122+
// Run the command in a subfolder of the container
123+
GenerateArbitraryFiles bool
124+
GenerateArbitraryFilesFolderName string
122125
}
123126

124127
var commands = []Command{
@@ -215,6 +218,8 @@ fi`,
215218
OnlyOnRecentSapMachine: true,
216219
RequiredTools: []string{"asprof"},
217220
GenerateFiles: false,
221+
GenerateArbitraryFiles: true,
222+
GenerateArbitraryFilesFolderName: "asprof",
218223
SshCommand: `$ASPROF_COMMAND $(pidof java) $$ARGS`,
219224
},
220225
{
@@ -244,7 +249,7 @@ fi`,
244249
NeedsFileName: true,
245250
FileExtension: ".jfr",
246251
FileNamePart: "asprof",
247-
SshCommand: `$ASPROF_COMMAND start $$ARGS -f $$FILE_NAME -e ctimer $(pidof java)`,
252+
SshCommand: `$ASPROF_COMMAND start $$ARGS -f $$FILE_NAME -e cpu $(pidof java)`,
248253
},
249254
{
250255
Name: "start-asprof-wall-clock-profile",
@@ -352,7 +357,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
352357
}
353358

354359
command := commands[index]
355-
if !command.GenerateFiles {
360+
if !command.GenerateFiles && !command.GenerateArbitraryFiles {
356361
for _, flag := range fileFlags {
357362
if commandFlags.IsSet(flag) {
358363
return "", &InvalidUsageError{message: fmt.Sprintf("The flag %q is not supported for %s", flag, command.Name)}
@@ -382,10 +387,10 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
382387

383388
for _, requiredTool := range command.RequiredTools {
384389
uppercase := strings.ToUpper(requiredTool)
385-
var toolCommand = fmt.Sprintf("%s_COMMAND=$(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)
390+
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)
386391
if requiredTool == "jcmd" {
387392
// add code that first checks whether asprof is present and if so use `asprof jcmd` instead of `jcmd`
388-
remoteCommandTokens = append(remoteCommandTokens, toolCommand, "ASPROF_COMMAND=$(find -executable -name asprof | head -1 | tr -d [:space:]); if [ -n \"${ASPROF_COMMAND}\" ]; then JCMD_COMMAND=\"${ASPROF_COMMAND} jcmd\"; fi")
393+
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")
389394
} else {
390395
remoteCommandTokens = append(remoteCommandTokens, toolCommand)
391396
}
@@ -398,15 +403,23 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
398403
"$$APP_NAME": applicationName,
399404
}
400405

401-
if command.GenerateFiles || command.NeedsFileName {
406+
if command.GenerateFiles || command.NeedsFileName || command.GenerateArbitraryFiles {
402407
fspath, err = util.GetAvailablePath(applicationName, remoteDir)
403408
if err != nil {
404409
return "", err
405410
}
411+
if command.GenerateArbitraryFiles {
412+
fspath = fspath + "/" + command.GenerateArbitraryFilesFolderName
413+
}
406414

407415
fileName = fspath + "/" + applicationName + "-" + command.FileNamePart + "-" + uuidGenerator.Generate() + command.FileExtension
408416
replacements["$$FILE_NAME"] = fileName
409417
replacements["$$FSPATH"] = fspath
418+
if command.GenerateArbitraryFiles {
419+
// prepend 'mkdir -p $$FSPATH' to the command to create the directory if it does not exist
420+
remoteCommandTokens = append([]string{"mkdir -p " + fspath}, remoteCommandTokens...)
421+
remoteCommandTokens = append(remoteCommandTokens, "cd " + fspath)
422+
}
410423
}
411424

412425
var commandText = command.SshCommand
@@ -472,6 +485,34 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
472485
fmt.Println(toSentenceCase(command.FileLabel) + " file deleted in app container")
473486
}
474487
}
488+
if command.GenerateArbitraryFiles {
489+
// download all files in the generic folder
490+
files, err := util.ListFiles(cfSSHArguments, fspath)
491+
if err != nil {
492+
return "", err
493+
}
494+
if copyToLocal {
495+
for _, file := range files {
496+
localFileFullPath := localDir + "/" + file
497+
err = util.CopyOverCat(cfSSHArguments, fspath+"/"+file, localFileFullPath)
498+
if err == nil {
499+
fmt.Printf("File %s saved to: %s\n", file, localFileFullPath)
500+
} else {
501+
return "", err
502+
}
503+
}
504+
} else {
505+
fmt.Println(toSentenceCase(command.FileLabel) + " will not be copied as parameter `local-dir` was not set")
506+
}
507+
508+
if !keepAfterDownload {
509+
err = util.DeleteRemoteFile(cfSSHArguments, fspath)
510+
if err != nil {
511+
return "", err
512+
}
513+
fmt.Println("File folder deleted in app container")
514+
}
515+
}
475516
// We keep this around to make the compiler happy, but commandExecutor.Execute will cause an os.Exit
476517
return strings.Join(output, "\n"), err
477518
}

cf_cli_java_plugin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
JcmdDetectionCommand = "JCMD_COMMAND=$(find -executable -name jcmd | head -1 | tr -d [:space:]); if [ -z \"${JCMD_COMMAND}\" ]; then echo \"jcmd not found\"; exit 1; fi; ASPROF_COMMAND=$(find -executable -name asprof | head -1 | tr -d [:space:]); if [ -n \"${ASPROF_COMMAND}\" ]; then JCMD_COMMAND=\"${ASPROF_COMMAND} jcmd\"; fi"
18+
JcmdDetectionCommand = "JCMD_COMMAND=$(realpath $(find -executable -name jcmd | head -1 | tr -d [:space:])); if [ -z \"${JCMD_COMMAND}\" ]; then echo \"jcmd not found\"; exit 1; fi; ASPROF_COMMAND=$(realpath $(find -executable -name asprof | head -1 | tr -d [:space:])); if [ -n \"${ASPROF_COMMAND}\" ]; then JCMD_COMMAND=\"${ASPROF_COMMAND} jcmd\"; fi"
1919
)
2020

2121
type commandOutput struct {

utils/cf_java_plugin_util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ type CfJavaPluginUtil interface {
88
FindHeapDumpFile(args []string, fullpath string, fspath string) (string, error)
99
FindJFRFile(args []string, fullpath string, fspath string) (string, error)
1010
FindFile(args []string, fullpath string, fspath string, pattern string) (string, error)
11+
ListFiles(args []string, path string) ([]string, error)
1112
}

utils/cfutils.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (checker CfJavaPluginUtilImpl) CopyOverCat(args []string, src string, dest
186186
}
187187

188188
func (checker CfJavaPluginUtilImpl) DeleteRemoteFile(args []string, path string) error {
189-
args = append(args, "rm "+path)
189+
args = append(args, "rm -fr "+path)
190190
_, err := exec.Command("cf", args...).Output()
191191

192192
if err != nil {
@@ -218,3 +218,14 @@ func (checker CfJavaPluginUtilImpl) FindFile(args []string, fullpath string, fsp
218218
return strings.Trim(string(output[:]), "\n"), nil
219219

220220
}
221+
222+
func (checker CfJavaPluginUtilImpl) ListFiles(args []string, path string) ([]string, error) {
223+
cmd := "ls " + path
224+
args = append(args, cmd)
225+
output, err := exec.Command("cf", args...).Output()
226+
227+
if err != nil {
228+
return nil, errors.New("error occured while listing files: " + string(output[:]))
229+
}
230+
return strings.Split(strings.Trim(string(output[:]), "\n"), "\n"), nil
231+
}

utils/fakes/fake_utils_impl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ func (fake FakeCfJavaPluginUtil) FindJFRFile(args []string, fullpath string, fsp
9191

9292
func (fake FakeCfJavaPluginUtil) FindFile(args []string, fullpath string, fspath string, pattern string) (string, error) {
9393
return fake.FindHeapDumpFile(args, fullpath, fspath) // same as FindHeapDumpFile, just to avoid duplication
94+
}
95+
96+
func (fake FakeCfJavaPluginUtil) ListFiles(args []string, path string) ([]string, error) {
97+
return []string{fake.OutputFileName}, nil
9498
}

0 commit comments

Comments
 (0)