common.go 3.6 KB

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