Skip to content

Commit

Permalink
Merge pull request #658 from cloudfoundry/override-package-dir
Browse files Browse the repository at this point in the history
Allow package cache dir to be overriden for create/delete env commands
  • Loading branch information
selzoc authored Aug 13, 2024
2 parents 18a084e + 026e8a7 commit 73b4202
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 23 deletions.
8 changes: 4 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,31 @@ func (c Cmd) Execute() (cmdErr error) {

case *CreateEnvOpts:
envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentPreparer {
return NewEnvFactory(deps, manifestPath, statePath, vars, op, opts.RecreatePersistentDisks).Preparer()
return NewEnvFactory(deps, manifestPath, statePath, vars, op, opts.RecreatePersistentDisks, opts.PackageDir).Preparer()
}

stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger)
return NewCreateEnvCmd(deps.UI, envProvider).Run(stage, *opts)

case *DeleteEnvOpts:
envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentDeleter {
return NewEnvFactory(deps, manifestPath, statePath, vars, op, false).Deleter()
return NewEnvFactory(deps, manifestPath, statePath, vars, op, false, opts.PackageDir).Deleter()
}

stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger)
return NewDeleteEnvCmd(deps.UI, envProvider).Run(stage, *opts)

case *StopEnvOpts:
envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentStateManager {
return NewEnvFactory(deps, manifestPath, statePath, vars, op, false).StateManager()
return NewEnvFactory(deps, manifestPath, statePath, vars, op, false, "").StateManager()
}

stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger)
return NewStopEnvCmd(deps.UI, envProvider).Run(stage, *opts)

case *StartEnvOpts:
envProvider := func(manifestPath string, statePath string, vars boshtpl.Variables, op patch.Op) DeploymentStateManager {
return NewEnvFactory(deps, manifestPath, statePath, vars, op, false).StateManager()
return NewEnvFactory(deps, manifestPath, statePath, vars, op, false, "").StateManager()
}

stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger)
Expand Down
3 changes: 2 additions & 1 deletion cmd/create_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ var _ = Describe("CreateEnvCmd", func() {
deploymentStateService,
fakeInstallationUUIDGenerator,
filepath.Join("fake-install-dir"),
"",
)
tempRootConfigurator := cmd.NewTempRootConfigurator(fs)

Expand Down Expand Up @@ -418,7 +419,7 @@ var _ = Describe("CreateEnvCmd", func() {
fakeInstallationParser.ParseManifest = installationManifest

installationPath := filepath.Join("fake-install-dir", "fake-installation-id")
target := biinstall.NewTarget(installationPath)
target := biinstall.NewTarget(installationPath, "")

installedJob := biinstall.NewInstalledJob(
biinstall.RenderedJobRef{
Expand Down
5 changes: 3 additions & 2 deletions cmd/deployment_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ cloud_provider:
},
}

target := biinstall.NewTarget(filepath.Join("fake-install-dir", "fake-installation-id"))
target := biinstall.NewTarget(filepath.Join("fake-install-dir", "fake-installation-id"), "")
mockInstallerFactory.EXPECT().NewInstaller(target).Return(mockCpiInstaller).AnyTimes()

expectCPIInstall = mockCpiInstaller.EXPECT().Install(installationManifest, gomock.Any()).Do(func(_ biinstallmanifest.Manifest, stage boshui.Stage) {
Expand Down Expand Up @@ -229,6 +229,7 @@ cloud_provider:
deploymentStateService,
fakeInstallationUUIDGenerator,
filepath.Join("fake-install-dir"),
"",
)

tempRootConfigurator := cmd.NewTempRootConfigurator(fs)
Expand Down Expand Up @@ -536,7 +537,7 @@ cloud_provider:
},
}

target := biinstall.NewTarget(filepath.Join("fake-install-dir", "fake-installation-id"))
target := biinstall.NewTarget(filepath.Join("fake-install-dir", "fake-installation-id"), "")
mockInstallerFactory.EXPECT().NewInstaller(target).Return(mockCpiInstaller).AnyTimes()

fakeInstallation := &fakecmd.FakeInstallation{}
Expand Down
3 changes: 2 additions & 1 deletion cmd/env_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewEnvFactory(
manifestVars boshtpl.Variables,
manifestOp patch.Op,
recreatePersistentDisks bool,
packageDir string,
) *envFactory {
f := envFactory{
deps: deps,
Expand Down Expand Up @@ -126,7 +127,7 @@ func NewEnvFactory(
}

f.targetProvider = boshinst.NewTargetProvider(
f.deploymentStateService, deps.UUIDGen, filepath.Join(workspaceRootPath, "installations"))
f.deploymentStateService, deps.UUIDGen, filepath.Join(workspaceRootPath, "installations"), packageDir)

{
diskRepo := biconfig.NewDiskRepo(f.deploymentStateService, deps.UUIDGen)
Expand Down
6 changes: 4 additions & 2 deletions cmd/opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type CreateEnvOpts struct {
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"`
PackageDir string `long:"package-dir" value-name:"DIR" description:"Package cache location override"`
cmd
}

Expand All @@ -208,8 +209,9 @@ type DeleteEnvOpts struct {
Args DeleteEnvArgs `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"`
SkipDrain bool `long:"skip-drain" description:"Skip running drain and pre-stop scripts"`
StatePath string `long:"state" value-name:"PATH" description:"State file path"`
PackageDir string `long:"package-dir" value-name:"DIR" description:"Package cache location override"`
cmd
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/opts/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,12 @@ var _ = Describe("Opts", func() {
))
})

It("has --package-dir", func() {
Expect(getStructTagForName("PackageDir", opts)).To(Equal(
`long:"package-dir" value-name:"DIR" description:"Package cache location override"`,
))
})

It("has --recreate", func() {
Expect(getStructTagForName("Recreate", opts)).To(Equal(
`long:"recreate" description:"Recreate VM in deployment"`,
Expand Down Expand Up @@ -899,6 +905,12 @@ var _ = Describe("Opts", func() {
))
})

It("has --package-dir", func() {
Expect(getStructTagForName("PackageDir", opts)).To(Equal(
`long:"package-dir" value-name:"DIR" description:"Package cache location override"`,
))
})

It("has --skip-drain", func() {
Expect(getStructTagForName("SkipDrain", opts)).To(Equal(
`long:"skip-drain" description:"Skip running drain and pre-stop scripts"`,
Expand Down
2 changes: 1 addition & 1 deletion cpi/release/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = Describe("Installer", func() {
installStage = fakeui.NewFakeStage()
installation = mocks.NewMockInstallation(mockCtrl)

target = biinstallation.NewTarget("fake-installation-path")
target = biinstallation.NewTarget("fake-installation-path", "")
mockInstallerFactory.EXPECT().NewInstaller(target).Return(mockInstaller).AnyTimes()
expectInstall = mockInstaller.EXPECT().Install(installationManifest, gomock.Any())
expectCleanup = mockInstaller.EXPECT().Cleanup(installation).Return(nil)
Expand Down
2 changes: 1 addition & 1 deletion installation/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = Describe("Installer", func() {
mockPackageCompiler = mock_install.NewMockPackageCompiler(mockCtrl)
fakeExtractor = &blobextractfakes.FakeExtractor{}

target = NewTarget("fake-installation-path")
target = NewTarget("fake-installation-path", "")
installationManifest = biinstallmanifest.Manifest{
Name: "fake-installation-name",
Properties: biproperty.Map{},
Expand Down
12 changes: 9 additions & 3 deletions installation/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
)

type Target struct {
path string
path string
packageDir string
}

func NewTarget(path string) Target {
func NewTarget(path string, packageDir string) Target {
return Target{
path,
packageDir,
}
}

Expand All @@ -31,7 +33,11 @@ func (t Target) TemplatesIndexPath() string {
}

func (t Target) PackagesPath() string {
return filepath.Join(t.path, "packages")
if t.packageDir != "" {
return t.packageDir
} else {
return filepath.Join(t.path, "packages")
}
}

func (t Target) JobsPath() string {
Expand Down
5 changes: 4 additions & 1 deletion installation/target_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ type targetProvider struct {
deploymentStateService biconfig.DeploymentStateService
uuidGenerator boshuuid.Generator
installationsRootPath string
packageDir string
}

func NewTargetProvider(
deploymentStateService biconfig.DeploymentStateService,
uuidGenerator boshuuid.Generator,
installationsRootPath string,
packageDir string,
) TargetProvider {
return &targetProvider{
deploymentStateService: deploymentStateService,
uuidGenerator: uuidGenerator,
installationsRootPath: installationsRootPath,
packageDir: packageDir,
}
}

Expand All @@ -51,5 +54,5 @@ func (p *targetProvider) NewTarget() (Target, error) {
}
}

return NewTarget(filepath.Join(p.installationsRootPath, installationID)), nil
return NewTarget(filepath.Join(p.installationsRootPath, installationID), p.packageDir), nil
}
19 changes: 18 additions & 1 deletion installation/target_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,24 @@ var _ = Describe("TargetProvider", func() {
logger,
configPath,
)
targetProvider = NewTargetProvider(deploymentStateService, fakeUUIDGenerator, installationsRootPath)
targetProvider = NewTargetProvider(deploymentStateService, fakeUUIDGenerator, installationsRootPath, "")
})

Context("when a packageDir is passed in", func() {
var packageDir string

BeforeEach(func() {
packageDir = "/some/good/dir"

targetProvider = NewTargetProvider(deploymentStateService, fakeUUIDGenerator, installationsRootPath, packageDir)
})

It("is passed through to the target", func() {
target, err := targetProvider.NewTarget()
Expect(err).ToNot(HaveOccurred())

Expect(target.PackagesPath()).To(Equal(packageDir))
})
})

Context("when the installation_id exists in the deployment state", func() {
Expand Down
20 changes: 17 additions & 3 deletions installation/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = Describe("Target", func() {
Describe("Paths", func() {
var target Target
BeforeEach(func() {
target = NewTarget("/home/fake/madcow")
target = NewTarget("/home/fake/madcow", "")
})

It("returns the blobstore path", func() {
Expand All @@ -28,8 +28,22 @@ var _ = Describe("Target", func() {
Expect(target.TemplatesIndexPath()).To(Equal(filepath.Join("/", "home", "fake", "madcow", "templates.json")))
})

It("returns the packages path", func() {
Expect(target.PackagesPath()).To(Equal(filepath.Join("/", "home", "fake", "madcow", "packages")))
Context("packageDir is NOT provided", func() {
It("returns the packages path as a subdirectory of the path", func() {
Expect(target.PackagesPath()).To(Equal(filepath.Join("/", "home", "fake", "madcow", "packages")))
})
})

Context("packageDir is provided", func() {
var packagesDir string
BeforeEach(func() {
packagesDir = "/some/good/path"
target = NewTarget("/home/fake/madcow", packagesDir)
})

It("returns the provided packages path", func() {
Expect(target.PackagesPath()).To(Equal(packagesDir))
})
})

It("returns the temp path", func() {
Expand Down
4 changes: 2 additions & 2 deletions installation/uninstaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("Uninstaller", func() {
err = fs.WriteFileString(filepath.Join(installationPath, "some-installation-artifact"), "something-blah")
Expect(err).ToNot(HaveOccurred())

installationTarget := installation.NewTarget(installationPath)
installationTarget := installation.NewTarget(installationPath, "")

uninstaller := installation.NewUninstaller(fs, boshlogger)

Expand All @@ -53,7 +53,7 @@ var _ = Describe("Uninstaller", func() {
return errors.New("can't remove that")
}

installationTarget := installation.NewTarget("/not/a/path")
installationTarget := installation.NewTarget("/not/a/path", "")

uninstaller := installation.NewUninstaller(fs, boshlogger)

Expand Down
3 changes: 2 additions & 1 deletion integration/create_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ cloud_provider:
Properties: biproperty.Map{},
}
installationPath := filepath.Join("fake-install-dir", "fake-installation-id")
target := biinstall.NewTarget(installationPath)
target := biinstall.NewTarget(installationPath, "")

installedJob := biinstall.InstalledJob{}
installedJob.Name = "fake-cpi-release-job-name"
Expand Down Expand Up @@ -450,6 +450,7 @@ cloud_provider:
deploymentStateService,
installationUuidGenerator,
filepath.Join("fake-install-dir"),
"",
)

tempRootConfigurator := cmd.NewTempRootConfigurator(fs)
Expand Down

0 comments on commit 73b4202

Please sign in to comment.