/
controller.go
248 lines (231 loc) · 6.76 KB
/
controller.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
package server
import (
"context"
"errors"
"net"
"sort"
"strings"
"github.com/deatheyes/chaos/common"
"github.com/deatheyes/chaos/config"
"github.com/deatheyes/chaos/control"
"github.com/deatheyes/chaos/pb"
"github.com/golang/glog"
iris "github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/logger"
"github.com/kataras/iris/v12/middleware/recover"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"gopkg.in/yaml.v2"
)
// API constants
const (
IDALL = "all" // flag to get all stages' detail
IDList = "list"
ConfigSeparator = "---\n" // separator to join configuration
ListSeparator = "," // separator to join stage list
)
// APIReturn is the HTTP return value
type APIReturn struct {
Status string `json:"status"` // process status
Data map[string]interface{} `json:"data"` // result returned
}
// Controller is a impliment of GRPC
type Controller struct {
AddressGRPC string
AddressHTTP string
ConfigPath string
TaskManager *control.TaskManager
Config *config.ControllerConfig
}
// NewController create a new Controller
func NewController(config map[string]string) *Controller {
return &Controller{
AddressGRPC: config[FIGRPC],
AddressHTTP: config[FIHTTP],
ConfigPath: config[ConfigPath],
}
}
// Run serve as a controller
func (c *Controller) Run() error {
// load config
conf, err := config.LoadControllerConfigFromFile(c.ConfigPath)
if err != nil {
glog.Warningf("[Controller] task manager load configuration faild: %v", err)
return err
}
c.Config = conf
// setup task manager
c.TaskManager = control.NewTaskManager()
if err := c.TaskManager.Init(); err != nil {
glog.Fatalf("[Controller] task manager init failed:%v", err)
return err
}
// run grpc
listen, err := net.Listen("tcp", c.AddressGRPC)
if err != nil {
glog.Fatalf("[Controller] listen to %s failed: %v", c.AddressGRPC)
return err
}
s := grpc.NewServer()
pb.RegisterControllerServer(s, c)
go func() {
s.Serve(listen)
}()
// run RESET server
app := iris.New()
app.Logger().SetLevel("debug")
app.Use(recover.New())
app.Use(logger.New())
app.Handle("GET", "/chaos/{id:string}", c.queryFaultInjection())
app.Handle("POST", "/chaos/{id:string}", c.runFaultInjection())
app.Handle("DELETE", "/chaos/{id:string}", c.removeFaultInjection())
app.Handle("POST", "exec/raw", c.executeRawCommand())
app.Run(iris.Addr(c.AddressHTTP))
return nil
}
// Register is a impliment of pb.Controller
func (c *Controller) Register(ctx context.Context, r *pb.RegisterRequest) (*pb.RegisterResponse, error) {
// get the agent address
pr, ok := peer.FromContext(ctx)
if !ok {
glog.Warning("[Controller] get agent info failed")
return &pb.RegisterResponse{Code: 1}, errors.New("get agent info failed")
}
address := pr.Addr.String()
segments := strings.Split(address, ":")
if len(segments) != 2 {
glog.Warningf("[Controller] invalidate agent address %s")
return &pb.RegisterResponse{Code: 1}, errors.New("invalidate agent address")
}
glog.Infof("[Controller] agent %s ask for register", address)
// register the agent
a := &config.Agent{
Role: r.GetRole(),
Address: segments[0] + r.GetAddress(),
Version: r.GetVersion(),
Selector: r.GetSelector(),
}
c.TaskManager.Register(a)
return &pb.RegisterResponse{Code: 0}, nil
}
func (c *Controller) queryFaultInjection() func(ctx iris.Context) {
return func(ctx iris.Context) {
var ret string
id := ctx.Params().Get("id")
if id == IDList {
// list all availiable stage
list := []string{}
for _, v := range c.TaskManager.StageDict {
list = append(list, v.Name)
}
sort.Strings(list)
ret = strings.Join(list, ListSeparator)
} else if id == IDALL {
// return all stages
segments := []string{}
for k, v := range c.TaskManager.StageConfigDict {
data, err := yaml.Marshal(v)
if err != nil {
glog.Warningf("[Controller] marshal stage %s configuration failed: %v", k, err)
} else {
segments = append(segments, string(data))
}
}
ret = strings.Join(segments, ConfigSeparator)
} else {
if v, ok := c.TaskManager.StageConfigDict[id]; !ok {
ret = control.ErrNotFound.Error()
} else {
data, err := yaml.Marshal(v)
if err != nil {
glog.Warningf("[Controller] marshal stage %s configuration failed: %v", id, err)
ret = err.Error()
} else {
ret = string(data)
}
}
}
ctx.WriteString(ret)
}
}
func (c *Controller) runFaultInjection() func(ctx iris.Context) {
return func(ctx iris.Context) {
id := ctx.Params().Get("id")
if err := c.TaskManager.RunStage(id); err != nil {
ctx.WriteString(err.Error())
}
}
}
func (c *Controller) removeFaultInjection() func(ctx iris.Context) {
return func(ctx iris.Context) {
id := ctx.Params().Get("id")
if _, ok := c.TaskManager.StageDict[id]; ok {
c.TaskManager.StageDict[id].Kill()
// temporary
// return the first target if exists
var out string
for _, t := range c.TaskManager.StageDict[id].TaskDict {
if len(t.GetLastTarget()) != 0 {
out = t.GetLastTarget()
break
}
}
ctx.WriteString(out)
} else {
ctx.WriteString(control.ErrNotFound.Error())
}
}
}
// errors for execution of raw command
var (
ErrNoCommand = errors.New("NoCommand")
ErrNoTarget = errors.New("NoTarget")
ErrUnknownTarget = errors.New("UnknownTarget")
ErrCreateConnection = errors.New("ConnectFailed")
)
func (c *Controller) executeRawCommand() func(ctx iris.Context) {
return func(ctx iris.Context) {
in := make(map[string]string)
if err := ctx.ReadJSON(&in); err != nil {
glog.Warningf("[Controller] read josn failed: %v", err)
ctx.WriteString(err.Error())
return
}
command, ok := in[common.ArgumentsKeyCommand]
if !ok {
glog.Warningf(ErrNoCommand.Error())
ctx.WriteString(ErrNoCommand.Error())
return
}
target, ok := in[common.ArgumentsKeyTarget]
if !ok {
glog.Warningf(ErrNoTarget.Error())
ctx.WriteString(ErrNoTarget.Error())
return
}
glog.Infof("[Controller] try to dispatch command %s to %s", command, target)
c.TaskManager.Lock()
defer c.TaskManager.Unlock()
for k := range c.TaskManager.Topology {
if k == target {
// execute command
conn, err := grpc.Dial(k, grpc.WithInsecure())
if err != nil {
glog.Warningf("[Controller] create connection to %s failed: %v", k, err)
ctx.WriteString(ErrCreateConnection.Error())
return
}
client := pb.NewAgentClient(conn)
_, err = client.Exec(context.Background(), &pb.ExecRequest{Command: command, Background: false})
if err != nil {
glog.Warningf("[Controller] execute command %s at %s failed: %v", command, k, err)
ctx.WriteString(err.Error())
return
}
glog.Infof("[Controller] exexute command %s done", command)
return
}
}
ctx.WriteString(ErrUnknownTarget.Error())
}
}