-
Notifications
You must be signed in to change notification settings - Fork 162
/
deploy.go
103 lines (83 loc) · 2.36 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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type DeployCmd struct {
ui boshui.UI
deployment boshdir.Deployment
releaseUploader ReleaseUploader
}
type ReleaseUploader interface {
UploadReleases([]byte) ([]byte, error)
}
func NewDeployCmd(
ui boshui.UI,
deployment boshdir.Deployment,
releaseUploader ReleaseUploader,
) DeployCmd {
return DeployCmd{ui, deployment, releaseUploader}
}
func (c DeployCmd) Run(opts DeployOpts) error {
tpl := boshtpl.NewTemplate(opts.Args.Manifest.Bytes)
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
}
bytes, err = c.releaseUploader.UploadReleases(bytes)
if err != nil {
return err
}
deploymentDiff, err := c.deployment.Diff(bytes, opts.NoRedact)
if err != nil {
return err
}
err = c.printManifestDiff(deploymentDiff, bytes, opts)
if err != nil {
return bosherr.WrapError(err, "Diffing manifest")
}
err = c.ui.AskForConfirmation()
if err != nil {
return err
}
updateOpts := boshdir.UpdateOpts{
Recreate: opts.Recreate,
Fix: opts.Fix,
SkipDrain: opts.SkipDrain,
DryRun: opts.DryRun,
Canaries: opts.Canaries,
MaxInFlight: opts.MaxInFlight,
Diff: deploymentDiff,
}
return c.deployment.Update(bytes, updateOpts)
}
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
}
func (c DeployCmd) printManifestDiff(diff boshdir.DeploymentDiff, bytes []byte, opts DeployOpts) error {
for _, line := range diff.Diff {
lineMod, _ := line[1].(string)
if lineMod == "added" {
c.ui.BeginLinef("+ %s\n", line[0])
} else if lineMod == "removed" {
c.ui.BeginLinef("- %s\n", line[0])
} else {
c.ui.BeginLinef(" %s\n", line[0])
}
}
return nil
}