Skip to content

Commit

Permalink
Merge pull request #54 from holgero/preserve-ssh-enablement
Browse files Browse the repository at this point in the history
Copy ssh enablement state to new application
  • Loading branch information
bluemixgaragelondon authored Jan 25, 2018
2 parents 5061a63 + 90a5cf0 commit 888c8a4
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
23 changes: 23 additions & 0 deletions blue_green_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type BlueGreenDeployer interface {
UnmapRoutesFromApp(string, ...plugin_models.GetApp_RouteSummary)
RenameApp(string, string)
MapRoutesToApp(string, ...plugin_models.GetApp_RouteSummary)
CheckSshEnablement(string) bool
SetSshAccess(string, bool)
}

type BlueGreenDeploy struct {
Expand Down Expand Up @@ -196,3 +198,24 @@ func (p *BlueGreenDeploy) MapRoutesToApp(appName string, routes ...plugin_models
p.mapRoute(appName, route)
}
}

func (p *BlueGreenDeploy) CheckSshEnablement(app string) bool {
if result, err := p.Connection.CliCommand("ssh-enabled", app); err != nil {
p.ErrorFunc("Check ssh enabled status failed", err)
return true
} else {
return (strings.Contains(result[0], "support is enabled"))
}
}

func (p *BlueGreenDeploy) SetSshAccess(app string, enableSsh bool) {
if enableSsh {
if _, err := p.Connection.CliCommand("enable-ssh", app); err != nil {
p.ErrorFunc("Could not enable ssh", err)
}
} else {
if _, err := p.Connection.CliCommand("disable-ssh", app); err != nil {
p.ErrorFunc("Could not disable ssh", err)
}
}
}
101 changes: 101 additions & 0 deletions blue_green_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"code.cloudfoundry.org/cli/plugin/models"
"code.cloudfoundry.org/cli/plugin/pluginfakes"
"fmt"
. "github.com/bluemixgaragelondon/cf-blue-green-deploy"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -104,6 +105,106 @@ var _ = Describe("BlueGreenDeploy", func() {
})
})

Describe("checks ssh enablement", func() {
Context("when ssh support is disabled", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return []string{fmt.Sprintf("ssh support is disabled for '%s'", args[0])}, nil
}
})

It("returns false", func() {
result := p.CheckSshEnablement("test-app")

Expect(result).To(BeFalse())
cfCommands := getAllCfCommands(connection)

Expect(cfCommands).To(Equal([]string{
"ssh-enabled test-app",
}))
})
})

Context("when ssh support is enabled", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return []string{fmt.Sprintf("ssh support is enabled for '%s'", args[0])}, nil
}
})

It("returns true", func() {
result := p.CheckSshEnablement("test-app")

Expect(result).To(BeTrue())
cfCommands := getAllCfCommands(connection)

Expect(cfCommands).To(Equal([]string{
"ssh-enabled test-app",
}))
})
})

Context("when cf cli errors", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return nil, errors.New("failed to check ssh enablement status")
}
})

It("it reports the error", func() {
p.CheckSshEnablement("test-app")
Expect(bgdExitsWithErrors[0]).To(MatchError("failed to check ssh enablement status"))
})
})
})

Describe("set ssh access", func() {
Context("when it just works", func() {
It("enables ssh", func() {
p.SetSshAccess("test-app", true)

cfCommands := getAllCfCommands(connection)

Expect(cfCommands).To(Equal([]string{
"enable-ssh test-app",
}))
})
It("disables ssh", func() {
p.SetSshAccess("test-app", false)

cfCommands := getAllCfCommands(connection)

Expect(cfCommands).To(Equal([]string{
"disable-ssh test-app",
}))
})
})
Context("when cf enable-ssh errors", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return nil, errors.New("failed to enable ssh")
}
})

It("it reports the error", func() {
p.SetSshAccess("test-app", true)
Expect(bgdExitsWithErrors[0]).To(MatchError("failed to enable ssh"))
})
})
Context("when cf disable-ssh errors", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return nil, errors.New("failed to disable ssh")
}
})

It("it reports the error", func() {
p.SetSshAccess("test-app", false)
Expect(bgdExitsWithErrors[0]).To(MatchError("failed to disable ssh"))
})
})
})

Describe("renaming an app", func() {
var app string

Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (p *CfPlugin) Deploy(defaultCfDomain string, manifestReader manifest.Manife
// If deploy is unsuccessful, p.ErrorFunc will be called which exits.
p.Deployer.PushNewApp(newAppName, tempRoute, args.ManifestPath, manifestScaleParameters)

if liveAppName != "" {
p.Deployer.SetSshAccess(newAppName, p.Deployer.CheckSshEnablement(appName))
}
promoteNewApp := true
smokeTestScript := args.SmokeTestPath
if smokeTestScript != "" {
Expand Down
15 changes: 14 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
"github.com/bluemixgaragelondon/cf-blue-green-deploy/manifest/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"strings"
)

var _ = Describe("BGD Plugin", func() {

Describe("blue green flow", func() {
Context("when there is a previous live app", func() {
It("calls methods in correct order", func() {
b := &BlueGreenDeployFake{liveApp: &plugin_models.GetAppModel{Name: "app-name-live"}}
b := &BlueGreenDeployFake{liveApp: &plugin_models.GetAppModel{Name: "app-name-live"}, appSshEnabled:false}
p := CfPlugin{
Deployer: b,
}
Expand All @@ -29,6 +30,8 @@ var _ = Describe("BGD Plugin", func() {
"delete old apps",
"get current live app",
"push app-name-new",
"check ssh enablement for 'app-name'",
"set ssh enablement for 'app-name-new' to 'false'",
"unmap 1 routes from app-name-new",
"mapped 1 routes",
"rename app-name-live to app-name-old",
Expand Down Expand Up @@ -480,6 +483,7 @@ var _ = Describe("BGD Plugin", func() {
type BlueGreenDeployFake struct {
flow []string
liveApp *plugin_models.GetAppModel
appSshEnabled bool
passSmokeTest bool
mappedRoutes []plugin_models.GetApp_RouteSummary
scale *ScaleParameters
Expand Down Expand Up @@ -533,3 +537,12 @@ func (p *BlueGreenDeployFake) MapRoutesToApp(appName string, routes ...plugin_mo
func (p *BlueGreenDeployFake) UnmapRoutesFromApp(oldAppName string, routes ...plugin_models.GetApp_RouteSummary) {
p.flow = append(p.flow, fmt.Sprintf("unmap %d routes from %s", len(routes), oldAppName))
}

func (p *BlueGreenDeployFake) CheckSshEnablement(app string) bool {
p.flow = append(p.flow, fmt.Sprintf("check ssh enablement for '%s'", app))
return strings.Contains(app, "ssh-enabled-app")
}

func (p *BlueGreenDeployFake) SetSshAccess(app string, enableSsh bool) {
p.flow = append(p.flow, fmt.Sprintf("set ssh enablement for '%s' to '%v'", app, enableSsh))
}

0 comments on commit 888c8a4

Please sign in to comment.