github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/aci/render.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 aci
    16  
    17  import (
    18  	"errors"
    19  
    20  	"github.com/hashicorp/errwrap"
    21  	"github.com/rkt/rkt/pkg/user"
    22  
    23  	ptar "github.com/rkt/rkt/pkg/tar"
    24  
    25  	"github.com/appc/spec/pkg/acirenderer"
    26  	"github.com/appc/spec/schema/types"
    27  )
    28  
    29  // Given an imageID, start with the matching image available in the store,
    30  // build its dependency list and render it inside dir
    31  func RenderACIWithImageID(imageID types.Hash, dir string, ap acirenderer.ACIRegistry, uidRange *user.UidRange) error {
    32  	renderedACI, err := acirenderer.GetRenderedACIWithImageID(imageID, ap)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	return renderImage(renderedACI, dir, ap, uidRange)
    37  }
    38  
    39  // Given an image app name and optional labels, get the best matching image
    40  // available in the store, build its dependency list and render it inside dir
    41  func RenderACI(name types.ACIdentifier, labels types.Labels, dir string, ap acirenderer.ACIRegistry, uidRange *user.UidRange) error {
    42  	renderedACI, err := acirenderer.GetRenderedACI(name, labels, ap)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	return renderImage(renderedACI, dir, ap, uidRange)
    47  }
    48  
    49  // Given an already populated dependency list, it will extract, under the provided
    50  // directory, the rendered ACI
    51  func RenderACIFromList(imgs acirenderer.Images, dir string, ap acirenderer.ACIProvider, uidRange *user.UidRange) error {
    52  	renderedACI, err := acirenderer.GetRenderedACIFromList(imgs, ap)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return renderImage(renderedACI, dir, ap, uidRange)
    57  }
    58  
    59  // Given a RenderedACI, it will extract, under the provided directory, the
    60  // needed files from the right source ACI.
    61  // The manifest will be extracted from the upper ACI.
    62  // No file overwriting is done as it should usually be called
    63  // providing an empty directory.
    64  func renderImage(renderedACI acirenderer.RenderedACI, dir string, ap acirenderer.ACIProvider, uidRange *user.UidRange) error {
    65  	for _, ra := range renderedACI {
    66  		rs, err := ap.ReadStream(ra.Key)
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		// Overwrite is not needed. If a file needs to be overwritten then the renderedACI builder has a bug
    72  		if err := ptar.ExtractTar(rs, dir, false, uidRange, ra.FileMap); err != nil {
    73  			rs.Close()
    74  			return errwrap.Wrap(errors.New("error extracting ACI"), err)
    75  		}
    76  		rs.Close()
    77  	}
    78  
    79  	return nil
    80  }