forked from goodrain/rainbond
/
exec.go
219 lines (194 loc) · 5.02 KB
/
exec.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
// WUTONG, Application Management Platform
// Copyright (C) 2014-2020 Wutong Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Wutong,
// one or multiple Commercial Licenses authorized by Wutong Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package app
import (
"context"
"fmt"
"io"
"os"
"syscall"
"time"
"unsafe"
"github.com/barnettZQG/gotty/server"
"github.com/kr/pty"
"github.com/sirupsen/logrus"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
)
type execContext struct {
tty, pty *os.File
kubeRequest *restclient.Request
config *restclient.Config
sizeUpdate chan remotecommand.TerminalSize
closed bool
}
// NewExecContext new exec Context
func NewExecContext(kubeRequest *restclient.Request, config *restclient.Config) (server.Slave, error) {
pty, tty, err := pty.Open()
if err != nil {
logrus.Errorf("open pty failure %s", err.Error())
return nil, err
}
ec := &execContext{
tty: tty,
pty: pty,
kubeRequest: kubeRequest,
config: config,
sizeUpdate: make(chan remotecommand.TerminalSize, 2),
}
if err := ec.Run(); err != nil {
return nil, err
}
return ec, nil
}
// NewLogContext new log Context
func NewLogContext(kubeRequest *restclient.Request, config *restclient.Config) (server.Slave, error) {
pty, tty, err := pty.Open()
if err != nil {
logrus.Errorf("open pty failure %s", err.Error())
return nil, err
}
ec := &execContext{
tty: tty,
pty: pty,
kubeRequest: kubeRequest,
config: config,
sizeUpdate: make(chan remotecommand.TerminalSize, 2),
}
if err := ec.RunLog(); err != nil {
return nil, err
}
return ec, nil
}
func (e *execContext) WaitingStop() bool {
return !e.closed
}
func (e *execContext) Run() error {
exec, err := remotecommand.NewSPDYExecutor(e.config, "POST", e.kubeRequest.URL())
if err != nil {
return fmt.Errorf("create executor failure %s", err.Error())
}
errCh := make(chan error)
go func() {
out := CreateOut(e.tty)
t := out.SetTTY()
errCh <- t.Safe(func() error {
defer e.Close()
if err := exec.Stream(remotecommand.StreamOptions{
Stdin: out.Stdin,
Stdout: out.Stdout,
Stderr: nil,
Tty: true,
TerminalSizeQueue: e,
}); err != nil {
logrus.Errorf("executor stream failure %s", err.Error())
return err
}
return nil
})
}()
// 如果在 200 毫秒内 errCh 有返回,则说明出错了,返回错误
// 否则认为是正常的,返回 nil
timeout := time.After(200 * time.Millisecond)
select {
case err := <-errCh:
return err
case <-timeout:
return nil
}
}
func (e *execContext) RunLog() error {
errCh := make(chan error)
go func() {
out := CreateOut(e.tty)
t := out.SetTTY()
errCh <- t.Safe(func() error {
defer e.Close()
rc, err := e.kubeRequest.Stream(context.Background())
if err != nil {
logrus.Errorf("stream failure %s", err.Error())
return err
}
defer rc.Close()
if _, err := io.Copy(out.Stdout, rc); err != nil {
logrus.Errorf("copy failure %s", err.Error())
return err
}
return nil
})
}()
// 如果在 200 毫秒内 errCh 有返回,则说明出错了,返回错误
// 否则认为是正常的,返回 nil
timeout := time.After(200 * time.Millisecond)
select {
case err := <-errCh:
return err
case <-timeout:
return nil
}
}
func (e *execContext) Read(p []byte) (n int, err error) {
return e.pty.Read(p)
}
func (e *execContext) Write(p []byte) (n int, err error) {
return e.pty.Write(p)
}
func (e *execContext) Close() error {
e1 := e.pty.Close()
e2 := e.tty.Close()
if e1 != nil {
return e1
}
return e2
}
func (e *execContext) WindowTitleVariables() map[string]interface{} {
return map[string]interface{}{}
}
func (e *execContext) Next() *remotecommand.TerminalSize {
size, ok := <-e.sizeUpdate
if !ok {
return nil
}
logrus.Infof("width %d height %d", size.Width, size.Height)
return &size
}
func (e *execContext) ResizeTerminal(width int, height int) error {
logrus.Infof("set width %d height %d", width, height)
e.sizeUpdate <- remotecommand.TerminalSize{
Width: uint16(width),
Height: uint16(height),
}
window := struct {
row uint16
col uint16
x uint16
y uint16
}{
uint16(height),
uint16(width),
0,
0,
}
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
e.pty.Fd(),
syscall.TIOCSWINSZ,
uintptr(unsafe.Pointer(&window)),
)
if errno != 0 {
return errno
}
return nil
}