2017-07-14 14:31:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/containers/storage"
|
2017-08-31 15:17:21 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libpod/images"
|
2017-07-14 14:31:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-14 14:31:02 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dockerArchive = "docker-archive:"
|
|
|
|
)
|
|
|
|
|
|
|
|
type saveOptions struct {
|
|
|
|
output string
|
|
|
|
quiet bool
|
|
|
|
images []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
saveFlags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "output, o",
|
|
|
|
Usage: "Write to a file, default is STDOUT",
|
|
|
|
Value: "/dev/stdout",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quiet, q",
|
|
|
|
Usage: "Suppress the output",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
saveDescription = "Save an image to docker-archive on the local machine"
|
|
|
|
saveCommand = cli.Command{
|
|
|
|
Name: "save",
|
|
|
|
Usage: "Save image to an archive",
|
|
|
|
Description: saveDescription,
|
|
|
|
Flags: saveFlags,
|
|
|
|
Action: saveCmd,
|
|
|
|
ArgsUsage: "",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// saveCmd saves the image to either docker-archive or oci
|
|
|
|
func saveCmd(c *cli.Context) error {
|
|
|
|
args := c.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
return errors.Errorf("need at least 1 argument")
|
|
|
|
}
|
|
|
|
|
2017-07-28 19:51:11 +00:00
|
|
|
config, err := getConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get config")
|
|
|
|
}
|
|
|
|
store, err := getStore(config)
|
2017-07-14 14:31:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := c.String("output")
|
|
|
|
quiet := c.Bool("quiet")
|
|
|
|
|
|
|
|
if output == "/dev/stdout" {
|
|
|
|
fi := os.Stdout
|
|
|
|
if logrus.IsTerminal(fi) {
|
|
|
|
return errors.Errorf("refusing to save to terminal. Use -o flag or redirect")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := saveOptions{
|
|
|
|
output: output,
|
|
|
|
quiet: quiet,
|
|
|
|
images: args,
|
|
|
|
}
|
|
|
|
|
|
|
|
return saveImage(store, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// saveImage pushes the image to docker-archive or oci by
|
2017-07-12 17:37:16 +00:00
|
|
|
// calling pushImage
|
2017-07-14 14:31:02 +00:00
|
|
|
func saveImage(store storage.Store, opts saveOptions) error {
|
|
|
|
dst := dockerArchive + opts.output
|
|
|
|
|
2017-08-31 15:17:21 +00:00
|
|
|
pushOpts := images.CopyOptions{
|
2017-07-14 14:31:02 +00:00
|
|
|
SignaturePolicyPath: "",
|
|
|
|
Store: store,
|
|
|
|
}
|
|
|
|
|
|
|
|
// only one image is supported for now
|
|
|
|
// future pull requests will fix this
|
|
|
|
for _, image := range opts.images {
|
|
|
|
dest := dst + ":" + image
|
2017-08-31 15:17:21 +00:00
|
|
|
if err := images.PushImage(image, dest, pushOpts); err != nil {
|
2017-07-14 14:31:02 +00:00
|
|
|
return errors.Wrapf(err, "unable to save %q", image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|