common.go 3.6 KB

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