forked from AliyunContainerService/pouch
/
exec.go
147 lines (125 loc) · 3.34 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
package main
import (
"context"
"fmt"
"io"
"os"
"sync"
"github.com/alibaba/pouch/apis/types"
"github.com/docker/docker/pkg/stdcopy"
"github.com/spf13/cobra"
)
// execDescription is used to describe exec command in detail and auto generate command doc.
// TODO: add description
var execDescription = ""
// ExecCommand is used to implement 'exec' command.
type ExecCommand struct {
baseCommand
Interactive bool
Terminal bool
Detach bool
User string
}
// Init initializes ExecCommand command.
func (e *ExecCommand) Init(c *Cli) {
e.cli = c
e.cmd = &cobra.Command{
Use: "exec [OPTIONS] CONTAINER COMMAND [ARG...]",
Short: "Exec a process in a running container",
Long: execDescription,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return e.runExec(args)
},
Example: execExample(),
}
e.addFlags()
}
// addFlags adds flags for specific command.
func (e *ExecCommand) addFlags() {
flagSet := e.cmd.Flags()
flagSet.SetInterspersed(false)
flagSet.BoolVarP(&e.Detach, "detach", "d", false, "Run the process in the background")
flagSet.BoolVarP(&e.Terminal, "tty", "t", false, "Allocate a tty device")
flagSet.BoolVarP(&e.Interactive, "interactive", "i", false, "Open container's STDIN")
flagSet.StringVarP(&e.User, "user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
}
// runExec is the entry of ExecCommand command.
func (e *ExecCommand) runExec(args []string) error {
ctx := context.Background()
apiClient := e.cli.Client()
// create exec process.
id := args[0]
command := args[1:]
createExecConfig := &types.ExecCreateConfig{
Cmd: command,
Tty: e.Terminal,
Detach: e.Detach,
AttachStderr: !e.Detach,
AttachStdout: !e.Detach,
AttachStdin: !e.Detach && e.Interactive,
Privileged: false,
User: e.User,
}
createResp, err := apiClient.ContainerCreateExec(ctx, id, createExecConfig)
if err != nil {
return fmt.Errorf("failed to create exec: %v", err)
}
// start exec process.
startExecConfig := &types.ExecStartConfig{
Detach: e.Detach,
Tty: e.Terminal,
}
conn, reader, err := apiClient.ContainerStartExec(ctx, createResp.ID, startExecConfig)
if err != nil {
return fmt.Errorf("failed to create exec: %v", err)
}
defer conn.Close()
// handle stdio.
var wg sync.WaitGroup
if createExecConfig.AttachStderr || createExecConfig.AttachStdout {
wg.Add(1)
go func() {
defer wg.Done()
if !e.Terminal {
stdcopy.StdCopy(os.Stdout, os.Stderr, reader)
} else {
io.Copy(os.Stdout, reader)
}
}()
}
if createExecConfig.AttachStdin {
if e.Terminal {
in, out, err := setRawMode(true, false)
if err != nil {
return fmt.Errorf("failed to set raw mode")
}
defer func() {
if err := restoreMode(in, out); err != nil {
fmt.Fprintf(os.Stderr, "failed to restore term mode")
}
}()
}
go func() {
io.Copy(conn, os.Stdin)
}()
}
wg.Wait()
execInfo, err := apiClient.ContainerExecInspect(ctx, createResp.ID)
if err != nil {
return err
}
code := execInfo.ExitCode
if code != 0 {
return ExitError{Code: int(code)}
}
return nil
}
// execExample shows examples in exec command, and is used in auto-generated cli docs.
func execExample() string {
return `$ pouch exec -it 25bf50 ps
PID USER TIME COMMAND
1 root 0:00 /bin/sh
38 root 0:00 ps
`
}