-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctl.go
513 lines (456 loc) · 13.9 KB
/
ctl.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/jbox-web/supervisord/config"
"github.com/jbox-web/supervisord/types"
"github.com/jbox-web/supervisord/xmlrpcclient"
"github.com/jessevdk/go-flags"
)
// CtlCommand the entry of ctl command
type CtlCommand struct {
ServerURL string `short:"s" long:"serverurl" description:"URL on which supervisord server is listening"`
User string `short:"u" long:"user" description:"the user name"`
Password string `short:"P" long:"password" description:"the password"`
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
}
// StatusCommand get the status of all supervisor managed programs
type StatusCommand struct {
}
// StartCommand start the given program
type StartCommand struct {
}
// StopCommand stop the given program
type StopCommand struct {
}
// RestartCommand restart the given program
type RestartCommand struct {
}
// ShutdownCommand shutdown the supervisor
type ShutdownCommand struct {
}
// ReloadCommand reload all the programs
type ReloadCommand struct {
}
// PidCommand get the pid of program
type PidCommand struct {
}
// SignalCommand send signal of program
type SignalCommand struct {
}
// LogtailCommand tail the stdout/stderr log of program through http interface
type LogtailCommand struct {
}
// CmdCheckWrapperCommand A wrapper can be used to check whether
// number of parameters is valid or not
type CmdCheckWrapperCommand struct {
// Original cmd
cmd flags.Commander
// leastNumArgs indicates how many arguments
// this cmd should have at least
leastNumArgs int
// Print usage when arguments not valid
usage string
}
var ctlCommand CtlCommand
var statusCommand = CmdCheckWrapperCommand{&StatusCommand{}, 0, ""}
var startCommand = CmdCheckWrapperCommand{&StartCommand{}, 0, ""}
var stopCommand = CmdCheckWrapperCommand{&StopCommand{}, 0, ""}
var restartCommand = CmdCheckWrapperCommand{&RestartCommand{}, 0, ""}
var shutdownCommand = CmdCheckWrapperCommand{&ShutdownCommand{}, 0, ""}
var reloadCommand = CmdCheckWrapperCommand{&ReloadCommand{}, 0, ""}
var pidCommand = CmdCheckWrapperCommand{&PidCommand{}, 1, "pid <program>"}
var signalCommand = CmdCheckWrapperCommand{&SignalCommand{}, 2, "signal <signal_name> <program>[...]"}
var logtailCommand = CmdCheckWrapperCommand{&LogtailCommand{}, 1, "logtail <program>"}
func (x *CtlCommand) getServerURL() string {
options.Configuration, _ = findSupervisordConf()
if x.ServerURL != "" {
return x.ServerURL
} else if _, err := os.Stat(options.Configuration); err == nil {
myconfig := config.NewConfig(options.Configuration)
myconfig.Load()
if entry, ok := myconfig.GetSupervisorctl(); ok {
serverurl := entry.GetString("serverurl", "")
if serverurl != "" {
return serverurl
}
}
}
return "http://localhost:9001"
}
func (x *CtlCommand) getUser() string {
options.Configuration, _ = findSupervisordConf()
if x.User != "" {
return x.User
} else if _, err := os.Stat(options.Configuration); err == nil {
myconfig := config.NewConfig(options.Configuration)
myconfig.Load()
if entry, ok := myconfig.GetSupervisorctl(); ok {
user := entry.GetString("username", "")
return user
}
}
return ""
}
func (x *CtlCommand) getPassword() string {
options.Configuration, _ = findSupervisordConf()
if x.Password != "" {
return x.Password
} else if _, err := os.Stat(options.Configuration); err == nil {
myconfig := config.NewConfig(options.Configuration)
myconfig.Load()
if entry, ok := myconfig.GetSupervisorctl(); ok {
password := entry.GetString("password", "")
return password
}
}
return ""
}
func (x *CtlCommand) createRPCClient() *xmlrpcclient.XMLRPCClient {
rpcc := xmlrpcclient.NewXMLRPCClient(x.getServerURL(), x.Verbose)
rpcc.SetUser(x.getUser())
rpcc.SetPassword(x.getPassword())
return rpcc
}
// Execute implements flags.Commander interface to execute the control commands
func (x *CtlCommand) Execute(args []string) error {
if len(args) == 0 {
return nil
}
rpcc := x.createRPCClient()
verb := args[0]
switch verb {
////////////////////////////////////////////////////////////////////////////////
// STATUS
////////////////////////////////////////////////////////////////////////////////
case "status":
x.status(rpcc, args[1:])
////////////////////////////////////////////////////////////////////////////////
// START or STOP
////////////////////////////////////////////////////////////////////////////////
case "start", "stop":
x.startStopProcesses(rpcc, verb, args[1:])
////////////////////////////////////////////////////////////////////////////////
// SHUTDOWN
////////////////////////////////////////////////////////////////////////////////
case "shutdown":
x.shutdown(rpcc)
case "reload":
x.reload(rpcc)
case "signal":
sigName, processes := args[1], args[2:]
x.signal(rpcc, sigName, processes)
case "pid":
x.getPid(rpcc, args[1])
default:
fmt.Println("unknown command")
}
return nil
}
// get the status of processes
func (x *CtlCommand) status(rpcc *xmlrpcclient.XMLRPCClient, processes []string) {
processesMap := make(map[string]bool)
for _, process := range processes {
processesMap[process] = true
}
if reply, err := rpcc.GetAllProcessInfo(); err == nil {
x.showProcessInfo(&reply, processesMap)
} else {
os.Exit(1)
}
}
// start or stop the processes
// verb must be: start or stop
func (x *CtlCommand) startStopProcesses(rpcc *xmlrpcclient.XMLRPCClient, verb string, processes []string) {
state := map[string]string{
"start": "started",
"stop": "stopped",
}
x._startStopProcesses(rpcc, verb, processes, state[verb], true)
}
func (x *CtlCommand) _startStopProcesses(rpcc *xmlrpcclient.XMLRPCClient, verb string, processes []string, state string, showProcessInfo bool) {
if len(processes) <= 0 {
fmt.Printf("Please specify process for %s\n", verb)
}
for _, pname := range processes {
if pname == "all" {
reply, err := rpcc.ChangeAllProcessState(verb)
if err == nil {
if showProcessInfo {
x.showProcessInfo(&reply, make(map[string]bool))
}
} else {
fmt.Printf("Fail to change all process state to %s", state)
}
} else {
if reply, err := rpcc.ChangeProcessState(verb, pname); err == nil {
if showProcessInfo {
fmt.Printf("%s: ", pname)
if !reply.Value {
fmt.Printf("not ")
}
fmt.Printf("%s\n", state)
}
} else {
fmt.Printf("%s: failed [%v]\n", pname, err)
os.Exit(1)
}
}
}
}
func (x *CtlCommand) restartProcesses(rpcc *xmlrpcclient.XMLRPCClient, processes []string) {
x._startStopProcesses(rpcc, "stop", processes, "stopped", false)
x._startStopProcesses(rpcc, "start", processes, "restarted", true)
}
// shutdown the supervisord
func (x *CtlCommand) shutdown(rpcc *xmlrpcclient.XMLRPCClient) {
if reply, err := rpcc.Shutdown(); err == nil {
if reply.Value {
fmt.Printf("Shut Down\n")
} else {
fmt.Printf("Hmmm! Something gone wrong?!\n")
}
} else {
os.Exit(1)
}
}
// reload all the programs in the supervisord
func (x *CtlCommand) reload(rpcc *xmlrpcclient.XMLRPCClient) {
if reply, err := rpcc.ReloadConfig(); err == nil {
if len(reply.AddedGroup) > 0 {
fmt.Printf("Added Groups: %s\n", strings.Join(reply.AddedGroup, ","))
}
if len(reply.ChangedGroup) > 0 {
fmt.Printf("Changed Groups: %s\n", strings.Join(reply.ChangedGroup, ","))
}
if len(reply.RemovedGroup) > 0 {
fmt.Printf("Removed Groups: %s\n", strings.Join(reply.RemovedGroup, ","))
}
} else {
os.Exit(1)
}
}
// send signal to one or more processes
func (x *CtlCommand) signal(rpcc *xmlrpcclient.XMLRPCClient, sigName string, processes []string) {
for _, process := range processes {
if process == "all" {
reply, err := rpcc.SignalAll(process)
if err == nil {
x.showProcessInfo(&reply, make(map[string]bool))
} else {
fmt.Printf("Fail to send signal %s to all process", sigName)
os.Exit(1)
}
} else {
reply, err := rpcc.SignalProcess(sigName, process)
if err == nil && reply.Success {
fmt.Printf("Succeed to send signal %s to process %s\n", sigName, process)
} else {
fmt.Printf("Fail to send signal %s to process %s\n", sigName, process)
os.Exit(1)
}
}
}
}
// get the pid of running program
func (x *CtlCommand) getPid(rpcc *xmlrpcclient.XMLRPCClient, process string) {
procInfo, err := rpcc.GetProcessInfo(process)
if err != nil {
fmt.Printf("program '%s' not found\n", process)
os.Exit(1)
} else {
fmt.Printf("%d\n", procInfo.Pid)
}
}
func (x *CtlCommand) getProcessInfo(rpcc *xmlrpcclient.XMLRPCClient, process string) (types.ProcessInfo, error) {
return rpcc.GetProcessInfo(process)
}
// check if group name should be displayed
func (x *CtlCommand) showGroupName() bool {
val, ok := os.LookupEnv("SUPERVISOR_GROUP_DISPLAY")
if !ok {
return false
}
val = strings.ToLower(val)
return val == "yes" || val == "true" || val == "y" || val == "t" || val == "1"
}
func (x *CtlCommand) showProcessInfo(reply *xmlrpcclient.AllProcessInfoReply, processesMap map[string]bool) {
for _, pinfo := range reply.Value {
description := pinfo.Description
if strings.ToLower(description) == "<string></string>" {
description = ""
}
if x.inProcessMap(&pinfo, processesMap) {
processName := pinfo.GetFullName()
if !x.showGroupName() {
processName = pinfo.Name
}
fmt.Printf("%s%-33s%-10s%s%s\n", x.getANSIColor(strings.ToUpper(pinfo.Statename)), processName, pinfo.Statename, description, "\x1b[0m")
}
}
}
func (x *CtlCommand) inProcessMap(procInfo *types.ProcessInfo, processesMap map[string]bool) bool {
if len(processesMap) <= 0 {
return true
}
for procName := range processesMap {
if procName == procInfo.Name || procName == procInfo.GetFullName() {
return true
}
// check the wildcast '*'
pos := strings.Index(procName, ":")
if pos != -1 {
groupName := procName[0:pos]
programName := procName[pos+1:]
if programName == "*" && groupName == procInfo.Group {
return true
}
}
}
return false
}
func (x *CtlCommand) getANSIColor(statename string) string {
if statename == "RUNNING" {
// green
return "\x1b[0;32m"
} else if statename == "BACKOFF" || statename == "FATAL" {
// red
return "\x1b[0;31m"
} else {
// yellow
return "\x1b[1;33m"
}
}
// Execute implements flags.Commander interface to get status of program
func (sc *StatusCommand) Execute(args []string) error {
ctlCommand.status(ctlCommand.createRPCClient(), args)
return nil
}
// Execute start the given programs
func (sc *StartCommand) Execute(args []string) error {
ctlCommand.startStopProcesses(ctlCommand.createRPCClient(), "start", args)
return nil
}
// Execute stop the given programs
func (sc *StopCommand) Execute(args []string) error {
ctlCommand.startStopProcesses(ctlCommand.createRPCClient(), "stop", args)
return nil
}
// Execute restart the programs
func (rc *RestartCommand) Execute(args []string) error {
ctlCommand.restartProcesses(ctlCommand.createRPCClient(), args)
return nil
}
// Execute shutdown the supervisor
func (sc *ShutdownCommand) Execute(args []string) error {
ctlCommand.shutdown(ctlCommand.createRPCClient())
return nil
}
// Execute stop the running programs and reload the supervisor configuration
func (rc *ReloadCommand) Execute(args []string) error {
ctlCommand.reload(ctlCommand.createRPCClient())
return nil
}
// Execute send signal to program
func (rc *SignalCommand) Execute(args []string) error {
sigName, processes := args[0], args[1:]
ctlCommand.signal(ctlCommand.createRPCClient(), sigName, processes)
return nil
}
// Execute get the pid of program
func (pc *PidCommand) Execute(args []string) error {
ctlCommand.getPid(ctlCommand.createRPCClient(), args[0])
return nil
}
// Execute tail the stdout/stderr of a program through http interface
func (lc *LogtailCommand) Execute(args []string) error {
program := args[0]
go func() {
lc.tailLog(program, "stderr")
}()
return lc.tailLog(program, "stdout")
}
func (lc *LogtailCommand) tailLog(program string, dev string) error {
_, err := ctlCommand.getProcessInfo(ctlCommand.createRPCClient(), program)
if err != nil {
fmt.Printf("Not exist program %s\n", program)
return err
}
url := fmt.Sprintf("%s/logtail/%s/%s", ctlCommand.getServerURL(), program, dev)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.SetBasicAuth(ctlCommand.getUser(), ctlCommand.getPassword())
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
buf := make([]byte, 10240)
for {
n, err := resp.Body.Read(buf)
if err != nil {
return err
}
if dev == "stdout" {
os.Stdout.Write(buf[0:n])
} else {
os.Stderr.Write(buf[0:n])
}
}
return nil
}
// Execute check if the number of arguments is ok
func (wc *CmdCheckWrapperCommand) Execute(args []string) error {
if len(args) < wc.leastNumArgs {
err := fmt.Errorf("Invalid arguments.\nUsage: supervisord ctl %v", wc.usage)
fmt.Printf("%v\n", err)
return err
}
return wc.cmd.Execute(args)
}
func init() {
ctlCmd, _ := parser.AddCommand("ctl",
"Control a running daemon",
"The ctl subcommand resembles supervisorctl command of original daemon.",
&ctlCommand)
ctlCmd.AddCommand("status",
"show program status",
"show all or some program status",
&statusCommand)
ctlCmd.AddCommand("start",
"start programs",
"start one or more programs",
&startCommand)
ctlCmd.AddCommand("stop",
"stop programs",
"stop one or more programs",
&stopCommand)
ctlCmd.AddCommand("restart",
"restart programs",
"restart one or more programs",
&restartCommand)
ctlCmd.AddCommand("shutdown",
"shutdown supervisord",
"shutdown supervisord",
&shutdownCommand)
ctlCmd.AddCommand("reload",
"reload the programs",
"reload the programs",
&reloadCommand)
ctlCmd.AddCommand("signal",
"send signal to program",
"send signal to program",
&signalCommand)
ctlCmd.AddCommand("pid",
"get the pid of specified program",
"get the pid of specified program",
&pidCommand)
ctlCmd.AddCommand("logtail",
"get the standard output&standard error of the program",
"get the standard output&standard error of the program",
&logtailCommand)
}