2018-09-24 18:37:45 +00:00
|
|
|
package logmon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/client/logmon/proto"
|
|
|
|
"github.com/hashicorp/nomad/helper/discover"
|
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2019-02-21 20:02:41 +00:00
|
|
|
// LaunchLogMon launches a new logmon or reattaches to an existing one.
|
2018-09-24 18:37:45 +00:00
|
|
|
// TODO: Integrate with base plugin loader
|
2019-01-15 17:31:57 +00:00
|
|
|
func LaunchLogMon(logger hclog.Logger, reattachConfig *plugin.ReattachConfig) (LogMon, *plugin.Client, error) {
|
2019-01-05 00:08:47 +00:00
|
|
|
logger = logger.Named("logmon")
|
2018-09-24 18:37:45 +00:00
|
|
|
bin, err := discover.NomadExecutable()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:02:41 +00:00
|
|
|
conf := &plugin.ClientConfig{
|
2018-09-24 18:37:45 +00:00
|
|
|
HandshakeConfig: base.Handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
2019-01-05 00:08:47 +00:00
|
|
|
"logmon": &Plugin{},
|
2018-09-24 18:37:45 +00:00
|
|
|
},
|
|
|
|
AllowedProtocols: []plugin.Protocol{
|
|
|
|
plugin.ProtocolGRPC,
|
|
|
|
},
|
2019-01-05 00:08:47 +00:00
|
|
|
Logger: logger,
|
2019-02-21 20:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only set one of Cmd or Reattach
|
|
|
|
if reattachConfig == nil {
|
|
|
|
conf.Cmd = exec.Command(bin, "logmon")
|
|
|
|
} else {
|
|
|
|
conf.Reattach = reattachConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
client := plugin.NewClient(conf)
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := rpcClient.Dispense("logmon")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := raw.(LogMon)
|
|
|
|
return l, client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Plugin struct {
|
|
|
|
plugin.NetRPCUnsupportedPlugin
|
|
|
|
impl LogMon
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlugin(i LogMon) plugin.Plugin {
|
|
|
|
return &Plugin{impl: i}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
|
|
|
|
proto.RegisterLogMonServer(s, &logmonServer{
|
|
|
|
impl: p.impl,
|
|
|
|
broker: broker,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
|
2019-04-25 18:32:24 +00:00
|
|
|
return &logmonClient{
|
|
|
|
doneCtx: ctx,
|
|
|
|
client: proto.NewLogMonClient(c),
|
|
|
|
}, nil
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|