-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
assetstore.go
392 lines (335 loc) · 9.29 KB
/
assetstore.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
Copyright 2019 The Kubernetes Authors.
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 fi
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi/utils"
"k8s.io/kops/util/pkg/hashing"
)
type asset struct {
Key string
AssetPath string
resource Resource
source *Source
}
type Source struct {
Parent *Source
URL string
Hash *hashing.Hash
ExtractFromArchive string
}
// Key builds a unique key for this source
func (s *Source) Key() string {
var k string
if s.Parent != nil {
k = s.Parent.Key() + "/"
}
if s.URL != "" {
k += s.URL
} else if s.ExtractFromArchive != "" {
k += s.ExtractFromArchive
} else {
klog.Fatalf("expected either URL or ExtractFromArchive to be set")
}
return k
}
func (s *Source) String() string {
return "Source[" + s.Key() + "]"
}
type HasSource interface {
GetSource() *Source
}
// assetResource implements Resource, but also implements HasFetchInstructions
type assetResource struct {
Asset *asset
}
var _ Resource = &assetResource{}
var _ HasSource = &assetResource{}
func (r *assetResource) Open() (io.Reader, error) {
return r.Asset.resource.Open()
}
func (r *assetResource) GetSource() *Source {
return r.Asset.source
}
type AssetStore struct {
cacheDir string
assets []*asset
}
func NewAssetStore(cacheDir string) *AssetStore {
a := &AssetStore{
cacheDir: cacheDir,
}
return a
}
func (a *AssetStore) FindMatches(expr *regexp.Regexp) map[string]Resource {
matches := make(map[string]Resource)
klog.Infof("Matching assets for %q:", expr.String())
for _, a := range a.assets {
if expr.MatchString(a.AssetPath) {
klog.Infof(" %s", a.AssetPath)
matches[a.Key] = &assetResource{Asset: a}
}
}
return matches
}
func (a *AssetStore) FindMatch(expr *regexp.Regexp) (name string, res Resource, err error) {
matches := a.FindMatches(expr)
switch len(matches) {
case 0:
return "", nil, fmt.Errorf("found no matching assets for expr: %q", expr.String())
case 1:
var n string
var r Resource
for k, v := range matches {
klog.Infof("Found single matching asset for expr %q: %q", expr.String(), k)
n = k
r = v
}
return n, r, nil
default:
return "", nil, fmt.Errorf("found multiple matching assets for expr: %q", expr.String())
}
}
func (a *AssetStore) Find(key string, assetPath string) (Resource, error) {
var matches []*asset
for _, asset := range a.assets {
if asset.Key != key {
continue
}
if assetPath != "" {
if !strings.HasSuffix(asset.AssetPath, assetPath) {
continue
}
}
matches = append(matches, asset)
}
if len(matches) == 0 {
return nil, nil
}
if len(matches) == 1 {
klog.Infof("Resolved asset %s:%s to %s", key, assetPath, matches[0].AssetPath)
return &assetResource{Asset: matches[0]}, nil
}
klog.Infof("Matching assets:")
for _, match := range matches {
klog.Infof(" %s %s", match.Key, match.AssetPath)
}
return nil, fmt.Errorf("found multiple matching assets for key: %q", key)
}
// Add an asset into the store, in one of the recognized formats (see Assets in types package)
func (a *AssetStore) AddForTest(id string, path string, content string) {
a.assets = append(a.assets, &asset{
Key: id,
AssetPath: path,
resource: NewStringResource(content),
})
}
func hashFromHTTPHeader(url string) (*hashing.Hash, error) {
klog.Infof("Doing HTTP HEAD on %q", url)
response, err := http.Head(url)
if err != nil {
return nil, fmt.Errorf("error doing HEAD on %q: %v", url, err)
}
defer response.Body.Close()
etag := response.Header.Get("ETag")
etag = strings.TrimSpace(etag)
etag = strings.Trim(etag, "'\"")
if etag != "" {
if len(etag) == 32 {
// Likely md5
return hashing.HashAlgorithmMD5.FromString(etag)
}
}
return nil, fmt.Errorf("unable to determine hash from HTTP HEAD: %q", url)
}
// Add an asset into the store, in one of the recognized formats (see Assets in types package)
func (a *AssetStore) Add(id string) error {
if strings.HasPrefix(id, "http://") || strings.HasPrefix(id, "https://") {
return a.addURLs(strings.Split(id, ","), nil)
}
i := strings.Index(id, "@http://")
if i == -1 {
i = strings.Index(id, "@https://")
}
if i != -1 {
urls := strings.Split(id[i+1:], ",")
hash, err := hashing.FromString(id[:i])
if err != nil {
return err
}
return a.addURLs(urls, hash)
}
// TODO: local files!
return fmt.Errorf("unknown asset format: %q", id)
}
func (a *AssetStore) addURLs(urls []string, hash *hashing.Hash) error {
if len(urls) == 0 {
return fmt.Errorf("no urls were specified")
}
var err error
if hash == nil {
for _, url := range urls {
hash, err = hashFromHTTPHeader(url)
if err != nil {
klog.Warningf("unable to get hash from %q: %v", url, err)
continue
} else {
break
}
}
if err != nil {
return err
}
}
// We assume the first url is the "main" url, and download to that _name_, wherever we get it from
primaryURL := urls[0]
localFile := path.Join(a.cacheDir, hash.String()+"_"+utils.SanitizeString(primaryURL))
for _, url := range urls {
_, err = DownloadURL(url, localFile, hash)
if err != nil {
klog.Warningf("error downloading url %q: %v", url, err)
continue
} else {
break
}
}
if err != nil {
return err
}
key := path.Base(primaryURL)
assetPath := primaryURL
r := NewFileResource(localFile)
source := &Source{URL: primaryURL, Hash: hash}
asset := &asset{
Key: key,
AssetPath: assetPath,
resource: r,
source: source,
}
klog.V(2).Infof("added asset %q for %q", asset.Key, asset.resource)
a.assets = append(a.assets, asset)
// normalize filename suffix
file := strings.ToLower(assetPath)
// pickup both tar.gz and tgz files
if strings.HasSuffix(file, ".tar.gz") || strings.HasSuffix(file, ".tgz") {
err = a.addArchive(source, localFile)
if err != nil {
return err
}
}
return nil
}
//func (a *AssetStore) addFile(assetPath string, p string) error {
// r := NewFileResource(p)
// return a.addResource(assetPath, r)
//}
//func (a *AssetStore) addResource(assetPath string, r Resource) error {
// hash, err := HashForResource(r, HashAlgorithmSHA256)
// if err != nil {
// return err
// }
//
// localFile := path.Join(a.assetDir, hash + "_" + utils.SanitizeString(assetPath))
// hasHash, err := fileHasHash(localFile, hash)
// if err != nil {
// return err
// }
//
// if !hasHash {
// err = WriteFile(localFile, r, 0644, 0755)
// if err != nil {
// return err
// }
// }
//
// asset := &asset{
// Key: localFile,
// AssetPath: assetPath,
// resource: r,
// }
// klog.V(2).Infof("added asset %q for %q", asset.Key, asset.resource)
// a.assets = append(a.assets, asset)
//
// if strings.HasSuffix(assetPath, ".tar.gz") {
// err = a.addArchive(localFile)
// if err != nil {
// return err
// }
// }
//
// return nil
//}
func (a *AssetStore) addArchive(archiveSource *Source, archiveFile string) error {
extracted := path.Join(a.cacheDir, "extracted/"+path.Base(archiveFile))
if _, err := os.Stat(extracted); os.IsNotExist(err) {
// We extract to a temporary dir which we then rename so this is atomic
// (untarring can be slow, and we might crash / be interrupted half-way through)
extractedTemp := extracted + ".tmp-" + strconv.FormatInt(time.Now().UnixNano(), 10)
err := os.MkdirAll(extractedTemp, 0755)
if err != nil {
return fmt.Errorf("error creating directories %q: %v", path.Dir(extractedTemp), err)
}
args := []string{"tar", "zxf", archiveFile, "-C", extractedTemp}
klog.Infof("running extract command %s", args)
cmd := exec.Command(args[0], args[1:]...)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error expanding asset file %q %v: %s", archiveFile, err, string(output))
}
if err := os.Rename(extractedTemp, extracted); err != nil {
return fmt.Errorf("error renaming extracted temp dir %s -> %s: %v", extractedTemp, extracted, err)
}
}
localBase := extracted
assetBase := ""
walker := func(localPath string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error descending into path %q: %v", localPath, err)
}
if info.IsDir() {
return nil
}
relativePath, err := filepath.Rel(localBase, localPath)
if err != nil {
return fmt.Errorf("error finding relative path for %q: %v", localPath, err)
}
assetPath := path.Join(assetBase, relativePath)
key := info.Name()
r := NewFileResource(localPath)
asset := &asset{
Key: key,
AssetPath: assetPath,
resource: r,
source: &Source{Parent: archiveSource, ExtractFromArchive: assetPath},
}
klog.V(2).Infof("added asset %q for %q", asset.Key, asset.resource)
a.assets = append(a.assets, asset)
return nil
}
err := filepath.Walk(localBase, walker)
if err != nil {
return fmt.Errorf("error adding expanded asset files in %q: %v", extracted, err)
}
return nil
}