2014-08-27 01:21:04 +02:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2014-10-24 19:12:35 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-08-27 01:21:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// scans string for api version in the URL path. returns the trimmed hostname, if version found, string and API version.
|
2014-10-06 21:34:39 +02:00
|
|
|
func scanForAPIVersion(hostname string) (string, APIVersion) {
|
2014-08-27 01:21:04 +02:00
|
|
|
var (
|
|
|
|
chunks []string
|
|
|
|
apiVersionStr string
|
|
|
|
)
|
|
|
|
if strings.HasSuffix(hostname, "/") {
|
|
|
|
chunks = strings.Split(hostname[:len(hostname)-1], "/")
|
|
|
|
apiVersionStr = chunks[len(chunks)-1]
|
|
|
|
} else {
|
|
|
|
chunks = strings.Split(hostname, "/")
|
|
|
|
apiVersionStr = chunks[len(chunks)-1]
|
|
|
|
}
|
|
|
|
for k, v := range apiVersions {
|
|
|
|
if apiVersionStr == v {
|
|
|
|
hostname = strings.Join(chunks[:len(chunks)-1], "/")
|
|
|
|
return hostname, k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hostname, DefaultAPIVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEndpoint(hostname string) (*Endpoint, error) {
|
2014-10-03 21:46:42 +02:00
|
|
|
endpoint, err := newEndpoint(hostname)
|
2014-08-27 01:21:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint.URL.Scheme = "https"
|
|
|
|
if _, err := endpoint.Ping(); err != nil {
|
|
|
|
log.Debugf("Registry %s does not work (%s), falling back to http", endpoint, err)
|
|
|
|
// TODO: Check if http fallback is enabled
|
|
|
|
endpoint.URL.Scheme = "http"
|
|
|
|
if _, err = endpoint.Ping(); err != nil {
|
|
|
|
return nil, errors.New("Invalid Registry endpoint: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 21:46:42 +02:00
|
|
|
return endpoint, nil
|
|
|
|
}
|
|
|
|
func newEndpoint(hostname string) (*Endpoint, error) {
|
|
|
|
var (
|
|
|
|
endpoint Endpoint
|
|
|
|
trimmedHostname string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if !strings.HasPrefix(hostname, "http") {
|
|
|
|
hostname = "https://" + hostname
|
|
|
|
}
|
|
|
|
trimmedHostname, endpoint.Version = scanForAPIVersion(hostname)
|
|
|
|
endpoint.URL, err = url.Parse(trimmedHostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-27 01:21:04 +02:00
|
|
|
return &endpoint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Endpoint struct {
|
|
|
|
URL *url.URL
|
|
|
|
Version APIVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the formated URL for the root of this registry Endpoint
|
|
|
|
func (e Endpoint) String() string {
|
|
|
|
return fmt.Sprintf("%s/v%d/", e.URL.String(), e.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Endpoint) VersionString(version APIVersion) string {
|
|
|
|
return fmt.Sprintf("%s/v%d/", e.URL.String(), version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Endpoint) Ping() (RegistryInfo, error) {
|
|
|
|
if e.String() == IndexServerAddress() {
|
|
|
|
// Skip the check, we now this one is valid
|
|
|
|
// (and we never want to fallback to http in case of error)
|
|
|
|
return RegistryInfo{Standalone: false}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", e.String()+"_ping", nil)
|
|
|
|
if err != nil {
|
|
|
|
return RegistryInfo{Standalone: false}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := doRequest(req, nil, ConnectTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return RegistryInfo{Standalone: false}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
jsonString, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return RegistryInfo{Standalone: false}, fmt.Errorf("Error while reading the http response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the header is absent, we assume true for compatibility with earlier
|
|
|
|
// versions of the registry. default to true
|
|
|
|
info := RegistryInfo{
|
|
|
|
Standalone: true,
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(jsonString, &info); err != nil {
|
|
|
|
log.Debugf("Error unmarshalling the _ping RegistryInfo: %s", err)
|
|
|
|
// don't stop here. Just assume sane defaults
|
|
|
|
}
|
|
|
|
if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" {
|
|
|
|
log.Debugf("Registry version header: '%s'", hdr)
|
|
|
|
info.Version = hdr
|
|
|
|
}
|
|
|
|
log.Debugf("RegistryInfo.Version: %q", info.Version)
|
|
|
|
|
|
|
|
standalone := resp.Header.Get("X-Docker-Registry-Standalone")
|
|
|
|
log.Debugf("Registry standalone header: '%s'", standalone)
|
|
|
|
// Accepted values are "true" (case-insensitive) and "1".
|
|
|
|
if strings.EqualFold(standalone, "true") || standalone == "1" {
|
|
|
|
info.Standalone = true
|
|
|
|
} else if len(standalone) > 0 {
|
|
|
|
// there is a header set, and it is not "true" or "1", so assume fails
|
|
|
|
info.Standalone = false
|
|
|
|
}
|
2014-10-03 02:41:57 +02:00
|
|
|
log.Debugf("RegistryInfo.Standalone: %t", info.Standalone)
|
2014-08-27 01:21:04 +02:00
|
|
|
return info, nil
|
|
|
|
}
|