Skip to content

Commit b9c44d2

Browse files
authored
fix heap dumps on instances except 0 (#31)
1 parent 560fdff commit b9c44d2

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

cf_cli_java_plugin.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,13 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
244244
return "cf " + strings.Join(cfSSHArguments, " "), nil
245245
}
246246

247-
cfSSHArguments = append(cfSSHArguments, remoteCommand)
247+
fullCommand := append(cfSSHArguments, remoteCommand)
248+
249+
output, err := commandExecutor.Execute(fullCommand)
248250

249-
output, err := commandExecutor.Execute(cfSSHArguments)
250251
if command == heapDumpCommand {
251252

252-
finalFile, err := util.FindDumpFile(applicationName, heapdumpFileName, fspath)
253+
finalFile, err := util.FindDumpFile(cfSSHArguments, heapdumpFileName, fspath)
253254
if err == nil && finalFile != "" {
254255
heapdumpFileName = finalFile
255256
fmt.Println("Successfully created heap dump in application container at: " + heapdumpFileName)
@@ -263,7 +264,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
263264

264265
if copyToLocal {
265266
localFileFullPath := localDir + "/" + applicationName + "-heapdump-" + uuidGenerator.Generate() + ".hprof"
266-
err = util.CopyOverCat(applicationName, heapdumpFileName, localFileFullPath)
267+
err = util.CopyOverCat(cfSSHArguments, heapdumpFileName, localFileFullPath)
267268
if err == nil {
268269
fmt.Println("Heap dump file saved to: " + localFileFullPath)
269270
} else {
@@ -274,7 +275,7 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
274275
}
275276

276277
if !keepAfterDownload {
277-
err = util.DeleteRemoteFile(applicationName, heapdumpFileName)
278+
err = util.DeleteRemoteFile(cfSSHArguments, heapdumpFileName)
278279
if err != nil {
279280
return "", err
280281
}
@@ -303,7 +304,7 @@ func (c *JavaPlugin) GetMetadata() plugin.PluginMetadata {
303304
Version: plugin.VersionType{
304305
Major: 3,
305306
Minor: 0,
306-
Build: 2,
307+
Build: 3,
307308
},
308309
MinCliVersion: plugin.VersionType{
309310
Major: 6,

utils/cf_java_plugin_util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package utils
33
type CfJavaPluginUtil interface {
44
CheckRequiredTools(app string) (bool, error)
55
GetAvailablePath(data string, userpath string) (string, error)
6-
CopyOverCat(app string, src string, dest string) error
7-
DeleteRemoteFile(app string, path string) error
8-
FindDumpFile(app string, fullpath string, fspath string) (string, error)
6+
CopyOverCat(args []string, src string, dest string) error
7+
DeleteRemoteFile(args []string, path string) error
8+
FindDumpFile(args []string, fullpath string, fspath string) (string, error)
99
}

utils/cfutils.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ func (checker CfJavaPluginUtilImpl) GetAvailablePath(data string, userpath strin
160160
return "/tmp", nil
161161
}
162162

163-
func (checker CfJavaPluginUtilImpl) CopyOverCat(app string, src string, dest string) error {
163+
func (checker CfJavaPluginUtilImpl) CopyOverCat(args []string, src string, dest string) error {
164164
f, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
165165
if err != nil {
166166
return errors.New("Error creating local file at " + dest + ". Please check that you are allowed to create files at the given local path.")
167167
}
168168
defer f.Close()
169-
cat := exec.Command("cf", "ssh", app, "-c", "cat "+src)
169+
170+
args = append(args, "cat "+src)
171+
cat := exec.Command("cf", args...)
170172

171173
cat.Stdout = f
172174

@@ -183,8 +185,9 @@ func (checker CfJavaPluginUtilImpl) CopyOverCat(app string, src string, dest str
183185
return nil
184186
}
185187

186-
func (checker CfJavaPluginUtilImpl) DeleteRemoteFile(app string, path string) error {
187-
_, err := exec.Command("cf", "ssh", app, "-c", "rm "+path).Output()
188+
func (checker CfJavaPluginUtilImpl) DeleteRemoteFile(args []string, path string) error {
189+
args = append(args, "rm "+path)
190+
_, err := exec.Command("cf", args...).Output()
188191

189192
if err != nil {
190193
return errors.New("error occured while removing dump file generated")
@@ -194,10 +197,11 @@ func (checker CfJavaPluginUtilImpl) DeleteRemoteFile(app string, path string) er
194197
return nil
195198
}
196199

197-
func (checker CfJavaPluginUtilImpl) FindDumpFile(app string, fullpath string, fspath string) (string, error) {
200+
func (checker CfJavaPluginUtilImpl) FindDumpFile(args []string, fullpath string, fspath string) (string, error) {
198201
cmd := " [ -f '" + fullpath + "' ] && echo '" + fullpath + "' || find " + fspath + " -name 'java_pid*.hprof' -printf '%T@ %p\\0' | sort -zk 1nr | sed -z 's/^[^ ]* //' | tr '\\0' '\\n' | head -n 1 "
199202

200-
output, err := exec.Command("cf", "ssh", app, "-c", cmd).Output()
203+
args = append(args, cmd)
204+
output, err := exec.Command("cf", args...).Output()
201205

202206
if err != nil {
203207
return "", errors.New("error while checking the generated file")

utils/fakes/fake_utils_impl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (fake FakeCfJavaPluginUtil) GetAvailablePath(data string, userpath string)
5050
return "/tmp", nil
5151
}
5252

53-
func (fake FakeCfJavaPluginUtil) CopyOverCat(app string, src string, dest string) error {
53+
func (fake FakeCfJavaPluginUtil) CopyOverCat(args []string, src string, dest string) error {
5454

5555
if !fake.LocalPathValid {
5656
return errors.New("Error occured during create desination file: " + dest + ", please check you are allowed to create file in the path.")
@@ -59,7 +59,7 @@ func (fake FakeCfJavaPluginUtil) CopyOverCat(app string, src string, dest string
5959
return nil
6060
}
6161

62-
func (fake FakeCfJavaPluginUtil) DeleteRemoteFile(app string, path string) error {
62+
func (fake FakeCfJavaPluginUtil) DeleteRemoteFile(args []string, path string) error {
6363
if path != fake.Fspath+"/"+fake.OutputFileName {
6464
return errors.New("error occured while removing dump file generated")
6565

@@ -68,9 +68,9 @@ func (fake FakeCfJavaPluginUtil) DeleteRemoteFile(app string, path string) error
6868
return nil
6969
}
7070

71-
func (fake FakeCfJavaPluginUtil) FindDumpFile(app string, fullpath string, fspath string) (string, error) {
71+
func (fake FakeCfJavaPluginUtil) FindDumpFile(args []string, fullpath string, fspath string) (string, error) {
7272

73-
expectedFullPath := fake.Fspath + "/" + app + "-heapdump-" + fake.UUID + ".hprof"
73+
expectedFullPath := fake.Fspath + "/" + args[1] + "-heapdump-" + fake.UUID + ".hprof"
7474
if fspath != fake.Fspath || fullpath != expectedFullPath {
7575
return "", errors.New("error while checking the generated file")
7676
}

0 commit comments

Comments
 (0)