forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctl.go
161 lines (147 loc) · 4.45 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
package main
import (
"fmt"
"github.com/ochinchina/supervisord/config"
"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"`
}
var ctlCommand CtlCommand
func (x *CtlCommand) getServerUrl() string {
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) Execute(args []string) error {
if len(args) == 0 {
return nil
}
rpcc := xmlrpcclient.NewXmlRPCClient(x.getServerUrl())
rpcc.SetUser(x.User)
rpcc.SetPassword(x.Password)
verb := args[0]
switch verb {
////////////////////////////////////////////////////////////////////////////////
// STATUS
////////////////////////////////////////////////////////////////////////////////
case "status":
processes := args[1:]
processesMap := make(map[string]bool)
for _, process := range processes {
processesMap[strings.ToLower(process)] = true
}
if reply, err := rpcc.GetAllProcessInfo(); err == nil {
x.showProcessInfo(&reply, processesMap)
}
////////////////////////////////////////////////////////////////////////////////
// START or STOP
////////////////////////////////////////////////////////////////////////////////
case "start", "stop":
state := map[string]string{
"start": "started",
"stop": "stopped",
}
processes := args[1:]
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 {
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 {
fmt.Printf("%s: ", pname)
if !reply.Value {
fmt.Printf("not ")
}
fmt.Printf("%s\n", state[verb])
} else {
fmt.Printf("%s: failed [%v]\n", pname, err)
}
}
}
////////////////////////////////////////////////////////////////////////////////
// SHUTDOWN
////////////////////////////////////////////////////////////////////////////////
case "shutdown":
if reply, err := rpcc.Shutdown(); err == nil {
if reply.Value {
fmt.Printf("Shut Down\n")
} else {
fmt.Printf("Hmmm! Something gone wrong?!\n")
}
}
case "reload":
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, ","))
}
}
case "signal":
sig_name, processes := args[1], args[2:]
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)
}
} 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)
}
}
}
default:
fmt.Println("unknown command")
}
return nil
}
func (x *CtlCommand) showProcessInfo(reply *xmlrpcclient.AllProcessInfoReply, processesMap map[string]bool) {
for _, pinfo := range reply.Value {
name := strings.ToLower(pinfo.Name)
description := pinfo.Description
if strings.ToLower(description) == "<string></string>" {
description = ""
}
if len(processesMap) <= 0 || processesMap[name] {
fmt.Printf("%-33s%-10s%s\n", name, pinfo.Statename, description)
}
}
}
func init() {
parser.AddCommand("ctl",
"Control a running daemon",
"The ctl subcommand resembles supervisorctl command of original daemon.",
&ctlCommand)
}