common.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package api
  2. import (
  3. "fmt"
  4. "mime"
  5. "os"
  6. "path/filepath"
  7. "sort"
  8. "strings"
  9. "github.com/Sirupsen/logrus"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/pkg/parsers"
  12. "github.com/docker/docker/pkg/version"
  13. "github.com/docker/libtrust"
  14. )
  15. // Common constants for daemon and client.
  16. const (
  17. APIVERSION version.Version = "1.19" // Current REST API version
  18. DEFAULTHTTPHOST = "127.0.0.1" // Default HTTP Host used if only port is provided to -H flag e.g. docker -d -H tcp://:8080
  19. DEFAULTUNIXSOCKET = "/var/run/docker.sock" // Docker daemon by default always listens on the default unix socket
  20. DefaultDockerfileName string = "Dockerfile" // Default filename with Docker commands, read by docker build
  21. )
  22. func ValidateHost(val string) (string, error) {
  23. host, err := parsers.ParseHost(DEFAULTHTTPHOST, DEFAULTUNIXSOCKET, val)
  24. if err != nil {
  25. return val, err
  26. }
  27. return host, nil
  28. }
  29. type ByPrivatePort []types.Port
  30. func (r ByPrivatePort) Len() int { return len(r) }
  31. func (r ByPrivatePort) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  32. func (r ByPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort }
  33. func DisplayablePorts(ports []types.Port) string {
  34. var (
  35. result = []string{}
  36. hostMappings = []string{}
  37. firstInGroupMap map[string]int
  38. lastInGroupMap map[string]int
  39. )
  40. firstInGroupMap = make(map[string]int)
  41. lastInGroupMap = make(map[string]int)
  42. sort.Sort(ByPrivatePort(ports))
  43. for _, port := range ports {
  44. var (
  45. current = port.PrivatePort
  46. portKey = port.Type
  47. firstInGroup int
  48. lastInGroup int
  49. )
  50. if port.IP != "" {
  51. if port.PublicPort != current {
  52. hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
  53. continue
  54. }
  55. portKey = fmt.Sprintf("%s/%s", port.IP, port.Type)
  56. }
  57. firstInGroup = firstInGroupMap[portKey]
  58. lastInGroup = lastInGroupMap[portKey]
  59. if firstInGroup == 0 {
  60. firstInGroupMap[portKey] = current
  61. lastInGroupMap[portKey] = current
  62. continue
  63. }
  64. if current == (lastInGroup + 1) {
  65. lastInGroupMap[portKey] = current
  66. continue
  67. }
  68. result = append(result, FormGroup(portKey, firstInGroup, lastInGroup))
  69. firstInGroupMap[portKey] = current
  70. lastInGroupMap[portKey] = current
  71. }
  72. for portKey, firstInGroup := range firstInGroupMap {
  73. result = append(result, FormGroup(portKey, firstInGroup, lastInGroupMap[portKey]))
  74. }
  75. result = append(result, hostMappings...)
  76. return strings.Join(result, ", ")
  77. }
  78. func FormGroup(key string, start, last int) string {
  79. var (
  80. group string
  81. parts = strings.Split(key, "/")
  82. groupType = parts[0]
  83. ip = ""
  84. )
  85. if len(parts) > 1 {
  86. ip = parts[0]
  87. groupType = parts[1]
  88. }
  89. if start == last {
  90. group = fmt.Sprintf("%d", start)
  91. } else {
  92. group = fmt.Sprintf("%d-%d", start, last)
  93. }
  94. if ip != "" {
  95. group = fmt.Sprintf("%s:%s->%s", ip, group, group)
  96. }
  97. return fmt.Sprintf("%s/%s", group, groupType)
  98. }
  99. func MatchesContentType(contentType, expectedType string) bool {
  100. mimetype, _, err := mime.ParseMediaType(contentType)
  101. if err != nil {
  102. logrus.Errorf("Error parsing media type: %s error: %v", contentType, err)
  103. }
  104. return err == nil && mimetype == expectedType
  105. }
  106. // LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
  107. // otherwise generates a new one
  108. func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
  109. err := os.MkdirAll(filepath.Dir(trustKeyPath), 0700)
  110. if err != nil {
  111. return nil, err
  112. }
  113. trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
  114. if err == libtrust.ErrKeyFileDoesNotExist {
  115. trustKey, err = libtrust.GenerateECP256PrivateKey()
  116. if err != nil {
  117. return nil, fmt.Errorf("Error generating key: %s", err)
  118. }
  119. if err := libtrust.SaveKey(trustKeyPath, trustKey); err != nil {
  120. return nil, fmt.Errorf("Error saving key file: %s", err)
  121. }
  122. } else if err != nil {
  123. return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
  124. }
  125. return trustKey, nil
  126. }