forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
110 lines (87 loc) · 2.54 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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
semver "github.com/cppforlife/go-semi-semantic/version"
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
uploadReleaseCmd ReleaseUploadingCmd
}
type ReleaseUploadingCmd interface {
Run(UploadReleaseOpts) error
}
func NewDeployCmd(ui boshui.UI, deployment boshdir.Deployment, uploadReleaseCmd ReleaseUploadingCmd) DeployCmd {
return DeployCmd{ui: ui, deployment: deployment, uploadReleaseCmd: uploadReleaseCmd}
}
func (c DeployCmd) Run(opts DeployOpts) error {
tpl := boshtpl.NewTemplate(opts.Args.Manifest.Bytes)
bytes, err := tpl.Evaluate(opts.VarFlags.AsVariables())
if err != nil {
return bosherr.WrapErrorf(err, "Evaluating manifest")
}
man, err := boshdir.NewManifestFromBytes(bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Checking manifest")
}
if man.Name != c.deployment.Name() {
errMsg := "Expected manifest to specify deployment name '%s' but was '%s'"
return bosherr.Errorf(errMsg, c.deployment.Name(), man.Name)
}
err = c.printManifestDiff(bytes, opts)
if err != nil {
return bosherr.WrapError(err, "Diffing manifest")
}
err = c.ui.AskForConfirmation()
if err != nil {
return err
}
for _, rel := range man.Releases {
err = c.uploadRelease(rel)
if err != nil {
return bosherr.WrapErrorf(err, "Uploading release '%s/%s'", rel.Name, rel.Version)
}
}
updateOpts := boshdir.UpdateOpts{
Recreate: opts.Recreate,
Fix: opts.Fix,
SkipDrain: opts.SkipDrain,
}
return c.deployment.Update(bytes, updateOpts)
}
func (c DeployCmd) uploadRelease(rel boshdir.ManifestRelease) error {
ver, err := semver.NewVersionFromString(rel.Version)
if err != nil {
return err
}
opts := UploadReleaseOpts{
Name: rel.Name,
Version: VersionArg(ver),
Args: UploadReleaseArgs{URL: URLArg(rel.URL)},
SHA1: rel.SHA1,
}
if opts.Args.URL.IsEmpty() {
return nil
}
return c.uploadReleaseCmd.Run(opts)
}
func (c DeployCmd) printManifestDiff(bytes []byte, opts DeployOpts) error {
diff, err := c.deployment.Diff(bytes, opts.NoRedact)
if err != nil {
return err
}
for _, line := range 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
}