2014-11-14 02:42:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2014-12-12 06:24:25 +00:00
|
|
|
"fmt"
|
2014-11-14 02:42:49 +00:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/storage"
|
2014-11-14 02:42:49 +00:00
|
|
|
)
|
|
|
|
|
2014-11-18 01:33:03 +00:00
|
|
|
// simultaneousLayerPushWindow is the size of the parallel layer push window.
|
|
|
|
// A layer may not be pushed until the layer preceeding it by the length of the
|
|
|
|
// push window has been successfully pushed.
|
|
|
|
const simultaneousLayerPushWindow = 4
|
|
|
|
|
2014-11-22 03:29:08 +00:00
|
|
|
type pushFunction func(fsLayer storage.FSLayer) error
|
2014-11-18 01:33:03 +00:00
|
|
|
|
2014-11-14 02:42:49 +00:00
|
|
|
// Push implements a client push workflow for the image defined by the given
|
|
|
|
// name and tag pair, using the given ObjectStore for local manifest and layer
|
|
|
|
// storage
|
|
|
|
func Push(c Client, objectStore ObjectStore, name, tag string) error {
|
|
|
|
manifest, err := objectStore.Manifest(name, tag)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"name": name,
|
|
|
|
"tag": tag,
|
|
|
|
}).Info("No image found")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-18 01:33:03 +00:00
|
|
|
errChans := make([]chan error, len(manifest.FSLayers))
|
|
|
|
for i := range manifest.FSLayers {
|
|
|
|
errChans[i] = make(chan error)
|
|
|
|
}
|
2014-11-14 02:42:49 +00:00
|
|
|
|
2014-11-21 08:20:16 +00:00
|
|
|
cancelCh := make(chan struct{})
|
|
|
|
|
2014-11-18 01:33:03 +00:00
|
|
|
// Iterate over each layer in the manifest, simultaneously pushing no more
|
|
|
|
// than simultaneousLayerPushWindow layers at a time. If an error is
|
|
|
|
// received from a layer push, we abort the push.
|
|
|
|
for i := 0; i < len(manifest.FSLayers)+simultaneousLayerPushWindow; i++ {
|
|
|
|
dependentLayer := i - simultaneousLayerPushWindow
|
|
|
|
if dependentLayer >= 0 {
|
|
|
|
err := <-errChans[dependentLayer]
|
|
|
|
if err != nil {
|
|
|
|
log.WithField("error", err).Warn("Push aborted")
|
2014-11-21 08:20:16 +00:00
|
|
|
close(cancelCh)
|
2014-11-18 01:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 01:33:03 +00:00
|
|
|
if i < len(manifest.FSLayers) {
|
|
|
|
go func(i int) {
|
2014-11-21 08:20:16 +00:00
|
|
|
select {
|
|
|
|
case errChans[i] <- pushLayer(c, objectStore, name, manifest.FSLayers[i]):
|
|
|
|
case <-cancelCh: // recv broadcast notification about cancelation
|
|
|
|
}
|
2014-11-18 01:33:03 +00:00
|
|
|
}(i)
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.PutImageManifest(name, tag, manifest)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"manifest": manifest,
|
|
|
|
}).Warn("Unable to upload manifest")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-18 01:33:03 +00:00
|
|
|
|
2014-11-22 03:29:08 +00:00
|
|
|
func pushLayer(c Client, objectStore ObjectStore, name string, fsLayer storage.FSLayer) error {
|
2014-11-18 01:33:03 +00:00
|
|
|
log.WithField("layer", fsLayer).Info("Pushing layer")
|
|
|
|
|
|
|
|
layer, err := objectStore.Layer(fsLayer.BlobSum)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"layer": fsLayer,
|
|
|
|
}).Warn("Unable to read local layer")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
layerReader, err := layer.Reader()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"layer": fsLayer,
|
|
|
|
}).Warn("Unable to read local layer")
|
|
|
|
return err
|
|
|
|
}
|
2014-11-20 02:06:54 +00:00
|
|
|
defer layerReader.Close()
|
2014-11-18 01:33:03 +00:00
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
if layerReader.CurrentSize() != layerReader.Size() {
|
2014-11-18 01:33:03 +00:00
|
|
|
log.WithFields(log.Fields{
|
2014-11-18 23:44:39 +00:00
|
|
|
"layer": fsLayer,
|
|
|
|
"currentSize": layerReader.CurrentSize(),
|
|
|
|
"size": layerReader.Size(),
|
|
|
|
}).Warn("Local layer incomplete")
|
2014-12-12 06:24:25 +00:00
|
|
|
return fmt.Errorf("Local layer incomplete")
|
2014-11-18 01:33:03 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
length, err := c.BlobLength(name, fsLayer.BlobSum)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"layer": fsLayer,
|
|
|
|
}).Warn("Unable to check existence of remote layer")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if length >= 0 {
|
|
|
|
log.WithField("layer", fsLayer).Info("Layer already exists")
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-18 01:33:03 +00:00
|
|
|
|
2014-11-20 02:06:54 +00:00
|
|
|
location, err := c.InitiateBlobUpload(name)
|
2014-11-18 01:33:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"layer": fsLayer,
|
2014-11-20 02:06:54 +00:00
|
|
|
}).Warn("Unable to upload layer")
|
2014-11-18 01:33:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
err = c.UploadBlob(location, layerReader, int(layerReader.CurrentSize()), fsLayer.BlobSum)
|
2014-11-18 01:33:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"layer": fsLayer,
|
|
|
|
}).Warn("Unable to upload layer")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|