forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpause.go
72 lines (62 loc) · 2.24 KB
/
pause.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
package main
import (
"context"
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
)
// pauseDescription is used to describe pause command in detail and auto generate command doc.
var pauseDescription = "Pause one or more running containers in Pouchd. " +
"when pausing, the container will pause its running but hold all the relevant resource. " +
"This is useful when you wish to pause a container for a while and to restore the running status later. " +
"The container you paused will pause without being terminated."
// PauseCommand use to implement 'pause' command, it pauses one or more containers.
type PauseCommand struct {
baseCommand
}
// Init initialize pause command.
func (p *PauseCommand) Init(c *Cli) {
p.cli = c
p.cmd = &cobra.Command{
Use: "pause CONTAINER [CONTAINER...]",
Short: "Pause one or more running containers",
Long: pauseDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return p.runPause(args)
},
Example: pauseExample(),
}
}
// runPause is the entry of pause command.
func (p *PauseCommand) runPause(args []string) error {
ctx := context.Background()
apiClient := p.cli.Client()
var errs []string
for _, name := range args {
if err := apiClient.ContainerPause(ctx, name); err != nil {
errs = append(errs, err.Error())
continue
}
fmt.Printf("%s\n", name)
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
// pauseExample shows examples in pause command, and is used in auto-generated cli docs.
func pauseExample() string {
return `$ pouch ps
Name ID Status Created Image Runtime
foo2 87259c Up 25 seconds 26 seconds ago registry.hub.docker.com/library/busybox:latest runc
foo1 77188c Up 46 seconds 47 seconds ago registry.hub.docker.com/library/busybox:latest runc
$ pouch pause foo1 foo2
foo1
foo2
$ pouch ps
Name ID Status Created Image Runtime
foo2 87259c Up 1 minute(paused) 1 minute ago registry.hub.docker.com/library/busybox:latest runc
foo1 77188c Up 1 minute(paused) 1 minute ago registry.hub.docker.com/library/busybox:latest runc`
}