common.go 3.5 KB

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