2014-02-24 14:48:14 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-12 18:26:23 -04:00
|
|
|
"mime"
|
2014-11-17 14:23:41 -05:00
|
|
|
"os"
|
2015-01-23 17:44:30 -05:00
|
|
|
"path/filepath"
|
2015-04-03 20:39:06 -04:00
|
|
|
"sort"
|
2014-05-12 18:26:23 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-03 20:39:06 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/version"
|
2014-12-03 20:36:14 -05:00
|
|
|
"github.com/docker/libtrust"
|
2014-02-24 14:48:14 -05:00
|
|
|
)
|
|
|
|
|
2015-03-23 21:30:09 -04:00
|
|
|
// Common constants for daemon and client.
|
2014-02-24 14:48:14 -05:00
|
|
|
const (
|
2015-04-13 10:17:14 -04:00
|
|
|
APIVERSION version.Version = "1.19" // Current REST API version
|
|
|
|
DefaultDockerfileName string = "Dockerfile" // Default filename with Docker commands, read by docker build
|
2014-02-24 14:48:14 -05:00
|
|
|
)
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
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 }
|
|
|
|
|
2015-04-07 15:34:30 -04:00
|
|
|
func DisplayablePorts(ports []types.Port) string {
|
2015-04-03 20:39:06 -04:00
|
|
|
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 {
|
2015-02-16 14:08:32 -05:00
|
|
|
firstInGroupMap[portKey] = current
|
2015-01-28 07:56:22 -05:00
|
|
|
lastInGroupMap[portKey] = current
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if current == (lastInGroup + 1) {
|
|
|
|
lastInGroupMap[portKey] = current
|
|
|
|
continue
|
|
|
|
}
|
2015-02-16 14:08:32 -05:00
|
|
|
result = append(result, FormGroup(portKey, firstInGroup, lastInGroup))
|
|
|
|
firstInGroupMap[portKey] = current
|
2015-01-28 07:56:22 -05:00
|
|
|
lastInGroupMap[portKey] = current
|
|
|
|
}
|
2015-02-16 14:08:32 -05:00
|
|
|
for portKey, firstInGroup := range firstInGroupMap {
|
|
|
|
result = append(result, FormGroup(portKey, firstInGroup, lastInGroupMap[portKey]))
|
2014-02-24 14:48:14 -05:00
|
|
|
}
|
2015-01-28 07:56:22 -05:00
|
|
|
result = append(result, hostMappings...)
|
2014-02-24 14:48:14 -05:00
|
|
|
return strings.Join(result, ", ")
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:56:22 -05: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 14:48:14 -05:00
|
|
|
func MatchesContentType(contentType, expectedType string) bool {
|
|
|
|
mimetype, _, err := mime.ParseMediaType(contentType)
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
|
2014-02-24 14:48:14 -05:00
|
|
|
}
|
|
|
|
return err == nil && mimetype == expectedType
|
|
|
|
}
|
2014-11-17 14:23:41 -05: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 17:44:30 -05:00
|
|
|
err := os.MkdirAll(filepath.Dir(trustKeyPath), 0700)
|
2014-11-17 14:23:41 -05: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 15:58:45 -05:00
|
|
|
return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
|
2014-11-17 14:23:41 -05:00
|
|
|
}
|
|
|
|
return trustKey, nil
|
|
|
|
}
|