-
Notifications
You must be signed in to change notification settings - Fork 2
/
MomKat.go
415 lines (339 loc) · 13.2 KB
/
MomKat.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
// MeerKat Runtime of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2020.11.
package momkat
import (
"strconv"
"strings"
"sync"
"time"
"context"
"google.golang.org/grpc"
cblog "github.com/cloud-barista/cb-log"
common "github.com/cloud-barista/poc-cicd-spider/api-runtime/meerkat-runtime/common"
th "github.com/cloud-barista/poc-cicd-spider/api-runtime/meerkat-runtime/table-handler"
kv "github.com/cloud-barista/poc-cicd-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
// Loop:
// 1. start timer
// 2. called by others => reset timer
// 3. fired and be a MomKat
// (1) get childkat list
// (2) get the fist command
// (3) MomKat: if Command is not nil, run it!
// (4) check childkat liveness and set
// (5) request the Command to all ChildKat
// (6) clear the fist command after all completions.
func CheckChildKatAndSet(myServerID string) {
for true {
common.StartTimer()
if common.ResetFlag == true {
continue
}
// role of MomKat
SetImMomKat(myServerID)
// (1) childKatList := getChildKatStatusInfoList2()
childKatStatusInfoList := getChildKatStatusInfoList2(myServerID)
// (2) get the first command
cmd := getFirstCommand()
wg := new(sync.WaitGroup)
// (3) MomKat: if Command is not nil, run it!
// MomKat is also a MeerKat.
if (cmd!=nil) {
wg.Add(1)
go func() {
RunMomKatCommandAndSetResult(myServerID, cmd)
wg.Done()
}()
}
// only all childkats except this momkat
for _, childKatStatusInfo:= range childKatStatusInfoList {
// sould clone info object because childKatStatusInfo is a point of childKatStatusInfoList's children
statusInfo := common.StatusInfo{childKatStatusInfo.RowNumber, childKatStatusInfo.CheckBit,
childKatStatusInfo.ServerID, childKatStatusInfo.Status, childKatStatusInfo.Time, childKatStatusInfo.Count}
wg.Add(1)
go func() {
// (4) check childkat liveness and set
GetAndSetStatus(statusInfo)
// (5) request the Command to all ChildKat
if (cmd!=nil) && (cmd.CMDTYPE==common.ALL) {
if statusInfo.Status == "L" { // only live ChildKat
RunCommandAndSetResult(statusInfo, cmd)
}
}
wg.Done()
}()
}
wg.Wait()
// (6) clear the fist command and pupup command after all completions.
if cmd != nil {
popupCommand()
}
} // end of for true
}
// 1. retrive first command from Sheets
// 2. make a Command objject and return it
func getFirstCommand() *common.Command {
cblogger := cblog.GetLogger("CB-SPIDER")
srv, err := common.GetTableHandler()
if err != nil {
cblogger.Fatalf("Unable to retrieve Sheets client: %v", err)
}
values, err := th.ReadRange(srv, &th.CellRange{Sheet:common.CommandSheetName, X:common.CommandIDX, Y:common.CommandTableY, X2:common.CommandCMDX})
if err != nil {
cblogger.Errorf("could not read Range: %v", err)
return nil
}
if len(values) <= 0 {
return nil
}
return &common.Command{CMDID:values[0], CMDTYPE:values[1], CMD:values[2], Time:common.GetCurrentTime()}
}
func popupCommand(){
cblogger := cblog.GetLogger("CB-SPIDER")
srv, err := common.GetTableHandler()
if err != nil {
cblogger.Fatalf("Unable to retrieve Sheets client: %v", err)
}
values, err := th.ReadRange2(srv, &th.CellRange2{Sheet:common.CommandSheetName, X:common.CommandIDX, Y:common.CommandTableY,
X2:common.CommandCMDX, Y2:strSum(common.CommandTableY, common.MaxCommands)})
if err != nil {
cblogger.Errorf("could not read Range: %v", err)
}
// popup command, values type: [][]string
switch len(values) {
case 0:
return
case 1:
th.WriteRange(srv, &th.CellRange{Sheet:common.CommandSheetName, X:common.CommandIDX, Y:common.CommandTableY,
X2:common.CommandCMDX}, []string{"", "", ""})
default :
for i, _ := range values {
if i < (len(values)-1) {
values[i] = values[i+1]
}
}
values[len(values)-1] = []string{"", "", ""}
th.WriteRange2(srv, &th.CellRange2{Sheet:common.CommandSheetName, X:common.CommandIDX, Y:common.CommandTableY,
X2:common.CommandCMDX, Y2:strSum(common.CommandTableY, common.MaxCommands)}, values)
} // end of switch
}
func SetImMomKat(myServerID string) {
cblogger := cblog.GetLogger("CB-SPIDER")
srv, err := common.GetTableHandler()
if err != nil {
cblogger.Fatalf("Unable to retrieve Sheets client: %v", err)
}
strY := strDecrement(common.StatusTableY) // minus 1
strY = strDecrement(strY) // minus 1
err = th.WriteCell(srv, &th.Cell{Sheet:common.StatusSheetName, X:common.StatusSpiderIDX, Y:strY}, myServerID)
if err != nil {
cblogger.Fatalf("Unable to write data into sheet: %v", err)
}
}
func GetAndSetStatus(statusInfo common.StatusInfo) {
cblogger := cblog.GetLogger("CB-SPIDER")
serverIP := (strings.Split(statusInfo.ServerID, "-"))[0]
client, ctx, err := getStatusClient(serverIP)
if err != nil {
cblogger.Errorf("could not get Client: %v", err)
}
_, err = client.GetChildStatus(ctx, &common.Empty{})
if err != nil {
//cblogger.Errorf("could not Fetch Resource Status Information: %v", err)
cblogger.Infof("%s: could not Fetch Resource Status Information: %v", serverIP, err)
statusInfo.Count = strIncrement(statusInfo.Count)
switch statusInfo.Status {
case "L":
statusInfo.Status = "N"
statusInfo.Count = "1"
case "N":
if statusInfo.Count >= common.TransCount {
statusInfo.Status = "Z"
statusInfo.Count = "1"
}
case "Z":
if statusInfo.Count >= common.TransCount {
statusInfo.Status = "D"
statusInfo.Count = "1"
}
case "D":
if statusInfo.Count >= common.TransCount {
// delete this spider in the list
common.ClearCheckBit(statusInfo.RowNumber)
}
} // end of switch
} else { // end of if
if statusInfo.Status == "L" {
statusInfo.Count = strIncrement(statusInfo.Count)
} else {
statusInfo.Status = "L"
statusInfo.Count = "1"
}
}
statusInfo.Time = common.GetCurrentTime()
cblogger.Info("[" + statusInfo.ServerID + "] " + statusInfo.Status + "-" + statusInfo.Time)
err = common.WriteStatusInfo(&statusInfo)
if err != nil {
cblogger.Errorf("could not write Cell: %v", err)
}
}
func RunMomKatCommandAndSetResult(myServerID string, cmd *common.Command) {
cblogger := cblog.GetLogger("CB-SPIDER")
cmdResult, err := RunCommand(myServerID, cmd)
if err != nil {
//cblogger.Errorf("could not Run Command: %v", err)
cblogger.Infof("%s: could not Run Command(%#v) - %v", myServerID, cmd, err)
}
// @todo Now, refined the time because time difference
cmdResult.Time = common.GetCurrentTime()
cblogger.Info("[" + cmdResult.ServerID + "] " + cmdResult.CMD + "-" + cmdResult.Result + "-" + cmdResult.Time)
/*
cmdResultInfo := common.CommandResultInfo{RowNumber:common.CommandTableY, ServerID:cmdResult.ServerID, ResultNow:cmdResult.Result, Time:cmdResult.Time}
err = common.WriteCommandResult(&cmdResultInfo)
if err != nil {
cblogger.Errorf("could not write Cell: %v", err)
}
*/
}
func RunCommandAndSetResult(statusInfo common.StatusInfo, cmd *common.Command) {
cblogger := cblog.GetLogger("CB-SPIDER")
serverIP := (strings.Split(statusInfo.ServerID, "-"))[0]
client, ctx, err := getRunCommandClient(serverIP)
if err != nil {
cblogger.Errorf("could not get Client: %v", err)
}
cmdResult, err := client.RunCommand(ctx, cmd)
if err != nil {
//cblogger.Errorf("could not Run Command: %v", err)
cblogger.Infof("%s: could not Run Command(%#v) - %v", serverIP, cmd, err)
}
// @todo Now, refined the time because time difference
cmdResult.Time = common.GetCurrentTime()
cblogger.Info("[" + cmdResult.ServerID + "] " + cmdResult.CMD + "-" + cmdResult.Result + "-" + cmdResult.Time)
/*
cmdResultInfo := common.CommandResultInfo{RowNumber:common.CommandTableY, ServerID:cmdResult.ServerID, ResultNow:cmdResult.Result, Time:cmdResult.Time}
err = common.WriteCommandResult(&cmdResultInfo)
if err != nil {
cblogger.Errorf("could not write Cell: %v", err)
}
*/
}
func strIncrement(strCount string) string {
intCount, _ := strconv.Atoi(strCount)
strCount = strconv.Itoa(intCount+1)
return strCount
}
func strDecrement(strCount string) string {
intCount, _ := strconv.Atoi(strCount)
strCount = strconv.Itoa(intCount-1)
return strCount
}
func strSum(strCount1 string, strCount2 string) string {
intCount1, _ := strconv.Atoi(strCount1)
intCount2, _ := strconv.Atoi(strCount2)
strCount := strconv.Itoa(intCount1+intCount2)
return strCount
}
func getStatusClient(serverPort string) (common.ChildStatusClient, context.Context, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
// Set up a connection to the server.
conn, err := grpc.Dial(serverPort, grpc.WithInsecure())
if err != nil {
cblogger.Errorf("did not connect: %v", err)
}
client := common.NewChildStatusClient(conn)
ctx, _ := context.WithTimeout(context.Background(), common.ChildKatCallTimeout*time.Millisecond)
return client, ctx, nil
}
func getRunCommandClient(serverPort string) (common.RunCommandClient, context.Context, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
// Set up a connection to the server.
conn, err := grpc.Dial(serverPort, grpc.WithInsecure())
if err != nil {
cblogger.Errorf("did not connect: %v", err)
}
client := common.NewRunCommandClient(conn)
ctx, _ := context.WithTimeout(context.Background(), common.ChildKatCallTimeout*time.Millisecond)
return client, ctx, nil
}
// 1. check all Check Bits
// 2. make the list of live children
func getChildKatStatusInfoList2(myServerID string) []common.StatusInfo {
cblogger := cblog.GetLogger("CB-SPIDER")
srv, err := common.GetTableHandler()
if err != nil {
cblogger.Fatalf("Unable to retrieve Sheets client: %v", err)
}
max := common.GetMaxSpiders()
if max == -1 {
return nil
}
childKatStatusInfoList := []common.StatusInfo{}
intY, _ := strconv.Atoi(common.StatusTableY)
values, err := th.ReadRange2(srv, &th.CellRange2{Sheet:common.StatusSheetName, X:common.StatusRowLockX, Y:common.StatusTableY, X2:common.StatusCountX, Y2:strSum(common.StatusTableY, common.MaxSpiders)})
if err != nil {
cblogger.Errorf("could not read Range: %v", err)
return nil
}
for count, row := range values {
// skip self check.
if row[1] == myServerID {
continue
}
if row[0] == "1" {
thisY := intY + count
strY := strconv.Itoa(thisY)
statusInfo := common.StatusInfo{strY, row[0], row[1], row[2], row[3], row[4]}
// add this server into the effective childKat list
childKatStatusInfoList = append(childKatStatusInfoList, statusInfo)
}
}
return childKatStatusInfoList
}
// 1. check all Check Bits
// 2. make the list of live children
// deprecated because Goolge Sheeets access Quota limits
func getChildKatServerList() []kv.KeyValue {
cblogger := cblog.GetLogger("CB-SPIDER")
srv, err := common.GetTableHandler()
if err != nil {
cblogger.Fatalf("Unable to retrieve Sheets client: %v", err)
}
max := common.GetMaxSpiders()
if max == -1 {
return nil;
}
childKatList := []kv.KeyValue{}
for i:=0;i<max;i++ {
intY, _ := strconv.Atoi(common.StatusTableY)
intY += i
strY := strconv.Itoa(intY)
value, err := th.ReadCell(srv, &th.Cell{Sheet:common.StatusSheetName, X:common.StatusRowLockX, Y:strY})
if err != nil {
cblogger.Errorf("could not read Cell: %v", err)
break;
}
if value == "1" {
serverID, err := th.ReadCell(srv, &th.Cell{Sheet:common.StatusSheetName, X:common.StatusSpiderIDX, Y:strY})
if err != nil {
cblogger.Errorf("could not read Cell: %v", err)
break;
}
serverIP := (strings.Split(serverID, "-"))[0]
childKat := kv.KeyValue{strY, serverIP}
// add this server into the effective childKat list
childKatList = append(childKatList, childKat)
}
}
return childKatList
}
func RunCommand(myServerID string, cmd *common.Command) (*common.CommandResult, error) {
strResult := common.RunCommand(cmd.CMD, myServerID)
time := common.GetCurrentTime()
return &common.CommandResult{ServerID: myServerID, CMD: cmd.CMD, Result:strResult, Time: time}, nil
}