forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctl.go
416 lines (366 loc) · 10.6 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
package main
import (
"fmt"
"github.com/ochinchina/supervisord/config"
"github.com/ochinchina/supervisord/types"
"github.com/ochinchina/supervisord/xmlrpcclient"
"os"
"strings"
)
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"`
}
type StatusCommand struct {
}
type StartCommand struct {
}
type StopCommand struct {
}
type RestartCommand struct {
}
type ShutdownCommand struct {
}
type ReloadCommand struct {
}
type PidCommand struct {
}
type SignalCommand struct {
}
var ctlCommand CtlCommand
var statusCommand StatusCommand
var startCommand StartCommand
var stopCommand StopCommand
var restartCommand RestartCommand
var shutdownCommand ShutdownCommand
var reloadCommand ReloadCommand
var pidCommand PidCommand
var signalCommand SignalCommand
func (x *CtlCommand) getServerUrl() string {
options.Configuration, _ = findSupervisordConf()
if x.ServerUrl != "" {
return x.ServerUrl
} else if _, err := os.Stat(options.Configuration); err == nil {
config := config.NewConfig(options.Configuration)
config.Load()
if entry, ok := config.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 {
config := config.NewConfig(options.Configuration)
config.Load()
if entry, ok := config.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 {
config := config.NewConfig(options.Configuration)
config.Load()
if entry, ok := config.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
}
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":
sig_name, processes := args[1], args[2:]
x.signal(rpcc, sig_name, 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, sig_name 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", sig_name)
os.Exit(1)
}
} else {
reply, err := rpcc.SignalProcess(sig_name, process)
if err == nil && reply.Success {
fmt.Printf("Succeed to send signal %s to process %s\n", sig_name, process)
} else {
fmt.Printf("Fail to send signal %s to process %s\n", sig_name, 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)
}
}
// 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(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"
}
}
func (sc *StatusCommand) Execute(args []string) error {
ctlCommand.status(ctlCommand.createRpcClient(), args)
return nil
}
func (sc *StartCommand) Execute(args []string) error {
ctlCommand.startStopProcesses(ctlCommand.createRpcClient(), "start", args)
return nil
}
func (sc *StopCommand) Execute(args []string) error {
ctlCommand.startStopProcesses(ctlCommand.createRpcClient(), "stop", args)
return nil
}
func (rc *RestartCommand) Execute(args []string) error {
ctlCommand.restartProcesses(ctlCommand.createRpcClient(), args)
return nil
}
func (sc *ShutdownCommand) Execute(args []string) error {
ctlCommand.shutdown(ctlCommand.createRpcClient())
return nil
}
func (rc *ReloadCommand) Execute(args []string) error {
ctlCommand.reload(ctlCommand.createRpcClient())
return nil
}
func (rc *SignalCommand) Execute(args []string) error {
sig_name, processes := args[0], args[1:]
ctlCommand.signal(ctlCommand.createRpcClient(), sig_name, processes)
return nil
}
func (pc *PidCommand) Execute(args []string) error {
ctlCommand.getPid(ctlCommand.createRpcClient(), args[0])
return nil
}
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)
}