Skip to content

Commit

Permalink
Merge pull request #634 from fmoehler/add-global-flags-from-deploy-co…
Browse files Browse the repository at this point in the history
…nfig-in-deploy-go

enable global flags for deploy command
  • Loading branch information
beyhan authored Nov 24, 2023
2 parents d2c8b95 + 1a8d04d commit d00df5a
Show file tree
Hide file tree
Showing 3 changed files with 201 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 @@ -341,7 +341,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
64 changes: 63 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,59 @@ 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"`
ExcludeDeployments []string `yaml:"exclude"`
}

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

err := yaml.Unmarshal([]byte(config.Content), &conf)
if err != nil {
return err
}

deploymentIncluded := applies(conf.IncludeDeployments, c.deployment.Name())
deploymentExcluded := applies(conf.ExcludeDeployments, c.deployment.Name())

if conf.ExcludeDeployments != nil &&
conf.IncludeDeployments != nil {
c.ui.PrintLinef("Ignoring deployment flags from config of type '%s' (name: '%s'). Please use only 'include'- OR 'exclude'-property in the config.", config.Type, config.Name)
} else {
if (conf.IncludeDeployments == nil && conf.ExcludeDeployments == nil) ||
deploymentIncluded ||
(!deploymentExcluded && conf.ExcludeDeployments != nil) {
c.ui.ErrorLinef("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 @@ -78,6 +114,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 applies(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
138 changes: 137 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 @@ -296,5 +299,138 @@ releases:
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})

It("overwrites the opts with the flags from configs of type deploy", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default", Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix",
Current: 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,
}))
})

It("overwrites the opts with the flags from configs of type deploy if the deployment is included", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\ninclude:\n - dep",
Current: true,
},
}

director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")

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

_, updateOpts := deployment.UpdateArgsForCall(0)

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

It("does not overwrite the opts with the flags from configs of type deploy if the deployment is not included", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\ninclude:\n - foo",
Current: true,
},
}

director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")

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

_, updateOpts := deployment.UpdateArgsForCall(0)

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

It("does not overwrite the opts with the flags from configs of type deploy if the deployment is excluded", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\nexclude:\n - dep",
Current: true,
},
}

director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")

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

_, updateOpts := deployment.UpdateArgsForCall(0)

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

It("overwrites the opts with the flags from configs of type deploy if the deployment is not excluded", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\nexclude:\n - foo",
Current: true,
},
}

director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")

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 d00df5a

Please sign in to comment.