-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.go
306 lines (261 loc) · 6.87 KB
/
docker.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
package main
import (
"fmt"
"io"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/crypto/ssh"
)
const (
CREATE_RECORD int = iota
INGRESS_UPDATE_RECORD
EGRESS_SUCCESS_RECORD
EGRESS_RETRY_RECORD
DELETE_RECORD
)
type Request struct {
cmd string
mod string
rcvCh chan *Response
}
type Response struct {
cmd string
output string
mod string
}
type MySsh struct {
client *ssh.Client
r io.Reader
w io.Writer
session *ssh.Session
IngressInputCh chan interface{}
EgressInputCh chan interface{}
ReadCh chan string
RecycleDuration time.Duration
MyMap map[string]map[string]interface{}
}
func main() {
elem := MySsh{RecycleDuration: 5}
elem.MyMap = make(map[string]map[string]interface{})
elem.ReadCh = make(chan string)
elem.IngressInputCh = make(chan interface{}, 10)
elem.EgressInputCh = make(chan interface{}, 10)
config := &ssh.ClientConfig{User: "root", HostKeyCallback: ssh.InsecureIgnoreHostKey(), Timeout: 0}
fmt.Println("Client:", config)
client, err := ssh.Dial("tcp", "192.168.100.105:22", config)
if err != nil {
fmt.Println("Connedtion failed :", err)
} else {
fmt.Println("Connedtion Success:", err)
elem.client = client
}
leftChan := elem.CreateIngressPipeline(elem.IngressInputCh)
finalCh := elem.CreateEgressPipeline(leftChan)
slice := []string{"intf", "asic"}
for _, mod := range slice {
str := "docker ps | grep " + mod + " | grep opt | wc"
elem.SendCommand(mod, str)
}
finalGo(finalCh)
}
func finalGo(finalCh chan interface{}) {
for val := range finalCh {
if val == "end" {
fmt.Println("End: ", val)
}
fmt.Println("Fina Reveive Value ", val)
}
}
func (c *MySsh) CreateIngressPipeline(rightChan chan interface{}) chan interface{} {
var leftChan chan interface{}
leftChan = c.SaveContext(rightChan)
leftChan = c.Audit(leftChan)
leftChan = c.RecordIngress(leftChan)
leftChan = c.Final(leftChan) // THis will put the message on teh caller queue
return leftChan
}
func (c *MySsh) CreateEgressPipeline(rightChan chan interface{}) chan interface{} {
var leftChan chan interface{}
leftChan = c.RecordEgress(rightChan)
leftChan = c.ProcessEgress(leftChan)
leftChan = c.FinalEgress(leftChan)
return leftChan
}
func (c *MySsh) RecordMap(cmd int, mod string) {
switch cmd {
case INGRESS_UPDATE_RECORD:
// Use Locks here
if _, ok := c.MyMap[mod]; !ok {
fmt.Println("Creating Map element for mod :", mod)
c.MyMap[mod] = make(map[string]interface{})
c.MyMap[mod]["CreateTS"] = time.Now()
c.MyMap[mod]["Status"] = "InProgress"
c.MyMap[mod]["RETRYCOUNT"] = 0
}
case EGRESS_SUCCESS_RECORD:
c.MyMap[mod]["EndTS"] = time.Now()
c.MyMap[mod]["Status"] = "SUCCESS"
case EGRESS_RETRY_RECORD:
if _, ok := c.MyMap[mod]; !ok {
fmt.Println("Something terible worng : record not created during ingress", mod)
return
}
c.MyMap[mod]["EndTS"] = time.Now()
c.MyMap[mod]["Status"] = "RETRYING"
valueInt := c.MyMap[mod]["RETRYCOUNT"].(int)
c.MyMap[mod]["RETRYCOUNT"] = valueInt + 1
if valueInt > 5 {
fmt.Println("This record had tried maximum : Mark it failure", mod)
}
}
c.DumpMap()
}
func DumpValue(value interface{}) {
switch value.(type) {
case int:
fmt.Println("Int:", value.(int))
case time.Time:
fmt.Println("Int:", value.(time.Time))
case string:
fmt.Println("Int:", value.(string))
}
}
func (c *MySsh) DumpMap() {
for key, value := range c.MyMap {
fmt.Printf(key + ":")
for k, v := range value {
fmt.Printf(k)
DumpValue(v)
}
}
}
func (c *MySsh) SendCommand(mod string, cmd string) {
req := &Request{cmd: cmd + "\n", mod: mod, rcvCh: make(chan *Response)}
fmt.Println("Sending Command ", cmd, time.Now())
c.IngressInputCh <- req
}
func ReadUntil(sshOut io.Reader, expString string) string {
buf := make([]byte, 1000)
readStr := ""
for {
n, err := sshOut.Read(buf)
readStr = readStr + string(buf[:n])
if err == io.EOF || n == 0 {
fmt.Println("Break,", err, n)
break
}
if strings.Contains(readStr, expString) {
break
}
}
return readStr
}
func (c *MySsh) Final(rchan chan interface{}) chan interface{} {
leftCh := make(chan interface{})
go func() {
for val := range rchan {
session, _ := c.client.NewSession()
req := val.(*Request)
str, _ := session.Output(req.cmd)
resp := &Response{output: string(str), cmd: req.cmd, mod: req.mod}
leftCh <- resp
session.Close()
}
}()
return leftCh
}
func (c *MySsh) SaveContext(rchan chan interface{}) chan interface{} {
leftCh := make(chan interface{})
go func() {
for val := range rchan {
fmt.Println("Inside Save Context")
leftCh <- val
}
}()
return leftCh
}
func (c *MySsh) Audit(rchan chan interface{}) chan interface{} {
leftCh := make(chan interface{})
go func() {
for val := range rchan {
fmt.Println("Inside Audit")
leftCh <- val
}
}()
return leftCh
}
func (c *MySsh) RecordIngress(rchan chan interface{}) chan interface{} {
leftCh := make(chan interface{})
go func() {
for val := range rchan {
fmt.Println("Inside recordIngress")
c.RecordMap(INGRESS_UPDATE_RECORD, val.(*Request).mod)
leftCh <- val
}
}()
return leftCh
}
func (c *MySsh) RecordEgress(rightCh chan interface{}) chan interface{} {
leftCh := make(chan interface{})
go func() {
for val := range rightCh {
fmt.Println("Inside Record Egress, timestamp on Map ")
leftCh <- val
}
}()
return leftCh
}
func (c *MySsh) ProcessRegExpReponse(leftCh chan interface{}, val interface{}) {
// If the command is not success, call sendCommand Again - After 2 seconds/3seconds etc
// Update Failure Count
// If more than required number, then stop doing this
status := false
defer func() {
if status {
// On Success, inform the Final Go Routine
c.RecordMap(EGRESS_SUCCESS_RECORD, val.(*Response).mod)
leftCh <- "Success"
} else {
// This create a new go routine - for recycling on failure
fmt.Println("Recycling the command again:", val.(*Response).mod)
time.Sleep(time.Second * c.RecycleDuration)
c.RecordMap(EGRESS_RETRY_RECORD, val.(*Response).mod)
c.SendCommand(val.(*Response).mod, val.(*Response).cmd)
}
}()
str := val.(*Response).output
rexp, _ := regexp.Compile(`\s+(\d+)\s+`)
slice := rexp.FindStringSubmatch(str)
if len(slice) == 0 {
fmt.Println("REgular expression ERROR:", str)
return
}
if value, err := strconv.Atoi(slice[1]); err == nil {
if value == 1 {
status = true
}
}
return
}
func (c *MySsh) ProcessEgress(rchan chan interface{}) chan interface{} {
leftCh := make(chan interface{})
go func() {
for val := range rchan {
fmt.Println("Check state in PRocessegress ", val.(*Response).cmd)
go c.ProcessRegExpReponse(leftCh, val)
}
}()
return leftCh
}
func (c *MySsh) FinalEgress(rchan chan interface{}) chan interface{} {
leftCh := make(chan interface{})
go func() {
for val := range rchan {
fmt.Println("Inside FinalEgress, ", val)
leftCh <- val
}
}()
return leftCh
}