2018-10-06 03:32:43 +00:00
|
|
|
package docklog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/drivers/docker/docklog/proto"
|
|
|
|
"github.com/hashicorp/nomad/helper/discover"
|
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2018-11-06 05:39:00 +00:00
|
|
|
const PluginName = "docker_logger"
|
2018-10-16 17:49:50 +00:00
|
|
|
|
|
|
|
// LaunchDockerLogger launches an instance of DockerLogger
|
|
|
|
func LaunchDockerLogger(logger hclog.Logger) (DockerLogger, *plugin.Client, error) {
|
2018-11-06 05:39:00 +00:00
|
|
|
logger = logger.Named(PluginName)
|
2018-10-06 03:32:43 +00:00
|
|
|
bin, err := discover.NomadExecutable()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := plugin.NewClient(&plugin.ClientConfig{
|
|
|
|
HandshakeConfig: base.Handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
2018-11-12 18:51:53 +00:00
|
|
|
PluginName: &Plugin{impl: NewDockerLogger(logger)},
|
2018-10-06 03:32:43 +00:00
|
|
|
},
|
2018-11-06 05:39:00 +00:00
|
|
|
Cmd: exec.Command(bin, PluginName),
|
2018-10-06 03:32:43 +00:00
|
|
|
AllowedProtocols: []plugin.Protocol{
|
|
|
|
plugin.ProtocolGRPC,
|
|
|
|
},
|
2019-01-09 23:06:58 +00:00
|
|
|
Logger: logger,
|
2018-10-06 03:32:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-06 05:39:00 +00:00
|
|
|
raw, err := rpcClient.Dispense(PluginName)
|
2018-10-06 03:32:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:49:50 +00:00
|
|
|
l := raw.(DockerLogger)
|
2018-10-06 03:32:43 +00:00
|
|
|
return l, client, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-12 18:51:53 +00:00
|
|
|
func ReattachDockerLogger(reattachCfg *plugin.ReattachConfig) (DockerLogger, *plugin.Client, error) {
|
|
|
|
client := plugin.NewClient(&plugin.ClientConfig{
|
|
|
|
HandshakeConfig: base.Handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
|
|
|
PluginName: &Plugin{impl: NewDockerLogger(hclog.L().Named(PluginName))},
|
|
|
|
},
|
|
|
|
Reattach: reattachCfg,
|
|
|
|
AllowedProtocols: []plugin.Protocol{
|
|
|
|
plugin.ProtocolGRPC,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := rpcClient.Dispense(PluginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := raw.(DockerLogger)
|
|
|
|
return l, client, nil
|
|
|
|
}
|
|
|
|
|
2018-10-06 03:32:43 +00:00
|
|
|
// Plugin is the go-plugin implementation
|
|
|
|
type Plugin struct {
|
|
|
|
plugin.NetRPCUnsupportedPlugin
|
2018-10-16 17:49:50 +00:00
|
|
|
impl DockerLogger
|
2018-10-06 03:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 05:39:00 +00:00
|
|
|
func NewPlugin(impl DockerLogger) *Plugin {
|
|
|
|
return &Plugin{impl: impl}
|
|
|
|
}
|
|
|
|
|
2018-10-06 03:32:43 +00:00
|
|
|
// GRPCServer registered the server side implementation with the grpc server
|
|
|
|
func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
|
2018-10-16 17:49:50 +00:00
|
|
|
proto.RegisterDockerLoggerServer(s, &dockerLoggerServer{
|
2018-10-06 03:32:43 +00:00
|
|
|
impl: p.impl,
|
|
|
|
broker: broker,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GRPCClient returns a client side implementation of the plugin
|
|
|
|
func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
|
2018-10-16 17:49:50 +00:00
|
|
|
return &dockerLoggerClient{client: proto.NewDockerLoggerClient(c)}, nil
|
2018-10-06 03:32:43 +00:00
|
|
|
}
|