This commit is contained in:
Daniel J Walsh 2017-09-11 13:42:16 +00:00 committed by GitHub
commit 140627993e
5 changed files with 117 additions and 108 deletions

View file

@ -2,22 +2,14 @@ package main
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"io/ioutil"
"github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/libpod/images" "github.com/kubernetes-incubator/cri-o/libpod/images"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
type loadOptions struct {
input string
quiet bool
image string
}
var ( var (
loadFlags = []cli.Flag{ loadFlags = []cli.Flag{
cli.StringFlag{ cli.StringFlag{
@ -44,14 +36,6 @@ var (
// loadCmd gets the image/file to be loaded from the command line // loadCmd gets the image/file to be loaded from the command line
// and calls loadImage to load the image to containers-storage // and calls loadImage to load the image to containers-storage
func loadCmd(c *cli.Context) error { func loadCmd(c *cli.Context) error {
config, err := getConfig(c)
if err != nil {
return errors.Wrapf(err, "could not get config")
}
store, err := getStore(config)
if err != nil {
return err
}
args := c.Args() args := c.Args()
var image string var image string
@ -62,6 +46,11 @@ func loadCmd(c *cli.Context) error {
return errors.New("too many arguments. Requires exactly 1") return errors.New("too many arguments. Requires exactly 1")
} }
runtime, err := getRuntime(c)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
input := c.String("input") input := c.String("input")
if input == "/dev/stdin" { if input == "/dev/stdin" {
@ -93,33 +82,21 @@ func loadCmd(c *cli.Context) error {
} }
} }
opts := loadOptions{ var output io.Writer
input: input, if !c.Bool("quiet") {
quiet: c.Bool("quiet"), output = os.Stdout
image: image,
} }
src := images.DockerArchive + ":" + input
return loadImage(store, opts) if err := runtime.PullImage(src, false, output); err != nil {
} src = images.OCIArchive + ":" + input
// loadImage loads the image from docker-archive or oci to containers-storage
// using the pullImage function
func loadImage(store storage.Store, opts loadOptions) error {
loadOpts := images.CopyOptions{
Quiet: opts.quiet,
Store: store,
}
src := images.DockerArchive + ":" + opts.input
if err := images.PullImage(src, false, loadOpts); err != nil {
src = images.OCIArchive + ":" + opts.input
// generate full src name with specified image:tag // generate full src name with specified image:tag
if opts.image != "" { if image != "" {
src = src + ":" + opts.image src = src + ":" + image
} }
if err := images.PullImage(src, false, loadOpts); err != nil { if err := runtime.PullImage(src, false, output); err != nil {
return errors.Wrapf(err, "error pulling from %q", opts.input) return errors.Wrapf(err, "error pulling %q", src)
} }
} }
return nil return nil
} }

View file

@ -2,12 +2,13 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"github.com/containers/image/types" "github.com/containers/image/types"
"github.com/containers/storage/pkg/archive" "github.com/containers/storage/pkg/archive"
"github.com/kubernetes-incubator/cri-o/libpod"
"github.com/kubernetes-incubator/cri-o/libpod/common" "github.com/kubernetes-incubator/cri-o/libpod/common"
"github.com/kubernetes-incubator/cri-o/libpod/images"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
@ -70,7 +71,6 @@ func pushCmd(c *cli.Context) error {
srcName := c.Args().Get(0) srcName := c.Args().Get(0)
destName := c.Args().Get(1) destName := c.Args().Get(1)
signaturePolicy := c.String("signature-policy")
registryCredsString := c.String("creds") registryCredsString := c.String("creds")
certPath := c.String("cert-dir") certPath := c.String("cert-dir")
skipVerify := !c.BoolT("tls-verify") skipVerify := !c.BoolT("tls-verify")
@ -94,19 +94,19 @@ func pushCmd(c *cli.Context) error {
registryCreds = creds registryCreds = creds
} }
config, err := getConfig(c) runtime, err := getRuntime(c)
if err != nil { if err != nil {
return errors.Wrapf(err, "Could not get config") return errors.Wrapf(err, "could not create runtime")
}
store, err := getStore(config)
if err != nil {
return err
} }
options := images.CopyOptions{ var writer io.Writer
if !c.Bool("quiet") {
writer = os.Stdout
}
options := libpod.CopyOptions{
Compression: archive.Uncompressed, Compression: archive.Uncompressed,
SignaturePolicyPath: signaturePolicy, SignaturePolicyPath: c.String("signature-policy"),
Store: store,
DockerRegistryOptions: common.DockerRegistryOptions{ DockerRegistryOptions: common.DockerRegistryOptions{
DockerRegistryCreds: registryCreds, DockerRegistryCreds: registryCreds,
DockerCertPath: certPath, DockerCertPath: certPath,
@ -117,8 +117,6 @@ func pushCmd(c *cli.Context) error {
SignBy: signBy, SignBy: signBy,
}, },
} }
if !c.Bool("quiet") {
options.ReportWriter = os.Stderr return runtime.PushImage(srcName, destName, options, writer)
}
return images.PushImage(srcName, destName, options)
} }

View file

@ -1,22 +1,16 @@
package main package main
import ( import (
"io"
"os" "os"
"github.com/containers/storage" "github.com/kubernetes-incubator/cri-o/libpod"
"github.com/kubernetes-incubator/cri-o/libpod/images" "github.com/kubernetes-incubator/cri-o/libpod/images"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
type saveOptions struct {
output string
quiet bool
format string
images []string
}
var ( var (
saveFlags = []cli.Flag{ saveFlags = []cli.Flag{
cli.StringFlag{ cli.StringFlag{
@ -54,17 +48,16 @@ func saveCmd(c *cli.Context) error {
return errors.Errorf("need at least 1 argument") return errors.Errorf("need at least 1 argument")
} }
config, err := getConfig(c) runtime, err := getRuntime(c)
if err != nil { if err != nil {
return errors.Wrapf(err, "could not get config") return errors.Wrapf(err, "could not create runtime")
} }
store, err := getStore(config) var writer io.Writer
if err != nil { if !c.Bool("quiet") {
return err writer = os.Stdout
} }
output := c.String("output") output := c.String("output")
if output == "/dev/stdout" { if output == "/dev/stdout" {
fi := os.Stdout fi := os.Stdout
if logrus.IsTerminal(fi) { if logrus.IsTerminal(fi) {
@ -72,41 +65,27 @@ func saveCmd(c *cli.Context) error {
} }
} }
opts := saveOptions{
output: output,
quiet: c.Bool("quiet"),
format: c.String("format"),
images: args,
}
return saveImage(store, opts)
}
// saveImage pushes the image to docker-archive or oci by
// calling pushImage
func saveImage(store storage.Store, opts saveOptions) error {
var dst string var dst string
switch opts.format { switch c.String("format") {
case images.OCIArchive: case images.OCIArchive:
dst = images.OCIArchive + ":" + opts.output dst = images.OCIArchive + ":" + output
case images.DockerArchive: case images.DockerArchive:
fallthrough fallthrough
case "": case "":
dst = images.DockerArchive + ":" + opts.output dst = images.DockerArchive + ":" + output
default: default:
return errors.Errorf("unknown format option %q", opts.format) return errors.Errorf("unknown format option %q", c.String("format"))
} }
saveOpts := images.CopyOptions{ saveOpts := libpod.CopyOptions{
SignaturePolicyPath: "", SignaturePolicyPath: "",
Store: store,
} }
// only one image is supported for now // only one image is supported for now
// future pull requests will fix this // future pull requests will fix this
for _, image := range opts.images { for _, image := range args {
dest := dst + ":" + image dest := dst + ":" + image
if err := images.PushImage(image, dest, saveOpts); err != nil { if err := runtime.PushImage(image, dest, saveOpts, writer); err != nil {
return errors.Wrapf(err, "unable to save %q", image) return errors.Wrapf(err, "unable to save %q", image)
} }
} }

View file

@ -3,7 +3,7 @@ package main
import ( import (
"github.com/containers/image/docker/reference" "github.com/containers/image/docker/reference"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/libpod/images" "github.com/kubernetes-incubator/cri-o/libpod"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -24,37 +24,34 @@ func tagCmd(c *cli.Context) error {
if len(args) < 2 { if len(args) < 2 {
return errors.Errorf("image name and at least one new name must be specified") return errors.Errorf("image name and at least one new name must be specified")
} }
config, err := getConfig(c) runtime, err := getRuntime(c)
if err != nil { if err != nil {
return errors.Wrapf(err, "Could not get config") return errors.Wrapf(err, "could not create runtime")
} }
store, err := getStore(config) img, err := runtime.GetImage(args[0])
if err != nil {
return err
}
img, err := images.FindImage(store, args[0])
if err != nil { if err != nil {
return err return err
} }
if img == nil { if img == nil {
return errors.New("null image") return errors.New("null image")
} }
err = addImageNames(store, img, args[1:]) err = addImageNames(runtime, img, args[1:])
if err != nil { if err != nil {
return errors.Wrapf(err, "error adding names %v to image %q", args[1:], args[0]) return errors.Wrapf(err, "error adding names %v to image %q", args[1:], args[0])
} }
return nil return nil
} }
func addImageNames(store storage.Store, image *storage.Image, addNames []string) error { func addImageNames(runtime *libpod.Runtime, image *storage.Image, addNames []string) error {
// Add tags to the names if applicable // Add tags to the names if applicable
names, err := expandedTags(addNames) names, err := expandedTags(addNames)
if err != nil { if err != nil {
return err return err
} }
err = store.SetNames(image.ID, append(image.Names, names...)) for _, name := range names {
if err != nil { if err := runtime.TagImage(image, name); err != nil {
return errors.Wrapf(err, "error adding names (%v) to image %q", names, image.ID) return errors.Wrapf(err, "error adding name (%v) to image %q", name, image.ID)
}
} }
return nil return nil
} }

View file

@ -11,6 +11,7 @@ import (
"github.com/containers/image/signature" "github.com/containers/image/signature"
is "github.com/containers/image/storage" is "github.com/containers/image/storage"
"github.com/containers/image/transports/alltransports" "github.com/containers/image/transports/alltransports"
"github.com/containers/image/types"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/containers/storage/pkg/archive" "github.com/containers/storage/pkg/archive"
"github.com/kubernetes-incubator/cri-o/libpod/common" "github.com/kubernetes-incubator/cri-o/libpod/common"
@ -41,6 +42,9 @@ type CopyOptions struct {
// strip or add signatures to the image when pushing (uploading) the // strip or add signatures to the image when pushing (uploading) the
// image to a registry. // image to a registry.
common.SigningOptions common.SigningOptions
// SigningPolicyPath this points to a alternative signature policy file, used mainly for testing
SignaturePolicyPath string
} }
// Image API // Image API
@ -153,7 +157,7 @@ func (r *Runtime) PushImage(source string, destination string, options CopyOptio
defer policyContext.Destroy() defer policyContext.Destroy()
// Look up the image name and its layer, then build the imagePushData from // Look up the image name and its layer, then build the imagePushData from
// the image // the image
img, err := images.FindImage(r.store, source) img, err := r.GetImage(source)
if err != nil { if err != nil {
return errors.Wrapf(err, "error locating image %q for importing settings", source) return errors.Wrapf(err, "error locating image %q for importing settings", source)
} }
@ -183,25 +187,79 @@ func (r *Runtime) PushImage(source string, destination string, options CopyOptio
// TagImage adds a tag to the given image // TagImage adds a tag to the given image
func (r *Runtime) TagImage(image *storage.Image, tag string) error { func (r *Runtime) TagImage(image *storage.Image, tag string) error {
return ctr.ErrNotImplemented tags, err := r.store.Names(image.ID)
if err != nil {
return err
}
for _, key := range tags {
if key == tag {
return nil
}
}
tags = append(tags, tag)
return r.store.SetNames(image.ID, tags)
} }
// UntagImage removes a tag from the given image // UntagImage removes a tag from the given image
func (r *Runtime) UntagImage(image *storage.Image, tag string) error { func (r *Runtime) UntagImage(image *storage.Image, tag string) error {
return ctr.ErrNotImplemented tags, err := r.store.Names(image.ID)
if err != nil {
return err
}
for i, key := range tags {
if key == tag {
tags[i] = tags[len(tags)-1]
tags = tags[:len(tags)-1]
break
}
}
return r.store.SetNames(image.ID, tags)
} }
// RemoveImage deletes an image from local storage // RemoveImage deletes an image from local storage
// Images being used by running containers cannot be removed // Images being used by running containers cannot be removed
func (r *Runtime) RemoveImage(image *storage.Image) error { func (r *Runtime) RemoveImage(image *storage.Image) error {
return ctr.ErrNotImplemented _, err := r.store.DeleteImage(image.ID, false)
return err
} }
// GetImage retrieves an image matching the given name or hash from system // GetImage retrieves an image matching the given name or hash from system
// storage // storage
// If no matching image can be found, an error is returned // If no matching image can be found, an error is returned
func (r *Runtime) GetImage(image string) (*storage.Image, error) { func (r *Runtime) GetImage(image string) (*storage.Image, error) {
return nil, ctr.ErrNotImplemented var img *storage.Image
ref, err := is.Transport.ParseStoreReference(r.store, image)
if err == nil {
img, err = is.Transport.GetStoreImage(r.store, ref)
}
if err != nil {
img2, err2 := r.store.Image(image)
if err2 != nil {
if ref == nil {
return nil, errors.Wrapf(err, "error parsing reference to image %q", image)
}
return nil, errors.Wrapf(err, "unable to locate image %q", image)
}
img = img2
}
return img, nil
}
// GetImageRef searches for and returns a new types.Image matching the given name or ID in the given store.
func (r *Runtime) GetImageRef(image string) (types.Image, error) {
img, err := r.GetImage(image)
if err != nil {
return nil, errors.Wrapf(err, "unable to locate image %q", image)
}
ref, err := is.Transport.ParseStoreReference(r.store, "@"+img.ID)
if err != nil {
return nil, errors.Wrapf(err, "error parsing reference to image %q", img.ID)
}
imgRef, err := ref.NewImage(nil)
if err != nil {
return nil, errors.Wrapf(err, "error reading image %q", img.ID)
}
return imgRef, nil
} }
// GetImages retrieves all images present in storage // GetImages retrieves all images present in storage