2018-02-05 16:05:59 -05:00
|
|
|
package logger // import "github.com/docker/docker/daemon/logger"
|
2016-11-14 13:53:53 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/plugins/logdriver"
|
|
|
|
getter "github.com/docker/docker/pkg/plugingetter"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDriver returns a logging driver by its name.
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &logPluginProxy{p.Client()}
|
2017-12-14 10:27:10 -05:00
|
|
|
return makePluginCreator(name, d, p.ScopedPath), nil
|
2016-11-14 13:53:53 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 10:27:10 -05:00
|
|
|
func makePluginCreator(name string, l *logPluginProxy, scopePath func(s string) string) Creator {
|
2016-11-14 13:53:53 -05:00
|
|
|
return func(logCtx Info) (logger Logger, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
pluginGetter.Get(name, extName, getter.Release)
|
|
|
|
}
|
|
|
|
}()
|
2017-12-14 10:27:10 -05:00
|
|
|
|
|
|
|
unscopedPath := filepath.Join("/", "run", "docker", "logging")
|
|
|
|
logRoot := scopePath(unscopedPath)
|
|
|
|
if err := os.MkdirAll(logRoot, 0700); err != nil {
|
2016-11-14 13:53:53 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := stringid.GenerateNonCryptoID()
|
|
|
|
a := &pluginAdapter{
|
|
|
|
driverName: name,
|
|
|
|
id: id,
|
|
|
|
plugin: l,
|
2017-12-14 10:27:10 -05:00
|
|
|
fifoPath: filepath.Join(logRoot, id),
|
2016-11-14 13:53:53 -05: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 10:27:10 -05:00
|
|
|
if err := l.StartLogging(filepath.Join(unscopedPath, id), logCtx); err != nil {
|
2016-11-14 13:53:53 -05:00
|
|
|
return nil, errors.Wrapf(err, "error creating logger")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cap.ReadLogs {
|
|
|
|
return &pluginAdapterWithRead{a}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
}
|