forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stemcell_repo.go
196 lines (158 loc) · 4.79 KB
/
stemcell_repo.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package config
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type StemcellRepo interface // StemcellRepo persists stemcells metadata
{
UpdateCurrent(recordID string) error
FindCurrent() (StemcellRecord, bool, error)
ClearCurrent() error
Save(name, version, cid string, apiVersion int) (StemcellRecord, error)
Find(name, version string) (StemcellRecord, bool, error)
All() ([]StemcellRecord, error)
Delete(StemcellRecord) error
}
type stemcellRepo struct {
deploymentStateService DeploymentStateService
uuidGenerator boshuuid.Generator
}
func NewStemcellRepo(deploymentStateService DeploymentStateService, uuidGenerator boshuuid.Generator) StemcellRepo {
return stemcellRepo{
deploymentStateService: deploymentStateService,
uuidGenerator: uuidGenerator,
}
}
func (r stemcellRepo) Save(name, version, cid string, apiVersion int) (StemcellRecord, error) {
stemcellRecord := StemcellRecord{}
err := r.updateConfig(func(config *DeploymentState) error {
records := config.Stemcells
if records == nil {
records = []StemcellRecord{}
}
newRecord := StemcellRecord{
Name: name,
Version: version,
CID: cid,
ApiVersion: apiVersion,
}
var err error
newRecord.ID, err = r.uuidGenerator.Generate()
if err != nil {
return bosherr.WrapError(err, "Generating stemcell id")
}
for _, oldRecord := range records {
if oldRecord.Name == newRecord.Name && oldRecord.Version == newRecord.Version {
return bosherr.Errorf("Failed to save stemcell record '%+v' (duplicate name/version), existing record found '%+v'", newRecord, oldRecord)
}
}
records = append(records, newRecord)
config.Stemcells = records
stemcellRecord = newRecord
return nil
})
return stemcellRecord, err
}
func (r stemcellRepo) Find(name, version string) (StemcellRecord, bool, error) {
_, records, err := r.load()
if err != nil {
return StemcellRecord{}, false, err
}
for _, oldRecord := range records {
if oldRecord.Name == name && oldRecord.Version == version {
return oldRecord, true, nil
}
}
return StemcellRecord{}, false, nil
}
func (r stemcellRepo) FindCurrent() (StemcellRecord, bool, error) {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return StemcellRecord{}, false, bosherr.WrapError(err, "Loading existing config")
}
currentDiskID := deploymentState.CurrentStemcellID
if currentDiskID == "" {
return StemcellRecord{}, false, nil
}
for _, oldRecord := range deploymentState.Stemcells {
if oldRecord.ID == currentDiskID {
return oldRecord, true, nil
}
}
return StemcellRecord{}, false, nil
}
func (r stemcellRepo) All() ([]StemcellRecord, error) {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return []StemcellRecord{}, bosherr.WrapError(err, "Loading existing config")
}
return deploymentState.Stemcells, nil
}
func (r stemcellRepo) Delete(stemcellRecord StemcellRecord) error {
config, records, err := r.load()
if err != nil {
return err
}
newRecords := []StemcellRecord{}
for _, record := range records {
if stemcellRecord.ID != record.ID {
newRecords = append(newRecords, record)
}
}
config.Stemcells = newRecords
if config.CurrentStemcellID == stemcellRecord.ID {
config.CurrentStemcellID = ""
}
err = r.deploymentStateService.Save(config)
if err != nil {
return bosherr.WrapError(err, "Saving config")
}
return nil
}
func (r stemcellRepo) UpdateCurrent(recordID string) error {
return r.updateConfig(func(config *DeploymentState) error {
found := false
for _, oldRecord := range config.Stemcells {
if oldRecord.ID == recordID {
found = true
}
}
if !found {
return bosherr.Errorf("Verifying stemcell record exists with id '%s'", recordID)
}
config.CurrentStemcellID = recordID
return nil
})
}
func (r stemcellRepo) ClearCurrent() error {
return r.updateConfig(func(config *DeploymentState) error {
config.CurrentStemcellID = ""
return nil
})
}
func (r stemcellRepo) updateConfig(updateFunc func(*DeploymentState) error) error {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return bosherr.WrapError(err, "Loading existing config")
}
err = updateFunc(&deploymentState)
if err != nil {
return err
}
err = r.deploymentStateService.Save(deploymentState)
if err != nil {
return bosherr.WrapError(err, "Saving new config")
}
return nil
}
func (r stemcellRepo) load() (DeploymentState, []StemcellRecord, error) {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return deploymentState, []StemcellRecord{}, bosherr.WrapError(err, "Loading existing config")
}
records := deploymentState.Stemcells
if records == nil {
return deploymentState, []StemcellRecord{}, nil
}
return deploymentState, records, nil
}