distribution/pull_v2_windows.go
694df3ff
 package distribution
 
 import (
38aef56e
 	"fmt"
05bd0435
 	"net/http"
 	"os"
38aef56e
 	"runtime"
 	"sort"
 	"strings"
694df3ff
 
05bd0435
 	"github.com/docker/distribution"
 	"github.com/docker/distribution/context"
38aef56e
 	"github.com/docker/distribution/manifest/manifestlist"
2c60430a
 	"github.com/docker/distribution/manifest/schema2"
05bd0435
 	"github.com/docker/distribution/registry/client/transport"
38aef56e
 	"github.com/docker/docker/pkg/system"
1009e6a4
 	"github.com/sirupsen/logrus"
694df3ff
 )
 
2c60430a
 var _ distribution.Describable = &v2LayerDescriptor{}
05bd0435
 
2c60430a
 func (ld *v2LayerDescriptor) Descriptor() distribution.Descriptor {
 	if ld.src.MediaType == schema2.MediaTypeForeignLayer && len(ld.src.URLs) > 0 {
 		return ld.src
 	}
 	return distribution.Descriptor{}
05bd0435
 }
 
 func (ld *v2LayerDescriptor) open(ctx context.Context) (distribution.ReadSeekCloser, error) {
67fdf574
 	blobs := ld.repo.Blobs(ctx)
 	rsc, err := blobs.Open(ctx, ld.digest)
 
2c60430a
 	if len(ld.src.URLs) == 0 {
67fdf574
 		return rsc, err
05bd0435
 	}
 
67fdf574
 	// We're done if the registry has this blob.
 	if err == nil {
 		// Seek does an HTTP GET.  If it succeeds, the blob really is accessible.
 		if _, err = rsc.Seek(0, os.SEEK_SET); err == nil {
 			return rsc, nil
 		}
 		rsc.Close()
 	}
05bd0435
 
 	// Find the first URL that results in a 200 result code.
2c60430a
 	for _, url := range ld.src.URLs {
6b7d0280
 		logrus.Debugf("Pulling %v from foreign URL %v", ld.digest, url)
05bd0435
 		rsc = transport.NewHTTPReadSeeker(http.DefaultClient, url, nil)
67fdf574
 
 		// Seek does an HTTP GET.  If it succeeds, the blob really is accessible.
05bd0435
 		_, err = rsc.Seek(0, os.SEEK_SET)
 		if err == nil {
 			break
 		}
6b7d0280
 		logrus.Debugf("Download for %v failed: %v", ld.digest, err)
05bd0435
 		rsc.Close()
 		rsc = nil
 	}
 	return rsc, err
 }
38aef56e
 
d98ecf2d
 func filterManifests(manifests []manifestlist.ManifestDescriptor, os string) []manifestlist.ManifestDescriptor {
 	osVersion := ""
 	if os == "windows" {
8ed8f4a7
 		// TODO: Add UBR (Update Build Release) component after build
d98ecf2d
 		version := system.GetOSVersion()
 		osVersion = fmt.Sprintf("%d.%d.%d", version.MajorVersion, version.MinorVersion, version.Build)
8ed8f4a7
 		logrus.Debugf("will prefer entries with version %s", osVersion)
38aef56e
 	}
 
 	var matches []manifestlist.ManifestDescriptor
 	for _, manifestDescriptor := range manifests {
8ed8f4a7
 		// TODO: Consider filtering out greater versions, including only greater UBR
d98ecf2d
 		if manifestDescriptor.Platform.Architecture == runtime.GOARCH && manifestDescriptor.Platform.OS == os {
38aef56e
 			matches = append(matches, manifestDescriptor)
d98ecf2d
 			logrus.Debugf("found match for %s/%s with media type %s, digest %s", os, runtime.GOARCH, manifestDescriptor.MediaType, manifestDescriptor.Digest.String())
38aef56e
 		}
 	}
d98ecf2d
 	if os == "windows" {
8ed8f4a7
 		sort.Stable(manifestsByVersion{osVersion, matches})
d98ecf2d
 	}
38aef56e
 	return matches
 }
 
 func versionMatch(actual, expected string) bool {
8ed8f4a7
 	// Check whether the version matches up to the build, ignoring UBR
 	return strings.HasPrefix(actual, expected+".")
38aef56e
 }
 
8ed8f4a7
 type manifestsByVersion struct {
 	version string
 	list    []manifestlist.ManifestDescriptor
 }
38aef56e
 
 func (mbv manifestsByVersion) Less(i, j int) bool {
 	// TODO: Split version by parts and compare
 	// TODO: Prefer versions which have a greater version number
8ed8f4a7
 	// Move compatible versions to the top, with no other ordering changes
 	return versionMatch(mbv.list[i].Platform.OSVersion, mbv.version) && !versionMatch(mbv.list[j].Platform.OSVersion, mbv.version)
38aef56e
 }
 
 func (mbv manifestsByVersion) Len() int {
8ed8f4a7
 	return len(mbv.list)
38aef56e
 }
 
 func (mbv manifestsByVersion) Swap(i, j int) {
8ed8f4a7
 	mbv.list[i], mbv.list[j] = mbv.list[j], mbv.list[i]
38aef56e
 }