common.go 3.9 KB

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