Switch kpod load/push/save to use libpod runtime

Since this is the last use of libpod/images/copy.go,
removing that code

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh 2017-09-09 11:14:40 +00:00 committed by umohnani8
parent 8949e669c9
commit e70802863e
6 changed files with 162 additions and 105 deletions

View file

@ -1,13 +1,16 @@
package libpod
import (
"fmt"
"io"
"strings"
"syscall"
cp "github.com/containers/image/copy"
dockerarchive "github.com/containers/image/docker/archive"
"github.com/containers/image/docker/tarfile"
"github.com/containers/image/manifest"
ociarchive "github.com/containers/image/oci/archive"
"github.com/containers/image/signature"
is "github.com/containers/image/storage"
"github.com/containers/image/transports/alltransports"
@ -28,6 +31,15 @@ const (
DefaultRegistry = "docker://"
)
var (
// DockerArchive is the transport we prepend to an image name
// when saving to docker-archive
DockerArchive = dockerarchive.Transport.Name()
// OCIArchive is the transport we prepend to an image name
// when saving to oci-archive
OCIArchive = ociarchive.Transport.Name()
)
// CopyOptions contains the options given when pushing or pulling images
type CopyOptions struct {
// Compression specifies the type of compression which is applied to
@ -59,12 +71,25 @@ type ImageFilter func(*storage.Image) bool
// pulled. If allTags is true, all tags for the requested image will be pulled.
// Signature validation will be performed if the Runtime has been appropriately
// configured
func (r *Runtime) PullImage(imgName string, allTags bool, reportWriter io.Writer) error {
func (r *Runtime) PullImage(imgName string, allTags bool, signaturePolicyPath string, reportWriter io.Writer) error {
r.lock.Lock()
defer r.lock.Unlock()
if !r.valid {
return fmt.Errorf("runtime is not valid")
}
// PullImage copies the image from the source to the destination
var (
images []string
)
if signaturePolicyPath == "" {
signaturePolicyPath = r.config.SignaturePolicyPath
}
sc := common.GetSystemContext(signaturePolicyPath)
srcRef, err := alltransports.ParseImageName(imgName)
if err != nil {
defaultName := DefaultRegistry + imgName
@ -76,10 +101,11 @@ func (r *Runtime) PullImage(imgName string, allTags bool, reportWriter io.Writer
}
splitArr := strings.Split(imgName, ":")
archFile := splitArr[len(splitArr)-1]
// supports pulling from docker-archive, oci, and registries
if splitArr[0] == "docker-archive" {
tarSource := tarfile.NewSource(splitArr[len(splitArr)-1])
if srcRef.Transport().Name() == DockerArchive {
tarSource := tarfile.NewSource(archFile)
manifest, err := tarSource.LoadTarManifest()
if err != nil {
return errors.Errorf("error retrieving manifest.json: %v", err)
@ -91,7 +117,7 @@ func (r *Runtime) PullImage(imgName string, allTags bool, reportWriter io.Writer
} else {
// create an image object and use the hex value of the digest as the image ID
// for parsing the store reference
newImg, err := srcRef.NewImage(r.imageContext)
newImg, err := srcRef.NewImage(sc)
if err != nil {
return err
}
@ -104,9 +130,17 @@ func (r *Runtime) PullImage(imgName string, allTags bool, reportWriter io.Writer
}
}
}
} else if splitArr[0] == "oci" {
// needs to be implemented in future
return errors.Errorf("oci not supported")
} else if srcRef.Transport().Name() == OCIArchive {
// retrieve the manifest from index.json to access the image name
manifest, err := ociarchive.LoadManifestDescriptor(srcRef)
if err != nil {
return errors.Wrapf(err, "error loading manifest for %q", srcRef)
}
if manifest.Annotations == nil || manifest.Annotations["org.opencontainers.image.ref.name"] == "" {
return errors.Errorf("error, archive doesn't have a name annotation. Cannot store image with no name")
}
images = append(images, manifest.Annotations["org.opencontainers.image.ref.name"])
} else {
images = append(images, imgName)
}
@ -122,10 +156,13 @@ func (r *Runtime) PullImage(imgName string, allTags bool, reportWriter io.Writer
}
defer policyContext.Destroy()
copyOptions := common.GetCopyOptions(reportWriter, "", nil, nil, common.SigningOptions{})
copyOptions := common.GetCopyOptions(reportWriter, signaturePolicyPath, nil, nil, common.SigningOptions{})
for _, image := range images {
destRef, err := is.Transport.ParseStoreReference(r.store, srcRef.DockerReference().String())
reference := image
if srcRef.DockerReference() != nil {
reference = srcRef.DockerReference().String()
}
destRef, err := is.Transport.ParseStoreReference(r.store, reference)
if err != nil {
return errors.Errorf("error parsing dest reference name: %v", err)
}
@ -138,6 +175,13 @@ func (r *Runtime) PullImage(imgName string, allTags bool, reportWriter io.Writer
// PushImage pushes the given image to a location described by the given path
func (r *Runtime) PushImage(source string, destination string, options CopyOptions, reportWriter io.Writer) error {
r.lock.Lock()
defer r.lock.Unlock()
if !r.valid {
return fmt.Errorf("runtime is not valid")
}
// PushImage pushes the src image to the destination
//func PushImage(source, destination string, options CopyOptions) error {
if source == "" || destination == "" {
@ -150,14 +194,19 @@ func (r *Runtime) PushImage(source string, destination string, options CopyOptio
return errors.Wrapf(err, "error getting destination imageReference for %q", destination)
}
policyContext, err := common.GetPolicyContext(r.GetConfig().SignaturePolicyPath)
signaturePolicyPath := r.config.SignaturePolicyPath
if options.SignaturePolicyPath != "" {
signaturePolicyPath = options.SignaturePolicyPath
}
policyContext, err := common.GetPolicyContext(signaturePolicyPath)
if err != nil {
return errors.Wrapf(err, "Could not get default policy context for signature policy path %q", r.GetConfig().SignaturePolicyPath)
return errors.Wrapf(err, "Could not get default policy context for signature policy path %q", signaturePolicyPath)
}
defer policyContext.Destroy()
// Look up the image name and its layer, then build the imagePushData from
// the image
img, err := r.GetImage(source)
img, err := r.getImage(source)
if err != nil {
return errors.Wrapf(err, "error locating image %q for importing settings", source)
}
@ -175,7 +224,7 @@ func (r *Runtime) PushImage(source string, destination string, options CopyOptio
return errors.Wrapf(err, "error copying layers and metadata")
}
copyOptions := common.GetCopyOptions(reportWriter, r.GetConfig().SignaturePolicyPath, nil, &options.DockerRegistryOptions, options.SigningOptions)
copyOptions := common.GetCopyOptions(reportWriter, signaturePolicyPath, nil, &options.DockerRegistryOptions, options.SigningOptions)
// Copy the image to the remote destination
err = cp.Image(policyContext, dest, src, copyOptions)
@ -187,6 +236,13 @@ func (r *Runtime) PushImage(source string, destination string, options CopyOptio
// TagImage adds a tag to the given image
func (r *Runtime) TagImage(image *storage.Image, tag string) error {
r.lock.Lock()
defer r.lock.Unlock()
if !r.valid {
return fmt.Errorf("runtime is not valid")
}
tags, err := r.store.Names(image.ID)
if err != nil {
return err
@ -202,6 +258,13 @@ func (r *Runtime) TagImage(image *storage.Image, tag string) error {
// UntagImage removes a tag from the given image
func (r *Runtime) UntagImage(image *storage.Image, tag string) error {
r.lock.Lock()
defer r.lock.Unlock()
if !r.valid {
return fmt.Errorf("runtime is not valid")
}
tags, err := r.store.Names(image.ID)
if err != nil {
return err
@ -219,6 +282,13 @@ func (r *Runtime) UntagImage(image *storage.Image, tag string) error {
// RemoveImage deletes an image from local storage
// Images being used by running containers cannot be removed
func (r *Runtime) RemoveImage(image *storage.Image) error {
r.lock.Lock()
defer r.lock.Unlock()
if !r.valid {
return fmt.Errorf("runtime is not valid")
}
_, err := r.store.DeleteImage(image.ID, false)
return err
}
@ -227,6 +297,16 @@ func (r *Runtime) RemoveImage(image *storage.Image) error {
// storage
// If no matching image can be found, an error is returned
func (r *Runtime) GetImage(image string) (*storage.Image, error) {
r.lock.Lock()
defer r.lock.Unlock()
if !r.valid {
return nil, fmt.Errorf("runtime is not valid")
}
return r.getImage(image)
}
func (r *Runtime) getImage(image string) (*storage.Image, error) {
var img *storage.Image
ref, err := is.Transport.ParseStoreReference(r.store, image)
if err == nil {
@ -247,7 +327,14 @@ func (r *Runtime) GetImage(image string) (*storage.Image, error) {
// 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)
r.lock.Lock()
defer r.lock.Unlock()
if !r.valid {
return nil, fmt.Errorf("runtime is not valid")
}
img, err := r.getImage(image)
if err != nil {
return nil, errors.Wrapf(err, "unable to locate image %q", image)
}