-
Notifications
You must be signed in to change notification settings - Fork 174
/
preparation.go
325 lines (291 loc) · 8.33 KB
/
preparation.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
/*
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package preparation provides functionality to extract tags, branches and commits from repository configurations.
package preparation
import (
"archive/tar"
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"cloud.google.com/go/pubsub"
"cloud.google.com/go/storage"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/osv.dev/docker/indexer/config"
"github.com/google/osv.dev/docker/indexer/shared"
"golang.org/x/sync/semaphore"
log "github.com/golang/glog"
)
const workers = 5
// Result is the data structure returned by the stage.
type Result struct {
Name string
BaseCPE string
CheckoutOptions *git.CheckoutOptions
Commit plumbing.Hash
Reference plumbing.Hash
CommitTag string
When time.Time
Type string
Addr string
FileExts []string
EmptyBucketBitmap []byte
FileCount int
}
// Checker interface is used to check whether a name/hash pair already exists in storage.
type Checker interface {
Exists(ctx context.Context, addr string, hashType string, hash plumbing.Hash) (bool, error)
}
// Stage holds the data types necessary to process repository configuration.
type Stage struct {
Checker Checker
RepoHdl *storage.BucketHandle
Output *pubsub.Topic
}
// Run runs the stage and outputs Result data types to the results channel.
func (s *Stage) Run(ctx context.Context, cfgs []*config.RepoConfig) error {
wCtx, wCancel := context.WithCancel(ctx)
defer wCancel()
sem := semaphore.NewWeighted(workers)
for _, repoCfg := range cfgs {
if err := sem.Acquire(wCtx, 1); err != nil {
return fmt.Errorf("failed to acquire semaphore: %v", err)
}
go func(ctx context.Context, repoCfg *config.RepoConfig) {
defer sem.Release(1)
var err error
select {
case <-ctx.Done():
log.Error(context.Canceled)
return
default:
}
log.Infof("received config for %s", repoCfg.Name)
switch repoCfg.Type {
case shared.Git:
err = s.processGit(ctx, repoCfg)
default:
log.Errorf("unsupported config type: %s", repoCfg.Type)
}
if err != nil {
log.Errorf("preparation failed for %s: %v", repoCfg.Name, err)
}
}(wCtx, repoCfg)
}
return sem.Acquire(ctx, workers)
}
func (s *Stage) objectExists(ctx context.Context, name string) bool {
objItr := s.RepoHdl.Objects(ctx, &storage.Query{Prefix: name + shared.TarExt})
_, err := objItr.Next()
return err == nil
}
func (s *Stage) processGit(ctx context.Context, repoCfg *config.RepoConfig) error {
var (
err error
repo *git.Repository
repoDir string
)
if !s.objectExists(ctx, repoCfg.Name) {
repo, repoDir, err = s.cloneGitRepo(ctx, repoCfg.Name, repoCfg.Address)
} else {
repo, repoDir, err = s.updateGitRepo(ctx, repoCfg.Name)
}
if repoDir != "" {
defer func() {
if err := os.RemoveAll(repoDir); err != nil {
log.Errorf("failed to remove local repo: %v", err)
}
}()
}
if err != nil {
return fmt.Errorf("failed to clone/update repo: %w", err)
}
comItr, err := repo.CommitObjects()
if err != nil {
return fmt.Errorf("failed to get commit objects: %w", err)
}
allCommits := make(map[plumbing.Hash]*object.Commit)
comItr.ForEach(func(c *object.Commit) error {
allCommits[c.Hash] = c
return nil
})
commitTracker := make(map[plumbing.Hash]bool)
// repoInfo is used as the iterator function to create RepositoryInformation structs.
repoInfo := func(ref *plumbing.Reference) error {
// Resolve the real commit hash
commitHash, err := repo.ResolveRevision(plumbing.Revision(ref.Name().String()))
if err != nil {
log.Errorf("Failed to resolve %s: %v", ref.Name().String(), err)
// Ignore errors as this will block the iteration otherwise.
return nil
}
found, err := s.Checker.Exists(ctx, repoCfg.Address, shared.MD5, ref.Hash())
if err != nil {
return err
}
if found {
return nil
}
var when time.Time
if c, ok := allCommits[*commitHash]; ok {
when = c.Author.When
}
commitTag := ref.Name().String()
result := &Result{
Name: repoCfg.Name,
BaseCPE: repoCfg.BaseCPE,
CheckoutOptions: &git.CheckoutOptions{
Branch: ref.Name(),
},
When: when,
Commit: *commitHash,
Reference: ref.Hash(),
CommitTag: commitTag,
Type: shared.Git,
Addr: repoCfg.Address,
FileExts: repoCfg.FileExts,
}
commitTracker[*commitHash] = true
buf, err := json.Marshal(result)
if err != nil {
return err
}
log.Infof("publishing %s at version: %s", result.Name, commitTag)
pubRes := s.Output.Publish(ctx, &pubsub.Message{Data: buf})
_, err = pubRes.Get(ctx)
return err
}
repoItr, err := repo.Tags()
if err != nil {
return err
}
if err := repoItr.ForEach(repoInfo); err != nil {
return err
}
if repoCfg.BranchVersioning {
repoItr, err := repo.Branches()
if err != nil {
return err
}
if err := repoItr.ForEach(repoInfo); err != nil {
return err
}
}
if repoCfg.HashAllCommits {
for h, c := range allCommits {
if found := commitTracker[h]; !found {
exists, err := s.Checker.Exists(ctx, repoCfg.Address, shared.MD5, h)
if err != nil {
return err
}
if exists {
continue
}
result := &Result{
Name: repoCfg.Name,
CheckoutOptions: &git.CheckoutOptions{
Hash: h,
Force: true,
},
Reference: h,
When: c.Author.When,
Commit: h,
Type: shared.Git,
FileExts: repoCfg.FileExts,
}
buf, err := json.Marshal(result)
if err != nil {
return err
}
pubRes := s.Output.Publish(ctx, &pubsub.Message{Data: buf})
_, err = pubRes.Get(ctx)
return err
}
}
}
return nil
}
func (s *Stage) cloneGitRepo(ctx context.Context, name, address string) (*git.Repository, string, error) {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return nil, "", fmt.Errorf("failed to create tmp dir: %v", err)
}
repo, err := git.PlainClone(tmpDir, false, &git.CloneOptions{
URL: address,
})
if err != nil {
return nil, tmpDir, fmt.Errorf("failed to clone repository for %s: %v", name, err)
}
return repo, tmpDir, s.copyToBucket(ctx, tmpDir, name)
}
func (s *Stage) updateGitRepo(ctx context.Context, name string) (*git.Repository, string, error) {
repoDir, err := shared.CopyFromBucket(ctx, s.RepoHdl, name)
if err != nil {
return nil, "", err
}
repo, err := git.PlainOpen(repoDir)
if err != nil {
log.Error(err)
return nil, "", err
}
if err := repo.Fetch(&git.FetchOptions{
Tags: git.AllTags,
}); err != nil && err != git.NoErrAlreadyUpToDate {
log.Errorf("failed to fetch '%s' with %v", name, err)
return nil, "", err
}
if err := s.copyToBucket(ctx, repoDir, name); err != nil {
return nil, repoDir, err
}
return repo, repoDir, nil
}
func (r *Stage) copyToBucket(ctx context.Context, dir, name string) error {
var filePaths []string
if err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
filePaths = append(filePaths, path)
return nil
}); err != nil {
return fmt.Errorf("failed to collect paths for %s: %v", name, err)
}
obj := r.RepoHdl.Object(name + shared.TarExt)
objW := obj.NewWriter(ctx)
defer objW.Close()
tarW := tar.NewWriter(objW)
defer tarW.Close()
for _, p := range filePaths {
buf, err := os.ReadFile(p)
if err != nil {
return fmt.Errorf("failed to read file %s for %s: %v", p, name, err)
}
if err := tarW.WriteHeader(&tar.Header{
Name: strings.ReplaceAll(p, dir, ""),
Mode: 0660,
Size: int64(len(buf)),
}); err != nil {
return fmt.Errorf("failed to write file header for %s to tar archive for %s: %v", p, name, err)
}
if _, err := tarW.Write(buf); err != nil {
return fmt.Errorf("failed to write file %s for tar archive %s: %v", p, name, err)
}
}
return nil
}