diff --git a/cmd/create_env.go b/cmd/create_env.go index 5b26497b0..fecdeb3c5 100644 --- a/cmd/create_env.go +++ b/cmd/create_env.go @@ -25,9 +25,10 @@ func (c *CreateEnvCmd) Run(stage boshui.Stage, opts CreateEnvOpts) error { depPreparer := c.envProvider(opts.Args.Manifest.Path, opts.StatePath, opts.VarFlags.AsVariables(), opts.OpsFlags.AsOp()) //nolint:staticcheck return depPreparer.PrepareDeployment(stage, DeploymentOptions{ - Recreate: opts.Recreate, - RecreatePersistentDisks: opts.RecreatePersistentDisks, - FixStemcell: opts.FixStemcell, - SkipDrain: opts.SkipDrain, + Recreate: opts.Recreate, + RecreatePersistentDisks: opts.RecreatePersistentDisks, + FixStemcell: opts.FixStemcell, + SkipDrain: opts.SkipDrain, + SkipDeleteUnusedStemcells: opts.SkipDeleteUnusedStemcells, }) } diff --git a/cmd/create_env_test.go b/cmd/create_env_test.go index d934561cb..12a25fbbf 100644 --- a/cmd/create_env_test.go +++ b/cmd/create_env_test.go @@ -825,6 +825,17 @@ var _ = Describe("CreateEnvCmd", func() { Expect(mockStemcellManager.DeleteUnusedCallCount()).To(Equal(1)) }) + Context("when SkipDeleteUnusedStemcells is specified", func() { + It("does not delete unused stemcells", func() { + defaultCreateEnvOpts.SkipDeleteUnusedStemcells = true + + err := command.Run(fakeStage, defaultCreateEnvOpts) + Expect(err).NotTo(HaveOccurred()) + + Expect(mockStemcellManager.DeleteUnusedCallCount()).To(Equal(0)) + }) + }) + Context("when SkipDrain is specified", func() { BeforeEach(func() { expectedSkipDrain = true diff --git a/cmd/deployment_preparer.go b/cmd/deployment_preparer.go index 808112b8b..7fee9c73e 100644 --- a/cmd/deployment_preparer.go +++ b/cmd/deployment_preparer.go @@ -103,10 +103,11 @@ type DeploymentPreparer struct { // DeploymentOptions carries the per-run switches for PrepareDeployment. type DeploymentOptions struct { - Recreate bool - RecreatePersistentDisks bool - FixStemcell bool - SkipDrain bool + Recreate bool + RecreatePersistentDisks bool + FixStemcell bool + SkipDrain bool + SkipDeleteUnusedStemcells bool } func (c *DeploymentPreparer) PrepareDeployment(stage biui.Stage, opts DeploymentOptions) (err error) { @@ -114,6 +115,7 @@ func (c *DeploymentPreparer) PrepareDeployment(stage biui.Stage, opts Deployment recreatePersistentDisks := opts.RecreatePersistentDisks fix := opts.FixStemcell skipDrain := opts.SkipDrain + skipDeleteUnusedStemcells := opts.SkipDeleteUnusedStemcells c.ui.BeginLinef("Deployment state: '%s'\n", c.deploymentStateService.Path()) @@ -233,6 +235,7 @@ func (c *DeploymentPreparer) PrepareDeployment(stage biui.Stage, opts Deployment manifestSHA, skipDrain, fix, + skipDeleteUnusedStemcells, stage, cloud, ) @@ -264,6 +267,7 @@ func (c *DeploymentPreparer) deploy( manifestSHA string, skipDrain bool, fix bool, + skipDeleteUnusedStemcells bool, stage biui.Stage, cloud bicloud.Cloud, ) (err error) { @@ -322,6 +326,11 @@ func (c *DeploymentPreparer) deploy( // TODO: cleanup unused disks here? + if skipDeleteUnusedStemcells { + c.ui.BeginLinef("Skipping deletion of unused stemcells\n") + return nil + } + err = stemcellManager.DeleteUnused(stage) if err != nil { return err diff --git a/cmd/opts/opts.go b/cmd/opts/opts.go index b2bf5ec7a..7b6a72f30 100644 --- a/cmd/opts/opts.go +++ b/cmd/opts/opts.go @@ -193,12 +193,13 @@ type CreateEnvOpts struct { Args CreateEnvArgs `positional-args:"true" required:"true"` VarFlags OpsFlags - SkipDrain bool `long:"skip-drain" description:"Skip running drain and pre-stop scripts"` - StatePath string `long:"state" value-name:"PATH" description:"State file path"` - Recreate bool `long:"recreate" description:"Recreate VM in deployment"` - RecreatePersistentDisks bool `long:"recreate-persistent-disks" description:"Recreate persistent disks in the deployment"` - FixStemcell bool `long:"fix-stemcell" description:"Forces re-upload of the stemcell; any existing stemcell image is orphaned and the VM is recreated"` - PackageDir string `long:"package-dir" value-name:"DIR" description:"Package cache location override"` + SkipDrain bool `long:"skip-drain" description:"Skip running drain and pre-stop scripts"` + StatePath string `long:"state" value-name:"PATH" description:"State file path"` + Recreate bool `long:"recreate" description:"Recreate VM in deployment"` + RecreatePersistentDisks bool `long:"recreate-persistent-disks" description:"Recreate persistent disks in the deployment"` + FixStemcell bool `long:"fix-stemcell" description:"Forces re-upload of the stemcell; any existing stemcell image is orphaned and the VM is recreated"` + SkipDeleteUnusedStemcells bool `long:"skip-delete-unused-stemcells" description:"Keep stemcells that are no longer current; use when another deployment reuses the same stemcell image"` + PackageDir string `long:"package-dir" value-name:"DIR" description:"Package cache location override"` cmd }