api/common.go
551355c9
 package api
 
 import (
 	"fmt"
d54dec4d
 	"mime"
2cd5b7da
 	"path/filepath"
065648a8
 	"sort"
d54dec4d
 	"strings"
 
6f4d8470
 	"github.com/Sirupsen/logrus"
065648a8
 	"github.com/docker/docker/api/types"
86d1223a
 	"github.com/docker/docker/pkg/system"
b3ee9ac7
 	"github.com/docker/docker/pkg/version"
7c7026bd
 	"github.com/docker/libtrust"
551355c9
 )
 
c136591f
 // Common constants for daemon and client.
551355c9
 const (
4b9fe9c2
 	APIVERSION            version.Version = "1.19"       // Current REST API version
 	DefaultDockerfileName string          = "Dockerfile" // Default filename with Docker commands, read by docker build
551355c9
 )
 
065648a8
 type ByPrivatePort []types.Port
 
 func (r ByPrivatePort) Len() int           { return len(r) }
 func (r ByPrivatePort) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
 func (r ByPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort }
 
1bfa80bd
 func DisplayablePorts(ports []types.Port) string {
065648a8
 	var (
 		result          = []string{}
 		hostMappings    = []string{}
 		firstInGroupMap map[string]int
 		lastInGroupMap  map[string]int
 	)
 	firstInGroupMap = make(map[string]int)
 	lastInGroupMap = make(map[string]int)
 	sort.Sort(ByPrivatePort(ports))
 	for _, port := range ports {
 		var (
 			current      = port.PrivatePort
 			portKey      = port.Type
 			firstInGroup int
 			lastInGroup  int
 		)
 		if port.IP != "" {
 			if port.PublicPort != current {
 				hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
 				continue
 			}
 			portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
 		}
 		firstInGroup = firstInGroupMap[portKey]
 		lastInGroup = lastInGroupMap[portKey]
 
 		if firstInGroup == 0 {
ba99a73c
 			firstInGroupMap[portKey] = current
5dfef4fe
 			lastInGroupMap[portKey] = current
 			continue
 		}
 
 		if current == (lastInGroup + 1) {
 			lastInGroupMap[portKey] = current
 			continue
 		}
ba99a73c
 		result = append(result, FormGroup(portKey, firstInGroup, lastInGroup))
 		firstInGroupMap[portKey] = current
5dfef4fe
 		lastInGroupMap[portKey] = current
 	}
ba99a73c
 	for portKey, firstInGroup := range firstInGroupMap {
 		result = append(result, FormGroup(portKey, firstInGroup, lastInGroupMap[portKey]))
551355c9
 	}
5dfef4fe
 	result = append(result, hostMappings...)
551355c9
 	return strings.Join(result, ", ")
 }
 
5dfef4fe
 func FormGroup(key string, start, last int) string {
 	var (
 		group     string
 		parts     = strings.Split(key, "/")
 		groupType = parts[0]
 		ip        = ""
 	)
 	if len(parts) > 1 {
 		ip = parts[0]
 		groupType = parts[1]
 	}
 	if start == last {
 		group = fmt.Sprintf("%d", start)
 	} else {
 		group = fmt.Sprintf("%d-%d", start, last)
 	}
 	if ip != "" {
 		group = fmt.Sprintf("%s:%s->%s", ip, group, group)
 	}
 	return fmt.Sprintf("%s/%s", group, groupType)
 }
 
551355c9
 func MatchesContentType(contentType, expectedType string) bool {
 	mimetype, _, err := mime.ParseMediaType(contentType)
 	if err != nil {
6f4d8470
 		logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
551355c9
 	}
 	return err == nil && mimetype == expectedType
 }
9a85f60c
 
 // LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
 // otherwise generates a new one
 func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
86d1223a
 	err := system.MkdirAll(filepath.Dir(trustKeyPath), 0700)
 	if err != nil {
9a85f60c
 		return nil, err
 	}
 	trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
 	if err == libtrust.ErrKeyFileDoesNotExist {
 		trustKey, err = libtrust.GenerateECP256PrivateKey()
 		if err != nil {
 			return nil, fmt.Errorf("Error generating key: %s", err)
 		}
 		if err := libtrust.SaveKey(trustKeyPath, trustKey); err != nil {
 			return nil, fmt.Errorf("Error saving key file: %s", err)
 		}
 	} else if err != nil {
a90e91b5
 		return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
9a85f60c
 	}
 	return trustKey, nil
 }