common.go 3.6 KB

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