Skip to content

Commit

Permalink
bbl down/destroy should bosh clean up
Browse files Browse the repository at this point in the history
- it runs `bosh clean-up --all` before destroying BOSH Director

[#161992133](https://www.pivotaltracker.com/story/show/161992133)

Co-authored-by: Sara Montecino <smontecino@pivotal.io>
  • Loading branch information
2 people authored and crhntr committed Nov 28, 2018
1 parent 5371d5a commit e326d21
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 24 deletions.
2 changes: 1 addition & 1 deletion application/configuration_test.go
@@ -1,9 +1,9 @@
package application_test

import (
"github.com/cloudfoundry/bosh-bootloader/application"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/cloudfoundry/bosh-bootloader/application"
)

var _ = Describe("StringSlice", func() {
Expand Down
1 change: 0 additions & 1 deletion aws/client_test.go
Expand Up @@ -346,7 +346,6 @@ var _ = Describe("Client", func() {
})
})


Context("when there are multiple nat instances", func() {
BeforeEach(func() {
ec2Client.DescribeInstancesCall.Returns.Output = &awsec2.DescribeInstancesOutput{
Expand Down
2 changes: 1 addition & 1 deletion bbl/main.go
Expand Up @@ -118,8 +118,8 @@ func main() {
sshKeyGetter := bosh.NewSSHKeyGetter(stateStore, afs)
allProxyGetter := bosh.NewAllProxyGetter(sshKeyGetter, afs)
credhubGetter := bosh.NewCredhubGetter(stateStore, afs)
boshManager := bosh.NewManager(boshExecutor, logger, stateStore, sshKeyGetter, afs)
boshCLIProvider := bosh.NewCLIProvider(allProxyGetter, boshPath)
boshManager := bosh.NewManager(boshExecutor, logger, stateStore, sshKeyGetter, afs, boshCLIProvider)

configUpdater := bosh.NewConfigUpdater(boshCLIProvider)

Expand Down
41 changes: 30 additions & 11 deletions bosh/manager.go
Expand Up @@ -24,11 +24,12 @@ type managerFs interface {
}

type Manager struct {
executor executor
logger logger
stateStore stateStore
sshKeyGetter sshKeyGetter
fs managerFs
executor executor
logger logger
stateStore stateStore
sshKeyGetter sshKeyGetter
fs managerFs
boshCLIProvider boshCLIProvider
}

type directorVars struct {
Expand Down Expand Up @@ -65,13 +66,14 @@ type sshKeyGetter interface {
Get(string) (string, error)
}

func NewManager(executor executor, logger logger, stateStore stateStore, sshKeyGetter sshKeyGetter, fs deleterFs) *Manager {
func NewManager(executor executor, logger logger, stateStore stateStore, sshKeyGetter sshKeyGetter, fs deleterFs, boshCLIProvider boshCLIProvider) *Manager {
return &Manager{
executor: executor,
logger: logger,
stateStore: stateStore,
sshKeyGetter: sshKeyGetter,
fs: fs,
executor: executor,
logger: logger,
stateStore: stateStore,
sshKeyGetter: sshKeyGetter,
fs: fs,
boshCLIProvider: boshCLIProvider,
}
}

Expand Down Expand Up @@ -193,6 +195,23 @@ func (m *Manager) InitializeDirector(state storage.State) error {
return nil
}

func (m *Manager) CleanUpDirector(state storage.State) error {
boshCLI, err := m.boshCLIProvider.AuthenticatedCLI(
state.Jumpbox,
os.Stderr,
state.BOSH.DirectorAddress,
state.BOSH.DirectorUsername,
state.BOSH.DirectorPassword,
state.BOSH.DirectorSSLCA,
)

if err != nil {
return err
}

return boshCLI.Run(nil, "", []string{"clean-up", "--all"})
}

func (m *Manager) CreateDirector(state storage.State, terraformOutputs terraform.Outputs) (storage.State, error) {
m.logger.Step("creating bosh director")

Expand Down
86 changes: 78 additions & 8 deletions bosh/manager_test.go
Expand Up @@ -3,6 +3,7 @@ package bosh_test
import (
"errors"
"io/ioutil"
"os"

"github.com/cloudfoundry/bosh-bootloader/bosh"
"github.com/cloudfoundry/bosh-bootloader/fakes"
Expand All @@ -17,11 +18,13 @@ import (

var _ = Describe("Manager", func() {
var (
boshExecutor *fakes.BOSHExecutor
logger *fakes.Logger
stateStore *fakes.StateStore
sshKeyGetter *fakes.SSHKeyGetter
fs *fakes.FileIO
boshExecutor *fakes.BOSHExecutor
logger *fakes.Logger
stateStore *fakes.StateStore
sshKeyGetter *fakes.SSHKeyGetter
fs *fakes.FileIO
boshCLIProvider *fakes.BOSHCLIProvider
boshCLI *fakes.BOSHCLI

boshManager *bosh.Manager
terraformOutputs terraform.Outputs
Expand All @@ -38,14 +41,14 @@ var _ = Describe("Manager", func() {
sshKeyGetter = &fakes.SSHKeyGetter{}
sshKeyGetter.GetCall.Returns.PrivateKey = "some-jumpbox-private-key"
fs = &fakes.FileIO{}

boshCLIProvider = &fakes.BOSHCLIProvider{}
stateStore = &fakes.StateStore{}
stateStore.GetVarsDirCall.Returns.Directory = "some-bbl-vars-dir"
stateStore.GetStateDirCall.Returns.Directory = "some-state-dir"
stateStore.GetDirectorDeploymentDirCall.Returns.Directory = "some-director-deployment-dir"
stateStore.GetJumpboxDeploymentDirCall.Returns.Directory = "some-jumpbox-deployment-dir"

boshManager = bosh.NewManager(boshExecutor, logger, stateStore, sshKeyGetter, fs)
boshManager = bosh.NewManager(boshExecutor, logger, stateStore, sshKeyGetter, fs, boshCLIProvider)

boshVars = `admin_password: some-admin-password
director_ssl:
Expand Down Expand Up @@ -160,7 +163,7 @@ director_ssl:
DirectorSSLCA: "some-ca",
DirectorSSLCertificate: "some-certificate",
DirectorSSLPrivateKey: "some-private-key",
State: nil,
State: nil,
}))
})

Expand Down Expand Up @@ -350,6 +353,73 @@ director_ssl:
})
})

Describe("CleanUpDirector", func() {
BeforeEach(func() {
boshCLI = &fakes.BOSHCLI{}
boshCLIProvider.AuthenticatedCLICall.Returns.AuthenticatedCLI = boshCLI
boshCLIProvider.AuthenticatedCLICall.Returns.Error = nil
})

It("authenticates the CLI", func() {
state := storage.State{
Jumpbox: storage.Jumpbox{
URL: "some-jumpbox-url:22",
},
BOSH: storage.BOSH{
DirectorAddress: "director-address",
DirectorUsername: "director-username",
DirectorPassword: "director-password",
DirectorSSLCA: "ca-cert",
},
}

err := boshManager.CleanUpDirector(state)
Expect(err).NotTo(HaveOccurred())

Expect(boshCLIProvider.AuthenticatedCLICall.CallCount).To(Equal(1))
Expect(boshCLIProvider.AuthenticatedCLICall.Receives.Jumpbox).To(Equal(state.Jumpbox))
Expect(boshCLIProvider.AuthenticatedCLICall.Receives.Stderr).To(Equal(os.Stderr))
Expect(boshCLIProvider.AuthenticatedCLICall.Receives.DirectorAddress).To(Equal(state.BOSH.DirectorAddress))
Expect(boshCLIProvider.AuthenticatedCLICall.Receives.DirectorUsername).To(Equal(state.BOSH.DirectorUsername))
Expect(boshCLIProvider.AuthenticatedCLICall.Receives.DirectorPassword).To(Equal(state.BOSH.DirectorPassword))
Expect(boshCLIProvider.AuthenticatedCLICall.Receives.DirectorCACert).To(Equal(state.BOSH.DirectorSSLCA))
})

It("runs bosh clean-up --all", func() {
err := boshManager.CleanUpDirector(storage.State{})
Expect(err).NotTo(HaveOccurred())

Expect(boshCLI.RunCall.CallCount).To(Equal(1))
Expect(boshCLI.RunCall.Receives.Args).To(Equal([]string{"clean-up", "--all"}))
})

Context("when an error occurs", func() {
Context("authenticating the cli", func() {
It("returns an error", func() {
boshCLIProvider.AuthenticatedCLICall.Returns.Error = errors.New("failed to authenticate cli")

err := boshManager.CleanUpDirector(storage.State{})
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("failed to authenticate cli"))

Expect(boshCLIProvider.AuthenticatedCLICall.CallCount).To(Equal(1))
})
})

Context("running bosh clean-up --all", func() {
It("returns an error", func() {
boshCLI.RunCall.Returns.Error = errors.New("failed to run bosh clean-up")

err := boshManager.CleanUpDirector(storage.State{})
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("failed to run bosh clean-up"))

Expect(boshCLI.RunCall.CallCount).To(Equal(1))
})
})
})
})

Describe("DeleteJumpbox", func() {
var (
incomingState storage.State
Expand Down
7 changes: 6 additions & 1 deletion commands/destroy.go
Expand Up @@ -173,7 +173,12 @@ func (d Destroy) deleteBOSH(state storage.State, terraformOutputs terraform.Outp
return state, nil
}

err := d.boshManager.DeleteDirector(state, terraformOutputs)
err := d.boshManager.CleanUpDirector(state)
if err != nil {
return state, err
}

err = d.boshManager.DeleteDirector(state, terraformOutputs)
if err != nil {
return state, err
}
Expand Down
28 changes: 27 additions & 1 deletion commands/destroy_test.go
Expand Up @@ -264,6 +264,20 @@ var _ = Describe("Destroy", func() {
})
})

It("invokes bosh clean up", func() {
state := storage.State{
BOSH: storage.BOSH{
DirectorName: "some-director",
},
}

err := destroy.Execute([]string{}, state)
Expect(err).NotTo(HaveOccurred())

Expect(boshManager.CleanUpDirectorCall.CallCount).To(Equal(1))
Expect(boshManager.CleanUpDirectorCall.Receives.State).To(Equal(state))
})

It("invokes bosh delete", func() {
state := storage.State{
BOSH: storage.BOSH{
Expand Down Expand Up @@ -356,6 +370,19 @@ var _ = Describe("Destroy", func() {
})
})

Context("when bosh clean-up fails", func() {
It("returns an error", func() {
boshManager.CleanUpDirectorCall.Returns.Error = errors.New("bosh clean-up --all failed")

err := destroy.Execute([]string{}, storage.State{
BOSH: storage.BOSH{
DirectorName: "some-director",
},
})
Expect(err).To(MatchError("bosh clean-up --all failed"))
})
})

Context("when bosh delete fails", func() {
It("returns an error", func() {
boshManager.DeleteDirectorCall.Returns.Error = errors.New("bosh delete-env failed")
Expand Down Expand Up @@ -453,7 +480,6 @@ var _ = Describe("Destroy", func() {
Expect(boshManager.DeleteDirectorCall.CallCount).To(Equal(0))
})
})

})
})

Expand Down
1 change: 1 addition & 0 deletions commands/interfaces.go
Expand Up @@ -35,6 +35,7 @@ type boshManager interface {
InitializeDirector(bblState storage.State) error
CreateDirector(bblState storage.State, terraformOutputs terraform.Outputs) (storage.State, error)
InitializeJumpbox(bblState storage.State) error
CleanUpDirector(state storage.State) error
CreateJumpbox(bblState storage.State, terraformOutputs terraform.Outputs) (storage.State, error)
DeleteDirector(bblState storage.State, terraformOutputs terraform.Outputs) error
DeleteJumpbox(bblState storage.State, terraformOutputs terraform.Outputs) error
Expand Down
15 changes: 15 additions & 0 deletions fakes/bosh_manager.go
Expand Up @@ -69,6 +69,15 @@ type BOSHManager struct {
Error error
}
}
CleanUpDirectorCall struct {
CallCount int
Receives struct {
State storage.State
}
Returns struct {
Error error
}
}
DeleteJumpboxCall struct {
CallCount int
Receives struct {
Expand Down Expand Up @@ -134,6 +143,12 @@ func (b *BOSHManager) DeleteDirector(state storage.State, terraformOutputs terra
return b.DeleteDirectorCall.Returns.Error
}

func (b *BOSHManager) CleanUpDirector(state storage.State) error {
b.CleanUpDirectorCall.CallCount++
b.CleanUpDirectorCall.Receives.State = state
return b.CleanUpDirectorCall.Returns.Error
}

func (b *BOSHManager) DeleteJumpbox(state storage.State, terraformOutputs terraform.Outputs) error {
b.DeleteJumpboxCall.CallCount++
b.DeleteJumpboxCall.Receives.State = state
Expand Down

0 comments on commit e326d21

Please sign in to comment.