github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/copy.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     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 buildah
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/sirupsen/logrus"
    21  
    22  	"github.com/sealerio/sealer/pkg/define/options"
    23  
    24  	"github.com/containers/buildah"
    25  
    26  	"time"
    27  
    28  	buildahcli "github.com/containers/buildah/pkg/cli"
    29  	"github.com/containers/buildah/pkg/parse"
    30  	"github.com/pkg/errors"
    31  )
    32  
    33  // Copy will copy files in the host to the container.
    34  // this is a basic ability, but not used in sealer now.
    35  func (engine *Engine) Copy(opts *options.CopyOptions) error {
    36  	if len(opts.Container) == 0 {
    37  		return errors.Errorf("container ID must be specified")
    38  	}
    39  	if len(opts.SourcesRel2CxtDir) == 0 {
    40  		return errors.Errorf("src must be specified")
    41  	}
    42  	if len(opts.DestinationInContainer) == 0 {
    43  		return errors.Errorf("destination in container must be specified")
    44  	}
    45  
    46  	name := opts.Container
    47  	dest := opts.DestinationInContainer
    48  	store := engine.ImageStore()
    49  
    50  	var idMappingOptions *buildah.IDMappingOptions
    51  	contextdir := opts.ContextDir
    52  	if opts.IgnoreFile != "" && contextdir == "" {
    53  		return errors.Errorf("--ignorefile option requires that you specify a context dir using --contextdir")
    54  	}
    55  
    56  	builder, err := OpenBuilder(getContext(), store, name)
    57  	if err != nil {
    58  		return errors.Wrapf(err, "error reading build container %q", name)
    59  	}
    60  
    61  	builder.ContentDigester.Restart()
    62  
    63  	options := buildah.AddAndCopyOptions{
    64  		ContextDir:       contextdir,
    65  		IDMappingOptions: idMappingOptions,
    66  	}
    67  	if opts.ContextDir != "" {
    68  		var excludes []string
    69  
    70  		excludes, options.IgnoreFile, err = parse.ContainerIgnoreFile(options.ContextDir, opts.IgnoreFile)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		options.Excludes = excludes
    75  	}
    76  
    77  	err = builder.Add(dest, false, options, opts.SourcesRel2CxtDir...)
    78  	if err != nil {
    79  		return errors.Wrapf(err, "error adding content to container %q", builder.Container)
    80  	}
    81  
    82  	contentType, digest := builder.ContentDigester.Digest()
    83  	if !opts.Quiet {
    84  		logrus.Infof("%s", digest.Hex())
    85  	}
    86  	if contentType != "" {
    87  		contentType = contentType + ":"
    88  	}
    89  	conditionallyAddHistory(builder, opts, "/bin/sh -c #(nop) %s %s%s", "COPY", contentType, digest.Hex())
    90  	return builder.Save()
    91  }
    92  
    93  func conditionallyAddHistory(builder *buildah.Builder, opts *options.CopyOptions, createdByFmt string, args ...interface{}) {
    94  	if opts.AddHistory || buildahcli.DefaultHistory() {
    95  		now := time.Now().UTC()
    96  		created := &now
    97  		createdBy := fmt.Sprintf(createdByFmt, args...)
    98  		builder.AddPrependedEmptyLayer(created, createdBy, "", "")
    99  	}
   100  }