forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry_cache.go
357 lines (295 loc) · 8.82 KB
/
registry_cache.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package registry
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"runtime"
"time"
"github.com/pkg/errors"
"golang.org/x/mod/semver"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/buildpack"
"github.com/buildpacks/pack/pkg/logging"
)
const DefaultRegistryURL = "https://github.com/buildpacks/registry-index"
const DefaultRegistryName = "official"
const defaultRegistryDir = "registry"
// Cache is a RegistryCache
type Cache struct {
logger logging.Logger
url *url.URL
Root string
RegistryDir string
}
const GithubIssueTitleTemplate = "{{ if .Yanked }}YANK{{ else }}ADD{{ end }} {{.Namespace}}/{{.Name}}@{{.Version}}"
const GithubIssueBodyTemplate = `
id = "{{.Namespace}}/{{.Name}}"
version = "{{.Version}}"
{{ if .Yanked }}{{ else if .Address }}addr = "{{.Address}}"{{ end }}
`
const GitCommitTemplate = `{{ if .Yanked }}YANK{{else}}ADD{{end}} {{.Namespace}}/{{.Name}}@{{.Version}}`
// Entry is a list of buildpacks stored in a registry
type Entry struct {
Buildpacks []Buildpack `json:"buildpacks"`
}
// NewDefaultRegistryCache creates a new registry cache with default options
func NewDefaultRegistryCache(logger logging.Logger, home string) (Cache, error) {
return NewRegistryCache(logger, home, DefaultRegistryURL)
}
// NewRegistryCache creates a new registry cache
func NewRegistryCache(logger logging.Logger, home, registryURL string) (Cache, error) {
if _, err := os.Stat(home); err != nil {
return Cache{}, errors.Wrapf(err, "finding home %s", home)
}
normalizedURL, err := url.Parse(registryURL)
if err != nil {
return Cache{}, errors.Wrapf(err, "parsing registry url %s", registryURL)
}
key := sha256.New()
key.Write([]byte(normalizedURL.String()))
cacheDir := fmt.Sprintf("%s-%s", defaultRegistryDir, hex.EncodeToString(key.Sum(nil)))
return Cache{
url: normalizedURL,
logger: logger,
Root: filepath.Join(home, cacheDir),
}, nil
}
// LocateBuildpack stored in registry
func (r *Cache) LocateBuildpack(bp string) (Buildpack, error) {
err := r.Refresh()
if err != nil {
return Buildpack{}, errors.Wrap(err, "refreshing cache")
}
ns, name, version, err := buildpack.ParseRegistryID(bp)
if err != nil {
return Buildpack{}, errors.Wrap(err, "parsing buildpacks registry id")
}
entry, err := r.readEntry(ns, name)
if err != nil {
return Buildpack{}, errors.Wrap(err, "reading entry")
}
if len(entry.Buildpacks) > 0 {
if version == "" {
highestVersion := entry.Buildpacks[0]
if len(entry.Buildpacks) > 1 {
for _, bp := range entry.Buildpacks[1:] {
if semver.Compare(fmt.Sprintf("v%s", bp.Version), fmt.Sprintf("v%s", highestVersion.Version)) > 0 {
highestVersion = bp
}
}
}
return highestVersion, Validate(highestVersion)
}
for _, bpIndex := range entry.Buildpacks {
if bpIndex.Version == version {
return bpIndex, Validate(bpIndex)
}
}
return Buildpack{}, fmt.Errorf("could not find version for buildpack: %s", bp)
}
return Buildpack{}, fmt.Errorf("no entries for buildpack: %s", bp)
}
// Refresh local Registry Cache
func (r *Cache) Refresh() error {
r.logger.Debugf("Refreshing registry cache for %s/%s", r.url.Host, r.url.Path)
if err := r.Initialize(); err != nil {
return errors.Wrapf(err, "initializing (%s)", r.Root)
}
repository, err := git.PlainOpen(r.Root)
if err != nil {
return errors.Wrapf(err, "opening (%s)", r.Root)
}
w, err := repository.Worktree()
if err != nil {
return errors.Wrapf(err, "reading (%s)", r.Root)
}
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err == git.NoErrAlreadyUpToDate {
return nil
}
return err
}
// Initialize a local Registry Cache
func (r *Cache) Initialize() error {
_, err := os.Stat(r.Root)
if err != nil {
if os.IsNotExist(err) {
err = r.CreateCache()
if err != nil {
return errors.Wrap(err, "creating registry cache")
}
}
}
if err := r.validateCache(); err != nil {
err = os.RemoveAll(r.Root)
if err != nil {
return errors.Wrap(err, "resetting registry cache")
}
err = r.CreateCache()
if err != nil {
return errors.Wrap(err, "rebuilding registry cache")
}
}
return nil
}
// CreateCache creates the cache on the filesystem
func (r *Cache) CreateCache() error {
r.logger.Debugf("Creating registry cache for %s/%s", r.url.Host, r.url.Path)
registryDir, err := ioutil.TempDir(filepath.Dir(r.Root), "registry")
if err != nil {
return err
}
r.RegistryDir = registryDir
repository, err := git.PlainClone(r.RegistryDir, false, &git.CloneOptions{
URL: r.url.String(),
})
if err != nil {
return errors.Wrap(err, "cloning remote registry")
}
w, err := repository.Worktree()
if err != nil {
return err
}
err = os.Rename(w.Filesystem.Root(), r.Root)
if err != nil {
if err == os.ErrExist {
// If pack is run concurrently, this action might have already occurred
return nil
}
return err
}
return nil
}
func (r *Cache) validateCache() error {
r.logger.Debugf("Validating registry cache for %s/%s", r.url.Host, r.url.Path)
repository, err := git.PlainOpen(r.Root)
if err != nil {
return errors.Wrap(err, "opening registry cache")
}
remotes, err := repository.Remotes()
if err != nil {
return errors.Wrap(err, "accessing registry cache")
}
for _, remote := range remotes {
if remote.Config().Name == "origin" && remotes[0].Config().URLs[0] != r.url.String() {
return nil
}
}
return errors.New("invalid registry cache remote")
}
// Commit a Buildpack change
func (r *Cache) Commit(b Buildpack, username, msg string) error {
r.logger.Debugf("Creating commit in registry cache")
if msg == "" {
return errors.New("invalid commit message")
}
repository, err := git.PlainOpen(r.Root)
if err != nil {
return errors.Wrap(err, "opening registry cache")
}
w, err := repository.Worktree()
if err != nil {
return errors.Wrapf(err, "reading %s", style.Symbol(r.Root))
}
index, err := r.writeEntry(b)
if err != nil {
return errors.Wrapf(err, "writing %s", style.Symbol(index))
}
relativeIndexFile, err := filepath.Rel(r.Root, index)
if err != nil {
return errors.Wrap(err, "resolving relative path")
}
if _, err := w.Add(relativeIndexFile); err != nil {
return errors.Wrapf(err, "adding %s", style.Symbol(index))
}
if _, err := w.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: username,
Email: "",
When: time.Now(),
},
}); err != nil {
return errors.Wrapf(err, "committing")
}
return nil
}
func (r *Cache) writeEntry(b Buildpack) (string, error) {
var ns = b.Namespace
var name = b.Name
index, err := IndexPath(r.Root, ns, name)
if err != nil {
return "", err
}
if _, err := os.Stat(index); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(index), 0750); err != nil {
return "", errors.Wrapf(err, "creating directory structure for: %s/%s", ns, name)
}
} else {
if _, err := os.Stat(index); err == nil {
entry, err := r.readEntry(ns, name)
if err != nil {
return "", errors.Wrapf(err, "reading existing buildpack entries")
}
availableBuildpacks := entry.Buildpacks
if len(availableBuildpacks) != 0 {
if availableBuildpacks[len(availableBuildpacks)-1].Version == b.Version {
return "", errors.Wrapf(err, "same version exists, upgrade the version to add")
}
}
}
}
f, err := os.OpenFile(filepath.Clean(index), os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return "", errors.Wrapf(err, "creating buildpack file: %s/%s", ns, name)
}
defer f.Close()
newline := "\n"
if runtime.GOOS == "windows" {
newline = "\r\n"
}
fileContents, err := json.Marshal(b)
if err != nil {
return "", errors.Wrapf(err, "converting buildpack file to json: %s/%s", ns, name)
}
fileContentsFormatted := string(fileContents) + newline
if _, err := f.WriteString(fileContentsFormatted); err != nil {
return "", errors.Wrapf(err, "writing buildpack to file: %s/%s", ns, name)
}
return index, nil
}
func (r *Cache) readEntry(ns, name string) (Entry, error) {
index, err := IndexPath(r.Root, ns, name)
if err != nil {
return Entry{}, err
}
if _, err := os.Stat(index); err != nil {
return Entry{}, errors.Wrapf(err, "finding buildpack: %s/%s", ns, name)
}
file, err := os.Open(filepath.Clean(index))
if err != nil {
return Entry{}, errors.Wrapf(err, "opening index for buildpack: %s/%s", ns, name)
}
defer file.Close()
entry := Entry{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
var bp Buildpack
err = json.Unmarshal([]byte(scanner.Text()), &bp)
if err != nil {
return Entry{}, errors.Wrapf(err, "parsing index for buildpack: %s/%s", ns, name)
}
entry.Buildpacks = append(entry.Buildpacks, bp)
}
if err := scanner.Err(); err != nil {
return entry, errors.Wrapf(err, "reading index for buildpack: %s/%s", ns, name)
}
return entry, nil
}