2018-02-05 21:05:59 +00:00
|
|
|
package logger // import "github.com/docker/docker/daemon/logger"
|
2016-11-14 18:53:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/plugins/logdriver"
|
2018-05-30 19:00:42 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-11-14 18:53:53 +00:00
|
|
|
getter "github.com/docker/docker/pkg/plugingetter"
|
2018-04-25 01:45:00 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2016-11-14 18:53:53 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pluginGetter getter.PluginGetter
|
|
|
|
|
|
|
|
const extName = "LogDriver"
|
|
|
|
|
|
|
|
// logPlugin defines the available functions that logging plugins must implement.
|
|
|
|
type logPlugin interface {
|
|
|
|
StartLogging(streamPath string, info Info) (err error)
|
|
|
|
StopLogging(streamPath string) (err error)
|
|
|
|
Capabilities() (cap Capability, err error)
|
|
|
|
ReadLogs(info Info, config ReadConfig) (stream io.ReadCloser, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterPluginGetter sets the plugingetter
|
|
|
|
func RegisterPluginGetter(plugingetter getter.PluginGetter) {
|
|
|
|
pluginGetter = plugingetter
|
|
|
|
}
|
|
|
|
|
2023-04-25 05:39:28 +00:00
|
|
|
// getPlugin returns a logging driver by its name.
|
2016-11-14 18:53:53 +00:00
|
|
|
// If the driver is empty, it looks for the local driver.
|
|
|
|
func getPlugin(name string, mode int) (Creator, error) {
|
|
|
|
p, err := pluginGetter.Get(name, extName, mode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error looking up logging plugin %s: %v", name, err)
|
|
|
|
}
|
|
|
|
|
2018-04-25 01:45:00 +00:00
|
|
|
client, err := makePluginClient(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return makePluginCreator(name, client, p.ScopedPath), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePluginClient(p getter.CompatPlugin) (logPlugin, error) {
|
2018-05-30 19:00:42 +00:00
|
|
|
if pc, ok := p.(getter.PluginWithV1Client); ok {
|
|
|
|
return &logPluginProxy{pc.Client()}, nil
|
|
|
|
}
|
2018-04-25 01:45:00 +00:00
|
|
|
pa, ok := p.(getter.PluginAddr)
|
|
|
|
if !ok {
|
2018-05-30 19:00:42 +00:00
|
|
|
return nil, errdefs.System(errors.Errorf("got unknown plugin type %T", p))
|
2018-04-25 01:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
|
|
|
|
return nil, errors.Errorf("plugin protocol not supported: %s", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := pa.Addr()
|
|
|
|
c, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error making plugin client")
|
|
|
|
}
|
|
|
|
return &logPluginProxy{c}, nil
|
2016-11-14 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 01:45:00 +00:00
|
|
|
func makePluginCreator(name string, l logPlugin, scopePath func(s string) string) Creator {
|
2016-11-14 18:53:53 +00:00
|
|
|
return func(logCtx Info) (logger Logger, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
pluginGetter.Get(name, extName, getter.Release)
|
|
|
|
}
|
|
|
|
}()
|
2017-12-14 15:27:10 +00:00
|
|
|
|
|
|
|
unscopedPath := filepath.Join("/", "run", "docker", "logging")
|
|
|
|
logRoot := scopePath(unscopedPath)
|
|
|
|
if err := os.MkdirAll(logRoot, 0700); err != nil {
|
2016-11-14 18:53:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:21:18 +00:00
|
|
|
id := stringid.GenerateRandomID()
|
2016-11-14 18:53:53 +00:00
|
|
|
a := &pluginAdapter{
|
|
|
|
driverName: name,
|
|
|
|
id: id,
|
|
|
|
plugin: l,
|
2017-12-14 15:27:10 +00:00
|
|
|
fifoPath: filepath.Join(logRoot, id),
|
2016-11-14 18:53:53 +00:00
|
|
|
logInfo: logCtx,
|
|
|
|
}
|
|
|
|
|
|
|
|
cap, err := a.plugin.Capabilities()
|
|
|
|
if err == nil {
|
|
|
|
a.capabilities = cap
|
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := openPluginStream(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.stream = stream
|
|
|
|
a.enc = logdriver.NewLogEntryEncoder(a.stream)
|
|
|
|
|
2017-12-14 15:27:10 +00:00
|
|
|
if err := l.StartLogging(filepath.Join(unscopedPath, id), logCtx); err != nil {
|
2016-11-14 18:53:53 +00:00
|
|
|
return nil, errors.Wrapf(err, "error creating logger")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cap.ReadLogs {
|
|
|
|
return &pluginAdapterWithRead{a}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
}
|