-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathps.go
More file actions
358 lines (331 loc) · 8.49 KB
/
ps.go
File metadata and controls
358 lines (331 loc) · 8.49 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
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
package cmd
import (
"context"
"fmt"
"io"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/containerd/console"
"github.com/drycc/controller-sdk-go/api"
"github.com/drycc/controller-sdk-go/events"
"github.com/drycc/controller-sdk-go/ps"
"github.com/drycc/workflow-cli/pkg/logging"
"golang.org/x/net/websocket"
yaml "gopkg.in/yaml.v3"
)
const (
stdinChannel = "\x00"
stdoutChannel = "\x01"
stderrChannel = "\x02"
errorChannel = "\x03"
resizeChannel = "\x04"
)
// PsList lists an app's processes.
func (d *DryccCmd) PsList(appID string, results int) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
if results == defaultLimit {
results = s.Limit
}
processes, _, err := ps.List(s.Client, appID, results)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
printProcesses(d, appID, processes)
return nil
}
// PodLogs returns the logs from an pod.
func (d *DryccCmd) PsLogs(appID, podID string, lines int, follow bool, container string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
request := api.PodLogsRequest{
Lines: lines,
Follow: follow,
Container: container,
}
conn, err := ps.Logs(s.Client, appID, podID, request)
if err != nil {
return err
}
defer conn.Close()
for {
var message string
err := websocket.Message.Receive(conn, &message)
if err != nil {
if err != io.EOF {
log.Printf("error: %v", err)
}
break
}
logging.PrintLog(os.Stdout, strings.TrimRight(string(message), "\n"))
}
return nil
}
// PsList lists an app's processes.
func (d *DryccCmd) PsExec(appID, podID string, tty, stdin bool, command []string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
request := api.Command{
Tty: tty,
Stdin: stdin,
Command: command,
}
conn, err := ps.Exec(s.Client, appID, podID, request)
if err != nil {
return err
}
defer conn.Close()
if stdin {
streamExec(conn, tty)
} else {
printExec(d, conn)
}
return nil
}
// PsDescribe describe an app's processes.
func (d *DryccCmd) PsDescribe(appID, podID string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
// The 1000 is fake for now until API understands limits
podState, _, err := ps.Describe(s.Client, appID, podID, 1000)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
table := d.getDefaultFormatTable([]string{})
for _, containerState := range podState {
table.Append([]string{"Container:", containerState.Container})
table.Append([]string{"Image:", containerState.Image})
table.Append([]string{"Command:"})
for _, command := range containerState.Command {
table.Append([]string{"", fmt.Sprintf("- %v", command)})
}
table.Append([]string{"Args:"})
for _, arg := range containerState.Args {
table.Append([]string{"", fmt.Sprintf("- %v", arg)})
}
// State
for key := range containerState.State {
table.Append([]string{"State:", key})
value := containerState.State[key]
for innerKey := range value {
innerValue := strconv.Quote(fmt.Sprintf("%v", value[innerKey]))
if innerKey == "startedAt" || innerKey == "finishedAt" {
innerValue = d.formatTime(fmt.Sprintf("%s", value[innerKey]))
}
table.Append([]string{fmt.Sprintf(" %s:", innerKey), innerValue})
}
}
// LastState
for key := range containerState.LastState {
table.Append([]string{"Last State:", key})
value := containerState.LastState[key]
for innerKey := range value {
innerValue := strconv.Quote(fmt.Sprintf("%v", value[innerKey]))
if innerKey == "startedAt" || innerKey == "finishedAt" {
innerValue = d.formatTime(fmt.Sprintf("%s", value[innerKey]))
}
table.Append([]string{fmt.Sprintf(" %s:", innerKey), innerValue})
}
}
table.Append([]string{"Ready:", fmt.Sprintf("%v", containerState.Ready)})
table.Append([]string{"Restart Count:", fmt.Sprintf("%v", containerState.RestartCount)})
table.Append([]string{})
}
table.Render()
// display events
events, _, err := events.ListPodEvents(s.Client, appID, podID, 1000)
if err != nil {
return err
}
if len(events) != 0 {
// table event
te := d.getDefaultFormatTable([]string{})
te.Append([]string{"Events:"})
te.Append([]string{" REASON", "MESSAGE", "CREATED"})
for _, ev := range events {
te.Append([]string{
fmt.Sprintf(" %s", ev.Reason),
ev.Message,
d.formatTime(ev.Created),
})
}
te.Render()
}
return nil
}
// PsDelete delete an pod.
func (d *DryccCmd) PsDelete(appID string, podIDs []string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
pods := strings.Join(podIDs, ",")
d.Printf("Deleting %s from %s... ", pods, appID)
quit := progress(d.WOut)
err = ps.Delete(s.Client, appID, pods)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
func printProcesses(d *DryccCmd, appID string, input []api.Pods) {
processes := ps.ByType(input)
if len(processes) == 0 {
d.Println(fmt.Sprintf("No processes found in %s app.", appID))
} else {
table := d.getDefaultFormatTable([]string{"NAME", "RELEASE", "STATE", "PTYPE", "READY", "RESTARTS", "STARTED"})
for _, process := range processes {
for _, pod := range process.PodsList {
table.Append([]string{
pod.Name,
pod.Release,
pod.State,
pod.Type,
pod.Ready,
fmt.Sprintf("%v", pod.Restarts),
d.formatTime(pod.Started),
})
}
}
table.Render()
}
}
func printExec(d *DryccCmd, conn *websocket.Conn) error {
var data string
err := websocket.Message.Receive(conn, &data)
if err != nil {
if err != io.EOF {
log.Printf("error: %v", err)
}
return nil
}
message, err := parseChannelMessage(data)
if err == nil {
d.Printf("%s", message)
}
return err
}
func runRecvTask(conn *websocket.Conn, c console.Console, recvChan, sendChan chan string) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
var data string
err := websocket.Message.Receive(conn, &data)
if err != nil {
cancel()
break
}
message, err := parseChannelMessage(data)
if err != nil {
cancel()
break
}
recvChan <- message
}
}()
go func() {
buf := make([]byte, 1024)
for {
size, err := c.Read(buf)
if err == io.EOF {
cancel()
break
} else if err != nil {
continue
}
sendChan <- string(buf[:size])
}
}()
return ctx, cancel
}
func runResizeTask(conn *websocket.Conn, c console.Console) {
go func() {
var size console.WinSize
for {
if tmpSize, err := c.Size(); err == nil {
if size.Height != tmpSize.Height || size.Width != tmpSize.Width {
size = tmpSize
message := fmt.Sprintf(`{"Height": %d, "Width": %d}`, size.Height, size.Width)
if err := websocket.Message.Send(conn, resizeChannel+message); err != nil {
break
}
}
}
time.Sleep(time.Duration(1) * time.Second)
}
}()
}
func streamExec(conn *websocket.Conn, tty bool) error {
c := console.Current()
defer c.Reset()
if tty {
if err := c.SetRaw(); err != nil {
return err
}
runResizeTask(conn, c)
}
recvChan, sendChan := make(chan string, 10), make(chan string, 10)
ctx, cancel := runRecvTask(conn, c, recvChan, sendChan)
defer cancel()
defer close(recvChan)
defer close(sendChan)
for {
select {
case <-ctx.Done():
return nil
case message := <-sendChan:
if err := websocket.Message.Send(conn, stdinChannel+message); err != nil {
return err
}
case message := <-recvChan:
c.Write([]byte(message))
}
}
}
func parsePsTargets(targets []string) (map[string]int, error) {
targetMap := make(map[string]int)
regex := regexp.MustCompile(`^([a-z0-9]+(?:-[a-z0-9]+)*)=([0-9]+)$`)
var err error
for _, target := range targets {
if regex.MatchString(target) {
captures := regex.FindStringSubmatch(target)
targetMap[captures[1]], err = strconv.Atoi(captures[2])
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("'%s' does not match the pattern 'type=num', ex: web=2", target)
}
}
return targetMap, nil
}
func parseChannelMessage(data string) (string, error) {
channel, message := data[0], data[1:]
if string(channel) == errorChannel {
data := make(map[string]interface{})
yaml.Unmarshal([]byte(message), data)
if value, hasKey := data["message"]; hasKey {
if message, ok := value.(string); ok {
return message, nil
}
return "", fmt.Errorf("message is not string, type: %T", message)
}
return "", nil
}
return message, nil
}