This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_release_index.go
253 lines (193 loc) · 5.47 KB
/
fs_release_index.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package releasedir
import (
"fmt"
"os"
"path"
"sort"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
semver "github.com/cppforlife/go-semi-semantic/version"
"gopkg.in/yaml.v2"
boshrel "github.com/cloudfoundry/bosh-cli/release"
boshrelman "github.com/cloudfoundry/bosh-cli/release/manifest"
)
type FSReleaseIndex struct {
name string
dirPath string
reporter ReleaseIndexReporter
uuidGen boshuuid.Generator
fs boshsys.FileSystem
}
/*
---
builds:
70b9ea8efb83b882021792517b1164550b41bc27:
version: "1"
format-version: "2"
*/
type fsReleaseIndexSchema struct {
Builds map[string]fsReleaseIndexSchema_Entry `yaml:"builds"`
FormatVersion string `yaml:"format-version"`
}
type fsReleaseIndexSchema_Entry struct {
Version string `yaml:"version"`
}
type releaseIndexEntry struct {
Version semver.Version
}
func NewFSReleaseIndex(
name string,
dirPath string,
reporter ReleaseIndexReporter,
uuidGen boshuuid.Generator,
fs boshsys.FileSystem,
) FSReleaseIndex {
return FSReleaseIndex{
name: name,
dirPath: dirPath,
reporter: reporter,
uuidGen: uuidGen,
fs: fs,
}
}
func (i FSReleaseIndex) LastVersion(name string) (*semver.Version, error) {
if len(name) == 0 {
return nil, bosherr.Error("Expected non-empty release name")
}
schema, err := i.read(name)
if err != nil {
return nil, err
}
var versions []semver.Version
for _, entry := range schema.Builds {
ver, err := semver.NewVersionFromString(entry.Version)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Parsing release versions")
}
versions = append(versions, ver)
}
if len(versions) == 0 {
return nil, nil
}
sort.Sort(semver.AscSorting(versions))
return &versions[len(versions)-1], nil
}
func (i FSReleaseIndex) Contains(release boshrel.Release) (bool, error) {
if len(release.Name()) == 0 {
return false, bosherr.Error("Expected non-empty release name")
}
if len(release.Version()) == 0 {
return false, bosherr.Error("Expected non-empty release version")
}
schema, err := i.read(release.Name())
if err != nil {
return false, err
}
for _, entry := range schema.Builds {
if entry.Version == release.Version() {
return true, nil
}
}
return false, nil
}
func (i FSReleaseIndex) Add(manifest boshrelman.Manifest) error {
if len(manifest.Name) == 0 {
return bosherr.Error("Expected non-empty release name")
}
if len(manifest.Version) == 0 {
return bosherr.Error("Expected non-empty release version")
}
schema, err := i.read(manifest.Name)
if err != nil {
return err
}
for _, entry := range schema.Builds {
if entry.Version == manifest.Version {
return bosherr.Errorf("Release version '%s' already exists", manifest.Version)
}
}
uuid, err := i.uuidGen.Generate()
if err != nil {
return bosherr.WrapErrorf(err, "Generating key for release index entry")
}
schema.Builds[uuid] = fsReleaseIndexSchema_Entry{Version: manifest.Version}
desc := fmt.Sprintf("%s/%s", manifest.Name, manifest.Version)
err = i.saveManifest(manifest)
if err != nil {
i.reporter.ReleaseIndexAdded(i.name, desc, err)
return err
}
err = i.save(manifest.Name, schema)
if err != nil {
i.reporter.ReleaseIndexAdded(i.name, desc, err)
return err
}
i.reporter.ReleaseIndexAdded(i.name, desc, nil)
return nil
}
func (i FSReleaseIndex) ManifestPath(name, version string) string {
fileName := fmt.Sprintf("%s-%s.yml", name, version)
return path.Join(i.dirPath, name, fileName)
}
func (i FSReleaseIndex) ArchivePath(release boshrel.Release) (string, error) {
if len(release.Name()) == 0 {
return "", bosherr.Error("Expected non-empty release name")
}
if len(release.Version()) == 0 {
return "", bosherr.Error("Expected non-empty release version")
}
fileName := fmt.Sprintf("%s-%s.tgz", release.Name(), release.Version())
archivePath := path.Join(i.dirPath, release.Name(), fileName)
err := i.fs.MkdirAll(path.Dir(archivePath), os.ModePerm)
if err != nil {
return "", bosherr.WrapErrorf(err, "Creating release directory")
}
return archivePath, nil
}
func (i FSReleaseIndex) indexPath(name string) string {
return path.Join(i.dirPath, name, "index.yml")
}
func (i FSReleaseIndex) read(name string) (fsReleaseIndexSchema, error) {
var schema fsReleaseIndexSchema
// Default to an empty map
schema.Builds = map[string]fsReleaseIndexSchema_Entry{}
indexPath := i.indexPath(name)
if !i.fs.FileExists(indexPath) {
return schema, nil
}
bytes, err := i.fs.ReadFile(indexPath)
if err != nil {
return schema, bosherr.WrapErrorf(err, "Reading index")
}
err = yaml.Unmarshal(bytes, &schema)
if err != nil {
return schema, bosherr.WrapError(err, "Unmarshalling index")
}
return schema, nil
}
func (i FSReleaseIndex) save(name string, schema fsReleaseIndexSchema) error {
schema.FormatVersion = "2"
bytes, err := yaml.Marshal(schema)
if err != nil {
return bosherr.WrapError(err, "Marshalling index")
}
indexPath := i.indexPath(name)
err = i.fs.WriteFile(indexPath, bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Writing index")
}
return nil
}
func (i FSReleaseIndex) saveManifest(manifest boshrelman.Manifest) error {
bytes, err := yaml.Marshal(manifest)
if err != nil {
return bosherr.WrapError(err, "Marshalling manifest")
}
manifestPath := i.ManifestPath(manifest.Name, manifest.Version)
err = i.fs.WriteFile(manifestPath, bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Writing manifest")
}
return nil
}