This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 886
/
finder.go
87 lines (76 loc) · 2.31 KB
/
finder.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
// Copyright 2015 The rkt 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 image
import (
"fmt"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/common/apps"
"github.com/appc/spec/schema/types"
)
// Finder will try to get images from the store. If not found, it will
// try to fetch them.
type Finder action
// FindImages uses FindImage to attain a list of image hashes
func (f *Finder) FindImages(al *apps.Apps) error {
return al.Walk(func(app *apps.App) error {
h, err := f.FindImage(app.Image, app.Asc)
if err != nil {
return err
}
app.ImageID = *h
return nil
})
}
// FindImage tries to get a hash of a passed image, ideally from
// store. Otherwise this might involve fetching it from remote with
// the Fetcher.
func (f *Finder) FindImage(img string, asc string) (*types.Hash, error) {
ensureLogger(f.Debug)
// Check if it's an hash
if _, err := types.NewHash(img); err == nil {
h, err := f.getHashFromStore(img)
if err != nil {
return nil, err
}
return h, nil
}
d, err := DistFromImageString(img)
if err != nil {
return nil, err
}
// urls, names, paths have to be fetched, potentially remotely
ft := (*Fetcher)(f)
h, err := ft.FetchImage(d, img, asc)
if err != nil {
return nil, err
}
return h, nil
}
func (f *Finder) getHashFromStore(img string) (*types.Hash, error) {
h, err := types.NewHash(img)
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("%q is not a valid hash", img), err)
}
fullKey, err := f.S.ResolveKey(img)
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("could not resolve image %q", img), err)
}
h, err = types.NewHash(fullKey)
if err != nil {
// should never happen
log.PanicE("got an invalid hash from the store, looks like it is corrupted", err)
}
diag.Printf("using image from the store with hash %s", h.String())
return h, nil
}