2015-02-12 13:23:22 -05:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2015-10-22 22:37:58 -04:00
|
|
|
"bufio"
|
2015-10-20 19:05:21 -04:00
|
|
|
"compress/gzip"
|
2015-02-12 13:23:22 -05:00
|
|
|
"fmt"
|
2015-08-11 16:47:08 -04:00
|
|
|
"io"
|
2015-02-12 13:23:22 -05:00
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest"
|
2015-10-30 20:46:25 -04:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2015-07-20 13:57:15 -04:00
|
|
|
"github.com/docker/docker/image"
|
2015-02-12 13:23:22 -05:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/utils"
|
2015-07-21 18:54:26 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-02-12 13:23:22 -05:00
|
|
|
)
|
|
|
|
|
2015-10-22 22:37:58 -04:00
|
|
|
const compressionBufSize = 32768
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
type v2Pusher struct {
|
|
|
|
*TagStore
|
|
|
|
endpoint registry.APIEndpoint
|
|
|
|
localRepo Repository
|
|
|
|
repoInfo *registry.RepositoryInfo
|
|
|
|
config *ImagePushConfig
|
|
|
|
sf *streamformatter.StreamFormatter
|
|
|
|
repo distribution.Repository
|
2015-08-12 19:02:12 -04:00
|
|
|
|
|
|
|
// layersPushed is the set of layers known to exist on the remote side.
|
|
|
|
// This avoids redundant queries when pushing multiple tags that
|
|
|
|
// involve the same layers.
|
|
|
|
layersPushed map[digest.Digest]bool
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v2Pusher) Push() (fallback bool, err error) {
|
2015-08-27 07:03:00 -04:00
|
|
|
p.repo, err = NewV2Repository(p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "push", "pull")
|
2015-02-12 13:23:22 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error getting v2 registry: %v", err)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
return false, p.pushV2Repository(p.config.Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v2Pusher) getImageTags(askedTag string) ([]string, error) {
|
|
|
|
logrus.Debugf("Checking %q against %#v", askedTag, p.localRepo)
|
|
|
|
if len(askedTag) > 0 {
|
|
|
|
if _, ok := p.localRepo[askedTag]; !ok || utils.DigestReference(askedTag) {
|
|
|
|
return nil, fmt.Errorf("Tag does not exist for %s", askedTag)
|
|
|
|
}
|
|
|
|
return []string{askedTag}, nil
|
|
|
|
}
|
|
|
|
var tags []string
|
|
|
|
for tag := range p.localRepo {
|
|
|
|
if !utils.DigestReference(tag) {
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v2Pusher) pushV2Repository(tag string) error {
|
|
|
|
localName := p.repoInfo.LocalName
|
2015-08-11 12:44:50 -04:00
|
|
|
if _, found := p.poolAdd("push", localName); found {
|
|
|
|
return fmt.Errorf("push or pull %s is already in progress", localName)
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
defer p.poolRemove("push", localName)
|
|
|
|
|
|
|
|
tags, err := p.getImageTags(tag)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting tags for %s: %s", localName, err)
|
|
|
|
}
|
|
|
|
if len(tags) == 0 {
|
|
|
|
return fmt.Errorf("no tags to push for %s", localName)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
if err := p.pushV2Tag(tag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v2Pusher) pushV2Tag(tag string) error {
|
|
|
|
logrus.Debugf("Pushing repository: %s:%s", p.repo.Name(), tag)
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
layerID, exists := p.localRepo[tag]
|
2015-02-12 13:23:22 -05:00
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("tag does not exist: %s", tag)
|
|
|
|
}
|
|
|
|
|
2015-08-12 21:32:23 -04:00
|
|
|
layersSeen := make(map[string]bool)
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
layer, err := p.graph.Get(layerID)
|
2015-02-12 13:23:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-30 20:46:25 -04:00
|
|
|
m := &schema1.Manifest{
|
2015-02-12 13:23:22 -05:00
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 1,
|
|
|
|
},
|
|
|
|
Name: p.repo.Name(),
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: layer.Architecture,
|
2015-10-30 20:46:25 -04:00
|
|
|
FSLayers: []schema1.FSLayer{},
|
|
|
|
History: []schema1.History{},
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var metadata runconfig.Config
|
|
|
|
if layer != nil && layer.Config != nil {
|
|
|
|
metadata = *layer.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
out := p.config.OutStream
|
|
|
|
|
|
|
|
for ; layer != nil; layer, err = p.graph.GetParent(layer) {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-12 19:02:12 -04:00
|
|
|
// break early if layer has already been seen in this image,
|
|
|
|
// this prevents infinite loops on layers which loopback, this
|
|
|
|
// cannot be prevented since layer IDs are not merkle hashes
|
|
|
|
// TODO(dmcgowan): throw error if no valid use case is found
|
2015-08-12 21:32:23 -04:00
|
|
|
if layersSeen[layer.ID] {
|
2015-02-12 13:23:22 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Pushing layer: %s", layer.ID)
|
|
|
|
|
|
|
|
if layer.Config != nil && metadata.Image != layer.ID {
|
|
|
|
if err := runconfig.Merge(&metadata, layer.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var exists bool
|
2015-08-26 17:58:56 -04:00
|
|
|
dgst, err := p.graph.GetLayerDigest(layer.ID)
|
2015-02-12 13:23:22 -05:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2015-08-12 19:02:12 -04:00
|
|
|
if p.layersPushed[dgst] {
|
|
|
|
exists = true
|
|
|
|
// break out of switch, it is already known that
|
|
|
|
// the push is not needed and therefore doing a
|
|
|
|
// stat is unnecessary
|
|
|
|
break
|
|
|
|
}
|
2015-08-21 13:28:35 -04:00
|
|
|
_, err := p.repo.Blobs(context.Background()).Stat(context.Background(), dgst)
|
2015-02-12 13:23:22 -05:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
exists = true
|
|
|
|
out.Write(p.sf.FormatProgress(stringid.TruncateID(layer.ID), "Image already exists", nil))
|
|
|
|
case distribution.ErrBlobUnknown:
|
|
|
|
// nop
|
|
|
|
default:
|
|
|
|
out.Write(p.sf.FormatProgress(stringid.TruncateID(layer.ID), "Image push failed", nil))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case ErrDigestNotSet:
|
|
|
|
// nop
|
|
|
|
case digest.ErrDigestInvalidFormat, digest.ErrDigestUnsupported:
|
|
|
|
return fmt.Errorf("error getting image checksum: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if digest was empty or not saved, or if blob does not exist on the remote repository,
|
|
|
|
// then fetch it.
|
|
|
|
if !exists {
|
2015-10-19 14:27:56 -04:00
|
|
|
var pushDigest digest.Digest
|
|
|
|
if pushDigest, err = p.pushV2Image(p.repo.Blobs(context.Background()), layer); err != nil {
|
2015-02-12 13:23:22 -05:00
|
|
|
return err
|
2015-10-19 14:27:56 -04:00
|
|
|
}
|
|
|
|
if dgst == "" {
|
2015-02-12 13:23:22 -05:00
|
|
|
// Cache new checksum
|
2015-08-26 17:58:56 -04:00
|
|
|
if err := p.graph.SetLayerDigest(layer.ID, pushDigest); err != nil {
|
2015-02-12 13:23:22 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 14:27:56 -04:00
|
|
|
dgst = pushDigest
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
2015-08-26 17:58:56 -04:00
|
|
|
// read v1Compatibility config, generate new if needed
|
|
|
|
jsonData, err := p.graph.GenerateV1CompatibilityChain(layer.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-30 20:46:25 -04:00
|
|
|
m.FSLayers = append(m.FSLayers, schema1.FSLayer{BlobSum: dgst})
|
|
|
|
m.History = append(m.History, schema1.History{V1Compatibility: string(jsonData)})
|
2015-02-12 13:23:22 -05:00
|
|
|
|
2015-08-12 21:32:23 -04:00
|
|
|
layersSeen[layer.ID] = true
|
2015-08-12 19:02:12 -04:00
|
|
|
p.layersPushed[dgst] = true
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Signed manifest for %s:%s using daemon's key: %s", p.repo.Name(), tag, p.trustKey.KeyID())
|
2015-10-30 20:46:25 -04:00
|
|
|
signed, err := schema1.Sign(m, p.trustKey)
|
2015-02-12 13:23:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-15 16:42:45 -04:00
|
|
|
manifestDigest, manifestSize, err := digestFromManifest(signed, p.repo.Name())
|
2015-02-12 13:23:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if manifestDigest != "" {
|
2015-07-15 16:42:45 -04:00
|
|
|
out.Write(p.sf.FormatStatus("", "%s: digest: %s size: %d", tag, manifestDigest, manifestSize))
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 18:54:26 -04:00
|
|
|
manSvc, err := p.repo.Manifests(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return manSvc.Put(signed)
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
2015-07-20 13:57:15 -04:00
|
|
|
func (p *v2Pusher) pushV2Image(bs distribution.BlobService, img *image.Image) (digest.Digest, error) {
|
2015-02-12 13:23:22 -05:00
|
|
|
out := p.config.OutStream
|
|
|
|
|
2015-08-11 16:47:08 -04:00
|
|
|
out.Write(p.sf.FormatProgress(stringid.TruncateID(img.ID), "Preparing", nil))
|
2015-02-12 13:23:22 -05:00
|
|
|
|
|
|
|
image, err := p.graph.Get(img.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
arch, err := p.graph.TarLayer(image)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-08-11 16:47:08 -04:00
|
|
|
defer arch.Close()
|
2015-02-12 13:23:22 -05:00
|
|
|
|
|
|
|
// Send the layer
|
2015-08-21 13:28:35 -04:00
|
|
|
layerUpload, err := bs.Create(context.Background())
|
2015-02-12 13:23:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer layerUpload.Close()
|
|
|
|
|
|
|
|
reader := progressreader.New(progressreader.Config{
|
2015-10-20 19:05:21 -04:00
|
|
|
In: ioutil.NopCloser(arch), // we'll take care of close here.
|
2015-02-12 13:23:22 -05:00
|
|
|
Out: out,
|
|
|
|
Formatter: p.sf,
|
2015-08-11 16:47:08 -04:00
|
|
|
|
|
|
|
// TODO(stevvooe): This may cause a size reporting error. Try to get
|
|
|
|
// this from tar-split or elsewhere. The main issue here is that we
|
|
|
|
// don't want to buffer to disk *just* to calculate the size.
|
|
|
|
Size: img.Size,
|
|
|
|
|
|
|
|
NewLines: false,
|
|
|
|
ID: stringid.TruncateID(img.ID),
|
|
|
|
Action: "Pushing",
|
2015-02-12 13:23:22 -05:00
|
|
|
})
|
2015-08-11 16:47:08 -04:00
|
|
|
|
2015-10-20 19:05:21 -04:00
|
|
|
digester := digest.Canonical.New()
|
|
|
|
// HACK: The MultiWriter doesn't write directly to layerUpload because
|
|
|
|
// we must make sure the ReadFrom is used, not Write. Using Write would
|
|
|
|
// send a PATCH request for every Write call.
|
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
2015-10-22 22:37:58 -04:00
|
|
|
// Use a bufio.Writer to avoid excessive chunking in HTTP request.
|
|
|
|
bufWriter := bufio.NewWriterSize(io.MultiWriter(pipeWriter, digester.Hash()), compressionBufSize)
|
|
|
|
compressor := gzip.NewWriter(bufWriter)
|
2015-10-20 19:05:21 -04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(compressor, reader)
|
|
|
|
if err == nil {
|
|
|
|
err = compressor.Close()
|
|
|
|
}
|
2015-10-22 22:37:58 -04:00
|
|
|
if err == nil {
|
|
|
|
err = bufWriter.Flush()
|
|
|
|
}
|
2015-10-20 19:05:21 -04:00
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
} else {
|
|
|
|
pipeWriter.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-08-11 16:47:08 -04:00
|
|
|
out.Write(p.sf.FormatProgress(stringid.TruncateID(img.ID), "Pushing", nil))
|
2015-10-20 19:05:21 -04:00
|
|
|
nn, err := layerUpload.ReadFrom(pipeReader)
|
|
|
|
pipeReader.Close()
|
2015-02-12 13:23:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:47:08 -04:00
|
|
|
dgst := digester.Digest()
|
|
|
|
if _, err := layerUpload.Commit(context.Background(), distribution.Descriptor{Digest: dgst}); err != nil {
|
2015-02-12 13:23:22 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:47:08 -04:00
|
|
|
logrus.Debugf("uploaded layer %s (%s), %d bytes", img.ID, dgst, nn)
|
|
|
|
out.Write(p.sf.FormatProgress(stringid.TruncateID(img.ID), "Pushed", nil))
|
2015-02-12 13:23:22 -05:00
|
|
|
|
|
|
|
return dgst, nil
|
|
|
|
}
|