plugin.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package logger // import "github.com/docker/docker/daemon/logger"
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "github.com/docker/docker/api/types/plugins/logdriver"
  8. getter "github.com/docker/docker/pkg/plugingetter"
  9. "github.com/docker/docker/pkg/plugins"
  10. "github.com/docker/docker/pkg/stringid"
  11. "github.com/pkg/errors"
  12. )
  13. var pluginGetter getter.PluginGetter
  14. const extName = "LogDriver"
  15. // logPlugin defines the available functions that logging plugins must implement.
  16. type logPlugin interface {
  17. StartLogging(streamPath string, info Info) (err error)
  18. StopLogging(streamPath string) (err error)
  19. Capabilities() (cap Capability, err error)
  20. ReadLogs(info Info, config ReadConfig) (stream io.ReadCloser, err error)
  21. }
  22. // RegisterPluginGetter sets the plugingetter
  23. func RegisterPluginGetter(plugingetter getter.PluginGetter) {
  24. pluginGetter = plugingetter
  25. }
  26. // GetDriver returns a logging driver by its name.
  27. // If the driver is empty, it looks for the local driver.
  28. func getPlugin(name string, mode int) (Creator, error) {
  29. p, err := pluginGetter.Get(name, extName, mode)
  30. if err != nil {
  31. return nil, fmt.Errorf("error looking up logging plugin %s: %v", name, err)
  32. }
  33. client, err := makePluginClient(p)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return makePluginCreator(name, client, p.ScopedPath), nil
  38. }
  39. func makePluginClient(p getter.CompatPlugin) (logPlugin, error) {
  40. pa, ok := p.(getter.PluginAddr)
  41. if !ok {
  42. return &logPluginProxy{p.Client()}, nil
  43. }
  44. if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
  45. return nil, errors.Errorf("plugin protocol not supported: %s", p)
  46. }
  47. addr := pa.Addr()
  48. c, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout())
  49. if err != nil {
  50. return nil, errors.Wrap(err, "error making plugin client")
  51. }
  52. return &logPluginProxy{c}, nil
  53. }
  54. func makePluginCreator(name string, l logPlugin, scopePath func(s string) string) Creator {
  55. return func(logCtx Info) (logger Logger, err error) {
  56. defer func() {
  57. if err != nil {
  58. pluginGetter.Get(name, extName, getter.Release)
  59. }
  60. }()
  61. unscopedPath := filepath.Join("/", "run", "docker", "logging")
  62. logRoot := scopePath(unscopedPath)
  63. if err := os.MkdirAll(logRoot, 0700); err != nil {
  64. return nil, err
  65. }
  66. id := stringid.GenerateNonCryptoID()
  67. a := &pluginAdapter{
  68. driverName: name,
  69. id: id,
  70. plugin: l,
  71. fifoPath: filepath.Join(logRoot, id),
  72. logInfo: logCtx,
  73. }
  74. cap, err := a.plugin.Capabilities()
  75. if err == nil {
  76. a.capabilities = cap
  77. }
  78. stream, err := openPluginStream(a)
  79. if err != nil {
  80. return nil, err
  81. }
  82. a.stream = stream
  83. a.enc = logdriver.NewLogEntryEncoder(a.stream)
  84. if err := l.StartLogging(filepath.Join(unscopedPath, id), logCtx); err != nil {
  85. return nil, errors.Wrapf(err, "error creating logger")
  86. }
  87. if cap.ReadLogs {
  88. return &pluginAdapterWithRead{a}, nil
  89. }
  90. return a, nil
  91. }
  92. }