2014-02-24 19:48:14 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-12 22:26:23 +00:00
|
|
|
"mime"
|
2014-11-17 19:23:41 +00:00
|
|
|
"os"
|
2015-01-23 22:44:30 +00:00
|
|
|
"path/filepath"
|
2014-05-12 22:26:23 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/engine"
|
2014-07-29 00:23:38 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/version"
|
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-04-01 20:35:38 +00:00
|
|
|
APIVERSION version.Version = "1.19" // Current REST API version
|
2015-03-24 01:30:09 +00:00
|
|
|
DEFAULTHTTPHOST = "127.0.0.1" // Default HTTP Host used if only port is provided to -H flag e.g. docker -d -H tcp://:8080
|
|
|
|
DEFAULTUNIXSOCKET = "/var/run/docker.sock" // Docker daemon by default always listens on the default unix socket
|
|
|
|
DefaultDockerfileName string = "Dockerfile" // Default filename with Docker commands, read by docker build
|
2014-02-24 19:48:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateHost(val string) (string, error) {
|
2014-07-29 00:23:38 +00:00
|
|
|
host, err := parsers.ParseHost(DEFAULTHTTPHOST, DEFAULTUNIXSOCKET, val)
|
2014-02-24 19:48:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
2015-01-28 12:56:22 +00:00
|
|
|
// TODO remove, used on < 1.5 in getContainersJSON
|
2014-03-28 22:59:29 +00:00
|
|
|
func DisplayablePorts(ports *engine.Table) string {
|
2015-01-28 12:56:22 +00:00
|
|
|
var (
|
|
|
|
result = []string{}
|
|
|
|
hostMappings = []string{}
|
2015-02-16 19:08:32 +00:00
|
|
|
firstInGroupMap map[string]int
|
2015-01-28 12:56:22 +00:00
|
|
|
lastInGroupMap map[string]int
|
|
|
|
)
|
2015-02-16 19:08:32 +00:00
|
|
|
firstInGroupMap = make(map[string]int)
|
2015-01-28 12:56:22 +00:00
|
|
|
lastInGroupMap = make(map[string]int)
|
|
|
|
ports.SetKey("PrivatePort")
|
2014-03-16 16:28:13 +00:00
|
|
|
ports.Sort()
|
2014-02-24 19:48:14 +00:00
|
|
|
for _, port := range ports.Data {
|
2015-01-28 12:56:22 +00:00
|
|
|
var (
|
|
|
|
current = port.GetInt("PrivatePort")
|
|
|
|
portKey = port.Get("Type")
|
2015-02-16 19:08:32 +00:00
|
|
|
firstInGroup int
|
2015-01-28 12:56:22 +00:00
|
|
|
lastInGroup int
|
|
|
|
)
|
|
|
|
if port.Get("IP") != "" {
|
|
|
|
if port.GetInt("PublicPort") != current {
|
|
|
|
hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.Get("IP"), port.GetInt("PublicPort"), port.GetInt("PrivatePort"), port.Get("Type")))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
portKey = fmt.Sprintf("%s/%s", port.Get("IP"), port.Get("Type"))
|
2014-02-24 19:48:14 +00:00
|
|
|
}
|
2015-02-16 19:08:32 +00:00
|
|
|
firstInGroup = firstInGroupMap[portKey]
|
2015-01-28 12:56:22 +00:00
|
|
|
lastInGroup = lastInGroupMap[portKey]
|
|
|
|
|
2015-02-16 19:08:32 +00:00
|
|
|
if firstInGroup == 0 {
|
|
|
|
firstInGroupMap[portKey] = current
|
2015-01-28 12:56:22 +00:00
|
|
|
lastInGroupMap[portKey] = current
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if current == (lastInGroup + 1) {
|
|
|
|
lastInGroupMap[portKey] = current
|
|
|
|
continue
|
|
|
|
}
|
2015-02-16 19:08:32 +00:00
|
|
|
result = append(result, FormGroup(portKey, firstInGroup, lastInGroup))
|
|
|
|
firstInGroupMap[portKey] = current
|
2015-01-28 12:56:22 +00:00
|
|
|
lastInGroupMap[portKey] = current
|
|
|
|
}
|
2015-02-16 19:08:32 +00:00
|
|
|
for portKey, firstInGroup := range firstInGroupMap {
|
|
|
|
result = append(result, FormGroup(portKey, firstInGroup, lastInGroupMap[portKey]))
|
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-01-28 12:56:22 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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-01-23 22:44:30 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(trustKeyPath), 0700)
|
2014-11-17 19:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
}
|