-
Notifications
You must be signed in to change notification settings - Fork 162
/
record.go
116 lines (95 loc) · 2.81 KB
/
record.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
package deployment
import (
biconfig "github.com/cloudfoundry/bosh-cli/config"
birel "github.com/cloudfoundry/bosh-cli/release"
bistemcell "github.com/cloudfoundry/bosh-cli/stemcell"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Record interface {
IsDeployed(manifestSHA string, releases []birel.Release, stemcell bistemcell.ExtractedStemcell) (bool, error)
Clear() error
Update(manifestSHA string, releases []birel.Release) error
}
type deploymentRecord struct {
deploymentRepo biconfig.DeploymentRepo
releaseRepo biconfig.ReleaseRepo
stemcellRepo biconfig.StemcellRepo
}
func NewRecord(
deploymentRepo biconfig.DeploymentRepo,
releaseRepo biconfig.ReleaseRepo,
stemcellRepo biconfig.StemcellRepo,
) Record {
return &deploymentRecord{
deploymentRepo: deploymentRepo,
releaseRepo: releaseRepo,
stemcellRepo: stemcellRepo,
}
}
func (v *deploymentRecord) IsDeployed(newManifestSHA string, releases []birel.Release, stemcell bistemcell.ExtractedStemcell) (bool, error) {
deployedManifestSHA, found, err := v.deploymentRepo.FindCurrent()
if err != nil {
return false, bosherr.WrapError(err, "Finding sha of currently deployed manifest")
}
if !found {
return false, nil
}
if deployedManifestSHA != newManifestSHA {
return false, nil
}
currentStemcell, found, err := v.stemcellRepo.FindCurrent()
if err != nil {
return false, bosherr.WrapError(err, "Finding currently deployed stemcell")
}
if !found {
return false, nil
}
if currentStemcell.Name != stemcell.Manifest().Name || currentStemcell.Version != stemcell.Manifest().Version {
return false, nil
}
currentReleaseRecords, err := v.releaseRepo.List()
if err != nil {
return false, bosherr.WrapError(err, "Finding currently deployed release")
}
if len(currentReleaseRecords) == 0 {
return false, nil
}
if len(releases) != len(currentReleaseRecords) {
return false, nil
}
for _, release := range releases {
found := false
for _, releaseRecord := range currentReleaseRecords {
if releaseRecord.Name == release.Name() && releaseRecord.Version == release.Version() {
found = true
break
}
}
if !found {
return false, nil
}
}
return true, nil
}
func (v *deploymentRecord) Clear() error {
err := v.deploymentRepo.UpdateCurrent("")
if err != nil {
return bosherr.WrapError(err, "Clearing sha of deployed manifest")
}
err = v.releaseRepo.Update([]birel.Release{})
if err != nil {
return bosherr.WrapError(err, "Clearing releases")
}
return nil
}
func (v *deploymentRecord) Update(manifestSHA string, releases []birel.Release) error {
err := v.deploymentRepo.UpdateCurrent(manifestSHA)
if err != nil {
return bosherr.WrapError(err, "Saving sha of deployed manifest")
}
err = v.releaseRepo.Update(releases)
if err != nil {
return bosherr.WrapError(err, "Updating releases")
}
return nil
}