open-nomad/client/driver/executor_plugin.go

182 lines
5.1 KiB
Go
Raw Normal View History

2016-02-05 00:03:17 +00:00
package driver
2016-02-03 19:54:54 +00:00
import (
"encoding/gob"
2016-02-03 19:54:54 +00:00
"log"
"net/rpc"
"github.com/hashicorp/go-plugin"
2016-02-05 00:03:17 +00:00
"github.com/hashicorp/nomad/client/driver/executor"
2016-04-28 23:06:01 +00:00
cstructs "github.com/hashicorp/nomad/client/driver/structs"
2016-02-08 18:10:01 +00:00
"github.com/hashicorp/nomad/nomad/structs"
2016-02-03 19:54:54 +00:00
)
// Registering these types since we have to serialize and de-serialize the Task
// structs over the wire between drivers and the executor.
func init() {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
gob.Register([]map[string]string{})
2016-03-22 00:44:37 +00:00
gob.Register([]map[string]int{})
}
2016-02-03 19:54:54 +00:00
type ExecutorRPC struct {
client *rpc.Client
2016-03-29 23:27:31 +00:00
logger *log.Logger
2016-02-03 19:54:54 +00:00
}
2016-02-05 00:38:17 +00:00
// LaunchCmdArgs wraps a user command and the args for the purposes of RPC
2016-02-03 19:54:54 +00:00
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
}
// LaunchSyslogServerArgs wraps the executor context for the purposes of RPC
type LaunchSyslogServerArgs struct {
Ctx *executor.ExecutorContext
}
2016-03-24 22:39:10 +00:00
// SyncServicesArgs wraps the consul context for the purposes of RPC
type SyncServicesArgs struct {
Ctx *executor.ConsulContext
}
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)
return ps, err
2016-02-03 19:54:54 +00:00
}
func (e *ExecutorRPC) LaunchSyslogServer(ctx *executor.ExecutorContext) (*executor.SyslogServerState, error) {
var ss *executor.SyslogServerState
err := e.client.Call("Plugin.LaunchSyslogServer", LaunchSyslogServerArgs{Ctx: ctx}, &ss)
return ss, err
}
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 {
return e.client.Call("Plugin.ShutDown", new(interface{}), new(interface{}))
2016-02-03 19:54:54 +00:00
}
func (e *ExecutorRPC) Exit() error {
return e.client.Call("Plugin.Exit", new(interface{}), new(interface{}))
2016-02-03 19:54:54 +00:00
}
2016-02-08 18:10:01 +00:00
func (e *ExecutorRPC) UpdateLogConfig(logConfig *structs.LogConfig) error {
return e.client.Call("Plugin.UpdateLogConfig", logConfig, new(interface{}))
}
func (e *ExecutorRPC) UpdateTask(task *structs.Task) error {
return e.client.Call("Plugin.UpdateTask", task, new(interface{}))
}
2016-03-24 22:39:10 +00:00
func (e *ExecutorRPC) SyncServices(ctx *executor.ConsulContext) error {
return e.client.Call("Plugin.SyncServices", SyncServicesArgs{Ctx: ctx}, new(interface{}))
}
func (e *ExecutorRPC) DeregisterServices() error {
return e.client.Call("Plugin.DeregisterServices", new(interface{}), new(interface{}))
}
2016-03-30 05:05:02 +00:00
func (e *ExecutorRPC) Version() (*executor.ExecutorVersion, error) {
var version executor.ExecutorVersion
2016-03-29 23:27:31 +00:00
err := e.client.Call("Plugin.Version", new(interface{}), &version)
2016-03-30 05:05:02 +00:00
return &version, err
2016-03-29 23:27:31 +00:00
}
2016-04-28 23:06:01 +00:00
func (e *ExecutorRPC) Stats() (*cstructs.TaskResourceUsage, error) {
var resourceUsage cstructs.TaskResourceUsage
err := e.client.Call("Plugin.Stats", new(interface{}), &resourceUsage)
return &resourceUsage, err
}
2016-02-03 19:54:54 +00:00
type ExecutorRPCServer struct {
2016-03-30 05:05:02 +00:00
Impl executor.Executor
logger *log.Logger
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
}
func (e *ExecutorRPCServer) LaunchSyslogServer(args LaunchSyslogServerArgs, ss *executor.SyslogServerState) error {
state, err := e.Impl.LaunchSyslogServer(args.Ctx)
if state != nil {
*ss = *state
}
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
}
func (e *ExecutorRPCServer) ShutDown(args interface{}, resp *interface{}) error {
return e.Impl.ShutDown()
2016-02-03 19:54:54 +00:00
}
func (e *ExecutorRPCServer) Exit(args interface{}, resp *interface{}) error {
return e.Impl.Exit()
2016-02-03 19:54:54 +00:00
}
2016-02-08 18:10:01 +00:00
func (e *ExecutorRPCServer) UpdateLogConfig(args *structs.LogConfig, resp *interface{}) error {
return e.Impl.UpdateLogConfig(args)
}
func (e *ExecutorRPCServer) UpdateTask(args *structs.Task, resp *interface{}) error {
return e.Impl.UpdateTask(args)
}
2016-03-24 22:39:10 +00:00
func (e *ExecutorRPCServer) SyncServices(args SyncServicesArgs, resp *interface{}) error {
return e.Impl.SyncServices(args.Ctx)
}
func (e *ExecutorRPCServer) DeregisterServices(args interface{}, resp *interface{}) error {
return e.Impl.DeregisterServices()
}
2016-03-30 05:05:02 +00:00
func (e *ExecutorRPCServer) Version(args interface{}, version *executor.ExecutorVersion) error {
ver, err := e.Impl.Version()
if ver != nil {
*version = *ver
}
return err
2016-03-29 23:27:31 +00:00
}
2016-04-28 23:06:01 +00:00
func (e *ExecutorRPCServer) Stats(args interface{}, resourceUsage *cstructs.TaskResourceUsage) error {
ru, err := e.Impl.Stats()
if ru != nil {
*resourceUsage = *ru
}
return err
}
2016-02-03 19:54:54 +00:00
type ExecutorPlugin struct {
logger *log.Logger
Impl *ExecutorRPCServer
2016-02-03 19:54:54 +00:00
}
func (p *ExecutorPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
if p.Impl == nil {
2016-03-30 05:05:02 +00:00
p.Impl = &ExecutorRPCServer{Impl: executor.NewExecutor(p.logger), logger: p.logger}
}
return p.Impl, nil
2016-02-03 19:54:54 +00:00
}
func (p *ExecutorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
2016-03-29 23:27:31 +00:00
return &ExecutorRPC{client: c, logger: p.logger}, nil
2016-02-03 19:54:54 +00:00
}