-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupervisord.go
420 lines (338 loc) · 11.8 KB
/
supervisord.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package supervisord
import (
"bufio"
"errors"
"fmt"
"github.com/Ligustah/xmlrpc"
"log"
"net"
"net/http"
"net/url"
"reflect"
)
var supervisorURL, _ = url.Parse("http://localhost/RPC2")
func unmarshalStruct(in xmlrpc.Struct, out interface{}) error {
t := reflect.TypeOf(out)
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
return errors.New("unmarshalStruct: out is not a struct pointer")
}
t = t.Elem()
v := reflect.ValueOf(out).Elem()
//log.Printf("type of out: %s with %d fields", t.Name(), t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldName := field.Tag.Get("xmlrpc")
if fieldName == "" {
fieldName = field.Name
} else if fieldName == "-" {
continue
}
//log.Printf("looking for field %s", fieldName)
if value, ok := in[fieldName]; ok {
vT := reflect.TypeOf(value)
if vT.AssignableTo(field.Type) {
v.Field(i).Set(reflect.ValueOf(value))
} else {
log.Println(in)
return fmt.Errorf("unmarshalStruct: incompatible type for field '%s' (%s != %s)",
field.Name, field.Type.Name(), vT.Name())
}
} else {
//log.Printf("field %s not found", fieldName)
//ignore struct fields that are not in the response struct
return fmt.Errorf("unmarshalStruct: field %s couldn't be found in input struct", fieldName)
}
}
return nil
}
type State struct {
Statecode int64 `xmlrpc:"statecode"`
Statename string `xmlrpc:"statename"`
}
type ProcessInfo struct {
Name string `xmlrpc:"name"`
Group string `xmlrpc:"group"`
Start int64 `xmlrpc:"start"`
Stop int64 `xmlrpc:"stop"`
Now int64 `xmlrpc:"now"`
State int64 `xmlrpc:"state"`
Statename string `xmlrpc:"statename"`
StdoutLogfile string `xmlrpc:"stdout_logfile"`
StderrLogfile string `xmlrpc:"stderr_logfile"`
SpawnErr string `xmlrpc:"spawnerr"`
ExitStatus int64 `xmlrpc:"exitstatus"`
Pid int64 `xmlrpc:"pid"`
}
type Supervisor interface {
//status and control
GetAPIVersion() (string, error)
GetSupervisorVersion() (string, error)
GetIdentification() (string, error)
GetState() (State, error)
GetPID() (int, error)
ReadLog(offset, length int) (string, error)
ClearLog() (bool, error)
Shutdown() (bool, error)
Restart() (bool, error)
ReloadConfig() ([]string, []string, []string, error)
//process control
GetProcessInfo(string) (ProcessInfo, error)
GetAllProcessInfo() ([]ProcessInfo, error)
StartProcess(string, bool) (bool, error)
StartAllProcesses(bool) ([]ProcessInfo, error)
StartProcessGroup(string, bool) ([]ProcessInfo, error)
StopProcess(string, bool) (bool, error)
StopAllProcesses(bool) ([]ProcessInfo, error)
StopProcessGroup(string, bool) ([]ProcessInfo, error)
SendProcessStdin(string, string) (bool, error)
SendRemoteCommEvent(string, string) (bool, error)
AddProcessGroup(string) (bool, error)
RemoveProcessGroup(string) (bool, error)
//process logging
ReadProcessStdoutLog(string, int64, int64) (string, error)
ReadProcessStderrLog(string, int64, int64) (string, error)
TailProcessStdoutLog(string, int64, int64) (string, int64, bool, error)
TailProcessStderrLog(string, int64, int64) (string, int64, bool, error)
ClearProcessLogs(string) (bool, error)
ClearAllProcessLogs() (bool, error)
// misc
Close() error
}
type supervisor struct {
rpcClient *xmlrpc.Client
}
//statically check that supervisor implements Supervisor
var _ Supervisor = (*supervisor)(nil)
func (s *supervisor) startStopProcess(action, name string, wait bool) (success bool, err error) {
err = s.rpcClient.Call(fmt.Sprintf("supervisor.%sProcess", action), xmlrpc.Params{[]interface{}{name, wait}}, &success)
return
}
func (s *supervisor) multiProcessAction(method string, args interface{}) (info []ProcessInfo, err error) {
var values []interface{}
if err = s.rpcClient.Call(fmt.Sprintf("supervisor.%s", method), args, &values); err != nil {
return
}
info = make([]ProcessInfo, len(values))
for i, v := range values {
if strct, ok := v.(xmlrpc.Struct); ok {
if err = unmarshalStruct(strct, &info[i]); err != nil {
return
}
} else {
return nil, fmt.Errorf("%s: unexpected return data type: %s", method, reflect.TypeOf(v).Name())
}
}
return
}
func (s *supervisor) readProcessLog(source, name string, offset, length int64) (result string, err error) {
err = s.rpcClient.Call(fmt.Sprintf("supervisor.readProcessStd%sLog", source),
xmlrpc.Params{[]interface{}{name, offset, length}}, &result)
return
}
func (s *supervisor) tailProcessLog(source, name string, inOffset, length int64) (result string, offset int64, overflow bool, err error) {
var values []interface{}
if err = s.rpcClient.Call(fmt.Sprintf("supervisor.tailProcessStd%sLog", source),
xmlrpc.Params{[]interface{}{name, offset, length}}, &values); err != nil {
return
}
// values should contain [string bytes, int offset, bool overflow]
if len(values) != 3 {
err = errors.New("tailProcessLog: array length != 3")
return
}
var ok bool
if result, ok = values[0].(string); !ok {
goto bad_type
}
if offset, ok = values[1].(int64); !ok {
goto bad_type
}
if overflow, ok = values[2].(bool); !ok {
goto bad_type
}
return
bad_type:
err = errors.New("tailProcessLog: incompatible type in result array")
return
}
func (s *supervisor) GetAPIVersion() (version string, err error) {
err = s.rpcClient.Call("supervisor.getAPIVersion", nil, &version)
return
}
func (s *supervisor) GetSupervisorVersion() (version string, err error) {
err = s.rpcClient.Call("supervisor.getSupervisorVersion", nil, &version)
return
}
func (s *supervisor) GetIdentification() (identification string, err error) {
err = s.rpcClient.Call("supervisor.getIdentification", nil, &identification)
return
}
func (s *supervisor) GetState() (state State, err error) {
values := xmlrpc.Struct{}
if err = s.rpcClient.Call("supervisor.getState", nil, &values); err != nil {
return
}
err = unmarshalStruct(values, &state)
return
}
func (s *supervisor) GetPID() (pid int, err error) {
err = s.rpcClient.Call("supervisor.getPID", nil, &pid)
return
}
func (s *supervisor) ReadLog(offset, length int) (log string, err error) {
err = s.rpcClient.Call("supervisor.readLog", xmlrpc.Params{[]interface{}{offset, length}}, &log)
return
}
func (s *supervisor) ClearLog() (success bool, err error) {
err = s.rpcClient.Call("supervisor.clearLog", nil, &success)
return
}
func (s *supervisor) Shutdown() (success bool, err error) {
err = s.rpcClient.Call("supervisor.shutdown", nil, &success)
return
}
func (s *supervisor) Restart() (success bool, err error) {
err = s.rpcClient.Call("supervisor.restart", nil, &success)
return
}
func (s *supervisor) ReloadConfig() (added, changed, removed []string, err error) {
copyInterfaceToStringSlice := func(out []string, in interface{}) ([]string, error) {
arr, ok := in.([]interface{})
if !ok {
return nil, errors.New("ReloadConfig: parameter not an array")
}
for _, s := range arr {
if str, ok := s.(string); ok {
out = append(out, str)
} else {
return nil, errors.New("ReloadConfig: array contains non-string")
}
}
return out, nil
}
var status []interface{}
//for some reason this returns [[added, changed, removed]]
err = s.rpcClient.Call("supervisor.reloadConfig", nil, &status)
if len(status) == 1 {
if inner, ok := status[0].([]interface{}); ok && len(inner) == 3 {
if added, err = copyInterfaceToStringSlice(added, inner[0]); err != nil {
return
}
if changed, err = copyInterfaceToStringSlice(changed, inner[1]); err != nil {
return
}
if removed, err = copyInterfaceToStringSlice(removed, inner[2]); err != nil {
return
}
}
//everything fine here
return
}
err = errors.New("Unexpected data returned from supervisor.reloadConfig")
return
}
func (s *supervisor) GetProcessInfo(name string) (info ProcessInfo, err error) {
values := xmlrpc.Struct{}
if err = s.rpcClient.Call("supervisor.getProcessInfo", name, &values); err != nil {
return
}
err = unmarshalStruct(values, &info)
return
}
func (s *supervisor) GetAllProcessInfo() ([]ProcessInfo, error) {
return s.multiProcessAction("getAllProcessInfo", nil)
}
func (s *supervisor) StartProcess(name string, wait bool) (bool, error) {
return s.startStopProcess("start", name, wait)
}
func (s *supervisor) StartAllProcesses(wait bool) ([]ProcessInfo, error) {
return s.multiProcessAction("startAllProcesses", wait)
}
func (s *supervisor) StartProcessGroup(name string, wait bool) ([]ProcessInfo, error) {
return s.multiProcessAction("startProcessGroup", xmlrpc.Params{[]interface{}{name, wait}})
}
func (s *supervisor) StopProcess(name string, wait bool) (bool, error) {
return s.startStopProcess("stop", name, wait)
}
func (s *supervisor) StopAllProcesses(wait bool) ([]ProcessInfo, error) {
return s.multiProcessAction("stopAllProcesses", wait)
}
func (s *supervisor) StopProcessGroup(name string, wait bool) ([]ProcessInfo, error) {
return s.multiProcessAction("stopProcessGroup", xmlrpc.Params{[]interface{}{name, wait}})
}
func (s *supervisor) SendProcessStdin(name, chars string) (success bool, err error) {
err = s.rpcClient.Call("supervisor.sendProcessStdin", xmlrpc.Params{[]interface{}{name, chars}}, &success)
return
}
func (s *supervisor) SendRemoteCommEvent(eventType, data string) (success bool, err error) {
err = s.rpcClient.Call("supervisor.sendRemoteCommEvent", xmlrpc.Params{[]interface{}{eventType, data}}, &success)
return
}
func (s *supervisor) AddProcessGroup(name string) (success bool, err error) {
err = s.rpcClient.Call("supervisor.addProcessGroup", name, &success)
return
}
func (s *supervisor) RemoveProcessGroup(name string) (success bool, err error) {
err = s.rpcClient.Call("supervisor.removeProcessGroup", name, &success)
return
}
func (s *supervisor) ReadProcessStdoutLog(name string, offset, length int64) (string, error) {
return s.readProcessLog("out", name, offset, length)
}
func (s *supervisor) ReadProcessStderrLog(name string, offset, length int64) (string, error) {
return s.readProcessLog("err", name, offset, length)
}
func (s *supervisor) TailProcessStdoutLog(name string, offset, length int64) (string, int64, bool, error) {
return s.tailProcessLog("out", name, offset, length)
}
func (s *supervisor) TailProcessStderrLog(name string, offset, length int64) (string, int64, bool, error) {
return s.tailProcessLog("err", name, offset, length)
}
func (s *supervisor) ClearProcessLogs(name string) (success bool, err error) {
err = s.rpcClient.Call("supervisor.clearProcessLogs", name, &success)
return
}
func (s *supervisor) ClearAllProcessLogs() (success bool, err error) {
err = s.rpcClient.Call("supervisor.clearAllProcessLogs", nil, &success)
return
}
func (s *supervisor) Close() error {
return s.rpcClient.Close()
}
type supervisorTransport struct {
}
func (st *supervisorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.URL == nil {
return nil, errors.New("unix: nil Request.URL")
}
if req.Header == nil {
return nil, errors.New("unix: nil Request.Header")
}
if req.URL.Scheme != "unix" {
panic("unix: unsupported protocol scheme")
}
sock, err := net.Dial("unix", req.URL.Path)
if err != nil {
return nil, err
}
defer sock.Close()
//create shallow copy of request object
newReq := new(http.Request)
*newReq = *req
newReq.URL = supervisorURL
newReq.Write(sock)
return http.ReadResponse(bufio.NewReader(sock), req)
}
// New returns a Supervisor interface type connected to the net.URL specified in u
//
// Optionally specify a http.Transport to use, will use default http.Transport if nil.
// This will also register a
func New(url string, transport *http.Transport) Supervisor {
if transport == nil {
transport = new(http.Transport)
}
transport.RegisterProtocol("unix", new(supervisorTransport))
//xmlrpc.NewClient never returns an error
client, _ := xmlrpc.NewClient(url, transport)
return &supervisor{client}
}