github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/pull.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  	"os"
    20  
    21  	"github.com/containers/buildah"
    22  	"github.com/containers/buildah/define"
    23  	"github.com/containers/buildah/pkg/parse"
    24  	"github.com/containers/common/pkg/auth"
    25  	"github.com/containers/image/v5/types"
    26  	"github.com/pkg/errors"
    27  	"github.com/sealerio/sealer/pkg/define/options"
    28  )
    29  
    30  func (engine *Engine) Pull(opts *options.PullOptions) (string, error) {
    31  	if len(opts.Image) == 0 {
    32  		return "", errors.Errorf("an image name must be specified")
    33  	}
    34  
    35  	systemCxt := engine.SystemContext()
    36  	store := engine.ImageStore()
    37  	if err := auth.CheckAuthFile(systemCxt.AuthFilePath); err != nil {
    38  		return "", err
    39  	}
    40  
    41  	// we need to new a systemContext instead of taking the systemContext of engine,
    42  	// because pullOption does not export platform option
    43  	newSystemCxt := systemContext()
    44  	_os, arch, variant, err := parse.Platform(opts.Platform)
    45  	if err != nil {
    46  		return "", errors.Errorf("failed to init platform from %s: %v", opts.Platform, err)
    47  	}
    48  	newSystemCxt.OSChoice = _os
    49  	newSystemCxt.ArchitectureChoice = arch
    50  	newSystemCxt.VariantChoice = variant
    51  	newSystemCxt.OCIInsecureSkipTLSVerify = opts.SkipTLSVerify
    52  	newSystemCxt.DockerInsecureSkipTLSVerify = types.NewOptionalBool(opts.SkipTLSVerify)
    53  
    54  	policy, ok := define.PolicyMap[opts.PullPolicy]
    55  	if !ok {
    56  		return "", fmt.Errorf("unsupported pull policy %q", opts.PullPolicy)
    57  	}
    58  	options := buildah.PullOptions{
    59  		Store:         store,
    60  		SystemContext: newSystemCxt,
    61  		// consider export this option later
    62  		AllTags:      false,
    63  		ReportWriter: os.Stderr,
    64  		MaxRetries:   maxPullPushRetries,
    65  		RetryDelay:   pullPushRetryDelay,
    66  		PullPolicy:   policy,
    67  	}
    68  
    69  	if opts.Quiet {
    70  		options.ReportWriter = nil // Turns off logging output
    71  	}
    72  
    73  	id, err := buildah.Pull(getContext(), opts.Image, options)
    74  	if err != nil {
    75  		return "", err
    76  	}
    77  
    78  	return id, nil
    79  }