diff --git a/blue_green_deploy.go b/blue_green_deploy.go index d24f844..0ce2722 100644 --- a/blue_green_deploy.go +++ b/blue_green_deploy.go @@ -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 { @@ -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) + } + } +} diff --git a/blue_green_deploy_test.go b/blue_green_deploy_test.go index 95f8be9..64fa1cc 100644 --- a/blue_green_deploy_test.go +++ b/blue_green_deploy_test.go @@ -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" @@ -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 diff --git a/main.go b/main.go index 433570f..d250d50 100644 --- a/main.go +++ b/main.go @@ -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 != "" { diff --git a/main_test.go b/main_test.go index 9f0313f..560602d 100644 --- a/main_test.go +++ b/main_test.go @@ -11,6 +11,7 @@ import ( "github.com/bluemixgaragelondon/cf-blue-green-deploy/manifest/fakes" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "strings" ) var _ = Describe("BGD Plugin", func() { @@ -18,7 +19,7 @@ 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, } @@ -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", @@ -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 @@ -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)) +} \ No newline at end of file