Skip to content

Commit

Permalink
add global flags to bosh deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoehler committed Nov 8, 2023
1 parent a3bcca5 commit cfbabbb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (c Cmd) Execute() (cmdErr error) {
case *DeployOpts:
director, deployment := c.directorAndDeployment()
releaseManager := c.releaseManager(director)
return NewDeployCmd(deps.UI, deployment, releaseManager).Run(*opts)
return NewDeployCmd(deps.UI, deployment, releaseManager, director).Run(*opts)

case *StartOpts:
return NewStartCmd(deps.UI, c.deployment()).Run(*opts)
Expand Down
49 changes: 48 additions & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"gopkg.in/yaml.v3"

. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
Expand All @@ -13,24 +14,44 @@ type DeployCmd struct {
ui boshui.UI
deployment boshdir.Deployment
releaseUploader ReleaseUploader
director boshdir.Director
}

type ReleaseUploader interface {
UploadReleases([]byte) ([]byte, error)
UploadReleasesWithFix([]byte) ([]byte, error)
}

type Conf struct {
Flags []string `yaml:"flags"`
IncludeDeployments []string `yaml:"include"`
}

func NewDeployCmd(
ui boshui.UI,
deployment boshdir.Deployment,
releaseUploader ReleaseUploader,
director boshdir.Director,
) DeployCmd {
return DeployCmd{ui, deployment, releaseUploader}
return DeployCmd{ui, deployment, releaseUploader, director}
}

func (c DeployCmd) Run(opts DeployOpts) error {
tpl := boshtpl.NewTemplate(opts.Args.Manifest.Bytes)

configs, _ := c.director.ListConfigs(1, boshdir.ConfigsFilter{Type: "deploy"})

for _, config := range configs {
var conf Conf

yaml.Unmarshal([]byte(config.Content), &conf)
if conf.IncludeDeployments == nil || includeContains(conf.IncludeDeployments, c.deployment.Name()) {
c.ui.PrintLinef("Using deployment flags from config of type '%s' (name: '%s')", config.Type, config.Name)

opts = setFlags(conf.Flags, opts)
}
}

bytes, err := tpl.Evaluate(opts.VarFlags.AsVariables(), opts.OpsFlags.AsOp(), boshtpl.EvaluateOpts{})
if err != nil {
return bosherr.WrapErrorf(err, "Evaluating manifest")
Expand Down Expand Up @@ -77,6 +98,32 @@ func (c DeployCmd) Run(opts DeployOpts) error {
return c.deployment.Update(bytes, updateOpts)
}

func setFlags(flags []string, opts DeployOpts) DeployOpts {
for j := range flags {
switch flags[j] {
case "fix-releases":
opts.FixReleases = true
case "fix":
opts.Fix = true
case "recreate":
opts.Recreate = true
case "recreate-persistent-disks":
opts.RecreatePersistentDisks = true
}
}

return opts
}

func includeContains(slice []string, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}

func (c DeployCmd) checkDeploymentName(bytes []byte) error {
manifest, err := boshdir.NewManifestFromBytes(bytes)
if err != nil {
Expand Down
21 changes: 20 additions & 1 deletion cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var _ = Describe("DeployCmd", func() {
ui *fakeui.FakeUI
deployment *fakedir.FakeDeployment
releaseUploader *fakecmd.FakeReleaseUploader
director *fakedir.FakeDirector
command DeployCmd
)

Expand All @@ -34,7 +35,9 @@ var _ = Describe("DeployCmd", func() {
UploadReleasesStub: func(bytes []byte) ([]byte, error) { return bytes, nil },
}

command = NewDeployCmd(ui, deployment, releaseUploader)
director = &fakedir.FakeDirector{}

command = NewDeployCmd(ui, deployment, releaseUploader, director)
})

Describe("Run", func() {
Expand Down Expand Up @@ -281,5 +284,21 @@ releases:
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})

It("overrides the opts with the flags from configs of type deploy", func() {
configs := []boshdir.Config{{"1", "default", "deploy", "0000", "", "flags:\n - fix", true}}

director.ListConfigsReturns(configs, nil)

err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))

_, updateOpts := deployment.UpdateArgsForCall(0)

Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
Fix: true,
}))
})
})
})

0 comments on commit cfbabbb

Please sign in to comment.