common.go 4.1 KB

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