github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/image_cmd_common.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 main
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  
    21  	"github.com/hashicorp/errwrap"
    22  	"github.com/rkt/rkt/store/imagestore"
    23  
    24  	"github.com/appc/spec/discovery"
    25  	"github.com/appc/spec/schema/types"
    26  )
    27  
    28  func getStoreKeyFromApp(s *imagestore.Store, img string) (string, error) {
    29  	app, err := discovery.NewAppFromString(img)
    30  	if err != nil {
    31  		return "", errwrap.Wrap(fmt.Errorf("cannot parse the image name %q", img), err)
    32  	}
    33  	labels, err := types.LabelsFromMap(app.Labels)
    34  	if err != nil {
    35  		return "", errwrap.Wrap(fmt.Errorf("invalid labels in the image %q", img), err)
    36  	}
    37  	key, err := s.GetACI(app.Name, labels)
    38  	if err != nil {
    39  		switch err.(type) {
    40  		case imagestore.ACINotFoundError:
    41  			return "", err
    42  		default:
    43  			return "", errwrap.Wrap(fmt.Errorf("cannot find image %q", img), err)
    44  		}
    45  	}
    46  	return key, nil
    47  }
    48  
    49  func getStoreKeyFromAppOrHash(s *imagestore.Store, input string) (string, error) {
    50  	var key string
    51  	if _, err := types.NewHash(input); err == nil {
    52  		key, err = s.ResolveKey(input)
    53  		if err != nil {
    54  			return "", errwrap.Wrap(errors.New("cannot resolve image ID"), err)
    55  		}
    56  	} else {
    57  		key, err = getStoreKeyFromApp(s, input)
    58  		if err != nil {
    59  			return "", errwrap.Wrap(errors.New("cannot find image"), err)
    60  		}
    61  	}
    62  	return key, nil
    63  }