Split layer and upload from repository
Layer upload moved to its own file with its own unit tests Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
@@ -1,21 +1,14 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
ctxu "github.com/docker/distribution/context"
|
||||
|
||||
"github.com/docker/distribution/manifest"
|
||||
|
||||
"github.com/docker/distribution/digest"
|
||||
@@ -276,7 +269,8 @@ func (ls *layers) Upload() (distribution.LayerUpload, error) {
|
||||
}
|
||||
|
||||
return &httpLayerUpload{
|
||||
layers: ls,
|
||||
repo: ls.repository,
|
||||
client: ls.client,
|
||||
uuid: uuid,
|
||||
startedAt: time.Now(),
|
||||
location: location,
|
||||
@@ -339,319 +333,3 @@ func (ls *layers) fetchLayer(dgst digest.Digest) (distribution.Layer, error) {
|
||||
return nil, &UnexpectedHTTPStatusError{Status: resp.Status}
|
||||
}
|
||||
}
|
||||
|
||||
type httpLayer struct {
|
||||
*layers
|
||||
|
||||
size int64
|
||||
digest digest.Digest
|
||||
createdAt time.Time
|
||||
|
||||
rc io.ReadCloser // remote read closer
|
||||
brd *bufio.Reader // internal buffered io
|
||||
offset int64
|
||||
err error
|
||||
}
|
||||
|
||||
func (hl *httpLayer) CreatedAt() time.Time {
|
||||
return hl.createdAt
|
||||
}
|
||||
|
||||
func (hl *httpLayer) Digest() digest.Digest {
|
||||
return hl.digest
|
||||
}
|
||||
|
||||
func (hl *httpLayer) Read(p []byte) (n int, err error) {
|
||||
if hl.err != nil {
|
||||
return 0, hl.err
|
||||
}
|
||||
|
||||
rd, err := hl.reader()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
n, err = rd.Read(p)
|
||||
hl.offset += int64(n)
|
||||
|
||||
// Simulate io.EOR error if we reach filesize.
|
||||
if err == nil && hl.offset >= hl.size {
|
||||
err = io.EOF
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (hl *httpLayer) Seek(offset int64, whence int) (int64, error) {
|
||||
if hl.err != nil {
|
||||
return 0, hl.err
|
||||
}
|
||||
|
||||
var err error
|
||||
newOffset := hl.offset
|
||||
|
||||
switch whence {
|
||||
case os.SEEK_CUR:
|
||||
newOffset += int64(offset)
|
||||
case os.SEEK_END:
|
||||
newOffset = hl.size + int64(offset)
|
||||
case os.SEEK_SET:
|
||||
newOffset = int64(offset)
|
||||
}
|
||||
|
||||
if newOffset < 0 {
|
||||
err = fmt.Errorf("cannot seek to negative position")
|
||||
} else {
|
||||
if hl.offset != newOffset {
|
||||
hl.reset()
|
||||
}
|
||||
|
||||
// No problems, set the offset.
|
||||
hl.offset = newOffset
|
||||
}
|
||||
|
||||
return hl.offset, err
|
||||
}
|
||||
|
||||
func (hl *httpLayer) Close() error {
|
||||
if hl.err != nil {
|
||||
return hl.err
|
||||
}
|
||||
|
||||
// close and release reader chain
|
||||
if hl.rc != nil {
|
||||
hl.rc.Close()
|
||||
}
|
||||
|
||||
hl.rc = nil
|
||||
hl.brd = nil
|
||||
|
||||
hl.err = fmt.Errorf("httpLayer: closed")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hl *httpLayer) reset() {
|
||||
if hl.err != nil {
|
||||
return
|
||||
}
|
||||
if hl.rc != nil {
|
||||
hl.rc.Close()
|
||||
hl.rc = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (hl *httpLayer) reader() (io.Reader, error) {
|
||||
if hl.err != nil {
|
||||
return nil, hl.err
|
||||
}
|
||||
|
||||
if hl.rc != nil {
|
||||
return hl.brd, nil
|
||||
}
|
||||
|
||||
// If the offset is great than or equal to size, return a empty, noop reader.
|
||||
if hl.offset >= hl.size {
|
||||
return ioutil.NopCloser(bytes.NewReader([]byte{})), nil
|
||||
}
|
||||
|
||||
blobURL, err := hl.ub.BuildBlobURL(hl.name, hl.digest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", blobURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if hl.offset > 0 {
|
||||
// TODO(stevvooe): Get this working correctly.
|
||||
|
||||
// If we are at different offset, issue a range request from there.
|
||||
req.Header.Add("Range", fmt.Sprintf("1-"))
|
||||
ctxu.GetLogger(hl.context).Infof("Range: %s", req.Header.Get("Range"))
|
||||
}
|
||||
|
||||
resp, err := hl.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case resp.StatusCode == 200:
|
||||
hl.rc = resp.Body
|
||||
default:
|
||||
defer resp.Body.Close()
|
||||
return nil, fmt.Errorf("unexpected status resolving reader: %v", resp.Status)
|
||||
}
|
||||
|
||||
if hl.brd == nil {
|
||||
hl.brd = bufio.NewReader(hl.rc)
|
||||
} else {
|
||||
hl.brd.Reset(hl.rc)
|
||||
}
|
||||
|
||||
return hl.brd, nil
|
||||
}
|
||||
|
||||
func (hl *httpLayer) Length() int64 {
|
||||
return hl.size
|
||||
}
|
||||
|
||||
func (hl *httpLayer) Handler(r *http.Request) (http.Handler, error) {
|
||||
panic("Not implemented")
|
||||
}
|
||||
|
||||
type httpLayerUpload struct {
|
||||
*layers
|
||||
|
||||
uuid string
|
||||
startedAt time.Time
|
||||
|
||||
location string // always the last value of the location header.
|
||||
offset int64
|
||||
closed bool
|
||||
}
|
||||
|
||||
var _ distribution.LayerUpload = &httpLayerUpload{}
|
||||
|
||||
func (hlu *httpLayerUpload) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
req, err := http.NewRequest("PATCH", hlu.location, r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer req.Body.Close()
|
||||
|
||||
resp, err := hlu.client.Do(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case resp.StatusCode == http.StatusAccepted:
|
||||
// TODO(dmcgowan): Validate headers
|
||||
hlu.uuid = resp.Header.Get("Docker-Upload-UUID")
|
||||
hlu.location, err = sanitizeLocation(resp.Header.Get("Location"), hlu.location)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rng := resp.Header.Get("Range")
|
||||
var start, end int64
|
||||
if n, err := fmt.Sscanf(rng, "%d-%d", &start, &end); err != nil {
|
||||
return 0, err
|
||||
} else if n != 2 || end < start {
|
||||
return 0, fmt.Errorf("bad range format: %s", rng)
|
||||
}
|
||||
|
||||
return (end - start + 1), nil
|
||||
case resp.StatusCode == http.StatusNotFound:
|
||||
return 0, &BlobUploadNotFoundError{Location: hlu.location}
|
||||
case resp.StatusCode >= 400 && resp.StatusCode < 500:
|
||||
return 0, parseHTTPErrorResponse(resp)
|
||||
default:
|
||||
return 0, &UnexpectedHTTPStatusError{Status: resp.Status}
|
||||
}
|
||||
}
|
||||
|
||||
func (hlu *httpLayerUpload) Write(p []byte) (n int, err error) {
|
||||
req, err := http.NewRequest("PATCH", hlu.location, bytes.NewReader(p))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
req.Header.Set("Content-Range", fmt.Sprintf("%d-%d", hlu.offset, hlu.offset+int64(len(p)-1)))
|
||||
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(p)))
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
|
||||
resp, err := hlu.client.Do(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case resp.StatusCode == http.StatusAccepted:
|
||||
// TODO(dmcgowan): Validate headers
|
||||
hlu.uuid = resp.Header.Get("Docker-Upload-UUID")
|
||||
hlu.location, err = sanitizeLocation(resp.Header.Get("Location"), hlu.location)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rng := resp.Header.Get("Range")
|
||||
var start, end int
|
||||
if n, err := fmt.Sscanf(rng, "%d-%d", &start, &end); err != nil {
|
||||
return 0, err
|
||||
} else if n != 2 || end < start {
|
||||
return 0, fmt.Errorf("bad range format: %s", rng)
|
||||
}
|
||||
|
||||
return (end - start + 1), nil
|
||||
case resp.StatusCode == http.StatusNotFound:
|
||||
return 0, &BlobUploadNotFoundError{Location: hlu.location}
|
||||
case resp.StatusCode >= 400 && resp.StatusCode < 500:
|
||||
return 0, parseHTTPErrorResponse(resp)
|
||||
default:
|
||||
return 0, &UnexpectedHTTPStatusError{Status: resp.Status}
|
||||
}
|
||||
}
|
||||
|
||||
func (hlu *httpLayerUpload) Seek(offset int64, whence int) (int64, error) {
|
||||
newOffset := hlu.offset
|
||||
|
||||
switch whence {
|
||||
case os.SEEK_CUR:
|
||||
newOffset += int64(offset)
|
||||
case os.SEEK_END:
|
||||
return newOffset, errors.New("Cannot seek from end on incomplete upload")
|
||||
case os.SEEK_SET:
|
||||
newOffset = int64(offset)
|
||||
}
|
||||
|
||||
hlu.offset = newOffset
|
||||
|
||||
return hlu.offset, nil
|
||||
}
|
||||
|
||||
func (hlu *httpLayerUpload) UUID() string {
|
||||
return hlu.uuid
|
||||
}
|
||||
|
||||
func (hlu *httpLayerUpload) StartedAt() time.Time {
|
||||
return hlu.startedAt
|
||||
}
|
||||
|
||||
func (hlu *httpLayerUpload) Finish(digest digest.Digest) (distribution.Layer, error) {
|
||||
// TODO(dmcgowan): Check if already finished, if so just fetch
|
||||
req, err := http.NewRequest("PUT", hlu.location, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
values := req.URL.Query()
|
||||
values.Set("digest", digest.String())
|
||||
req.URL.RawQuery = values.Encode()
|
||||
|
||||
resp, err := hlu.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case resp.StatusCode == http.StatusCreated:
|
||||
return hlu.Layers().Fetch(digest)
|
||||
case resp.StatusCode == http.StatusNotFound:
|
||||
return nil, &BlobUploadNotFoundError{Location: hlu.location}
|
||||
case resp.StatusCode >= 400 && resp.StatusCode < 500:
|
||||
return nil, parseHTTPErrorResponse(resp)
|
||||
default:
|
||||
return nil, &UnexpectedHTTPStatusError{Status: resp.Status}
|
||||
}
|
||||
}
|
||||
|
||||
func (hlu *httpLayerUpload) Cancel() error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (hlu *httpLayerUpload) Close() error {
|
||||
hlu.closed = true
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user