github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/image/finder.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package image 16 17 import ( 18 "fmt" 19 20 "github.com/hashicorp/errwrap" 21 "github.com/rkt/rkt/common/apps" 22 23 "github.com/appc/spec/schema/types" 24 ) 25 26 // Finder will try to get images from the store. If not found, it will 27 // try to fetch them. 28 type Finder action 29 30 // FindImages uses FindImage to attain a list of image hashes 31 func (f *Finder) FindImages(al *apps.Apps) error { 32 return al.Walk(func(app *apps.App) error { 33 h, err := f.FindImage(app.Image, app.Asc) 34 if err != nil { 35 return err 36 } 37 app.ImageID = *h 38 return nil 39 }) 40 } 41 42 // FindImage tries to get a hash of a passed image, ideally from 43 // store. Otherwise this might involve fetching it from remote with 44 // the Fetcher. 45 func (f *Finder) FindImage(img string, asc string) (*types.Hash, error) { 46 ensureLogger(f.Debug) 47 48 // Check if it's an hash 49 if _, err := types.NewHash(img); err == nil { 50 h, err := f.getHashFromStore(img) 51 if err != nil { 52 return nil, err 53 } 54 return h, nil 55 } 56 57 d, err := DistFromImageString(img) 58 if err != nil { 59 return nil, err 60 } 61 62 // urls, names, paths have to be fetched, potentially remotely 63 ft := (*Fetcher)(f) 64 h, err := ft.FetchImage(d, img, asc) 65 if err != nil { 66 return nil, err 67 } 68 return h, nil 69 } 70 71 func (f *Finder) getHashFromStore(img string) (*types.Hash, error) { 72 h, err := types.NewHash(img) 73 if err != nil { 74 return nil, errwrap.Wrap(fmt.Errorf("%q is not a valid hash", img), err) 75 } 76 fullKey, err := f.S.ResolveKey(img) 77 if err != nil { 78 return nil, errwrap.Wrap(fmt.Errorf("could not resolve image %q", img), err) 79 } 80 h, err = types.NewHash(fullKey) 81 if err != nil { 82 // should never happen 83 log.PanicE("got an invalid hash from the store, looks like it is corrupted", err) 84 } 85 diag.Printf("using image from the store with hash %s", h.String()) 86 return h, nil 87 }