-
Notifications
You must be signed in to change notification settings - Fork 24
/
containers.go
466 lines (375 loc) · 12.7 KB
/
containers.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package containers
import (
"errors"
"fmt"
"strings"
"sync"
"github.com/containers/podman-tui/ui/containers/cntdialogs"
"github.com/containers/podman-tui/ui/containers/cntdialogs/vterm"
"github.com/containers/podman-tui/ui/dialogs"
"github.com/containers/podman-tui/ui/style"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/rivo/tview"
)
const (
viewContainersIDColIndex = 0 + iota
viewContainersImageColIndex
viewContainersPodColIndex
viewContainersCreatedAtColIndex
viewContainersStatusColIndex
viewContainersNamesColIndex
viewContainersPortsColIndex
)
var (
errNoContainerAttach = errors.New("there is no container to attach")
errNoContainerHealthCheck = errors.New("there is no container to perform healthcheck")
errNoContainerCommit = errors.New("there is no container to commit")
errNoContainerStat = errors.New("there is no container to display stats")
errNoContainerCheckpoint = errors.New("there is no container to perform checkpoint")
errNoContainerExec = errors.New("there is no container to perform exec")
errNoContainerDiff = errors.New("there is no container to display diff")
errNoContainerInspect = errors.New("there is no container to inspect")
errNoContainerKill = errors.New("there is no container to kill")
errNoContainerLogs = errors.New("there is no container to display logs")
errNoContainerPause = errors.New("there is no container to pause")
errNoContainerUnpause = errors.New("there is no container to unpause")
errNoContainerPorts = errors.New("there is no container to display ports")
errNoContainerRename = errors.New("there is no container to rename")
errNoContainerRemove = errors.New("there is no container to remove")
errNoContainerStart = errors.New("there is no container to start")
errNoContainerStop = errors.New("there is no container to stop")
errNoContainerTop = errors.New("there is no container to display top")
errEmptyContainerImageName = errors.New("empty container name or image name")
)
// Containers implements the containers page primitive.
type Containers struct {
*tview.Box
title string
headers []string
table *tview.Table
errorDialog *dialogs.ErrorDialog
cmdDialog *dialogs.CommandDialog
cmdInputDialog *dialogs.SimpleInputDialog
confirmDialog *dialogs.ConfirmDialog
messageDialog *dialogs.MessageDialog
progressDialog *dialogs.ProgressDialog
topDialog *dialogs.TopDialog
createDialog *cntdialogs.ContainerCreateDialog
execDialog *cntdialogs.ContainerExecDialog
terminalDialog *vterm.VtermDialog
statsDialog *cntdialogs.ContainerStatsDialog
commitDialog *cntdialogs.ContainerCommitDialog
checkpointDialog *cntdialogs.ContainerCheckpointDialog
restoreDialog *cntdialogs.ContainerRestoreDialog
containersList containerListReport
selectedID string
selectedName string
confirmData string
fastRefreshChan chan bool
}
type containerListReport struct {
mu sync.Mutex
report []entities.ListContainer
}
// NewContainers returns containers page view.
func NewContainers() *Containers {
containers := &Containers{
Box: tview.NewBox(),
title: "containers",
headers: []string{"container id", "image", "pod", "created", "status", "names", "ports"},
errorDialog: dialogs.NewErrorDialog(),
cmdInputDialog: dialogs.NewSimpleInputDialog(""),
messageDialog: dialogs.NewMessageDialog(""),
progressDialog: dialogs.NewProgressDialog(),
confirmDialog: dialogs.NewConfirmDialog(),
topDialog: dialogs.NewTopDialog(),
createDialog: cntdialogs.NewContainerCreateDialog(),
execDialog: cntdialogs.NewContainerExecDialog(),
terminalDialog: vterm.NewVtermDialog(),
statsDialog: cntdialogs.NewContainerStatsDialog(),
commitDialog: cntdialogs.NewContainerCommitDialog(),
checkpointDialog: cntdialogs.NewContainerCheckpointDialog(),
restoreDialog: cntdialogs.NewContainerRestoreDialog(),
}
containers.topDialog.SetTitle("podman container top")
containers.cmdDialog = dialogs.NewCommandDialog([][]string{
{"attach", "attach to a running container"},
{"checkpoint", "checkpoints a running container"},
{"commit", "create an image from a container's changes"},
{"create", "create a new container but do not start"},
{"diff", "inspect changes to the selected container's file systems"},
{"exec", "execute the specified command inside a running container"},
{"healthcheck", "run the health check of a container"},
{"inspect", "display the configuration of a container"},
{"kill", "kill the selected running container with a SIGKILL signal"},
{"logs", "fetch the logs of the selected container"},
{"pause", "pause all the processes in the selected container"},
{"port", "list port mappings for the selected container"},
{"prune", "remove all non running containers"},
{"rename", "rename the selected container"},
{"restore", "restores a container from a checkpoint"},
{"rm", "remove the selected container"},
{"start", "start the selected containers"},
{"stats", "display container resource usage statistics"},
{"stop", "stop the selected containers"},
{"top", "display the running processes of the selected container"},
{"unpause", "unpause the selected container that was paused before"},
})
containers.table = tview.NewTable()
containers.table.SetTitle(fmt.Sprintf("[::b]%s[0]", strings.ToUpper(containers.title)))
containers.table.SetBorderColor(style.BorderColor)
containers.table.SetTitleColor(style.FgColor)
containers.table.SetBackgroundColor(style.BgColor)
containers.table.SetBorder(true)
for i := 0; i < len(containers.headers); i++ {
containers.table.SetCell(0, i,
tview.NewTableCell(fmt.Sprintf("[black::b]%s", strings.ToUpper(containers.headers[i]))). //nolint:perfsprint
SetExpansion(1).
SetBackgroundColor(style.PageHeaderBgColor).
SetTextColor(style.PageHeaderFgColor).
SetAlign(tview.AlignLeft).
SetSelectable(false))
}
containers.table.SetFixed(1, 1)
containers.table.SetSelectable(true, false)
// set command dialog functions
containers.cmdDialog.SetSelectedFunc(func() {
containers.cmdDialog.Hide()
containers.runCommand(containers.cmdDialog.GetSelectedItem())
})
containers.cmdDialog.SetCancelFunc(containers.cmdDialog.Hide)
// set input cmd dialog functions
containers.cmdInputDialog.SetCancelFunc(containers.cmdInputDialog.Hide)
containers.cmdInputDialog.SetSelectedFunc(containers.cmdInputDialog.Hide)
// set message dialog functions
containers.messageDialog.SetCancelFunc(containers.messageDialog.Hide)
// set container top dialog functions
containers.topDialog.SetCancelFunc(containers.topDialog.Hide)
// set confirm dialogs functions
containers.confirmDialog.SetSelectedFunc(func() {
containers.confirmDialog.Hide()
switch containers.confirmData {
case "prune":
containers.prune()
case "rm":
containers.remove()
}
})
containers.confirmDialog.SetCancelFunc(containers.confirmDialog.Hide)
// set create dialog functions
containers.createDialog.SetCancelFunc(func() {
containers.createDialog.Hide()
})
containers.createDialog.SetCreateFunc(func() {
containers.createDialog.Hide()
containers.create()
})
// set exec dialog functions
containers.execDialog.SetCancelFunc(containers.execDialog.Hide)
containers.execDialog.SetExecFunc(containers.exec)
// terminal dialog
containers.terminalDialog.SetCancelFunc(containers.terminalDialog.Hide)
containers.terminalDialog.SetFastRefreshHandler(func() {
containers.fastRefreshChan <- true
})
// set stats dialogs functions
containers.statsDialog.SetDoneFunc(containers.statsDialog.Hide)
// set commit dialog functions
containers.commitDialog.SetCommitFunc(containers.commit)
containers.commitDialog.SetCancelFunc(containers.commitDialog.Hide)
// set checkpoint dialog functions
containers.checkpointDialog.SetCheckpointFunc(containers.checkpoint)
containers.checkpointDialog.SetCancelFunc(containers.checkpointDialog.Hide)
// set restore dialog functions
containers.restoreDialog.SetRestoreFunc(containers.restore)
containers.restoreDialog.SetCancelFunc(containers.restoreDialog.Hide)
return containers
}
// GetTitle returns primitive title.
func (cnt *Containers) GetTitle() string {
return cnt.title
}
// HasFocus returns whether or not this primitive has focus.
func (cnt *Containers) HasFocus() bool { //nolint:cyclop
if cnt.table.HasFocus() || cnt.errorDialog.HasFocus() {
return true
}
if cnt.cmdDialog.HasFocus() || cnt.progressDialog.HasFocus() {
return true
}
if cnt.topDialog.HasFocus() || cnt.messageDialog.HasFocus() {
return true
}
if cnt.confirmDialog.HasFocus() || cnt.cmdInputDialog.HasFocus() {
return true
}
if cnt.createDialog.HasFocus() || cnt.execDialog.HasFocus() {
return true
}
if cnt.statsDialog.HasFocus() || cnt.commitDialog.HasFocus() {
return true
}
if cnt.checkpointDialog.HasFocus() || cnt.restoreDialog.HasFocus() {
return true
}
if cnt.Box.HasFocus() || cnt.terminalDialog.HasFocus() {
return true
}
return false
}
// SubDialogHasFocus returns whether or not sub dialog primitive has focus.
func (cnt *Containers) SubDialogHasFocus() bool { //nolint:cyclop
if cnt.statsDialog.HasFocus() || cnt.errorDialog.HasFocus() {
return true
}
if cnt.cmdDialog.HasFocus() || cnt.progressDialog.HasFocus() {
return true
}
if cnt.topDialog.HasFocus() || cnt.messageDialog.HasFocus() {
return true
}
if cnt.confirmDialog.HasFocus() || cnt.cmdInputDialog.HasFocus() {
return true
}
if cnt.createDialog.HasFocus() || cnt.execDialog.HasFocus() {
return true
}
if cnt.commitDialog.HasFocus() || cnt.checkpointDialog.HasFocus() {
return true
}
if cnt.restoreDialog.HasFocus() || cnt.terminalDialog.HasFocus() {
return true
}
return false
}
// Focus is called when this primitive receives focus.
func (cnt *Containers) Focus(delegate func(p tview.Primitive)) { //nolint:cyclop
// error dialog
if cnt.errorDialog.IsDisplay() {
delegate(cnt.errorDialog)
return
}
// command dialog
if cnt.cmdDialog.IsDisplay() {
delegate(cnt.cmdDialog)
return
}
// command input dialog
if cnt.cmdInputDialog.IsDisplay() {
delegate(cnt.cmdInputDialog)
return
}
// message dialog
if cnt.messageDialog.IsDisplay() {
delegate(cnt.messageDialog)
return
}
// container top dialog
if cnt.topDialog.IsDisplay() {
delegate(cnt.topDialog)
return
}
// confirm dialog
if cnt.confirmDialog.IsDisplay() {
delegate(cnt.confirmDialog)
return
}
// create dialog
if cnt.createDialog.IsDisplay() {
delegate(cnt.createDialog)
return
}
// exec dialog
if cnt.execDialog.IsDisplay() {
delegate(cnt.execDialog)
return
}
// stats dialog
if cnt.statsDialog.IsDisplay() {
delegate(cnt.statsDialog)
return
}
// commit dialog
if cnt.commitDialog.IsDisplay() {
delegate(cnt.commitDialog)
return
}
// checkpoint dialog
if cnt.checkpointDialog.IsDisplay() {
delegate(cnt.checkpointDialog)
return
}
// restore dialog
if cnt.restoreDialog.IsDisplay() {
delegate(cnt.restoreDialog)
return
}
// terminal dialog
if cnt.terminalDialog.IsDisplay() {
delegate(cnt.terminalDialog)
return
}
delegate(cnt.table)
}
func (cnt *Containers) getSelectedItem() (string, string) {
var (
cntID string
cntName string
)
if cnt.table.GetRowCount() <= 1 {
return cntID, cntName
}
row, _ := cnt.table.GetSelection()
cntID = cnt.table.GetCell(row, viewContainersIDColIndex).Text
cntName = cnt.table.GetCell(row, viewContainersNamesColIndex).Text
return cntID, cntName
}
// SetFastRefreshChannel sets channel for fastRefresh func.
func (cnt *Containers) SetFastRefreshChannel(refresh chan bool) {
cnt.fastRefreshChan = refresh
}
// HideAllDialogs hides all sub dialogs.
func (cnt *Containers) HideAllDialogs() { //nolint:cyclop
if cnt.errorDialog.IsDisplay() {
cnt.errorDialog.Hide()
}
if cnt.progressDialog.IsDisplay() {
cnt.progressDialog.Hide()
}
if cnt.confirmDialog.IsDisplay() {
cnt.confirmDialog.Hide()
}
if cnt.cmdDialog.IsDisplay() {
cnt.cmdDialog.Hide()
}
if cnt.cmdInputDialog.IsDisplay() {
cnt.cmdInputDialog.Hide()
}
if cnt.messageDialog.IsDisplay() {
cnt.messageDialog.Hide()
}
if cnt.topDialog.IsDisplay() {
cnt.topDialog.Hide()
}
if cnt.createDialog.IsDisplay() {
cnt.createDialog.Hide()
}
if cnt.execDialog.IsDisplay() {
cnt.execDialog.Hide()
}
if cnt.statsDialog.IsDisplay() {
cnt.statsDialog.Hide()
}
if cnt.commitDialog.IsDisplay() {
cnt.commitDialog.Hide()
}
if cnt.checkpointDialog.IsDisplay() {
cnt.checkpointDialog.Hide()
}
if cnt.restoreDialog.IsDisplay() {
cnt.restoreDialog.Hide()
}
if cnt.terminalDialog.IsDisplay() {
cnt.terminalDialog.Hide()
}
}