2015-04-17 20:32:51 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-08-21 04:50:15 +00:00
|
|
|
"errors"
|
2015-04-17 20:32:51 +00:00
|
|
|
"fmt"
|
2015-05-15 20:29:44 +00:00
|
|
|
"io"
|
2015-05-09 00:40:30 +00:00
|
|
|
"io/ioutil"
|
2015-04-17 20:32:51 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2015-12-15 02:34:18 +00:00
|
|
|
"sync"
|
2015-04-17 20:32:51 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2015-05-08 23:33:27 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-05-20 02:18:30 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-07-10 18:36:04 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-04-17 20:32:51 +00:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2015-05-15 23:50:17 +00:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-05-15 23:25:00 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/cache"
|
2015-05-21 00:12:40 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/cache/memory"
|
2015-04-17 20:32:51 +00:00
|
|
|
)
|
|
|
|
|
2015-07-22 22:18:03 +00:00
|
|
|
// Registry provides an interface for calling Repositories, which returns a catalog of repositories.
|
|
|
|
type Registry interface {
|
|
|
|
Repositories(ctx context.Context, repos []string, last string) (n int, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegistry creates a registry namespace which can be used to get a listing of repositories
|
|
|
|
func NewRegistry(ctx context.Context, baseURL string, transport http.RoundTripper) (Registry, error) {
|
|
|
|
ub, err := v2.NewURLBuilderFromString(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istry{
|
|
|
|
client: client,
|
|
|
|
ub: ub,
|
|
|
|
context: ctx,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type registry struct {
|
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
context context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repositories returns a lexigraphically sorted catalog given a base URL. The 'entries' slice will be filled up to the size
|
|
|
|
// of the slice, starting at the value provided in 'last'. The number of entries will be returned along with io.EOF if there
|
|
|
|
// are no more entries
|
|
|
|
func (r *registry) Repositories(ctx context.Context, entries []string, last string) (int, error) {
|
|
|
|
var numFilled int
|
|
|
|
var returnErr error
|
|
|
|
|
|
|
|
values := buildCatalogValues(len(entries), last)
|
|
|
|
u, err := r.ub.BuildCatalogURL(values)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := r.client.Get(u)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-07-22 22:18:03 +00:00
|
|
|
var ctlg struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
if err := decoder.Decode(&ctlg); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for cnt := range ctlg.Repositories {
|
|
|
|
entries[cnt] = ctlg.Repositories[cnt]
|
|
|
|
}
|
|
|
|
numFilled = len(ctlg.Repositories)
|
|
|
|
|
|
|
|
link := resp.Header.Get("Link")
|
|
|
|
if link == "" {
|
|
|
|
returnErr = io.EOF
|
|
|
|
}
|
2015-07-24 23:14:04 +00:00
|
|
|
} else {
|
2015-12-08 22:24:03 +00:00
|
|
|
return 0, HandleErrorResponse(resp)
|
2015-07-22 22:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return numFilled, returnErr
|
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// NewRepository creates a new Repository for the given repository name and base URL.
|
|
|
|
func NewRepository(ctx context.Context, name, baseURL string, transport http.RoundTripper) (distribution.Repository, error) {
|
|
|
|
if _, err := reference.ParseNamed(name); err != nil {
|
2015-04-17 20:32:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:50:17 +00:00
|
|
|
ub, err := v2.NewURLBuilderFromString(baseURL)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
// TODO(dmcgowan): create cookie jar
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &repository{
|
|
|
|
client: client,
|
|
|
|
ub: ub,
|
2015-09-08 23:00:48 +00:00
|
|
|
name: name,
|
2015-04-17 20:32:51 +00:00
|
|
|
context: ctx,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type repository struct {
|
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
context context.Context
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *repository) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:25:00 +00:00
|
|
|
func (r *repository) Blobs(ctx context.Context) distribution.BlobStore {
|
|
|
|
statter := &blobStatter{
|
2015-05-20 02:56:27 +00:00
|
|
|
name: r.Name(),
|
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-05-15 23:25:00 +00:00
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
return &blobs{
|
2015-05-20 02:56:27 +00:00
|
|
|
name: r.Name(),
|
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-05-21 00:12:40 +00:00
|
|
|
statter: cache.NewCachedBlobStatter(memory.NewInMemoryBlobDescriptorCacheProvider(), statter),
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 17:39:34 +00:00
|
|
|
func (r *repository) Manifests(ctx context.Context, options ...distribution.ManifestServiceOption) (distribution.ManifestService, error) {
|
|
|
|
// todo(richardscothern): options should be sent over the wire
|
2015-04-17 20:32:51 +00:00
|
|
|
return &manifests{
|
2015-05-20 02:56:27 +00:00
|
|
|
name: r.Name(),
|
|
|
|
ub: r.ub,
|
|
|
|
client: r.client,
|
2015-07-14 23:25:37 +00:00
|
|
|
etags: make(map[string]string),
|
2015-06-15 17:39:34 +00:00
|
|
|
}, nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (r *repository) Tags(ctx context.Context) distribution.TagService {
|
|
|
|
return &tags{
|
|
|
|
client: r.client,
|
|
|
|
ub: r.ub,
|
|
|
|
context: r.context,
|
|
|
|
name: r.Name(),
|
2015-05-20 02:56:27 +00:00
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// tags implements remote tagging operations.
|
|
|
|
type tags struct {
|
|
|
|
client *http.Client
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
context context.Context
|
|
|
|
name string
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// All returns all tags
|
|
|
|
func (t *tags) All(ctx context.Context) ([]string, error) {
|
|
|
|
var tags []string
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
u, err := t.ub.BuildTagsURL(t.name)
|
2015-05-09 00:40:30 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return tags, err
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
resp, err := t.client.Get(u)
|
2015-05-09 00:40:30 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return tags, err
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
2015-05-14 17:18:21 +00:00
|
|
|
defer resp.Body.Close()
|
2015-05-09 00:40:30 +00:00
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-05-09 00:40:30 +00:00
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return tags, err
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tagsResponse := struct {
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &tagsResponse); err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return tags, err
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
tags = tagsResponse.Tags
|
|
|
|
return tags, nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return tags, HandleErrorResponse(resp)
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
2015-05-09 00:40:30 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func descriptorFromResponse(response *http.Response) (distribution.Descriptor, error) {
|
|
|
|
desc := distribution.Descriptor{}
|
|
|
|
headers := response.Header
|
|
|
|
|
|
|
|
ctHeader := headers.Get("Content-Type")
|
|
|
|
if ctHeader == "" {
|
|
|
|
return distribution.Descriptor{}, errors.New("missing or empty Content-Type header")
|
2015-05-09 00:40:30 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
desc.MediaType = ctHeader
|
|
|
|
|
|
|
|
digestHeader := headers.Get("Docker-Content-Digest")
|
|
|
|
if digestHeader == "" {
|
|
|
|
bytes, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
_, desc, err := distribution.UnmarshalManifest(ctHeader, bytes)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
return desc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dgst, err := digest.ParseDigest(digestHeader)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
desc.Digest = dgst
|
|
|
|
|
|
|
|
lengthHeader := headers.Get("Content-Length")
|
|
|
|
if lengthHeader == "" {
|
|
|
|
return distribution.Descriptor{}, errors.New("missing or empty Content-Length header")
|
|
|
|
}
|
|
|
|
length, err := strconv.ParseInt(lengthHeader, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
desc.Size = length
|
|
|
|
|
|
|
|
return desc, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get issues a HEAD request for a Manifest against its named endpoint in order
|
|
|
|
// to construct a descriptor for the tag. If the registry doesn't support HEADing
|
|
|
|
// a manifest, fallback to GET.
|
|
|
|
func (t *tags) Get(ctx context.Context, tag string) (distribution.Descriptor, error) {
|
|
|
|
u, err := t.ub.BuildManifestURL(t.name, tag)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
var attempts int
|
|
|
|
resp, err := t.client.Head(u)
|
|
|
|
|
|
|
|
check:
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case resp.StatusCode >= 200 && resp.StatusCode < 400:
|
|
|
|
return descriptorFromResponse(resp)
|
|
|
|
case resp.StatusCode == http.StatusMethodNotAllowed:
|
|
|
|
resp, err = t.client.Get(u)
|
|
|
|
attempts++
|
|
|
|
if attempts > 1 {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
goto check
|
|
|
|
default:
|
2015-12-08 22:24:03 +00:00
|
|
|
return distribution.Descriptor{}, HandleErrorResponse(resp)
|
2015-08-21 04:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tags) Lookup(ctx context.Context, digest distribution.Descriptor) ([]string, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tags) Tag(ctx context.Context, tag string, desc distribution.Descriptor) error {
|
|
|
|
panic("not implemented")
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (t *tags) Untag(ctx context.Context, tag string) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
type manifests struct {
|
|
|
|
name string
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
|
|
|
etags map[string]string
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (ms *manifests) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
|
|
|
|
u, err := ms.ub.BuildManifestURL(ms.name, dgst.String())
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ms.client.Head(u)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-04-17 20:32:51 +00:00
|
|
|
return true, nil
|
2015-07-24 23:14:04 +00:00
|
|
|
} else if resp.StatusCode == http.StatusNotFound {
|
2015-04-17 20:32:51 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return false, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// AddEtagToTag allows a client to supply an eTag to Get which will be
|
2015-07-24 06:03:13 +00:00
|
|
|
// used for a conditional HTTP request. If the eTag matches, a nil manifest
|
2015-08-21 04:50:15 +00:00
|
|
|
// and ErrManifestNotModified error will be returned. etag is automatically
|
|
|
|
// quoted when added to this map.
|
2015-07-24 06:03:13 +00:00
|
|
|
func AddEtagToTag(tag, etag string) distribution.ManifestServiceOption {
|
2015-08-21 04:50:15 +00:00
|
|
|
return etagOption{tag, etag}
|
|
|
|
}
|
|
|
|
|
|
|
|
type etagOption struct{ tag, etag string }
|
|
|
|
|
|
|
|
func (o etagOption) Apply(ms distribution.ManifestService) error {
|
|
|
|
if ms, ok := ms.(*manifests); ok {
|
|
|
|
ms.etags[o.tag] = fmt.Sprintf(`"%s"`, o.etag)
|
|
|
|
return nil
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
return fmt.Errorf("etag options is a client-only option")
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (ms *manifests) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) {
|
|
|
|
|
|
|
|
var tag string
|
2015-07-14 23:25:37 +00:00
|
|
|
for _, option := range options {
|
2015-08-21 04:50:15 +00:00
|
|
|
if opt, ok := option.(withTagOption); ok {
|
|
|
|
tag = opt.tag
|
|
|
|
} else {
|
|
|
|
err := option.Apply(ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
var ref string
|
|
|
|
if tag != "" {
|
|
|
|
ref = tag
|
|
|
|
} else {
|
|
|
|
ref = dgst.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := ms.ub.BuildManifestURL(ms.name, ref)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
|
2015-07-14 23:25:37 +00:00
|
|
|
req, err := http.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
for _, t := range distribution.ManifestMediaTypes() {
|
|
|
|
req.Header.Add("Accept", t)
|
2015-07-14 23:25:37 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
|
|
|
|
if _, ok := ms.etags[ref]; ok {
|
|
|
|
req.Header.Set("If-None-Match", ms.etags[ref])
|
|
|
|
}
|
|
|
|
|
2015-07-14 23:25:37 +00:00
|
|
|
resp, err := ms.client.Do(req)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2015-07-24 23:14:04 +00:00
|
|
|
if resp.StatusCode == http.StatusNotModified {
|
2015-09-18 18:00:44 +00:00
|
|
|
return nil, distribution.ErrManifestNotModified
|
2015-07-24 23:14:04 +00:00
|
|
|
} else if SuccessStatus(resp.StatusCode) {
|
2015-08-21 04:50:15 +00:00
|
|
|
mt := resp.Header.Get("Content-Type")
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m, _, err := distribution.UnmarshalManifest(mt, body)
|
|
|
|
if err != nil {
|
2015-04-17 20:32:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
return m, nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return nil, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// WithTag allows a tag to be passed into Put which enables the client
|
|
|
|
// to build a correct URL.
|
|
|
|
func WithTag(tag string) distribution.ManifestServiceOption {
|
|
|
|
return withTagOption{tag}
|
|
|
|
}
|
|
|
|
|
|
|
|
type withTagOption struct{ tag string }
|
|
|
|
|
|
|
|
func (o withTagOption) Apply(m distribution.ManifestService) error {
|
|
|
|
if _, ok := m.(*manifests); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("withTagOption is a client-only option")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts a manifest. A tag can be specified using an options parameter which uses some shared state to hold the
|
|
|
|
// tag name in order to build the correct upload URL. This state is written and read under a lock.
|
|
|
|
func (ms *manifests) Put(ctx context.Context, m distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
|
|
|
|
var tag string
|
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
if opt, ok := option.(withTagOption); ok {
|
|
|
|
tag = opt.tag
|
|
|
|
} else {
|
|
|
|
err := option.Apply(ms)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestURL, err := ms.ub.BuildManifestURL(ms.name, tag)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
mediaType, p, err := m.Payload()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-06-15 17:39:34 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
putRequest, err := http.NewRequest("PUT", manifestURL, bytes.NewReader(p))
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
putRequest.Header.Set("Content-Type", mediaType)
|
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
resp, err := ms.client.Do(putRequest)
|
|
|
|
if err != nil {
|
2015-08-21 04:50:15 +00:00
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-08-21 04:50:15 +00:00
|
|
|
dgstHeader := resp.Header.Get("Docker-Content-Digest")
|
|
|
|
dgst, err := digest.ParseDigest(dgstHeader)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dgst, nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
|
2015-12-08 22:24:03 +00:00
|
|
|
return "", HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (ms *manifests) Delete(ctx context.Context, dgst digest.Digest) error {
|
2015-04-17 20:32:51 +00:00
|
|
|
u, err := ms.ub.BuildManifestURL(ms.name, dgst.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("DELETE", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ms.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-04-17 20:32:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// todo(richardscothern): Restore interface and implementation with merge of #1050
|
|
|
|
/*func (ms *manifests) Enumerate(ctx context.Context, manifests []distribution.Manifest, last distribution.Manifest) (n int, err error) {
|
|
|
|
panic("not supported")
|
|
|
|
}*/
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
type blobs struct {
|
2015-05-20 02:56:27 +00:00
|
|
|
name string
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
2015-05-15 23:25:00 +00:00
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
statter distribution.BlobDescriptorService
|
|
|
|
distribution.BlobDeleter
|
2015-12-15 02:34:18 +00:00
|
|
|
|
|
|
|
cacheLock sync.Mutex
|
|
|
|
cachedBlobUpload distribution.BlobWriter
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:00:28 +00:00
|
|
|
func sanitizeLocation(location, base string) (string, error) {
|
|
|
|
baseURL, err := url.Parse(base)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:00:28 +00:00
|
|
|
locationURL, err := url.Parse(location)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-08-27 02:00:28 +00:00
|
|
|
|
|
|
|
return baseURL.ResolveReference(locationURL).String(), nil
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
|
|
|
return bs.statter.Stat(ctx, dgst)
|
2015-05-15 23:25:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Get(ctx context.Context, dgst digest.Digest) ([]byte, error) {
|
2015-12-11 23:13:03 +00:00
|
|
|
reader, err := bs.Open(ctx, dgst)
|
2015-05-15 20:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-05-15 20:29:44 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
return ioutil.ReadAll(reader)
|
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Open(ctx context.Context, dgst digest.Digest) (distribution.ReadSeekCloser, error) {
|
2015-12-01 02:35:19 +00:00
|
|
|
blobURL, err := bs.ub.BuildBlobURL(bs.name, dgst)
|
2015-05-15 23:25:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-01 02:35:19 +00:00
|
|
|
return transport.NewHTTPReadSeeker(bs.client, blobURL,
|
|
|
|
func(resp *http.Response) error {
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return distribution.ErrBlobUnknown
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return HandleErrorResponse(resp)
|
2015-12-01 02:35:19 +00:00
|
|
|
}), nil
|
2015-05-15 20:29:44 +00:00
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error {
|
|
|
|
panic("not implemented")
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
|
|
|
|
writer, err := bs.Create(ctx)
|
2015-05-15 20:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
2015-05-22 01:44:08 +00:00
|
|
|
dgstr := digest.Canonical.New()
|
2015-05-21 06:44:08 +00:00
|
|
|
n, err := io.Copy(writer, io.TeeReader(bytes.NewReader(p), dgstr.Hash()))
|
2015-05-15 20:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
if n < int64(len(p)) {
|
|
|
|
return distribution.Descriptor{}, fmt.Errorf("short copy: wrote %d of %d", n, len(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
desc := distribution.Descriptor{
|
|
|
|
MediaType: mediaType,
|
2015-07-18 00:07:11 +00:00
|
|
|
Size: int64(len(p)),
|
2015-05-15 20:29:44 +00:00
|
|
|
Digest: dgstr.Digest(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer.Commit(ctx, desc)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Create(ctx context.Context) (distribution.BlobWriter, error) {
|
2015-12-15 02:34:18 +00:00
|
|
|
bs.cacheLock.Lock()
|
|
|
|
if bs.cachedBlobUpload != nil {
|
|
|
|
upload := bs.cachedBlobUpload
|
|
|
|
bs.cachedBlobUpload = nil
|
|
|
|
bs.cacheLock.Unlock()
|
|
|
|
|
|
|
|
return upload, nil
|
|
|
|
}
|
|
|
|
bs.cacheLock.Unlock()
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
u, err := bs.ub.BuildBlobUploadURL(bs.name)
|
2015-12-15 02:34:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-17 20:32:51 +00:00
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
resp, err := bs.client.Post(u, "", nil)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-04-17 20:32:51 +00:00
|
|
|
// TODO(dmcgowan): Check for invalid UUID
|
|
|
|
uuid := resp.Header.Get("Docker-Upload-UUID")
|
|
|
|
location, err := sanitizeLocation(resp.Header.Get("Location"), u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
return &httpBlobUpload{
|
2015-05-20 02:56:27 +00:00
|
|
|
statter: bs.statter,
|
2015-05-20 02:18:30 +00:00
|
|
|
client: bs.client,
|
2015-04-17 20:32:51 +00:00
|
|
|
uuid: uuid,
|
|
|
|
startedAt: time.Now(),
|
|
|
|
location: location,
|
|
|
|
}, nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return nil, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobs) Resume(ctx context.Context, id string) (distribution.BlobWriter, error) {
|
2015-04-17 20:32:51 +00:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2015-12-15 02:34:18 +00:00
|
|
|
func (bs *blobs) Mount(ctx context.Context, sourceRepo string, dgst digest.Digest) (distribution.Descriptor, error) {
|
|
|
|
u, err := bs.ub.BuildBlobUploadURL(bs.name, url.Values{"from": {sourceRepo}, "mount": {dgst.String()}})
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bs.client.Post(u, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusCreated:
|
|
|
|
return bs.Stat(ctx, dgst)
|
|
|
|
case http.StatusAccepted:
|
|
|
|
// Triggered a blob upload (legacy behavior), so cache the creation info
|
|
|
|
uuid := resp.Header.Get("Docker-Upload-UUID")
|
|
|
|
location, err := sanitizeLocation(resp.Header.Get("Location"), u)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bs.cacheLock.Lock()
|
|
|
|
bs.cachedBlobUpload = &httpBlobUpload{
|
|
|
|
statter: bs.statter,
|
|
|
|
client: bs.client,
|
|
|
|
uuid: uuid,
|
|
|
|
startedAt: time.Now(),
|
|
|
|
location: location,
|
|
|
|
}
|
|
|
|
bs.cacheLock.Unlock()
|
|
|
|
|
|
|
|
return distribution.Descriptor{}, HandleErrorResponse(resp)
|
|
|
|
default:
|
|
|
|
return distribution.Descriptor{}, HandleErrorResponse(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:52:22 +00:00
|
|
|
func (bs *blobs) Delete(ctx context.Context, dgst digest.Digest) error {
|
|
|
|
return bs.statter.Clear(ctx, dgst)
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:25:00 +00:00
|
|
|
type blobStatter struct {
|
2015-05-20 02:56:27 +00:00
|
|
|
name string
|
|
|
|
ub *v2.URLBuilder
|
|
|
|
client *http.Client
|
2015-05-15 23:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
func (bs *blobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
|
|
|
u, err := bs.ub.BuildBlobURL(bs.name, dgst)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 02:18:30 +00:00
|
|
|
resp, err := bs.client.Head(u)
|
2015-04-17 20:32:51 +00:00
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-04-17 20:32:51 +00:00
|
|
|
lengthHeader := resp.Header.Get("Content-Length")
|
2015-12-07 18:17:49 +00:00
|
|
|
if lengthHeader == "" {
|
|
|
|
return distribution.Descriptor{}, fmt.Errorf("missing content-length header for request: %s", u)
|
|
|
|
}
|
|
|
|
|
2015-04-17 20:32:51 +00:00
|
|
|
length, err := strconv.ParseInt(lengthHeader, 10, 64)
|
|
|
|
if err != nil {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, fmt.Errorf("error parsing content-length: %v", err)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{
|
|
|
|
MediaType: resp.Header.Get("Content-Type"),
|
2015-07-18 00:07:11 +00:00
|
|
|
Size: length,
|
2015-05-15 20:29:44 +00:00
|
|
|
Digest: dgst,
|
2015-04-17 20:32:51 +00:00
|
|
|
}, nil
|
2015-07-24 23:14:04 +00:00
|
|
|
} else if resp.StatusCode == http.StatusNotFound {
|
2015-05-15 20:29:44 +00:00
|
|
|
return distribution.Descriptor{}, distribution.ErrBlobUnknown
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return distribution.Descriptor{}, HandleErrorResponse(resp)
|
2015-04-17 20:32:51 +00:00
|
|
|
}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
func buildCatalogValues(maxEntries int, last string) url.Values {
|
|
|
|
values := url.Values{}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
if maxEntries > 0 {
|
|
|
|
values.Add("n", strconv.Itoa(maxEntries))
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
if last != "" {
|
|
|
|
values.Add("last", last)
|
|
|
|
}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
return values
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
2015-05-27 17:52:22 +00:00
|
|
|
|
|
|
|
func (bs *blobStatter) Clear(ctx context.Context, dgst digest.Digest) error {
|
|
|
|
blobURL, err := bs.ub.BuildBlobURL(bs.name, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("DELETE", blobURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bs.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if SuccessStatus(resp.StatusCode) {
|
2015-05-27 17:52:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-08 22:24:03 +00:00
|
|
|
return HandleErrorResponse(resp)
|
2015-05-27 17:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *blobStatter) SetDescriptor(ctx context.Context, dgst digest.Digest, desc distribution.Descriptor) error {
|
|
|
|
return nil
|
|
|
|
}
|