-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
258 lines (211 loc) · 6.14 KB
/
run.go
File metadata and controls
258 lines (211 loc) · 6.14 KB
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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/kr/pty"
"golang.org/x/crypto/ssh/terminal"
)
const TimeLayout = "2006-1-2_15:04:05"
func runterm(user, ns, pod string) (out string, err error) {
// s := fmt.Sprintf("kubectl exec -it -n %v %v sh", ns, pod)
// let kubectl-debug to limit network and user mission
image := "harbor.haodai.net/ops/netshoot:v2.8"
s := fmt.Sprintf("kubectl debug -n %v %v --image %v /gobash", ns, pod, image)
// log.Println("executing: ", s)
c := exec.Command("sh", "-c", s)
// stdinIn, _ := c.StdinPipe()
pr, pw := io.Pipe()
// c := exec.Command("/bin/bash", "-c", "kubectl exec -it -n flow-center tangguo-online-597fc44cb4-q8lsc sh")
f, err := pty.Start(c)
if err != nil {
panic(err)
}
oldState, err := terminal.MakeRaw(0)
if err != nil {
panic(err)
}
defer terminal.Restore(0, oldState)
// go io.Copy(f, os.Stdin)
// debug container won't enter it by default, need user press enter key to start
// defer func() { fmt.Fprintf(pw, "ls\n\r") }()
// var buf bytes.Buffer
tee := io.TeeReader(os.Stdin, pw)
defer pw.Close()
go func() {
// defer pw.Close()
// fmt.Fprintf(pw, "ls\r\n")
// return
// copy the data written to the PipeReader via the cmd to stdout
// if _, err := io.Copy(os.Stdout, pr); err != nil {
// log.Fatal(err)
// }
// scanner := bufio.NewScanner(pr)
// for scanner.Scan() {
// // ucl := strings.ToUpper(scanner.Text())
// text := scanner.Text()
// fmt.Println("got ", text)
// // fmt.Println("got", ucl)
// write(text + "\n")
// // fmt.Fprintf(f, "%v", text)
// }
reader := bufio.NewReader(pr)
var b strings.Builder
for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
b.WriteString(string(input))
// fmt.Printf("%c", input)
if input == '\r' {
input = '\n'
t := time.Now().Local().Format(TimeLayout)
record(fmt.Sprintf("%v user: %v, ns: %v, pod: %v, cmd: %v\n", t, user, ns, pod, b.String()))
b = strings.Builder{}
}
// write(fmt.Sprintf("%c", input))
// output = append(output, input)
}
// var output []rune
// for {
// input, _, err := reader.ReadRune()
// if err != nil && err == io.EOF {
// break
// }
// // fmt.Printf("%c", input)
// write(fmt.Sprintf("%c", input))
// // output = append(output, input)
// }
// write(fmt.Sprintf("got : %v", len(output)))
// for j := 0; j < len(output); j++ {
// // fmt.Printf("got %c", output[j])
// write(fmt.Sprintf("got %c", output[j]))
// }
}()
// go io.Copy(f, os.Stdin)
go io.Copy(f, tee)
// go io.Copy(f, stdinIn)
// go func() {
// _, errStdout = io.Copy(stdout, stdoutIn)
// }()
io.Copy(os.Stdout, f)
return
}
// func run(ns, pod string) (out string, err error) {
// s := fmt.Sprintf("kubectl exec -it -n %v %v sh", ns, pod)
// log.Println("executing: ", s)
// // cmd := exec.Command("sh", "-c", s)
// cmd := exec.Command("sh")
// cmd.Stdin = os.Stdin
// // cmd.Stdout = os.Stdout
// // cmd.Stderr = os.Stderr
// output, err := cmd.CombinedOutput()
// if err != nil {
// log.Printf("build execute build err: %v\noutput: %v\n", err, string(output))
// return
// }
// out = string(output)
// return
// }
// func run2(ns, pod string) (out string, err error) {
// // Could read $PAGER rather than hardcoding the path.
// // cmd := exec.Command("/usr/bin/less", file)
// // cmd := exec.Command("/usr/bin/cat")
// s := fmt.Sprintf("kubectl exec -it -n %v %v sh", ns, pod)
// log.Println("executing: ", s)
// cmd := exec.Command("sh", "-c", s)
// pr, pw := io.Pipe()
// // defer pw.Close()
// // go func() {
// // // defer pr.Close()
// // // copy the data written to the PipeReader via the cmd to stdout
// // if _, err := io.Copy(os.Stdin, pr); err != nil {
// // log.Fatal(err)
// // }
// // }()
// go func() {
// defer pw.Close()
// // // copy the data written to the PipeReader via the cmd to stdout
// // if _, err := io.Copy(os.Stdout, pr); err != nil {
// // log.Fatal(err)
// // }
// scanner := bufio.NewScanner(os.Stdin)
// for scanner.Scan() {
// // ucl := strings.ToUpper(scanner.Text())
// text := scanner.Text()
// // fmt.Println("got", ucl)
// write(text + "\n")
// fmt.Fprintf(pw, "%v", text)
// }
// }()
// // Feed it with the string you want to display.
// // cmd.Stdin = strings.NewReader("The text you want to show.")
// // cmd.Stdin = os.Stdin
// cmd.Stdin = pr
// // This is crucial - otherwise it will write to a null device.
// cmd.Stdout = os.Stdout
// // Fork off a process and wait for it to terminate.
// err = cmd.Run()
// if err != nil {
// log.Fatal(err)
// }
// return
// }
func record(line string) {
f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte(line)); err != nil {
log.Fatal("write err", err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
// func runpty(user, ns, pod string) (out string, err error) {
// // c := exec.Command("grep", "--color=auto", "bar")
// s := fmt.Sprintf("kubectl exec -it -n %v %v sh", ns, pod)
// log.Println("executing: ", s)
// c := exec.Command("sh", "-c", s)
// // pw, s_ := c.StdinPipe()
// f, err := pty.Start(c)
// if err != nil {
// panic(err)
// }
// // oldState, err := terminal.MakeRaw(0)
// // if err != nil {
// // panic(err)
// // }
// // defer terminal.Restore(0, oldState)
// // pr, pw := io.Pipe()
// go func() {
// // f.Write([]byte("ls\n"))
// // f.Write([]byte("bar\n"))
// // f.Write([]byte("baz\n"))
// // f.Write([]byte{4}) // EOT
// scanner := bufio.NewScanner(os.Stdin)
// for scanner.Scan() {
// // ucl := strings.ToUpper(scanner.Text())
// text := scanner.Text()
// // fmt.Println("got", ucl)
// t := time.Now().Local().Format(TimeLayout)
// record(fmt.Sprintf("user: %v, ns: %v, pod: %v, cmd: %v\n", user, ns, pod, text))
// // fmt.Fprintf(pw, "%v", text)
// // f.Write([]byte("ls\n"))
// f.Write([]byte(text + "\n"))
// }
// f.Write([]byte{4}) // eof
// }()
// // go func() {
// // io.Copy(os.Stdin, f)
// // }()
// io.Copy(os.Stdout, f)
// return
// }