forked from hashicorp/nomad
/
legacy_executor_wrapper.go
221 lines (187 loc) · 5.61 KB
/
legacy_executor_wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package executor
import (
"encoding/gob"
"fmt"
"net/rpc"
"os"
"syscall"
"time"
hclog "github.com/hashicorp/go-hclog"
plugin "github.com/hashicorp/go-plugin"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"golang.org/x/net/context"
)
const (
// pre09DockerSignal is used in executor.Shutdown to know if it should
// call the ShutDown RPC on the pre09 executor
pre09DockerSignal = "docker"
)
// 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{})
gob.Register([]map[string]int{})
gob.Register(syscall.Signal(0x1))
}
type legacyExecutorWrapper struct {
client *pre09ExecutorRPC
logger hclog.Logger
}
// validate that legacyExecutorWrapper is an executor
var _ Executor = (*legacyExecutorWrapper)(nil)
func (l *legacyExecutorWrapper) Launch(launchCmd *ExecCommand) (*ProcessState, error) {
return nil, fmt.Errorf("operation not supported for legacy exec wrapper")
}
func (l *legacyExecutorWrapper) Wait(ctx context.Context) (*ProcessState, error) {
ps, err := l.client.Wait()
if err != nil {
return nil, err
}
return &ProcessState{
Pid: ps.Pid,
ExitCode: ps.ExitCode,
Signal: ps.Signal,
Time: ps.Time,
}, nil
}
func (l *legacyExecutorWrapper) Shutdown(signal string, gracePeriod time.Duration) error {
// The legacy docker driver only used the executor to start a syslog server
// for logging. Thus calling ShutDown for docker will always return an error
// because it never started a process through the executor. If signal is set
// to 'docker' then we'll skip the ShutDown RPC and just call Exit.
//
// This is painful to look at but will only be around a few releases
if signal != pre09DockerSignal {
if err := l.client.ShutDown(); err != nil {
return err
}
}
if err := l.client.Exit(); err != nil {
return err
}
return nil
}
func (l *legacyExecutorWrapper) UpdateResources(*drivers.Resources) error {
return fmt.Errorf("operation not supported for legacy exec wrapper")
}
func (l *legacyExecutorWrapper) Version() (*ExecutorVersion, error) {
v, err := l.client.Version()
if err != nil {
return nil, err
}
return &ExecutorVersion{
Version: v.Version,
}, nil
}
func (l *legacyExecutorWrapper) Stats(ctx context.Context, interval time.Duration) (<-chan *cstructs.TaskResourceUsage, error) {
ch := make(chan *cstructs.TaskResourceUsage, 1)
stats, err := l.client.Stats()
if err != nil {
close(ch)
return nil, err
}
select {
case ch <- stats:
default:
}
go l.handleStats(ctx, interval, ch)
return ch, nil
}
func (l *legacyExecutorWrapper) handleStats(ctx context.Context, interval time.Duration, ch chan *cstructs.TaskResourceUsage) {
defer close(ch)
ticker := time.NewTicker(interval)
for range ticker.C {
stats, err := l.client.Stats()
if err != nil {
if err == rpc.ErrShutdown {
return
}
l.logger.Warn("stats collection from legacy executor failed, waiting for next interval", "error", err)
continue
}
if stats != nil {
select {
case ch <- stats:
default:
}
}
}
}
func (l *legacyExecutorWrapper) Signal(s os.Signal) error {
return l.client.Signal(s)
}
func (l *legacyExecutorWrapper) Exec(deadline time.Time, cmd string, args []string) ([]byte, int, error) {
return l.client.Exec(deadline, cmd, args)
}
func (l *legacyExecutorWrapper) ExecStreaming(ctx context.Context, cmd []string, tty bool,
stream drivers.ExecTaskStream) error {
return fmt.Errorf("operation not supported for legacy exec wrapper")
}
type pre09ExecutorRPC struct {
client *rpc.Client
logger hclog.Logger
}
type pre09ExecCmdArgs struct {
Deadline time.Time
Name string
Args []string
}
type pre09ExecCmdReturn struct {
Output []byte
Code int
}
func (e *pre09ExecutorRPC) Wait() (*ProcessState, error) {
var ps ProcessState
err := e.client.Call("Plugin.Wait", new(interface{}), &ps)
return &ps, err
}
func (e *pre09ExecutorRPC) ShutDown() error {
return e.client.Call("Plugin.ShutDown", new(interface{}), new(interface{}))
}
func (e *pre09ExecutorRPC) Exit() error {
return e.client.Call("Plugin.Exit", new(interface{}), new(interface{}))
}
func (e *pre09ExecutorRPC) Version() (*ExecutorVersion, error) {
var version ExecutorVersion
err := e.client.Call("Plugin.Version", new(interface{}), &version)
return &version, err
}
func (e *pre09ExecutorRPC) Stats() (*cstructs.TaskResourceUsage, error) {
var resourceUsage cstructs.TaskResourceUsage
err := e.client.Call("Plugin.Stats", new(interface{}), &resourceUsage)
return &resourceUsage, err
}
func (e *pre09ExecutorRPC) Signal(s os.Signal) error {
return e.client.Call("Plugin.Signal", &s, new(interface{}))
}
func (e *pre09ExecutorRPC) Exec(deadline time.Time, name string, args []string) ([]byte, int, error) {
req := pre09ExecCmdArgs{
Deadline: deadline,
Name: name,
Args: args,
}
var resp *pre09ExecCmdReturn
err := e.client.Call("Plugin.Exec", req, &resp)
if resp == nil {
return nil, 0, err
}
return resp.Output, resp.Code, err
}
type pre09ExecutorPlugin struct {
logger hclog.Logger
}
func newPre09ExecutorPlugin(logger hclog.Logger) plugin.Plugin {
return &pre09ExecutorPlugin{logger: logger}
}
func (p *pre09ExecutorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &legacyExecutorWrapper{
client: &pre09ExecutorRPC{client: c, logger: p.logger},
logger: p.logger,
}, nil
}
func (p *pre09ExecutorPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
return nil, fmt.Errorf("client only supported")
}