github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/image/pull.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package image
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/containerd/containerd"
    26  	"github.com/containerd/nerdctl/v2/pkg/api/types"
    27  	"github.com/containerd/nerdctl/v2/pkg/imgutil"
    28  	"github.com/containerd/nerdctl/v2/pkg/ipfs"
    29  	"github.com/containerd/nerdctl/v2/pkg/platformutil"
    30  	"github.com/containerd/nerdctl/v2/pkg/referenceutil"
    31  	"github.com/containerd/nerdctl/v2/pkg/signutil"
    32  	"github.com/containerd/nerdctl/v2/pkg/strutil"
    33  	v1 "github.com/opencontainers/image-spec/specs-go/v1"
    34  )
    35  
    36  // Pull pulls an image specified by `rawRef`.
    37  func Pull(ctx context.Context, client *containerd.Client, rawRef string, options types.ImagePullOptions) error {
    38  	ocispecPlatforms, err := platformutil.NewOCISpecPlatformSlice(options.AllPlatforms, options.Platform)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	unpack, err := strutil.ParseBoolOrAuto(options.Unpack)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	_, err = EnsureImage(ctx, client, rawRef, ocispecPlatforms, "always", unpack, options.Quiet, options)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  // EnsureImage pulls an image either from ipfs or from registry.
    57  func EnsureImage(ctx context.Context, client *containerd.Client, rawRef string, ocispecPlatforms []v1.Platform, pull string, unpack *bool, quiet bool, options types.ImagePullOptions) (*imgutil.EnsuredImage, error) {
    58  	var ensured *imgutil.EnsuredImage
    59  
    60  	if scheme, ref, err := referenceutil.ParseIPFSRefWithScheme(rawRef); err == nil {
    61  		if options.VerifyOptions.Provider != "none" {
    62  			return nil, errors.New("--verify flag is not supported on IPFS as of now")
    63  		}
    64  
    65  		var ipfsPath string
    66  		if options.IPFSAddress != "" {
    67  			dir, err := os.MkdirTemp("", "apidirtmp")
    68  			if err != nil {
    69  				return nil, err
    70  			}
    71  			defer os.RemoveAll(dir)
    72  			if err := os.WriteFile(filepath.Join(dir, "api"), []byte(options.IPFSAddress), 0600); err != nil {
    73  				return nil, err
    74  			}
    75  			ipfsPath = dir
    76  		}
    77  
    78  		ensured, err = ipfs.EnsureImage(ctx, client, options.Stdout, options.Stderr, options.GOptions.Snapshotter, scheme, ref,
    79  			pull, ocispecPlatforms, unpack, quiet, ipfsPath, options.RFlags)
    80  		if err != nil {
    81  			return nil, err
    82  		}
    83  		return ensured, nil
    84  	}
    85  
    86  	ref, err := signutil.Verify(ctx, rawRef, options.GOptions.HostsDir, options.GOptions.Experimental, options.VerifyOptions)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	ensured, err = imgutil.EnsureImage(ctx, client, options.Stdout, options.Stderr, options.GOptions.Snapshotter, ref,
    92  		pull, options.GOptions.InsecureRegistry, options.GOptions.HostsDir, ocispecPlatforms, unpack, quiet, options.RFlags)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return ensured, err
    97  }