github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/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/coreos/rkt/common/apps"
    21  	"github.com/hashicorp/errwrap"
    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, app.ImType)
    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, imgType apps.AppImageType) (*types.Hash, error) {
    46  	ensureLogger(f.Debug)
    47  	if imgType == apps.AppImageGuess {
    48  		imgType = guessImageType(img)
    49  	}
    50  
    51  	if imgType == apps.AppImageHash {
    52  		return f.getHashFromStore(img)
    53  	}
    54  
    55  	// urls, names, paths have to be fetched, potentially remotely
    56  	ft := (*Fetcher)(f)
    57  	key, err := ft.FetchImage(img, asc, imgType)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	h, err := types.NewHash(key)
    62  	if err != nil {
    63  		// should never happen
    64  		log.PanicE("got an invalid hash from the store, looks like it is corrupted", err)
    65  	}
    66  	return h, nil
    67  }
    68  
    69  func (f *Finder) getHashFromStore(img string) (*types.Hash, error) {
    70  	h, err := types.NewHash(img)
    71  	if err != nil {
    72  		return nil, errwrap.Wrap(fmt.Errorf("%q is not a valid hash", img), err)
    73  	}
    74  	fullKey, err := f.S.ResolveKey(img)
    75  	if err != nil {
    76  		return nil, errwrap.Wrap(fmt.Errorf("could not resolve image %q", img), err)
    77  	}
    78  	h, err = types.NewHash(fullKey)
    79  	if err != nil {
    80  		// should never happen
    81  		log.PanicE("got an invalid hash from the store, looks like it is corrupted", err)
    82  	}
    83  	log.Printf("using image from the store with hash %s", h.String())
    84  	return h, nil
    85  }