common.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package api
  2. import (
  3. "fmt"
  4. "mime"
  5. "path/filepath"
  6. "sort"
  7. "strings"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/pkg/system"
  11. "github.com/docker/docker/pkg/version"
  12. "github.com/docker/libtrust"
  13. )
  14. // Common constants for daemon and client.
  15. const (
  16. // Version of Current REST API
  17. Version version.Version = "1.21"
  18. // MinVersion represents Minimun REST API version supported
  19. MinVersion version.Version = "1.12"
  20. // DefaultDockerfileName is the Default filename with Docker commands, read by docker build
  21. DefaultDockerfileName string = "Dockerfile"
  22. )
  23. // byPrivatePort is temporary type used to sort types.Port by PrivatePort
  24. type byPrivatePort []types.Port
  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. // DisplayablePorts returns formatted string representing open ports of container
  29. // e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
  30. // it's used by command 'docker ps'
  31. func DisplayablePorts(ports []types.Port) string {
  32. var (
  33. result = []string{}
  34. hostMappings = []string{}
  35. firstInGroupMap map[string]int
  36. lastInGroupMap map[string]int
  37. )
  38. firstInGroupMap = make(map[string]int)
  39. lastInGroupMap = make(map[string]int)
  40. sort.Sort(byPrivatePort(ports))
  41. for _, port := range ports {
  42. var (
  43. current = port.PrivatePort
  44. portKey = port.Type
  45. firstInGroup int
  46. lastInGroup int
  47. )
  48. if port.IP != "" {
  49. if port.PublicPort != current {
  50. hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
  51. continue
  52. }
  53. portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
  54. }
  55. firstInGroup = firstInGroupMap[portKey]
  56. lastInGroup = lastInGroupMap[portKey]
  57. if firstInGroup == 0 {
  58. firstInGroupMap[portKey] = current
  59. lastInGroupMap[portKey] = current
  60. continue
  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. func formGroup(key string, start, last int) string {
  77. var (
  78. group string
  79. parts = strings.Split(key, "/")
  80. groupType = parts[0]
  81. ip = ""
  82. )
  83. if len(parts) > 1 {
  84. ip = parts[0]
  85. groupType = parts[1]
  86. }
  87. if start == last {
  88. group = fmt.Sprintf("%d", start)
  89. } else {
  90. group = fmt.Sprintf("%d-%d", start, 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. // MatchesContentType validates the content type against the expected one
  98. func MatchesContentType(contentType, expectedType string) bool {
  99. mimetype, _, err := mime.ParseMediaType(contentType)
  100. if err != nil {
  101. logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
  102. }
  103. return err == nil && mimetype == expectedType
  104. }
  105. // LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
  106. // otherwise generates a new one
  107. func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
  108. err := system.MkdirAll(filepath.Dir(trustKeyPath), 0700)
  109. if err != nil {
  110. return nil, err
  111. }
  112. trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
  113. if err == libtrust.ErrKeyFileDoesNotExist {
  114. trustKey, err = libtrust.GenerateECP256PrivateKey()
  115. if err != nil {
  116. return nil, fmt.Errorf("Error generating key: %s", err)
  117. }
  118. if err := libtrust.SaveKey(trustKeyPath, trustKey); err != nil {
  119. return nil, fmt.Errorf("Error saving key file: %s", err)
  120. }
  121. } else if err != nil {
  122. return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
  123. }
  124. return trustKey, nil
  125. }