2014-02-24 19:48:14 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-12 22:26:23 +00:00
|
|
|
"mime"
|
2015-01-23 22:44:30 +00:00
|
|
|
"path/filepath"
|
2015-04-04 00:39:06 +00:00
|
|
|
"sort"
|
2015-10-02 20:39:33 +00:00
|
|
|
"strconv"
|
2014-05-12 22:26:23 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-23 22:55:36 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/version"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2014-12-04 01:36:14 +00:00
|
|
|
"github.com/docker/libtrust"
|
2014-02-24 19:48:14 +00:00
|
|
|
)
|
|
|
|
|
2015-03-24 01:30:09 +00:00
|
|
|
// Common constants for daemon and client.
|
2014-02-24 19:48:14 +00:00
|
|
|
const (
|
2015-07-22 03:32:59 +00:00
|
|
|
// Version of Current REST API
|
2016-01-15 15:38:33 +00:00
|
|
|
DefaultVersion version.Version = "1.23"
|
2015-06-17 21:57:32 +00:00
|
|
|
|
2015-12-13 16:00:39 +00:00
|
|
|
// MinVersion represents Minimum REST API version supported
|
2015-06-17 21:57:32 +00:00
|
|
|
MinVersion version.Version = "1.12"
|
|
|
|
|
2015-12-31 13:57:58 +00:00
|
|
|
// NoBaseImageSpecifier is the symbol used by the FROM
|
|
|
|
// command to specify that no base image is to be used.
|
|
|
|
NoBaseImageSpecifier string = "scratch"
|
2014-02-24 19:48:14 +00:00
|
|
|
)
|
|
|
|
|
2015-10-19 15:38:54 +00:00
|
|
|
// byPortInfo is a temporary type used to sort types.Port by its fields
|
|
|
|
type byPortInfo []types.Port
|
2015-04-04 00:39:06 +00:00
|
|
|
|
2015-10-19 15:38:54 +00:00
|
|
|
func (r byPortInfo) Len() int { return len(r) }
|
|
|
|
func (r byPortInfo) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byPortInfo) Less(i, j int) bool {
|
|
|
|
if r[i].PrivatePort != r[j].PrivatePort {
|
|
|
|
return r[i].PrivatePort < r[j].PrivatePort
|
|
|
|
}
|
|
|
|
|
|
|
|
if r[i].IP != r[j].IP {
|
|
|
|
return r[i].IP < r[j].IP
|
|
|
|
}
|
|
|
|
|
|
|
|
if r[i].PublicPort != r[j].PublicPort {
|
|
|
|
return r[i].PublicPort < r[j].PublicPort
|
|
|
|
}
|
|
|
|
|
|
|
|
return r[i].Type < r[j].Type
|
|
|
|
}
|
2015-04-04 00:39:06 +00:00
|
|
|
|
2015-07-22 03:32:59 +00:00
|
|
|
// DisplayablePorts returns formatted string representing open ports of container
|
|
|
|
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
|
|
|
|
// it's used by command 'docker ps'
|
2015-04-07 19:34:30 +00:00
|
|
|
func DisplayablePorts(ports []types.Port) string {
|
2015-10-02 20:39:33 +00:00
|
|
|
type portGroup struct {
|
|
|
|
first int
|
|
|
|
last int
|
|
|
|
}
|
|
|
|
groupMap := make(map[string]*portGroup)
|
|
|
|
var result []string
|
|
|
|
var hostMappings []string
|
2015-10-19 15:38:54 +00:00
|
|
|
var groupMapKeys []string
|
|
|
|
sort.Sort(byPortInfo(ports))
|
2015-04-04 00:39:06 +00:00
|
|
|
for _, port := range ports {
|
2015-10-02 20:39:33 +00:00
|
|
|
current := port.PrivatePort
|
|
|
|
portKey := port.Type
|
2015-04-04 00:39:06 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-10-02 20:39:33 +00:00
|
|
|
group := groupMap[portKey]
|
2015-04-04 00:39:06 +00:00
|
|
|
|
2015-10-02 20:39:33 +00:00
|
|
|
if group == nil {
|
|
|
|
groupMap[portKey] = &portGroup{first: current, last: current}
|
2015-10-19 15:38:54 +00:00
|
|
|
// record order that groupMap keys are created
|
|
|
|
groupMapKeys = append(groupMapKeys, portKey)
|
2015-01-28 12:56:22 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-10-02 20:39:33 +00:00
|
|
|
if current == (group.last + 1) {
|
|
|
|
group.last = current
|
2015-01-28 12:56:22 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-10-02 20:39:33 +00:00
|
|
|
|
|
|
|
result = append(result, formGroup(portKey, group.first, group.last))
|
|
|
|
groupMap[portKey] = &portGroup{first: current, last: current}
|
2015-01-28 12:56:22 +00:00
|
|
|
}
|
2015-10-19 15:38:54 +00:00
|
|
|
for _, portKey := range groupMapKeys {
|
|
|
|
g := groupMap[portKey]
|
2015-10-02 20:39:33 +00:00
|
|
|
result = append(result, formGroup(portKey, g.first, g.last))
|
2014-02-24 19:48:14 +00:00
|
|
|
}
|
2015-01-28 12:56:22 +00:00
|
|
|
result = append(result, hostMappings...)
|
2014-02-24 19:48:14 +00:00
|
|
|
return strings.Join(result, ", ")
|
|
|
|
}
|
|
|
|
|
2015-07-22 03:32:59 +00:00
|
|
|
func formGroup(key string, start, last int) string {
|
2015-10-02 20:39:33 +00:00
|
|
|
parts := strings.Split(key, "/")
|
|
|
|
groupType := parts[0]
|
|
|
|
var ip string
|
2015-01-28 12:56:22 +00:00
|
|
|
if len(parts) > 1 {
|
|
|
|
ip = parts[0]
|
|
|
|
groupType = parts[1]
|
|
|
|
}
|
2015-10-02 20:39:33 +00:00
|
|
|
group := strconv.Itoa(start)
|
|
|
|
if start != last {
|
|
|
|
group = fmt.Sprintf("%s-%d", group, last)
|
2015-01-28 12:56:22 +00:00
|
|
|
}
|
|
|
|
if ip != "" {
|
|
|
|
group = fmt.Sprintf("%s:%s->%s", ip, group, group)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s", group, groupType)
|
|
|
|
}
|
|
|
|
|
2015-07-22 03:32:59 +00:00
|
|
|
// MatchesContentType validates the content type against the expected one
|
2014-02-24 19:48:14 +00:00
|
|
|
func MatchesContentType(contentType, expectedType string) bool {
|
|
|
|
mimetype, _, err := mime.ParseMediaType(contentType)
|
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
|
2014-02-24 19:48:14 +00:00
|
|
|
}
|
|
|
|
return err == nil && mimetype == expectedType
|
|
|
|
}
|
2014-11-17 19:23:41 +00:00
|
|
|
|
|
|
|
// LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
|
|
|
|
// otherwise generates a new one
|
|
|
|
func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
|
2015-04-23 22:55:36 +00:00
|
|
|
err := system.MkdirAll(filepath.Dir(trustKeyPath), 0700)
|
|
|
|
if err != nil {
|
2014-11-17 19:23:41 +00:00
|
|
|
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 {
|
2015-01-26 20:58:45 +00:00
|
|
|
return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
|
2014-11-17 19:23:41 +00:00
|
|
|
}
|
|
|
|
return trustKey, nil
|
|
|
|
}
|