github.com/rhcarvalho/docker@v1.8.2/api/common.go (about)

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