common.go 4.0 KB

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