github.com/a4a881d4/docker@v1.9.0-rc2/api/common.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "mime" 6 "path/filepath" 7 "sort" 8 "strconv" 9 "strings" 10 11 "github.com/Sirupsen/logrus" 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/pkg/system" 14 "github.com/docker/docker/pkg/version" 15 "github.com/docker/libtrust" 16 ) 17 18 // Common constants for daemon and client. 19 const ( 20 // Version of Current REST API 21 Version version.Version = "1.21" 22 23 // MinVersion represents Minimun REST API version supported 24 MinVersion version.Version = "1.12" 25 26 // DefaultDockerfileName is the Default filename with Docker commands, read by docker build 27 DefaultDockerfileName string = "Dockerfile" 28 ) 29 30 // byPrivatePort is temporary type used to sort types.Port by PrivatePort 31 type byPrivatePort []types.Port 32 33 func (r byPrivatePort) Len() int { return len(r) } 34 func (r byPrivatePort) Swap(i, j int) { r[i], r[j] = r[j], r[i] } 35 func (r byPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort } 36 37 // DisplayablePorts returns formatted string representing open ports of container 38 // e.g. "0.0.0.0:80->9090/tcp, 9988/tcp" 39 // it's used by command 'docker ps' 40 func DisplayablePorts(ports []types.Port) string { 41 type portGroup struct { 42 first int 43 last int 44 } 45 groupMap := make(map[string]*portGroup) 46 var result []string 47 var hostMappings []string 48 sort.Sort(byPrivatePort(ports)) 49 for _, port := range ports { 50 current := port.PrivatePort 51 portKey := port.Type 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 group := groupMap[portKey] 60 61 if group == nil { 62 groupMap[portKey] = &portGroup{first: current, last: current} 63 continue 64 } 65 if current == (group.last + 1) { 66 group.last = current 67 continue 68 } 69 70 result = append(result, formGroup(portKey, group.first, group.last)) 71 groupMap[portKey] = &portGroup{first: current, last: current} 72 } 73 for portKey, g := range groupMap { 74 result = append(result, formGroup(portKey, g.first, g.last)) 75 } 76 result = append(result, hostMappings...) 77 return strings.Join(result, ", ") 78 } 79 80 func formGroup(key string, start, last int) string { 81 parts := strings.Split(key, "/") 82 groupType := parts[0] 83 var ip string 84 if len(parts) > 1 { 85 ip = parts[0] 86 groupType = parts[1] 87 } 88 group := strconv.Itoa(start) 89 if start != last { 90 group = fmt.Sprintf("%s-%d", group, last) 91 } 92 if ip != "" { 93 group = fmt.Sprintf("%s:%s->%s", ip, group, group) 94 } 95 return fmt.Sprintf("%s/%s", group, groupType) 96 } 97 98 // MatchesContentType validates the content type against the expected one 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 }