github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/client/driver/executor_plugin.go (about) 1 package driver 2 3 import ( 4 "log" 5 "net/rpc" 6 7 "github.com/hashicorp/go-plugin" 8 "github.com/hashicorp/nomad/client/driver/executor" 9 "github.com/hashicorp/nomad/nomad/structs" 10 ) 11 12 type ExecutorRPC struct { 13 client *rpc.Client 14 } 15 16 // LaunchCmdArgs wraps a user command and the args for the purposes of RPC 17 type LaunchCmdArgs struct { 18 Cmd *executor.ExecCommand 19 Ctx *executor.ExecutorContext 20 } 21 22 func (e *ExecutorRPC) LaunchCmd(cmd *executor.ExecCommand, ctx *executor.ExecutorContext) (*executor.ProcessState, error) { 23 var ps *executor.ProcessState 24 err := e.client.Call("Plugin.LaunchCmd", LaunchCmdArgs{Cmd: cmd, Ctx: ctx}, &ps) 25 return ps, err 26 } 27 28 func (e *ExecutorRPC) Wait() (*executor.ProcessState, error) { 29 var ps executor.ProcessState 30 err := e.client.Call("Plugin.Wait", new(interface{}), &ps) 31 return &ps, err 32 } 33 34 func (e *ExecutorRPC) ShutDown() error { 35 return e.client.Call("Plugin.ShutDown", new(interface{}), new(interface{})) 36 } 37 38 func (e *ExecutorRPC) Exit() error { 39 return e.client.Call("Plugin.Exit", new(interface{}), new(interface{})) 40 } 41 42 func (e *ExecutorRPC) UpdateLogConfig(logConfig *structs.LogConfig) error { 43 return e.client.Call("Plugin.UpdateLogConfig", logConfig, new(interface{})) 44 } 45 46 type ExecutorRPCServer struct { 47 Impl executor.Executor 48 } 49 50 func (e *ExecutorRPCServer) LaunchCmd(args LaunchCmdArgs, ps *executor.ProcessState) error { 51 state, err := e.Impl.LaunchCmd(args.Cmd, args.Ctx) 52 if state != nil { 53 *ps = *state 54 } 55 return err 56 } 57 58 func (e *ExecutorRPCServer) Wait(args interface{}, ps *executor.ProcessState) error { 59 state, err := e.Impl.Wait() 60 if state != nil { 61 *ps = *state 62 } 63 return err 64 } 65 66 func (e *ExecutorRPCServer) ShutDown(args interface{}, resp *interface{}) error { 67 return e.Impl.ShutDown() 68 } 69 70 func (e *ExecutorRPCServer) Exit(args interface{}, resp *interface{}) error { 71 return e.Impl.Exit() 72 } 73 74 func (e *ExecutorRPCServer) UpdateLogConfig(args *structs.LogConfig, resp *interface{}) error { 75 return e.Impl.UpdateLogConfig(args) 76 } 77 78 type ExecutorPlugin struct { 79 logger *log.Logger 80 Impl *ExecutorRPCServer 81 } 82 83 func (p *ExecutorPlugin) Server(*plugin.MuxBroker) (interface{}, error) { 84 if p.Impl == nil { 85 p.Impl = &ExecutorRPCServer{Impl: executor.NewExecutor(p.logger)} 86 } 87 return p.Impl, nil 88 } 89 90 func (p *ExecutorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) { 91 return &ExecutorRPC{client: c}, nil 92 }