common.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package api
  2. import (
  3. "fmt"
  4. "mime"
  5. "path/filepath"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "github.com/Sirupsen/logrus"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/pkg/system"
  12. "github.com/docker/docker/pkg/version"
  13. "github.com/docker/libtrust"
  14. )
  15. // Common constants for daemon and client.
  16. const (
  17. // Version of Current REST API
  18. DefaultVersion version.Version = "1.22"
  19. // MinVersion represents Minimum REST API version supported
  20. MinVersion version.Version = "1.12"
  21. // DefaultDockerfileName is the Default filename with Docker commands, read by docker build
  22. DefaultDockerfileName string = "Dockerfile"
  23. )
  24. // byPortInfo is a temporary type used to sort types.Port by its fields
  25. type byPortInfo []types.Port
  26. func (r byPortInfo) Len() int { return len(r) }
  27. func (r byPortInfo) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  28. func (r byPortInfo) Less(i, j int) bool {
  29. if r[i].PrivatePort != r[j].PrivatePort {
  30. return r[i].PrivatePort < r[j].PrivatePort
  31. }
  32. if r[i].IP != r[j].IP {
  33. return r[i].IP < r[j].IP
  34. }
  35. if r[i].PublicPort != r[j].PublicPort {
  36. return r[i].PublicPort < r[j].PublicPort
  37. }
  38. return r[i].Type < r[j].Type
  39. }
  40. // DisplayablePorts returns formatted string representing open ports of container
  41. // e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
  42. // it's used by command 'docker ps'
  43. func DisplayablePorts(ports []types.Port) string {
  44. type portGroup struct {
  45. first int
  46. last int
  47. }
  48. groupMap := make(map[string]*portGroup)
  49. var result []string
  50. var hostMappings []string
  51. var groupMapKeys []string
  52. sort.Sort(byPortInfo(ports))
  53. for _, port := range ports {
  54. current := port.PrivatePort
  55. portKey := port.Type
  56. if port.IP != "" {
  57. if port.PublicPort != current {
  58. hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
  59. continue
  60. }
  61. portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
  62. }
  63. group := groupMap[portKey]
  64. if group == nil {
  65. groupMap[portKey] = &portGroup{first: current, last: current}
  66. // record order that groupMap keys are created
  67. groupMapKeys = append(groupMapKeys, portKey)
  68. continue
  69. }
  70. if current == (group.last + 1) {
  71. group.last = current
  72. continue
  73. }
  74. result = append(result, formGroup(portKey, group.first, group.last))
  75. groupMap[portKey] = &portGroup{first: current, last: current}
  76. }
  77. for _, portKey := range groupMapKeys {
  78. g := groupMap[portKey]
  79. result = append(result, formGroup(portKey, g.first, g.last))
  80. }
  81. result = append(result, hostMappings...)
  82. return strings.Join(result, ", ")
  83. }
  84. func formGroup(key string, start, last int) string {
  85. parts := strings.Split(key, "/")
  86. groupType := parts[0]
  87. var ip string
  88. if len(parts) > 1 {
  89. ip = parts[0]
  90. groupType = parts[1]
  91. }
  92. group := strconv.Itoa(start)
  93. if start != last {
  94. group = fmt.Sprintf("%s-%d", group, last)
  95. }
  96. if ip != "" {
  97. group = fmt.Sprintf("%s:%s->%s", ip, group, group)
  98. }
  99. return fmt.Sprintf("%s/%s", group, groupType)
  100. }
  101. // MatchesContentType validates the content type against the expected one
  102. func MatchesContentType(contentType, expectedType string) bool {
  103. mimetype, _, err := mime.ParseMediaType(contentType)
  104. if err != nil {
  105. logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
  106. }
  107. return err == nil && mimetype == expectedType
  108. }
  109. // LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
  110. // otherwise generates a new one
  111. func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
  112. err := system.MkdirAll(filepath.Dir(trustKeyPath), 0700)
  113. if err != nil {
  114. return nil, err
  115. }
  116. trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
  117. if err == libtrust.ErrKeyFileDoesNotExist {
  118. trustKey, err = libtrust.GenerateECP256PrivateKey()
  119. if err != nil {
  120. return nil, fmt.Errorf("Error generating key: %s", err)
  121. }
  122. if err := libtrust.SaveKey(trustKeyPath, trustKey); err != nil {
  123. return nil, fmt.Errorf("Error saving key file: %s", err)
  124. }
  125. } else if err != nil {
  126. return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
  127. }
  128. return trustKey, nil
  129. }