2014-08-08 02:58:58 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2015-03-04 15:05:17 -05:00
|
|
|
"encoding/json"
|
2015-01-30 17:37:44 -05:00
|
|
|
"errors"
|
2014-08-08 02:58:58 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2014-09-30 20:03:57 -04:00
|
|
|
"strings"
|
2014-12-18 04:38:31 -05:00
|
|
|
"sync"
|
2014-08-08 02:58:58 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-08 02:58:58 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2014-09-30 20:03:57 -04:00
|
|
|
"github.com/docker/docker/image"
|
2015-02-24 03:51:46 -05:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-08-08 02:58:58 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2015-03-04 15:05:17 -05:00
|
|
|
"github.com/docker/docker/runconfig"
|
2014-08-08 02:58:58 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2015-01-07 17:59:12 -05:00
|
|
|
"github.com/docker/libtrust"
|
2014-08-08 02:58:58 -04:00
|
|
|
)
|
|
|
|
|
2015-01-30 17:37:44 -05:00
|
|
|
var ErrV2RegistryUnavailable = errors.New("error v2 registry unavailable")
|
|
|
|
|
2014-08-08 02:58:58 -04:00
|
|
|
// Retrieve the all the images to be uploaded in the correct order
|
|
|
|
func (s *TagStore) getImageList(localRepo map[string]string, requestedTag string) ([]string, map[string][]string, error) {
|
|
|
|
var (
|
|
|
|
imageList []string
|
2014-08-29 07:21:28 -04:00
|
|
|
imagesSeen = make(map[string]bool)
|
|
|
|
tagsByImage = make(map[string][]string)
|
2014-08-08 02:58:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
for tag, id := range localRepo {
|
|
|
|
if requestedTag != "" && requestedTag != tag {
|
2015-02-26 21:23:50 -05:00
|
|
|
// Include only the requested tag.
|
2014-08-08 02:58:58 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
if utils.DigestReference(tag) {
|
|
|
|
// Ignore digest references.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-08 02:58:58 -04:00
|
|
|
var imageListForThisTag []string
|
|
|
|
|
|
|
|
tagsByImage[id] = append(tagsByImage[id], tag)
|
|
|
|
|
|
|
|
for img, err := s.graph.Get(id); img != nil; img, err = img.GetParent() {
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if imagesSeen[img.ID] {
|
|
|
|
// This image is already on the list, we can ignore it and all its parents
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
imagesSeen[img.ID] = true
|
|
|
|
imageListForThisTag = append(imageListForThisTag, img.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse the image list for this tag (so the "most"-parent image is first)
|
|
|
|
for i, j := 0, len(imageListForThisTag)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
imageListForThisTag[i], imageListForThisTag[j] = imageListForThisTag[j], imageListForThisTag[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// append to main image list
|
|
|
|
imageList = append(imageList, imageListForThisTag...)
|
|
|
|
}
|
|
|
|
if len(imageList) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("No images found for the requested repository / tag")
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Image list: %v", imageList)
|
|
|
|
logrus.Debugf("Tags by image: %v", tagsByImage)
|
2014-08-08 02:58:58 -04:00
|
|
|
|
|
|
|
return imageList, tagsByImage, nil
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
func (s *TagStore) getImageTags(localRepo map[string]string, askedTag string) ([]string, error) {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Checking %s against %#v", askedTag, localRepo)
|
2015-01-30 17:20:32 -05:00
|
|
|
if len(askedTag) > 0 {
|
2015-02-26 21:23:50 -05:00
|
|
|
if _, ok := localRepo[askedTag]; !ok || utils.DigestReference(askedTag) {
|
2015-03-04 15:05:17 -05:00
|
|
|
return nil, fmt.Errorf("Tag does not exist: %s", askedTag)
|
2015-01-30 17:20:32 -05:00
|
|
|
}
|
|
|
|
return []string{askedTag}, nil
|
|
|
|
}
|
|
|
|
var tags []string
|
|
|
|
for tag := range localRepo {
|
2015-02-26 21:23:50 -05:00
|
|
|
if !utils.DigestReference(tag) {
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
2015-01-30 17:20:32 -05:00
|
|
|
}
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 18:24:51 -05:00
|
|
|
// createImageIndex returns an index of an image's layer IDs and tags.
|
|
|
|
func (s *TagStore) createImageIndex(images []string, tags map[string][]string) []*registry.ImgData {
|
|
|
|
var imageIndex []*registry.ImgData
|
|
|
|
for _, id := range images {
|
|
|
|
if tags, hasTags := tags[id]; hasTags {
|
2014-08-08 02:58:58 -04:00
|
|
|
// If an image has tags you must add an entry in the image index
|
|
|
|
// for each tag
|
|
|
|
for _, tag := range tags {
|
|
|
|
imageIndex = append(imageIndex, ®istry.ImgData{
|
2015-01-13 18:24:51 -05:00
|
|
|
ID: id,
|
2014-08-08 02:58:58 -04:00
|
|
|
Tag: tag,
|
|
|
|
})
|
|
|
|
}
|
2015-01-13 18:24:51 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If the image does not have a tag it still needs to be sent to the
|
|
|
|
// registry with an empty tag so that it is accociated with the repository
|
|
|
|
imageIndex = append(imageIndex, ®istry.ImgData{
|
|
|
|
ID: id,
|
|
|
|
Tag: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return imageIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
type imagePushData struct {
|
|
|
|
id string
|
|
|
|
endpoint string
|
|
|
|
tokens []string
|
|
|
|
}
|
2014-08-08 02:58:58 -04:00
|
|
|
|
2015-01-13 18:24:51 -05:00
|
|
|
// lookupImageOnEndpoint checks the specified endpoint to see if an image exists
|
|
|
|
// and if it is absent then it sends the image id to the channel to be pushed.
|
2015-03-17 22:18:41 -04:00
|
|
|
func lookupImageOnEndpoint(wg *sync.WaitGroup, r *registry.Session, out io.Writer, sf *streamformatter.StreamFormatter,
|
2015-01-13 18:24:51 -05:00
|
|
|
images chan imagePushData, imagesToPush chan string) {
|
|
|
|
defer wg.Done()
|
|
|
|
for image := range images {
|
|
|
|
if err := r.LookupRemoteImage(image.id, image.endpoint, image.tokens); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error in LookupRemoteImage: %s", err)
|
2015-01-13 18:24:51 -05:00
|
|
|
imagesToPush <- image.id
|
|
|
|
continue
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Image %s already pushed, skipping", stringid.TruncateID(image.id)))
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
2015-01-13 18:24:51 -05:00
|
|
|
}
|
2014-08-08 02:58:58 -04:00
|
|
|
|
2015-01-13 18:24:51 -05:00
|
|
|
func (s *TagStore) pushImageToEndpoint(endpoint string, out io.Writer, remoteName string, imageIDs []string,
|
2015-03-17 22:18:41 -04:00
|
|
|
tags map[string][]string, repo *registry.RepositoryData, sf *streamformatter.StreamFormatter, r *registry.Session) error {
|
2015-01-13 18:24:51 -05:00
|
|
|
workerCount := len(imageIDs)
|
|
|
|
// start a maximum of 5 workers to check if images exist on the specified endpoint.
|
|
|
|
if workerCount > 5 {
|
|
|
|
workerCount = 5
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
wg = &sync.WaitGroup{}
|
|
|
|
imageData = make(chan imagePushData, workerCount*2)
|
|
|
|
imagesToPush = make(chan string, workerCount*2)
|
|
|
|
pushes = make(chan map[string]struct{}, 1)
|
|
|
|
)
|
|
|
|
for i := 0; i < workerCount; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go lookupImageOnEndpoint(wg, r, out, sf, imageData, imagesToPush)
|
|
|
|
}
|
|
|
|
// start a go routine that consumes the images to push
|
|
|
|
go func() {
|
|
|
|
shouldPush := make(map[string]struct{})
|
|
|
|
for id := range imagesToPush {
|
|
|
|
shouldPush[id] = struct{}{}
|
|
|
|
}
|
|
|
|
pushes <- shouldPush
|
|
|
|
}()
|
|
|
|
for _, id := range imageIDs {
|
|
|
|
imageData <- imagePushData{
|
|
|
|
id: id,
|
|
|
|
endpoint: endpoint,
|
|
|
|
tokens: repo.Tokens,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// close the channel to notify the workers that there will be no more images to check.
|
|
|
|
close(imageData)
|
|
|
|
wg.Wait()
|
|
|
|
close(imagesToPush)
|
|
|
|
// wait for all the images that require pushes to be collected into a consumable map.
|
|
|
|
shouldPush := <-pushes
|
|
|
|
// finish by pushing any images and tags to the endpoint. The order that the images are pushed
|
2015-01-28 21:15:56 -05:00
|
|
|
// is very important that is why we are still iterating over the ordered list of imageIDs.
|
2015-01-13 18:24:51 -05:00
|
|
|
for _, id := range imageIDs {
|
|
|
|
if _, push := shouldPush[id]; push {
|
|
|
|
if _, err := s.pushImage(r, out, id, endpoint, repo.Tokens, sf); err != nil {
|
|
|
|
// FIXME: Continue on error?
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tag := range tags[id] {
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Pushing tag for rev [%s] on {%s}", stringid.TruncateID(id), endpoint+"repositories/"+remoteName+"/tags/"+tag))
|
2015-01-13 18:24:51 -05:00
|
|
|
if err := r.PushRegistryTag(remoteName, id, tag, endpoint, repo.Tokens); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushRepository pushes layers that do not already exist on the registry.
|
|
|
|
func (s *TagStore) pushRepository(r *registry.Session, out io.Writer,
|
|
|
|
repoInfo *registry.RepositoryInfo, localRepo map[string]string,
|
2015-03-17 22:18:41 -04:00
|
|
|
tag string, sf *streamformatter.StreamFormatter) error {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Local repo: %s", localRepo)
|
2015-01-13 18:24:51 -05:00
|
|
|
out = utils.NewWriteFlusher(out)
|
|
|
|
imgList, tags, err := s.getImageList(localRepo, tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out.Write(sf.FormatStatus("", "Sending image list"))
|
|
|
|
|
|
|
|
imageIndex := s.createImageIndex(imgList, tags)
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Preparing to push %s with the following images and tags", localRepo)
|
2014-08-08 02:58:58 -04:00
|
|
|
for _, data := range imageIndex {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Pushing ID: %s with Tag: %s", data.ID, data.Tag)
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
// Register all the images in a repository with the registry
|
|
|
|
// If an image is not in this list it will not be associated with the repository
|
2015-01-13 18:24:51 -05:00
|
|
|
repoData, err := r.PushImageJSONIndex(repoInfo.RemoteName, imageIndex, false, nil)
|
2014-08-08 02:58:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nTag := 1
|
|
|
|
if tag == "" {
|
|
|
|
nTag = len(localRepo)
|
|
|
|
}
|
2015-01-13 18:24:51 -05:00
|
|
|
out.Write(sf.FormatStatus("", "Pushing repository %s (%d tags)", repoInfo.CanonicalName, nTag))
|
|
|
|
// push the repository to each of the endpoints only if it does not exist.
|
|
|
|
for _, endpoint := range repoData.Endpoints {
|
|
|
|
if err := s.pushImageToEndpoint(endpoint, out, repoInfo.RemoteName, imgList, tags, repoData, sf, r); err != nil {
|
|
|
|
return err
|
2014-12-17 17:17:14 -05:00
|
|
|
}
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
2015-01-13 18:24:51 -05:00
|
|
|
_, err = r.PushImageJSONIndex(repoInfo.RemoteName, imageIndex, true, repoData.Endpoints)
|
|
|
|
return err
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
|
2015-03-17 22:18:41 -04:00
|
|
|
func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep string, token []string, sf *streamformatter.StreamFormatter) (checksum string, err error) {
|
2014-08-08 02:58:58 -04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
|
|
|
jsonRaw, err := ioutil.ReadFile(path.Join(s.graph.Root, imgID, "json"))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Cannot retrieve the path for {%s}: %s", imgID, err)
|
|
|
|
}
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(imgID), "Pushing", nil))
|
2014-08-08 02:58:58 -04:00
|
|
|
|
|
|
|
imgData := ®istry.ImgData{
|
|
|
|
ID: imgID,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the json
|
|
|
|
if err := r.PushImageJSONRegistry(imgData, jsonRaw, ep, token); err != nil {
|
|
|
|
if err == registry.ErrAlreadyExists {
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(imgData.ID), "Image already pushed, skipping", nil))
|
2014-08-08 02:58:58 -04:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:00:57 -05:00
|
|
|
layerData, err := s.graph.TempLayerArchive(imgID, sf, out)
|
2014-08-08 02:58:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to generate layer archive: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(layerData.Name())
|
|
|
|
|
|
|
|
// Send the layer
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("rendered layer for %s of [%d] size", imgData.ID, layerData.Size)
|
2014-08-08 02:58:58 -04:00
|
|
|
|
2015-02-24 03:51:46 -05:00
|
|
|
checksum, checksumPayload, err := r.PushImageLayerRegistry(imgData.ID,
|
|
|
|
progressreader.New(progressreader.Config{
|
|
|
|
In: layerData,
|
|
|
|
Out: out,
|
|
|
|
Formatter: sf,
|
|
|
|
Size: int(layerData.Size),
|
|
|
|
NewLines: false,
|
2015-03-24 07:25:26 -04:00
|
|
|
ID: stringid.TruncateID(imgData.ID),
|
2015-02-24 03:51:46 -05:00
|
|
|
Action: "Pushing",
|
|
|
|
}), ep, token, jsonRaw)
|
2014-08-08 02:58:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
imgData.Checksum = checksum
|
|
|
|
imgData.ChecksumPayload = checksumPayload
|
|
|
|
// Send the checksum
|
|
|
|
if err := r.PushImageChecksumRegistry(imgData, ep, token); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(imgData.ID), "Image successfully pushed", nil))
|
2014-08-08 02:58:58 -04:00
|
|
|
return imgData.Checksum, nil
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:18:41 -04:00
|
|
|
func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *streamformatter.StreamFormatter) error {
|
2015-01-14 19:46:31 -05:00
|
|
|
endpoint, err := r.V2RegistryEndpoint(repoInfo.Index)
|
|
|
|
if err != nil {
|
2015-01-30 17:37:44 -05:00
|
|
|
if repoInfo.Index.Official {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Unable to push to V2 registry, falling back to v1: %s", err)
|
2015-01-30 17:37:44 -05:00
|
|
|
return ErrV2RegistryUnavailable
|
|
|
|
}
|
2015-01-14 19:46:31 -05:00
|
|
|
return fmt.Errorf("error getting registry endpoint: %s", err)
|
|
|
|
}
|
2015-01-30 17:20:32 -05:00
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
tags, err := s.getImageTags(localRepo, tag)
|
2015-01-30 17:20:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(tags) == 0 {
|
|
|
|
return fmt.Errorf("No tags to push for %s", repoInfo.LocalName)
|
|
|
|
}
|
|
|
|
|
2015-01-14 19:46:31 -05:00
|
|
|
auth, err := r.GetV2Authorization(endpoint, repoInfo.RemoteName, false)
|
2015-01-13 18:48:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting authorization: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:20:32 -05:00
|
|
|
for _, tag := range tags {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Pushing repository: %s:%s", repoInfo.CanonicalName, tag)
|
2015-01-13 18:48:49 -05:00
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
layerId, exists := localRepo[tag]
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("tag does not exist: %s", tag)
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
layer, err := s.graph.Get(layerId)
|
2015-01-13 18:48:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
m := ®istry.ManifestData{
|
|
|
|
SchemaVersion: 1,
|
|
|
|
Name: repoInfo.RemoteName,
|
|
|
|
Tag: tag,
|
|
|
|
Architecture: layer.Architecture,
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
2015-03-04 15:05:17 -05:00
|
|
|
var metadata runconfig.Config
|
|
|
|
if layer.Config != nil {
|
|
|
|
metadata = *layer.Config
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
layersSeen := make(map[string]bool)
|
|
|
|
layers := []*image.Image{layer}
|
|
|
|
for ; layer != nil; layer, err = layer.GetParent() {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if layersSeen[layer.ID] {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
layers = append(layers, layer)
|
|
|
|
layersSeen[layer.ID] = true
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
2015-03-04 15:05:17 -05:00
|
|
|
m.FSLayers = make([]*registry.FSLayer, len(layers))
|
|
|
|
m.History = make([]*registry.ManifestHistory, len(layers))
|
2015-01-21 17:16:39 -05:00
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
// Schema version 1 requires layer ordering from top to root
|
|
|
|
for i, layer := range layers {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Pushing layer: %s", layer.ID)
|
2015-01-30 17:20:32 -05:00
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
if layer.Config != nil && metadata.Image != layer.ID {
|
|
|
|
err = runconfig.Merge(&metadata, layer.Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-30 17:20:32 -05:00
|
|
|
}
|
2015-03-04 15:05:17 -05:00
|
|
|
jsonData, err := layer.RawJson()
|
2015-01-30 17:20:32 -05:00
|
|
|
if err != nil {
|
2015-03-04 15:05:17 -05:00
|
|
|
return fmt.Errorf("cannot retrieve the path for %s: %s", layer.ID, err)
|
2015-01-30 17:20:32 -05:00
|
|
|
}
|
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
checksum, err := layer.GetCheckSum(s.graph.ImageRoot(layer.ID))
|
2015-01-30 17:20:32 -05:00
|
|
|
if err != nil {
|
2015-03-04 15:05:17 -05:00
|
|
|
return fmt.Errorf("error getting image checksum: %s", err)
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
2015-01-30 17:20:32 -05:00
|
|
|
|
2015-03-04 15:05:17 -05:00
|
|
|
var exists bool
|
|
|
|
if len(checksum) > 0 {
|
|
|
|
sumParts := strings.SplitN(checksum, ":", 2)
|
|
|
|
if len(sumParts) < 2 {
|
|
|
|
return fmt.Errorf("Invalid checksum: %s", checksum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call mount blob
|
|
|
|
exists, err = r.HeadV2ImageBlob(endpoint, repoInfo.RemoteName, sumParts[0], sumParts[1], auth)
|
|
|
|
if err != nil {
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(layer.ID), "Image push failed", nil))
|
2015-03-04 15:05:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 17:20:32 -05:00
|
|
|
if !exists {
|
2015-03-04 15:05:17 -05:00
|
|
|
if cs, err := s.pushV2Image(r, layer, endpoint, repoInfo.RemoteName, sf, out, auth); err != nil {
|
2015-01-30 17:20:32 -05:00
|
|
|
return err
|
2015-03-04 15:05:17 -05:00
|
|
|
} else if cs != checksum {
|
|
|
|
// Cache new checksum
|
|
|
|
if err := layer.SaveCheckSum(s.graph.ImageRoot(layer.ID), cs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
checksum = cs
|
2015-01-30 17:20:32 -05:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(layer.ID), "Image already exists", nil))
|
2015-01-30 17:20:32 -05:00
|
|
|
}
|
2015-03-04 15:05:17 -05:00
|
|
|
m.FSLayers[i] = ®istry.FSLayer{BlobSum: checksum}
|
|
|
|
m.History[i] = ®istry.ManifestHistory{V1Compatibility: string(jsonData)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkValidManifest(m); err != nil {
|
|
|
|
return fmt.Errorf("invalid manifest: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Pushing %s:%s to v2 repository", repoInfo.LocalName, tag)
|
2015-03-04 15:05:17 -05:00
|
|
|
mBytes, err := json.MarshalIndent(m, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
js, err := libtrust.NewJSONSignature(mBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = js.Sign(s.trustKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signedBody, err := js.PrettySignature("signatures")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Infof("Signed manifest for %s:%s using daemon's key: %s", repoInfo.LocalName, tag, s.trustKey.KeyID())
|
2015-01-13 18:48:49 -05:00
|
|
|
|
2015-01-30 17:20:32 -05:00
|
|
|
// push the manifest
|
2015-03-18 02:45:30 -04:00
|
|
|
digest, err := r.PutV2ImageManifest(endpoint, repoInfo.RemoteName, tag, signedBody, mBytes, auth)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-01-30 17:20:32 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-02-26 21:23:50 -05:00
|
|
|
|
2015-03-18 02:45:30 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Digest: %s", digest))
|
2015-01-30 17:20:32 -05:00
|
|
|
}
|
|
|
|
return nil
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
|
|
|
|
2015-01-27 21:10:28 -05:00
|
|
|
// PushV2Image pushes the image content to the v2 registry, first buffering the contents to disk
|
2015-03-17 22:18:41 -04:00
|
|
|
func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint *registry.Endpoint, imageName string, sf *streamformatter.StreamFormatter, out io.Writer, auth *registry.RequestAuthorization) (string, error) {
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Buffering to Disk", nil))
|
2015-01-27 21:10:28 -05:00
|
|
|
|
|
|
|
image, err := s.graph.Get(img.ID)
|
|
|
|
if err != nil {
|
2015-03-04 15:05:17 -05:00
|
|
|
return "", err
|
2015-01-27 21:10:28 -05:00
|
|
|
}
|
|
|
|
arch, err := image.TarLayer()
|
|
|
|
if err != nil {
|
2015-03-04 15:05:17 -05:00
|
|
|
return "", err
|
2015-01-27 21:10:28 -05:00
|
|
|
}
|
2015-02-11 11:30:46 -05:00
|
|
|
defer arch.Close()
|
|
|
|
|
2015-01-27 21:10:28 -05:00
|
|
|
tf, err := s.graph.newTempFile()
|
|
|
|
if err != nil {
|
2015-03-04 15:05:17 -05:00
|
|
|
return "", err
|
2015-01-27 21:10:28 -05:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
tf.Close()
|
|
|
|
os.Remove(tf.Name())
|
|
|
|
}()
|
|
|
|
|
2015-03-30 14:19:12 -04:00
|
|
|
size, dgst, err := bufferToFile(tf, arch)
|
2015-01-27 21:10:28 -05:00
|
|
|
|
|
|
|
// Send the layer
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("rendered layer for %s of [%d] size", img.ID, size)
|
2015-01-27 21:10:28 -05:00
|
|
|
|
2015-03-11 11:17:48 -04:00
|
|
|
if err := r.PutV2ImageBlob(endpoint, imageName, dgst.Algorithm(), dgst.Hex(),
|
2015-02-24 03:51:46 -05:00
|
|
|
progressreader.New(progressreader.Config{
|
|
|
|
In: tf,
|
|
|
|
Out: out,
|
|
|
|
Formatter: sf,
|
|
|
|
Size: int(size),
|
|
|
|
NewLines: false,
|
2015-03-24 07:25:26 -04:00
|
|
|
ID: stringid.TruncateID(img.ID),
|
2015-02-24 03:51:46 -05:00
|
|
|
Action: "Pushing",
|
|
|
|
}), auth); err != nil {
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Image push failed", nil))
|
2015-03-04 15:05:17 -05:00
|
|
|
return "", err
|
2015-01-27 21:10:28 -05:00
|
|
|
}
|
2015-03-24 07:25:26 -04:00
|
|
|
out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Image successfully pushed", nil))
|
2015-03-11 11:17:48 -04:00
|
|
|
return dgst.String(), nil
|
2015-01-27 21:10:28 -05:00
|
|
|
}
|
|
|
|
|
2014-08-08 02:58:58 -04:00
|
|
|
// FIXME: Allow to interrupt current push when new push of same image is done.
|
2015-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdPush(job *engine.Job) error {
|
2014-08-08 02:58:58 -04:00
|
|
|
if n := len(job.Args); n != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Usage: %s IMAGE", job.Name)
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
localName = job.Args[0]
|
2015-03-17 22:18:41 -04:00
|
|
|
sf = streamformatter.NewStreamFormatter(job.GetenvBool("json"))
|
2014-08-08 02:58:58 -04:00
|
|
|
authConfig = ®istry.AuthConfig{}
|
|
|
|
metaHeaders map[string][]string
|
|
|
|
)
|
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
2015-03-31 19:21:37 -04:00
|
|
|
repoInfo, err := s.registryService.ResolveRepository(localName)
|
2014-10-06 21:54:52 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
|
2014-08-08 02:58:58 -04:00
|
|
|
tag := job.Getenv("tag")
|
|
|
|
job.GetenvJson("authConfig", authConfig)
|
|
|
|
job.GetenvJson("metaHeaders", &metaHeaders)
|
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
if _, err := s.poolAdd("push", repoInfo.LocalName); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
defer s.poolRemove("push", repoInfo.LocalName)
|
2014-08-08 02:58:58 -04:00
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
endpoint, err := repoInfo.GetEndpoint()
|
2014-08-08 02:58:58 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
|
2015-02-10 18:46:42 -05:00
|
|
|
r, err := registry.NewSession(authConfig, registry.HTTPRequestFactory(metaHeaders), endpoint, false)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
|
2015-02-10 18:46:42 -05:00
|
|
|
reposLen := 1
|
|
|
|
if tag == "" {
|
|
|
|
reposLen = len(s.Repositories[repoInfo.LocalName])
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
2015-02-10 18:46:42 -05:00
|
|
|
job.Stdout.Write(sf.FormatStatus("", "The push refers to a repository [%s] (len: %d)", repoInfo.CanonicalName, reposLen))
|
|
|
|
// If it fails, try to get the repository
|
|
|
|
localRepo, exists := s.Repositories[repoInfo.LocalName]
|
|
|
|
if !exists {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Repository does not exist: %s", repoInfo.LocalName)
|
2015-02-10 18:46:42 -05:00
|
|
|
}
|
2015-03-04 15:05:17 -05:00
|
|
|
|
2015-03-03 13:59:50 -05:00
|
|
|
if repoInfo.Index.Official || endpoint.Version == registry.APIVersion2 {
|
2015-03-04 15:05:17 -05:00
|
|
|
err := s.pushV2Repository(r, localRepo, job.Stdout, repoInfo, tag, sf)
|
|
|
|
if err == nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2015-03-04 15:05:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != ErrV2RegistryUnavailable {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Error pushing to registry: %s", err)
|
2015-03-04 15:05:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 18:46:42 -05:00
|
|
|
if err := s.pushRepository(r, job.Stdout, repoInfo, localRepo, tag, sf); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2015-02-10 18:46:42 -05:00
|
|
|
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|