2015-02-11 02:41:09 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-12 01:49:49 +01:00
|
|
|
"github.com/docker/distribution"
|
2015-02-11 02:41:09 +01:00
|
|
|
ctxu "github.com/docker/distribution/context"
|
|
|
|
"github.com/docker/distribution/digest"
|
2015-02-11 03:14:23 +01:00
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
2015-02-11 02:41:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// layerUploadController is used to control the various aspects of resumable
|
|
|
|
// layer upload. It implements the LayerUpload interface.
|
|
|
|
type layerUploadController struct {
|
|
|
|
layerStore *layerStore
|
|
|
|
|
|
|
|
uuid string
|
|
|
|
startedAt time.Time
|
|
|
|
|
2015-03-03 23:47:07 +01:00
|
|
|
// implementes io.WriteSeeker, io.ReaderFrom and io.Closer to satisy
|
|
|
|
// LayerUpload Interface
|
|
|
|
bufferedFileWriter
|
2015-02-11 02:41:09 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
var _ distribution.LayerUpload = &layerUploadController{}
|
2015-02-11 02:41:09 +01:00
|
|
|
|
|
|
|
// UUID returns the identifier for this upload.
|
|
|
|
func (luc *layerUploadController) UUID() string {
|
|
|
|
return luc.uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (luc *layerUploadController) StartedAt() time.Time {
|
|
|
|
return luc.startedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish marks the upload as completed, returning a valid handle to the
|
|
|
|
// uploaded layer. The final size and checksum are validated against the
|
|
|
|
// contents of the uploaded layer. The checksum should be provided in the
|
|
|
|
// format <algorithm>:<hex digest>.
|
2015-02-12 01:49:49 +01:00
|
|
|
func (luc *layerUploadController) Finish(digest digest.Digest) (distribution.Layer, error) {
|
2015-02-11 02:41:09 +01:00
|
|
|
ctxu.GetLogger(luc.layerStore.repository.ctx).Debug("(*layerUploadController).Finish")
|
2015-03-03 23:47:07 +01:00
|
|
|
|
|
|
|
err := luc.bufferedFileWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-11 02:41:09 +01:00
|
|
|
canonical, err := luc.validateLayer(digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := luc.moveLayer(canonical); err != nil {
|
|
|
|
// TODO(stevvooe): Cleanup?
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link the layer blob into the repository.
|
2015-03-05 01:31:31 +01:00
|
|
|
if err := luc.linkLayer(canonical, digest); err != nil {
|
2015-02-11 02:41:09 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := luc.removeResources(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return luc.layerStore.Fetch(canonical)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel the layer upload process.
|
|
|
|
func (luc *layerUploadController) Cancel() error {
|
|
|
|
ctxu.GetLogger(luc.layerStore.repository.ctx).Debug("(*layerUploadController).Cancel")
|
|
|
|
if err := luc.removeResources(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
luc.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateLayer checks the layer data against the digest, returning an error
|
|
|
|
// if it does not match. The canonical digest is returned.
|
|
|
|
func (luc *layerUploadController) validateLayer(dgst digest.Digest) (digest.Digest, error) {
|
|
|
|
digestVerifier := digest.NewDigestVerifier(dgst)
|
|
|
|
|
|
|
|
// TODO(stevvooe): Store resumable hash calculations in upload directory
|
|
|
|
// in driver. Something like a file at path <uuid>/resumablehash/<offest>
|
|
|
|
// with the hash state up to that point would be perfect. The hasher would
|
|
|
|
// then only have to fetch the difference.
|
|
|
|
|
|
|
|
// Read the file from the backend driver and validate it.
|
2015-03-03 23:47:07 +01:00
|
|
|
fr, err := newFileReader(luc.bufferedFileWriter.driver, luc.path)
|
2015-02-11 02:41:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := io.TeeReader(fr, digestVerifier)
|
|
|
|
|
|
|
|
// TODO(stevvooe): This is one of the places we need a Digester write
|
|
|
|
// sink. Instead, its read driven. This might be okay.
|
|
|
|
|
|
|
|
// Calculate an updated digest with the latest version.
|
2015-03-05 01:31:31 +01:00
|
|
|
canonical, err := digest.FromReader(tr)
|
2015-02-11 02:41:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !digestVerifier.Verified() {
|
2015-02-12 01:49:49 +01:00
|
|
|
return "", distribution.ErrLayerInvalidDigest{
|
2015-02-11 02:41:09 +01:00
|
|
|
Digest: dgst,
|
|
|
|
Reason: fmt.Errorf("content does not match digest"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return canonical, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// moveLayer moves the data into its final, hash-qualified destination,
|
|
|
|
// identified by dgst. The layer should be validated before commencing the
|
|
|
|
// move.
|
|
|
|
func (luc *layerUploadController) moveLayer(dgst digest.Digest) error {
|
|
|
|
blobPath, err := luc.layerStore.repository.registry.pm.path(blobDataPathSpec{
|
|
|
|
digest: dgst,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for existence
|
|
|
|
if _, err := luc.driver.Stat(blobPath); err != nil {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
break // ensure that it doesn't exist.
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the path exists, we can assume that the content has already
|
|
|
|
// been uploaded, since the blob storage is content-addressable.
|
|
|
|
// While it may be corrupted, detection of such corruption belongs
|
|
|
|
// elsewhere.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no data was received, we may not actually have a file on disk. Check
|
|
|
|
// the size here and write a zero-length file to blobPath if this is the
|
|
|
|
// case. For the most part, this should only ever happen with zero-length
|
|
|
|
// tars.
|
|
|
|
if _, err := luc.driver.Stat(luc.path); err != nil {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
// HACK(stevvooe): This is slightly dangerous: if we verify above,
|
|
|
|
// get a hash, then the underlying file is deleted, we risk moving
|
|
|
|
// a zero-length blob into a nonzero-length blob location. To
|
|
|
|
// prevent this horrid thing, we employ the hack of only allowing
|
|
|
|
// to this happen for the zero tarsum.
|
2015-03-05 05:26:56 +01:00
|
|
|
if dgst == digest.DigestSha256EmptyTar {
|
2015-02-11 02:41:09 +01:00
|
|
|
return luc.driver.PutContent(blobPath, []byte{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// We let this fail during the move below.
|
|
|
|
logrus.
|
|
|
|
WithField("upload.uuid", luc.UUID()).
|
|
|
|
WithField("digest", dgst).Warnf("attempted to move zero-length content with non-zero digest")
|
|
|
|
default:
|
|
|
|
return err // unrelated error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return luc.driver.Move(luc.path, blobPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// linkLayer links a valid, written layer blob into the registry under the
|
|
|
|
// named repository for the upload controller.
|
2015-03-05 01:31:31 +01:00
|
|
|
func (luc *layerUploadController) linkLayer(canonical digest.Digest, aliases ...digest.Digest) error {
|
|
|
|
dgsts := append([]digest.Digest{canonical}, aliases...)
|
2015-02-11 02:41:09 +01:00
|
|
|
|
2015-03-05 01:31:31 +01:00
|
|
|
// Don't make duplicate links.
|
|
|
|
seenDigests := make(map[digest.Digest]struct{}, len(dgsts))
|
|
|
|
|
|
|
|
for _, dgst := range dgsts {
|
|
|
|
if _, seen := seenDigests[dgst]; seen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seenDigests[dgst] = struct{}{}
|
|
|
|
|
|
|
|
layerLinkPath, err := luc.layerStore.repository.registry.pm.path(layerLinkPathSpec{
|
|
|
|
name: luc.layerStore.repository.Name(),
|
|
|
|
digest: dgst,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := luc.layerStore.repository.registry.driver.PutContent(layerLinkPath, []byte(canonical)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 02:41:09 +01:00
|
|
|
}
|
|
|
|
|
2015-03-05 01:31:31 +01:00
|
|
|
return nil
|
2015-02-11 02:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeResources should clean up all resources associated with the upload
|
|
|
|
// instance. An error will be returned if the clean up cannot proceed. If the
|
|
|
|
// resources are already not present, no error will be returned.
|
|
|
|
func (luc *layerUploadController) removeResources() error {
|
|
|
|
dataPath, err := luc.layerStore.repository.registry.pm.path(uploadDataPathSpec{
|
2015-02-12 02:00:42 +01:00
|
|
|
name: luc.layerStore.repository.Name(),
|
2015-02-11 02:41:09 +01:00
|
|
|
uuid: luc.uuid,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve and delete the containing directory, which should include any
|
|
|
|
// upload related files.
|
|
|
|
dirPath := path.Dir(dataPath)
|
|
|
|
|
|
|
|
if err := luc.driver.Delete(dirPath); err != nil {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
break // already gone!
|
|
|
|
default:
|
|
|
|
// This should be uncommon enough such that returning an error
|
|
|
|
// should be okay. At this point, the upload should be mostly
|
|
|
|
// complete, but perhaps the backend became unaccessible.
|
|
|
|
logrus.Errorf("unable to delete layer upload resources %q: %v", dirPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|