2014-08-08 02:58:58 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2014-08-08 02:58:58 -04:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
)
|
|
|
|
|
2015-04-20 23:16:25 -04:00
|
|
|
type ImagePushConfig struct {
|
|
|
|
MetaHeaders map[string][]string
|
2015-04-22 08:06:58 -04:00
|
|
|
AuthConfig *cliconfig.AuthConfig
|
2015-04-20 23:16:25 -04:00
|
|
|
Tag string
|
|
|
|
OutStream io.Writer
|
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
type Pusher interface {
|
|
|
|
// Push tries to push the image configured at the creation of Pusher.
|
|
|
|
// Push returns an error if any, as well as a boolean that determines whether to retry Push on the next configured endpoint.
|
|
|
|
//
|
|
|
|
// TODO(tiborvass): have Push() take a reference to repository + tag, so that the pusher itself is repository-agnostic.
|
|
|
|
Push() (fallback bool, err error)
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
func (s *TagStore) NewPusher(endpoint registry.APIEndpoint, localRepo Repository, repoInfo *registry.RepositoryInfo, imagePushConfig *ImagePushConfig, sf *streamformatter.StreamFormatter) (Pusher, error) {
|
|
|
|
switch endpoint.Version {
|
|
|
|
case registry.APIVersion2:
|
|
|
|
return &v2Pusher{
|
2015-07-22 20:29:36 -04:00
|
|
|
TagStore: s,
|
|
|
|
endpoint: endpoint,
|
|
|
|
localRepo: localRepo,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
config: imagePushConfig,
|
|
|
|
sf: sf,
|
|
|
|
layersSeen: make(map[string]bool),
|
2015-02-12 13:23:22 -05:00
|
|
|
}, nil
|
|
|
|
case registry.APIVersion1:
|
|
|
|
return &v1Pusher{
|
|
|
|
TagStore: s,
|
|
|
|
endpoint: endpoint,
|
|
|
|
localRepo: localRepo,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
config: imagePushConfig,
|
|
|
|
sf: sf,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
|
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-04-20 23:16:25 -04:00
|
|
|
func (s *TagStore) Push(localName string, imagePushConfig *ImagePushConfig) error {
|
2015-02-12 13:23:22 -05:00
|
|
|
var sf = streamformatter.NewJSONStreamFormatter()
|
2014-08-08 02:58:58 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
endpoints, err := s.registryService.LookupEndpoints(repoInfo.CanonicalName)
|
2015-02-10 18:46:42 -05: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
|
|
|
reposLen := 1
|
2015-04-20 23:16:25 -04:00
|
|
|
if imagePushConfig.Tag == "" {
|
2015-02-10 18:46:42 -05:00
|
|
|
reposLen = len(s.Repositories[repoInfo.LocalName])
|
2015-01-13 18:48:49 -05:00
|
|
|
}
|
2015-04-20 23:16:25 -04:00
|
|
|
|
|
|
|
imagePushConfig.OutStream.Write(sf.FormatStatus("", "The push refers to a repository [%s] (len: %d)", repoInfo.CanonicalName, reposLen))
|
|
|
|
|
2015-02-10 18:46:42 -05:00
|
|
|
// 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-02-12 13:23:22 -05:00
|
|
|
var lastErr error
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
logrus.Debugf("Trying to push %s to %s %s", repoInfo.CanonicalName, endpoint.URL, endpoint.Version)
|
|
|
|
|
|
|
|
pusher, err := s.NewPusher(endpoint, localRepo, repoInfo, imagePushConfig, sf)
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
continue
|
2015-03-04 15:05:17 -05:00
|
|
|
}
|
2015-02-12 13:23:22 -05:00
|
|
|
if fallback, err := pusher.Push(); err != nil {
|
|
|
|
if fallback {
|
|
|
|
lastErr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.Debugf("Not continuing with error: %v", err)
|
|
|
|
return err
|
2015-03-04 15:05:17 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
s.eventsService.Log("push", repoInfo.LocalName, "")
|
|
|
|
return nil
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|
2015-02-10 18:46:42 -05:00
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
if lastErr == nil {
|
|
|
|
lastErr = fmt.Errorf("no endpoints found for %s", repoInfo.CanonicalName)
|
|
|
|
}
|
|
|
|
return lastErr
|
2014-08-08 02:58:58 -04:00
|
|
|
}
|