github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/cmd/ctr/commands/images/export.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 images
    18  
    19  import (
    20  	"io"
    21  	"os"
    22  
    23  	"github.com/containerd/containerd/cmd/ctr/commands"
    24  	"github.com/containerd/containerd/images/archive"
    25  	"github.com/containerd/containerd/platforms"
    26  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    27  	"github.com/pkg/errors"
    28  	"github.com/urfave/cli"
    29  )
    30  
    31  var exportCommand = cli.Command{
    32  	Name:      "export",
    33  	Usage:     "export images",
    34  	ArgsUsage: "[flags] <out> <image> ...",
    35  	Description: `Export images to an OCI tar archive.
    36  
    37  Tar output is formatted as an OCI archive, a Docker manifest is provided for the platform.
    38  Use '--skip-manifest-json' to avoid including the Docker manifest.json file.
    39  Use '--platform' to define the output platform.
    40  When '--all-platforms' is given all images in a manifest list must be available.
    41  `,
    42  	Flags: []cli.Flag{
    43  		cli.BoolFlag{
    44  			Name:  "skip-manifest-json",
    45  			Usage: "do not add Docker compatible manifest.json to archive",
    46  		},
    47  		cli.StringSliceFlag{
    48  			Name:  "platform",
    49  			Usage: "Pull content from a specific platform",
    50  			Value: &cli.StringSlice{},
    51  		},
    52  		cli.BoolFlag{
    53  			Name:  "all-platforms",
    54  			Usage: "exports content from all platforms",
    55  		},
    56  	},
    57  	Action: func(context *cli.Context) error {
    58  		var (
    59  			out        = context.Args().First()
    60  			images     = context.Args().Tail()
    61  			exportOpts = []archive.ExportOpt{}
    62  		)
    63  		if out == "" || len(images) == 0 {
    64  			return errors.New("please provide both an output filename and an image reference to export")
    65  		}
    66  
    67  		if pss := context.StringSlice("platform"); len(pss) > 0 {
    68  			var all []ocispec.Platform
    69  			for _, ps := range pss {
    70  				p, err := platforms.Parse(ps)
    71  				if err != nil {
    72  					return errors.Wrapf(err, "invalid platform %q", ps)
    73  				}
    74  				all = append(all, p)
    75  			}
    76  			exportOpts = append(exportOpts, archive.WithPlatform(platforms.Ordered(all...)))
    77  		} else {
    78  			exportOpts = append(exportOpts, archive.WithPlatform(platforms.Default()))
    79  		}
    80  
    81  		if context.Bool("all-platforms") {
    82  			exportOpts = append(exportOpts, archive.WithAllPlatforms())
    83  		}
    84  
    85  		if context.Bool("skip-manifest-json") {
    86  			exportOpts = append(exportOpts, archive.WithSkipDockerManifest())
    87  		}
    88  
    89  		client, ctx, cancel, err := commands.NewClient(context)
    90  		if err != nil {
    91  			return err
    92  		}
    93  		defer cancel()
    94  
    95  		is := client.ImageService()
    96  		for _, img := range images {
    97  			exportOpts = append(exportOpts, archive.WithImage(is, img))
    98  		}
    99  
   100  		var w io.WriteCloser
   101  		if out == "-" {
   102  			w = os.Stdout
   103  		} else {
   104  			w, err = os.Create(out)
   105  			if err != nil {
   106  				return nil
   107  			}
   108  		}
   109  		defer w.Close()
   110  
   111  		return client.Export(ctx, w, exportOpts...)
   112  	},
   113  }