2014-11-11 02:57:38 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2014-11-21 03:57:01 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-11-11 02:57:38 +00:00
|
|
|
"net/http"
|
2015-01-05 07:59:29 +00:00
|
|
|
"net/url"
|
2014-11-21 03:57:01 +00:00
|
|
|
"strconv"
|
2014-11-11 02:57:38 +00:00
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/api/v2"
|
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/storage"
|
2014-11-11 02:57:38 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// layerUploadDispatcher constructs and returns the layer upload handler for
|
|
|
|
// the given request context.
|
|
|
|
func layerUploadDispatcher(ctx *Context, r *http.Request) http.Handler {
|
2014-11-21 03:57:01 +00:00
|
|
|
luh := &layerUploadHandler{
|
2014-11-11 02:57:38 +00:00
|
|
|
Context: ctx,
|
|
|
|
UUID: ctx.vars["uuid"],
|
|
|
|
}
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
handler := http.Handler(handlers.MethodHandler{
|
|
|
|
"POST": http.HandlerFunc(luh.StartLayerUpload),
|
|
|
|
"GET": http.HandlerFunc(luh.GetUploadStatus),
|
|
|
|
"HEAD": http.HandlerFunc(luh.GetUploadStatus),
|
|
|
|
"PUT": http.HandlerFunc(luh.PutLayerChunk),
|
|
|
|
"DELETE": http.HandlerFunc(luh.CancelLayerUpload),
|
|
|
|
})
|
|
|
|
|
|
|
|
if luh.UUID != "" {
|
|
|
|
luh.log = luh.log.WithField("uuid", luh.UUID)
|
|
|
|
|
2015-01-05 07:59:29 +00:00
|
|
|
state, err := ctx.tokenProvider.LayerUploadStateFromToken(r.FormValue("_state"))
|
|
|
|
if err != nil {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logrus.Infof("error resolving upload: %v", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
|
|
|
})
|
|
|
|
}
|
2014-11-21 03:57:01 +00:00
|
|
|
|
2015-01-05 07:59:29 +00:00
|
|
|
layers := ctx.services.Layers()
|
|
|
|
upload, err := layers.Resume(state)
|
2014-11-21 03:57:01 +00:00
|
|
|
if err != nil && err != storage.ErrLayerUploadUnknown {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logrus.Infof("error resolving upload: %v", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
luh.Upload = upload
|
|
|
|
handler = closeResources(handler, luh.Upload)
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
return handler
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// layerUploadHandler handles the http layer upload process.
|
|
|
|
type layerUploadHandler struct {
|
|
|
|
*Context
|
|
|
|
|
|
|
|
// UUID identifies the upload instance for the current request.
|
|
|
|
UUID string
|
2014-11-21 03:57:01 +00:00
|
|
|
|
|
|
|
Upload storage.LayerUpload
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartLayerUpload begins the layer upload process and allocates a server-
|
|
|
|
// side upload session.
|
|
|
|
func (luh *layerUploadHandler) StartLayerUpload(w http.ResponseWriter, r *http.Request) {
|
2014-11-21 03:57:01 +00:00
|
|
|
layers := luh.services.Layers()
|
|
|
|
upload, err := layers.Upload(luh.Name)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
luh.Upload = upload
|
|
|
|
defer luh.Upload.Close()
|
2014-11-11 02:57:38 +00:00
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
if err := luh.layerUploadResponse(w, r); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUploadStatus returns the status of a given upload, identified by uuid.
|
|
|
|
func (luh *layerUploadHandler) GetUploadStatus(w http.ResponseWriter, r *http.Request) {
|
2014-11-21 03:57:01 +00:00
|
|
|
if luh.Upload == nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
|
2014-11-21 03:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := luh.layerUploadResponse(w, r); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
return
|
|
|
|
}
|
2014-11-11 02:57:38 +00:00
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutLayerChunk receives a layer chunk during the layer upload process,
|
|
|
|
// possible completing the upload with a checksum and length.
|
|
|
|
func (luh *layerUploadHandler) PutLayerChunk(w http.ResponseWriter, r *http.Request) {
|
2014-11-21 03:57:01 +00:00
|
|
|
if luh.Upload == nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
|
2014-11-21 03:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var finished bool
|
2014-11-11 02:57:38 +00:00
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
// TODO(stevvooe): This is woefully incomplete. Missing stuff:
|
|
|
|
//
|
|
|
|
// 1. Extract information from range header, if present.
|
|
|
|
// 2. Check offset of current layer.
|
|
|
|
// 3. Emit correct error responses.
|
|
|
|
|
|
|
|
// Read in the chunk
|
|
|
|
io.Copy(luh.Upload, r.Body)
|
|
|
|
|
|
|
|
if err := luh.maybeCompleteUpload(w, r); err != nil {
|
|
|
|
if err != errNotReadyToComplete {
|
2014-12-13 01:43:30 +00:00
|
|
|
switch err := err.(type) {
|
|
|
|
case storage.ErrLayerInvalidSize:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
luh.Errors.Push(v2.ErrorCodeSizeInvalid, err)
|
|
|
|
return
|
|
|
|
case storage.ErrLayerInvalidDigest:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
luh.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
|
|
|
return
|
|
|
|
}
|
2014-11-21 03:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := luh.layerUploadResponse(w, r); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if finished {
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
}
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CancelLayerUpload cancels an in-progress upload of a layer.
|
|
|
|
func (luh *layerUploadHandler) CancelLayerUpload(w http.ResponseWriter, r *http.Request) {
|
2014-11-21 03:57:01 +00:00
|
|
|
if luh.Upload == nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
|
2014-11-21 03:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// layerUploadResponse provides a standard request for uploading layers and
|
|
|
|
// chunk responses. This sets the correct headers but the response status is
|
|
|
|
// left to the caller.
|
|
|
|
func (luh *layerUploadHandler) layerUploadResponse(w http.ResponseWriter, r *http.Request) error {
|
2015-01-05 07:59:29 +00:00
|
|
|
values := make(url.Values)
|
|
|
|
stateToken, err := luh.Context.tokenProvider.LayerUploadStateToToken(storage.LayerUploadState{Name: luh.Upload.Name(), UUID: luh.Upload.UUID(), Offset: luh.Upload.Offset()})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Infof("error building upload state token: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
values.Set("_state", stateToken)
|
|
|
|
uploadURL, err := luh.urlBuilder.BuildBlobUploadChunkURL(luh.Upload.Name(), luh.Upload.UUID(), values)
|
2014-11-21 03:57:01 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Infof("error building upload url: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Location", uploadURL)
|
|
|
|
w.Header().Set("Content-Length", "0")
|
|
|
|
w.Header().Set("Range", fmt.Sprintf("0-%d", luh.Upload.Offset()))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errNotReadyToComplete = fmt.Errorf("not ready to complete upload")
|
|
|
|
|
|
|
|
// maybeCompleteUpload tries to complete the upload if the correct parameters
|
|
|
|
// are available. Returns errNotReadyToComplete if not ready to complete.
|
|
|
|
func (luh *layerUploadHandler) maybeCompleteUpload(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
// If we get a digest and length, we can finish the upload.
|
|
|
|
dgstStr := r.FormValue("digest") // TODO(stevvooe): Support multiple digest parameters!
|
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 20:16:58 +00:00
|
|
|
sizeStr := r.FormValue("size")
|
2014-11-21 03:57:01 +00:00
|
|
|
|
2014-12-13 01:43:30 +00:00
|
|
|
if dgstStr == "" {
|
2014-11-21 03:57:01 +00:00
|
|
|
return errNotReadyToComplete
|
|
|
|
}
|
|
|
|
|
|
|
|
dgst, err := digest.ParseDigest(dgstStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-13 01:43:30 +00:00
|
|
|
var size int64
|
|
|
|
if sizeStr != "" {
|
|
|
|
size, err = strconv.ParseInt(sizeStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size = -1
|
2014-11-21 03:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
luh.completeUpload(w, r, size, dgst)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// completeUpload finishes out the upload with the correct response.
|
|
|
|
func (luh *layerUploadHandler) completeUpload(w http.ResponseWriter, r *http.Request, size int64, dgst digest.Digest) {
|
|
|
|
layer, err := luh.Upload.Finish(size, dgst)
|
|
|
|
if err != nil {
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-12 05:59:59 +00:00
|
|
|
layerURL, err := luh.urlBuilder.BuildBlobURL(layer.Name(), layer.Digest())
|
2014-11-21 03:57:01 +00:00
|
|
|
if err != nil {
|
2014-12-12 06:24:25 +00:00
|
|
|
luh.Errors.Push(v2.ErrorCodeUnknown, err)
|
2014-11-21 03:57:01 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Location", layerURL)
|
|
|
|
w.Header().Set("Content-Length", "0")
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|