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