-
Notifications
You must be signed in to change notification settings - Fork 159
/
deploy.go
155 lines (128 loc) · 3.96 KB
/
deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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"
boshtpl "github.com/cloudfoundry/bosh-cli/v7/director/template"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
)
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, 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")
}
err = c.checkDeploymentName(bytes)
if err != nil {
return err
}
if opts.FixReleases {
bytes, err = c.releaseUploader.UploadReleasesWithFix(bytes)
} else {
bytes, err = c.releaseUploader.UploadReleases(bytes)
}
if err != nil {
return err
}
deploymentDiff, err := c.deployment.Diff(bytes, opts.NoRedact)
if err != nil {
return err
}
diff := NewDiff(deploymentDiff.Diff)
diff.Print(c.ui)
err = c.ui.AskForConfirmation()
if err != nil {
return err
}
updateOpts := boshdir.UpdateOpts{
RecreatePersistentDisks: opts.RecreatePersistentDisks,
Recreate: opts.Recreate,
Fix: opts.Fix,
SkipDrain: opts.SkipDrain,
DryRun: opts.DryRun,
Canaries: opts.Canaries,
MaxInFlight: opts.MaxInFlight,
Diff: deploymentDiff,
ForceLatestVariables: opts.ForceLatestVariables,
}
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 {
return bosherr.WrapErrorf(err, "Parsing manifest")
}
if manifest.Name != c.deployment.Name() {
errMsg := "Expected manifest to specify deployment name '%s' but was '%s'"
return bosherr.Errorf(errMsg, c.deployment.Name(), manifest.Name)
}
return nil
}