github.com/grange74/docker@v1.6.0-rc3/api/common.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"mime"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	log "github.com/Sirupsen/logrus"
    11  	"github.com/docker/docker/engine"
    12  	"github.com/docker/docker/pkg/parsers"
    13  	"github.com/docker/docker/pkg/version"
    14  	"github.com/docker/libtrust"
    15  )
    16  
    17  const (
    18  	APIVERSION            version.Version = "1.18"
    19  	DEFAULTHTTPHOST                       = "127.0.0.1"
    20  	DEFAULTUNIXSOCKET                     = "/var/run/docker.sock"
    21  	DefaultDockerfileName string          = "Dockerfile"
    22  )
    23  
    24  func ValidateHost(val string) (string, error) {
    25  	host, err := parsers.ParseHost(DEFAULTHTTPHOST, DEFAULTUNIXSOCKET, val)
    26  	if err != nil {
    27  		return val, err
    28  	}
    29  	return host, nil
    30  }
    31  
    32  // TODO remove, used on < 1.5 in getContainersJSON
    33  func DisplayablePorts(ports *engine.Table) string {
    34  	var (
    35  		result          = []string{}
    36  		hostMappings    = []string{}
    37  		firstInGroupMap map[string]int
    38  		lastInGroupMap  map[string]int
    39  	)
    40  	firstInGroupMap = make(map[string]int)
    41  	lastInGroupMap = make(map[string]int)
    42  	ports.SetKey("PrivatePort")
    43  	ports.Sort()
    44  	for _, port := range ports.Data {
    45  		var (
    46  			current      = port.GetInt("PrivatePort")
    47  			portKey      = port.Get("Type")
    48  			firstInGroup int
    49  			lastInGroup  int
    50  		)
    51  		if port.Get("IP") != "" {
    52  			if port.GetInt("PublicPort") != current {
    53  				hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.Get("IP"), port.GetInt("PublicPort"), port.GetInt("PrivatePort"), port.Get("Type")))
    54  				continue
    55  			}
    56  			portKey = fmt.Sprintf("%s/%s", port.Get("IP"), port.Get("Type"))
    57  		}
    58  		firstInGroup = firstInGroupMap[portKey]
    59  		lastInGroup = lastInGroupMap[portKey]
    60  
    61  		if firstInGroup == 0 {
    62  			firstInGroupMap[portKey] = current
    63  			lastInGroupMap[portKey] = current
    64  			continue
    65  		}
    66  
    67  		if current == (lastInGroup + 1) {
    68  			lastInGroupMap[portKey] = current
    69  			continue
    70  		}
    71  		result = append(result, FormGroup(portKey, firstInGroup, lastInGroup))
    72  		firstInGroupMap[portKey] = current
    73  		lastInGroupMap[portKey] = current
    74  	}
    75  	for portKey, firstInGroup := range firstInGroupMap {
    76  		result = append(result, FormGroup(portKey, firstInGroup, lastInGroupMap[portKey]))
    77  	}
    78  	result = append(result, hostMappings...)
    79  	return strings.Join(result, ", ")
    80  }
    81  
    82  func FormGroup(key string, start, last int) string {
    83  	var (
    84  		group     string
    85  		parts     = strings.Split(key, "/")
    86  		groupType = parts[0]
    87  		ip        = ""
    88  	)
    89  	if len(parts) > 1 {
    90  		ip = parts[0]
    91  		groupType = parts[1]
    92  	}
    93  	if start == last {
    94  		group = fmt.Sprintf("%d", start)
    95  	} else {
    96  		group = fmt.Sprintf("%d-%d", start, last)
    97  	}
    98  	if ip != "" {
    99  		group = fmt.Sprintf("%s:%s->%s", ip, group, group)
   100  	}
   101  	return fmt.Sprintf("%s/%s", group, groupType)
   102  }
   103  
   104  func MatchesContentType(contentType, expectedType string) bool {
   105  	mimetype, _, err := mime.ParseMediaType(contentType)
   106  	if err != nil {
   107  		log.Errorf("Error parsing media type: %s error: %v", contentType, err)
   108  	}
   109  	return err == nil && mimetype == expectedType
   110  }
   111  
   112  // LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
   113  // otherwise generates a new one
   114  func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
   115  	err := os.MkdirAll(filepath.Dir(trustKeyPath), 0700)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
   120  	if err == libtrust.ErrKeyFileDoesNotExist {
   121  		trustKey, err = libtrust.GenerateECP256PrivateKey()
   122  		if err != nil {
   123  			return nil, fmt.Errorf("Error generating key: %s", err)
   124  		}
   125  		if err := libtrust.SaveKey(trustKeyPath, trustKey); err != nil {
   126  			return nil, fmt.Errorf("Error saving key file: %s", err)
   127  		}
   128  	} else if err != nil {
   129  		return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
   130  	}
   131  	return trustKey, nil
   132  }