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