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"
|
|
|
|
"path"
|
2014-05-12 18:26:23 -04:00
|
|
|
"strings"
|
|
|
|
|
2014-10-24 13:12:35 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2014-07-28 20:23:38 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/version"
|
2014-11-17 14:23:41 -05:00
|
|
|
"github.com/docker/docker/vendor/src/github.com/docker/libtrust"
|
2014-02-24 14:48:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-10-23 20:23:36 -04:00
|
|
|
APIVERSION version.Version = "1.16"
|
2014-04-01 18:46:52 -04:00
|
|
|
DEFAULTHTTPHOST = "127.0.0.1"
|
|
|
|
DEFAULTUNIXSOCKET = "/var/run/docker.sock"
|
2014-02-24 14:48:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateHost(val string) (string, error) {
|
2014-07-28 20:23:38 -04:00
|
|
|
host, err := parsers.ParseHost(DEFAULTHTTPHOST, DEFAULTUNIXSOCKET, val)
|
2014-02-24 14:48:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO remove, used on < 1.5 in getContainersJSON
|
2014-03-28 18:59:29 -04:00
|
|
|
func DisplayablePorts(ports *engine.Table) string {
|
2014-02-24 14:48:14 -05:00
|
|
|
result := []string{}
|
2014-03-16 12:28:13 -04:00
|
|
|
ports.SetKey("PublicPort")
|
|
|
|
ports.Sort()
|
2014-02-24 14:48:14 -05:00
|
|
|
for _, port := range ports.Data {
|
|
|
|
if port.Get("IP") == "" {
|
2014-05-12 18:26:23 -04:00
|
|
|
result = append(result, fmt.Sprintf("%d/%s", port.GetInt("PrivatePort"), port.Get("Type")))
|
2014-02-24 14:48:14 -05:00
|
|
|
} else {
|
|
|
|
result = append(result, fmt.Sprintf("%s:%d->%d/%s", port.Get("IP"), port.GetInt("PublicPort"), port.GetInt("PrivatePort"), port.Get("Type")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(result, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func MatchesContentType(contentType, expectedType string) bool {
|
|
|
|
mimetype, _, err := mime.ParseMediaType(contentType)
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error parsing media type: %s error: %s", contentType, err.Error())
|
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) {
|
|
|
|
err := os.MkdirAll(path.Dir(trustKeyPath), 0700)
|
|
|
|
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 {
|
|
|
|
log.Fatalf("Error loading key file: %s", err)
|
|
|
|
}
|
|
|
|
return trustKey, nil
|
|
|
|
}
|