open-nomad/client/logmon/plugin.go
Seth Hoenig 423ea1a5c4 client/logmon: acquire executable in init block
This PR causes the logmon task runner to acquire the binary of the
Nomad executable in an 'init' block, so as to almost certainly get
the name while the nomad file still exists.

This is an attempt at fixing the case where a deleted Nomad file
(e.g. during upgrade) may be getting renamed with a mysterious
suffix first.

If this doesn't work, as a last resort we can literally just trim
the mystery string.

Fixes: #14079
2022-08-24 13:17:20 -05:00

86 lines
1.7 KiB
Go

package logmon
import (
"context"
"os"
"os/exec"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/nomad/client/logmon/proto"
"github.com/hashicorp/nomad/plugins/base"
"google.golang.org/grpc"
)
var bin = getBin()
func getBin() string {
b, err := os.Executable()
if err != nil {
panic(err)
}
return b
}
// LaunchLogMon launches a new logmon or reattaches to an existing one.
// TODO: Integrate with base plugin loader
func LaunchLogMon(logger hclog.Logger, reattachConfig *plugin.ReattachConfig) (LogMon, *plugin.Client, error) {
logger = logger.Named("logmon")
conf := &plugin.ClientConfig{
HandshakeConfig: base.Handshake,
Plugins: map[string]plugin.Plugin{
"logmon": &Plugin{},
},
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolGRPC,
},
Logger: logger,
}
// Only set one of Cmd or Reattach
if reattachConfig == nil {
conf.Cmd = exec.Command(bin, "logmon")
} else {
conf.Reattach = reattachConfig
}
client := plugin.NewClient(conf)
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) {
return &logmonClient{
doneCtx: ctx,
client: proto.NewLogMonClient(c),
}, nil
}