2016-02-05 00:03:17 +00:00
|
|
|
package driver
|
2016-02-03 19:54:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-02-04 18:09:52 +00:00
|
|
|
"net"
|
2016-02-03 19:54:54 +00:00
|
|
|
"net/rpc"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-plugin"
|
2016-02-05 00:03:17 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
2016-02-03 19:54:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var HandshakeConfig = plugin.HandshakeConfig{
|
|
|
|
ProtocolVersion: 1,
|
|
|
|
MagicCookieKey: "executor_plugin",
|
|
|
|
MagicCookieValue: "value",
|
|
|
|
}
|
|
|
|
|
|
|
|
var PluginMap = map[string]plugin.Plugin{
|
|
|
|
"executor": new(ExecutorPlugin),
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:09:52 +00:00
|
|
|
type ExecutorReattachConfig struct {
|
|
|
|
Pid int
|
|
|
|
AddrNet string
|
|
|
|
AddrName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ExecutorReattachConfig) PluginConfig() *plugin.ReattachConfig {
|
|
|
|
var addr net.Addr
|
|
|
|
switch c.AddrNet {
|
|
|
|
case "unix", "unixgram", "unixpacket":
|
|
|
|
addr, _ = net.ResolveUnixAddr(c.AddrNet, c.AddrName)
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
addr, _ = net.ResolveTCPAddr(c.AddrNet, c.AddrName)
|
|
|
|
}
|
|
|
|
return &plugin.ReattachConfig{Pid: c.Pid, Addr: addr}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutorReattachConfig(c *plugin.ReattachConfig) *ExecutorReattachConfig {
|
|
|
|
return &ExecutorReattachConfig{Pid: c.Pid, AddrNet: c.Addr.Network(), AddrName: c.Addr.String()}
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:54:54 +00:00
|
|
|
type ExecutorRPC struct {
|
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type LaunchCmdArgs struct {
|
2016-02-05 00:03:17 +00:00
|
|
|
Cmd *executor.ExecCommand
|
|
|
|
Ctx *executor.ExecutorContext
|
2016-02-03 19:54:54 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 00:03:17 +00:00
|
|
|
func (e *ExecutorRPC) LaunchCmd(cmd *executor.ExecCommand, ctx *executor.ExecutorContext) (*executor.ProcessState, error) {
|
|
|
|
var ps *executor.ProcessState
|
2016-02-04 03:00:21 +00:00
|
|
|
err := e.client.Call("Plugin.LaunchCmd", LaunchCmdArgs{Cmd: cmd, Ctx: ctx}, &ps)
|
2016-02-04 01:22:21 +00:00
|
|
|
return ps, err
|
2016-02-03 19:54:54 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 00:03:17 +00:00
|
|
|
func (e *ExecutorRPC) Wait() (*executor.ProcessState, error) {
|
|
|
|
var ps executor.ProcessState
|
2016-02-03 19:54:54 +00:00
|
|
|
err := e.client.Call("Plugin.Wait", new(interface{}), &ps)
|
|
|
|
return &ps, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecutorRPC) ShutDown() error {
|
2016-02-05 00:03:17 +00:00
|
|
|
var ps executor.ProcessState
|
2016-02-03 19:54:54 +00:00
|
|
|
err := e.client.Call("Plugin.ShutDown", new(interface{}), &ps)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecutorRPC) Exit() error {
|
2016-02-05 00:03:17 +00:00
|
|
|
var ps executor.ProcessState
|
2016-02-03 19:54:54 +00:00
|
|
|
err := e.client.Call("Plugin.Exit", new(interface{}), &ps)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecutorRPCServer struct {
|
2016-02-05 00:03:17 +00:00
|
|
|
Impl executor.Executor
|
2016-02-03 19:54:54 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 00:03:17 +00:00
|
|
|
func (e *ExecutorRPCServer) LaunchCmd(args LaunchCmdArgs, ps *executor.ProcessState) error {
|
2016-02-04 03:00:21 +00:00
|
|
|
state, err := e.Impl.LaunchCmd(args.Cmd, args.Ctx)
|
2016-02-04 19:51:43 +00:00
|
|
|
if state != nil {
|
|
|
|
*ps = *state
|
|
|
|
}
|
2016-02-03 19:54:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-05 00:03:17 +00:00
|
|
|
func (e *ExecutorRPCServer) Wait(args interface{}, ps *executor.ProcessState) error {
|
2016-02-04 19:51:43 +00:00
|
|
|
state, err := e.Impl.Wait()
|
|
|
|
if state != nil {
|
|
|
|
*ps = *state
|
|
|
|
}
|
2016-02-03 19:54:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-05 00:03:17 +00:00
|
|
|
func (e *ExecutorRPCServer) ShutDown(args interface{}, ps *executor.ProcessState) error {
|
2016-02-03 19:54:54 +00:00
|
|
|
var err error
|
|
|
|
err = e.Impl.ShutDown()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-05 00:03:17 +00:00
|
|
|
func (e *ExecutorRPCServer) Exit(args interface{}, ps *executor.ProcessState) error {
|
2016-02-03 19:54:54 +00:00
|
|
|
var err error
|
|
|
|
err = e.Impl.Exit()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecutorPlugin struct {
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ExecutorPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
|
|
|
|
p.logger = log.New(os.Stdout, "executor-plugin-server:", log.LstdFlags)
|
2016-02-05 00:03:17 +00:00
|
|
|
return &ExecutorRPCServer{Impl: executor.NewExecutor(p.logger)}, nil
|
2016-02-03 19:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ExecutorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
|
|
|
|
p.logger = log.New(os.Stdout, "executor-plugin-client:", log.LstdFlags)
|
|
|
|
return &ExecutorRPC{client: c}, nil
|
|
|
|
}
|