Skip to content

Commit b3b341d

Browse files
author
Rajath Agasthya
committed
Log a warning when SSH user cleanup fails
There might be situations (like network connectivity loss) during the SSH command that could fail the SSH user cleanup. Log a warning when that happens to notify that there might be artifacts left over on the VM.
1 parent 20cb2ad commit b3b341d

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func (c Cmd) Execute() (cmdErr error) {
391391

392392
if opts.TargetDirector {
393393
agentClientFactory := bihttpagent.NewAgentClientFactory(1*time.Second, deps.Logger)
394-
return NewEnvSSHCmd(agentClientFactory, intSSHRunner, nonIntSSHRunner, resultsSSHRunner, deps.UI).Run(*opts)
394+
return NewEnvSSHCmd(agentClientFactory, intSSHRunner, nonIntSSHRunner, resultsSSHRunner, deps.UI, deps.Logger).Run(*opts)
395395
} else {
396396
sshHostBuilder := boshssh.NewHostBuilder()
397397
return NewSSHCmd(intSSHRunner, nonIntSSHRunner, resultsSSHRunner, deps.UI, sshHostBuilder).Run(*opts, c.getDeployment)

cmd/ssh.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package cmd
22

33
import (
44
"errors"
5+
"fmt"
56

67
bihttpagent "github.com/cloudfoundry/bosh-agent/v2/agentclient/http"
78
bosherr "github.com/cloudfoundry/bosh-utils/errors"
9+
boshlog "github.com/cloudfoundry/bosh-utils/logger"
810

911
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts" //nolint:staticcheck
1012
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
@@ -113,6 +115,7 @@ type EnvSSHCmd struct {
113115
nonIntSSHRunner boshssh.Runner
114116
resultsSSHRunner boshssh.Runner
115117
ui boshui.UI
118+
logger boshlog.Logger
116119
}
117120

118121
func NewEnvSSHCmd(
@@ -121,13 +124,15 @@ func NewEnvSSHCmd(
121124
nonIntSSHRunner boshssh.Runner,
122125
resultsSSHRunner boshssh.Runner,
123126
ui boshui.UI,
127+
logger boshlog.Logger,
124128
) EnvSSHCmd {
125129
return EnvSSHCmd{
126130
agentClientFactory: agentClientFactory,
127131
intSSHRunner: intSSHRunner,
128132
nonIntSSHRunner: nonIntSSHRunner,
129133
resultsSSHRunner: resultsSSHRunner,
130134
ui: ui,
135+
logger: logger,
131136
}
132137
}
133138

@@ -167,7 +172,10 @@ func (c EnvSSHCmd) Run(opts SSHOpts) error {
167172
}
168173

169174
defer func() {
170-
_, _ = agentClient.CleanUpSSH(sshOpts.Username) //nolint:errcheck
175+
_, err1 := agentClient.CleanUpSSH(sshOpts.Username)
176+
if err1 != nil {
177+
c.logger.Warn("SSH", fmt.Sprintf("SSH cleanup failed for user %s. Artifacts may be left over on the VM: %v", sshOpts.Username, err1))
178+
}
171179
}()
172180

173181
// host key will be returned by agent over HTTPS

cmd/ssh_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/cloudfoundry/bosh-agent/v2/agentclient"
77
mockhttpagent "github.com/cloudfoundry/bosh-agent/v2/agentclient/http/mocks"
8+
boshlog "github.com/cloudfoundry/bosh-utils/logger"
89
fakeuuid "github.com/cloudfoundry/bosh-utils/uuid/fakes"
910
"github.com/golang/mock/gomock"
1011
. "github.com/onsi/ginkgo/v2"
@@ -352,6 +353,7 @@ var _ = Describe("SSH", func() {
352353
nonIntSSHRunner *fakessh.FakeRunner
353354
resultsSSHRunner *fakessh.FakeRunner
354355
ui *fakeui.FakeUI
356+
logger boshlog.Logger
355357

356358
uuidGen *fakeuuid.FakeGenerator
357359

@@ -367,10 +369,11 @@ var _ = Describe("SSH", func() {
367369
nonIntSSHRunner = &fakessh.FakeRunner{}
368370
resultsSSHRunner = &fakessh.FakeRunner{}
369371
ui = &fakeui.FakeUI{}
372+
logger = boshlog.NewLogger(boshlog.LevelNone)
370373

371374
uuidGen = &fakeuuid.FakeGenerator{}
372375

373-
command = cmd.NewEnvSSHCmd(agentClientFactory, intSSHRunner, nonIntSSHRunner, resultsSSHRunner, ui)
376+
command = cmd.NewEnvSSHCmd(agentClientFactory, intSSHRunner, nonIntSSHRunner, resultsSSHRunner, ui, logger)
374377
})
375378

376379
AfterEach(func() {

0 commit comments

Comments
 (0)